Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/clusterfuzz/_internal/bot/tasks/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ def _setup_memory_tools_environment(testcase):
logs.error('Ignoring testcase env metadata that is not a dict.')
env = {}

is_windows = environment.platform() == 'WINDOWS'
options_applied = False
asan_options_applied = False
for options_name, options_value in env.items():
if not environment.is_memory_tool_options_variable(options_name):
logs.error('Ignoring unexpected variable in testcase env metadata: '
Expand All @@ -164,6 +166,14 @@ def _setup_memory_tools_environment(testcase):
'options are not a dict.')
continue

if is_windows and options_name == 'ASAN_OPTIONS':
# Re-apply required Windows ASan options for existing testcases whose
# stored env metadata previously stripped them during minimization.
options_value = {
**options_value,
**environment.REQUIRED_WINDOWS_ASAN_OPTIONS,
}

if not options_value:
environment.remove_key(options_name)
options_applied = True
Expand All @@ -177,10 +187,18 @@ def _setup_memory_tools_environment(testcase):
continue

options_applied = True
if options_name == 'ASAN_OPTIONS':
asan_options_applied = True

if not options_applied:
environment.reset_current_memory_tool_options(
redzone_size=testcase.redzone, disable_ubsan=testcase.disable_ubsan)
elif is_windows and not asan_options_applied:
job_name = environment.get_value('JOB_NAME') or testcase.job_type
if job_name and environment.get_memory_tool_name(job_name) == 'ASAN':
asan_options = environment.get_memory_tool_options('ASAN_OPTIONS', {})
asan_options.update(environment.REQUIRED_WINDOWS_ASAN_OPTIONS)
environment.set_memory_tool_options('ASAN_OPTIONS', asan_options)


def prepare_environment_for_testcase(testcase):
Expand Down
4 changes: 4 additions & 0 deletions src/clusterfuzz/_internal/bot/tasks/utasks/minimize_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,10 @@ def do_libfuzzer_minimization(
for options_name, options_value in options.items():
if utils.is_oss_fuzz() and options_name in MANDATORY_OSS_FUZZ_OPTIONS:
continue
if (environment.platform() == 'WINDOWS' and
options_env_var == 'ASAN_OPTIONS' and
options_name in environment.REQUIRED_WINDOWS_ASAN_OPTIONS):
continue

minimized_options.pop(options_name)
environment.set_memory_tool_options(options_env_var, minimized_options)
Expand Down
8 changes: 8 additions & 0 deletions src/clusterfuzz/_internal/system/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
'use_sigaltstack': 1,
}

# ASan options that must stay set on Windows so CHECK failures always emit an
# unsymbolized ASan report for offline llvm-symbolizer symbolization instead of
# omitting the report or falling back to in-process DbgHelp symbolization.
REQUIRED_WINDOWS_ASAN_OPTIONS = {
'handle_sigill': 1,
'symbolize': 0,
}

# CPU architectures whose binaries live in the default unsuffixed platform
# resource directory (e.g., `resources/platform/mac`) rather than an
# architecture-suffixed directory (e.g., `resources/platform/mac_arm64`).
Expand Down
46 changes: 46 additions & 0 deletions src/clusterfuzz/_internal/tests/core/bot/tasks/setup_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,3 +638,49 @@ def test_empty_options(self):
self.assertNotIn('ASAN_OPTIONS', os.environ)
self.mock.error.assert_not_called()
self.mock.reset_current_memory_tool_options.assert_not_called()

def test_windows_pins_required_asan_options(self):
"""Test that Windows restores handle_sigill=1 and symbolize=0 when loading
stored ASAN_OPTIONS."""
cases = [
({}, 'handle_sigill=1:symbolize=0'),
({
'redzone': 64
}, 'handle_sigill=1:redzone=64:symbolize=0'),
({
'redzone': 64,
'symbolize': 1,
'handle_sigill': 0
}, 'handle_sigill=1:redzone=64:symbolize=0'),
]
with unittest.mock.patch.object(
environment, 'platform', return_value='WINDOWS'):
for stored_options, expected in cases:
with self.subTest(stored_options=stored_options):
os.environ.pop('ASAN_OPTIONS', None)
self.testcase.set_metadata(
'env', {'ASAN_OPTIONS': stored_options}, update_testcase=False)
setup._setup_memory_tools_environment(self.testcase) # pylint: disable=protected-access
self.assertEqual(expected, os.environ['ASAN_OPTIONS'])
self.mock.error.assert_not_called()
self.mock.reset_current_memory_tool_options.assert_not_called()

def test_windows_pins_asan_options_when_missing_from_env(self):
"""Test that Windows sets required ASAN_OPTIONS when env metadata omits
ASAN_OPTIONS on an ASan job, without affecting non-ASan jobs."""
with unittest.mock.patch.object(
environment, 'platform', return_value='WINDOWS'):
environment.set_value('JOB_NAME', 'libfuzzer_chrome_asan')
self.testcase.set_metadata(
'env', {'UBSAN_OPTIONS': {
'halt_on_error': 1
}}, update_testcase=False)
setup._setup_memory_tools_environment(self.testcase) # pylint: disable=protected-access
self.assertEqual('halt_on_error=1', os.environ['UBSAN_OPTIONS'])
self.assertEqual('handle_sigill=1:symbolize=0',
os.environ['ASAN_OPTIONS'])

os.environ.pop('ASAN_OPTIONS', None)
environment.set_value('JOB_NAME', 'windows_ubsan_chrome')
setup._setup_memory_tools_environment(self.testcase) # pylint: disable=protected-access
self.assertNotIn('ASAN_OPTIONS', os.environ)
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,50 @@ def test_libfuzzer_skip_minimization_initial_crash_state(self):
'----------------------------------------+\n%s' % stacktrace,
crash_result_dict['crash_stacktrace'])

def test_libfuzzer_minimization_pins_windows_asan_options(self):
"""Test that Windows minimization keeps handle_sigill and symbolize in
ASAN_OPTIONS while still minimizing other options."""
from clusterfuzz._internal.crash_analysis.crash_result import CrashResult

data_types.Job(name='libfuzzer_asan_job').put()
testcase = data_types.Testcase(
minimized_keys='',
fuzzed_keys='FUZZED_KEY',
job_type='libfuzzer_asan_job',
security_flag=True)
testcase.put()

stacktrace = (
'==14970==ERROR: AddressSanitizer: heap-buffer-overflow on address '
'0x61b00001f7d0 at pc 0x00000064801b bp 0x7ffce478dbd0 sp '
'0x7ffce478dbc8 READ of size 4 at 0x61b00001f7d0 thread T0\n'
'#0 0x64801a in frame0() src/test.cpp:1819:15\n'
'#1 0x647ac5 in frame1() src/test.cpp:1954:25\n'
'#2 0xb1dee7 in frame2() src/test.cpp:160:9\n')
self.mock._run_libfuzzer_testcase.return_value = CrashResult( # pylint: disable=protected-access
1, 1.0, stacktrace)
self.mock._run_libfuzzer_tool.return_value = (None, None, None) # pylint: disable=protected-access

environment.set_value(
'ASAN_OPTIONS', 'alloc_dealloc_mismatch=0:handle_sigill=1:symbolize=0')

minimize_task_input = uworker_msg_pb2.MinimizeTaskInput()
with mock.patch.object(environment, 'platform', return_value='WINDOWS'):
output = minimize_task.do_libfuzzer_minimization(
None, minimize_task_input, testcase, '/testcase_file_path')

self.assertEqual(
'handle_sigill=1:symbolize=0',
output.minimize_task_output.memory_tool_options['ASAN_OPTIONS'])
self.assertEqual({
'handle_sigill': 1,
'symbolize': 0
},
testcase.get_metadata('env')['ASAN_OPTIONS'])
self.assertEqual('handle_sigill=1:symbolize=0', os.environ['ASAN_OPTIONS'])
# Initial crash run + one run testing removal of alloc_dealloc_mismatch.
self.assertEqual(2, self.mock._run_libfuzzer_testcase.call_count) # pylint: disable=protected-access


class MinimizeTaskTestUntrusted(
untrusted_runner_helpers.UntrustedRunnerIntegrationTest):
Expand Down
Loading