|
| 1 | +# Copyright 2020 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +"""Integration code for a LibAFL-based fuzzer.""" |
| 16 | + |
| 17 | +import os |
| 18 | +import subprocess |
| 19 | + |
| 20 | +from fuzzers import utils |
| 21 | + |
| 22 | + |
| 23 | +def prepare_fuzz_environment(input_corpus): |
| 24 | + """Prepare to fuzz with a LibAFL-based fuzzer.""" |
| 25 | + os.environ['ASAN_OPTIONS'] = 'abort_on_error=1:detect_leaks=0:'\ |
| 26 | + 'malloc_context_size=0:symbolize=0:'\ |
| 27 | + 'allocator_may_return_null=1:'\ |
| 28 | + 'detect_odr_violation=0:handle_segv=0:'\ |
| 29 | + 'handle_sigbus=0:handle_abort=0:'\ |
| 30 | + 'handle_sigfpe=0:handle_sigill=0' |
| 31 | + os.environ['UBSAN_OPTIONS'] = 'abort_on_error=1:'\ |
| 32 | + 'allocator_release_to_os_interval_ms=500:'\ |
| 33 | + 'handle_abort=0:handle_segv=0:'\ |
| 34 | + 'handle_sigbus=0:handle_sigfpe=0:'\ |
| 35 | + 'handle_sigill=0:print_stacktrace=0:'\ |
| 36 | + 'symbolize=0:symbolize_inline_frames=0' |
| 37 | + # Create at least one non-empty seed to start. |
| 38 | + utils.create_seed_file_for_empty_corpus(input_corpus) |
| 39 | + |
| 40 | + |
| 41 | +def build(): # pylint: disable=too-many-branches,too-many-statements |
| 42 | + """Build benchmark.""" |
| 43 | + os.environ['CC'] = '/libafl_fuzzbench/target/release/grimoire_cc' |
| 44 | + os.environ['CXX'] = '/libafl_fuzzbench/target/release/grimoire_cxx' |
| 45 | + |
| 46 | + os.environ['ASAN_OPTIONS'] = 'abort_on_error=0:allocator_may_return_null=1' |
| 47 | + os.environ['UBSAN_OPTIONS'] = 'abort_on_error=0' |
| 48 | + |
| 49 | + cflags = ['--libafl'] |
| 50 | + cxxflags = ['--libafl', '--std=c++14'] |
| 51 | + utils.append_flags('CFLAGS', cflags) |
| 52 | + utils.append_flags('CXXFLAGS', cxxflags) |
| 53 | + utils.append_flags('LDFLAGS', cflags) |
| 54 | + |
| 55 | + os.environ['FUZZER_LIB'] = '/stub_rt.a' |
| 56 | + utils.build_benchmark() |
| 57 | + |
| 58 | + |
| 59 | +def fuzz(input_corpus, output_corpus, target_binary): |
| 60 | + """Run fuzzer.""" |
| 61 | + prepare_fuzz_environment(input_corpus) |
| 62 | + dictionary_path = utils.get_dictionary_path(target_binary) |
| 63 | + command = [target_binary] |
| 64 | + if dictionary_path: |
| 65 | + command += (['-x', dictionary_path]) |
| 66 | + command += (['-o', output_corpus, '-i', input_corpus]) |
| 67 | + fuzzer_env = os.environ.copy() |
| 68 | + fuzzer_env['LD_PRELOAD'] = '/usr/lib/x86_64-linux-gnu/libjemalloc.so.2' |
| 69 | + print(command) |
| 70 | + subprocess.check_call(command, cwd=os.environ['OUT'], env=fuzzer_env) |
0 commit comments