|
| 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 sys |
| 19 | +import subprocess |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | +from fuzzers import utils |
| 23 | + |
| 24 | + |
| 25 | +def prepare_fuzz_environment(input_corpus): |
| 26 | + """Prepare to fuzz with a LibAFL-based fuzzer.""" |
| 27 | + os.environ['ASAN_OPTIONS'] = 'abort_on_error=1:detect_leaks=0:'\ |
| 28 | + 'malloc_context_size=0:symbolize=0:'\ |
| 29 | + 'allocator_may_return_null=1:'\ |
| 30 | + 'detect_odr_violation=0:handle_segv=0:'\ |
| 31 | + 'handle_sigbus=0:handle_abort=0:'\ |
| 32 | + 'handle_sigfpe=0:handle_sigill=0' |
| 33 | + os.environ['UBSAN_OPTIONS'] = 'abort_on_error=1:'\ |
| 34 | + 'allocator_release_to_os_interval_ms=500:'\ |
| 35 | + 'handle_abort=0:handle_segv=0:'\ |
| 36 | + 'handle_sigbus=0:handle_sigfpe=0:'\ |
| 37 | + 'handle_sigill=0:print_stacktrace=0:'\ |
| 38 | + 'symbolize=0:symbolize_inline_frames=0' |
| 39 | + # Create at least one non-empty seed to start. |
| 40 | + utils.create_seed_file_for_empty_corpus(input_corpus) |
| 41 | + |
| 42 | + |
| 43 | +def build_dfsan(): |
| 44 | + """Build benchmark with dfsan.""" |
| 45 | + new_env = os.environ.copy() |
| 46 | + new_env['CC'] = ('/dgfuzz/fuzzers/fuzzbench_dataflow_guided/afl-cc/' |
| 47 | + 'afl-clang-dgfuzz') |
| 48 | + new_env['CXX'] = ('/dgfuzz/fuzzers/fuzzbench_dataflow_guided/afl-cc/' |
| 49 | + 'afl-clang-dgfuzz++') |
| 50 | + |
| 51 | + new_env['ASAN_OPTIONS'] = 'abort_on_error=0:allocator_may_return_null=1' |
| 52 | + new_env['UBSAN_OPTIONS'] = 'abort_on_error=0' |
| 53 | + new_env['AFL_QUIET'] = '1' |
| 54 | + |
| 55 | + new_env['FUZZER_LIB'] = '/libAFLDriver.a' |
| 56 | + |
| 57 | + build_directory = new_env['OUT'] |
| 58 | + dfsan_build_directory = os.path.join(build_directory, 'dfsan') |
| 59 | + os.mkdir(dfsan_build_directory) |
| 60 | + new_env['OUT'] = dfsan_build_directory |
| 61 | + |
| 62 | + cfg_file = os.path.join(build_directory, 'aflpp_cfg.bin') |
| 63 | + new_env['AFL_LLVM_CFG_FILE'] = cfg_file |
| 64 | + if os.path.isfile(cfg_file): |
| 65 | + os.remove(cfg_file) |
| 66 | + Path(cfg_file).touch() |
| 67 | + |
| 68 | + src = os.getenv('SRC') |
| 69 | + work = os.getenv('WORK') |
| 70 | + |
| 71 | + with utils.restore_directory(src), utils.restore_directory(work): |
| 72 | + # Restore SRC to its initial state so we can build again without any |
| 73 | + # trouble. For some OSS-Fuzz projects, build_benchmark cannot be run |
| 74 | + # twice in the same directory without this. |
| 75 | + utils.build_benchmark(env=new_env) |
| 76 | + |
| 77 | + fuzz_target = os.getenv('FUZZ_TARGET') |
| 78 | + exec_path = os.path.join(dfsan_build_directory, fuzz_target) |
| 79 | + new_path = os.path.join(dfsan_build_directory, fuzz_target + '_dfsan') |
| 80 | + os.rename(exec_path, new_path) |
| 81 | + |
| 82 | + |
| 83 | +def build(): |
| 84 | + """Build benchmark.""" |
| 85 | + |
| 86 | + # first build it with DFSan enabled |
| 87 | + build_dfsan() |
| 88 | + |
| 89 | + os.environ['CC'] = ('/dgfuzz/fuzzers/fuzzbench_dataflow_guided/target/' |
| 90 | + 'release-fuzzbench/libafl_cc') |
| 91 | + os.environ['CXX'] = ('/dgfuzz/fuzzers/fuzzbench_dataflow_guided/target/' |
| 92 | + 'release-fuzzbench/libafl_cxx') |
| 93 | + |
| 94 | + os.environ['ASAN_OPTIONS'] = 'abort_on_error=0:allocator_may_return_null=1' |
| 95 | + os.environ['UBSAN_OPTIONS'] = 'abort_on_error=0' |
| 96 | + |
| 97 | + cflags = ['--libafl'] |
| 98 | + utils.append_flags('CFLAGS', cflags) |
| 99 | + utils.append_flags('CXXFLAGS', cflags) |
| 100 | + utils.append_flags('LDFLAGS', cflags) |
| 101 | + |
| 102 | + os.environ['FUZZER_LIB'] = '/stub_rt.a' |
| 103 | + build_directory = os.environ['OUT'] |
| 104 | + cfg_file = os.path.join(build_directory, 'libafl_cfg.bin') |
| 105 | + os.environ['AFL_LLVM_CFG_FILE'] = cfg_file |
| 106 | + if os.path.isfile(cfg_file): |
| 107 | + os.remove(cfg_file) |
| 108 | + Path(cfg_file).touch() |
| 109 | + utils.build_benchmark() |
| 110 | + |
| 111 | + |
| 112 | +def fuzz(input_corpus, output_corpus, target_binary): |
| 113 | + """Run fuzzer.""" |
| 114 | + prepare_fuzz_environment(input_corpus) |
| 115 | + dictionary_path = utils.get_dictionary_path(target_binary) |
| 116 | + command = [target_binary] |
| 117 | + if dictionary_path: |
| 118 | + command += (['-x', dictionary_path]) |
| 119 | + |
| 120 | + # Add the control flow graph file |
| 121 | + build_directory = os.environ['OUT'] |
| 122 | + cfg_file = os.path.join(build_directory, 'libafl_cfg.bin') |
| 123 | + if os.path.exists(cfg_file): |
| 124 | + command += (['-c', cfg_file]) |
| 125 | + else: |
| 126 | + sys.exit(1) |
| 127 | + |
| 128 | + # get the dfsan binary |
| 129 | + dfsan_build_directory = os.path.join(build_directory, 'dfsan') |
| 130 | + fuzz_target = os.getenv('FUZZ_TARGET') |
| 131 | + dfsan_fuzz_target = os.path.join(dfsan_build_directory, |
| 132 | + fuzz_target + '_dfsan') |
| 133 | + command += (['-d', dfsan_fuzz_target]) |
| 134 | + |
| 135 | + # Add the input and output corpus |
| 136 | + command += (['-o', output_corpus, '-i', input_corpus]) |
| 137 | + fuzzer_env = os.environ.copy() |
| 138 | + fuzzer_env['LD_PRELOAD'] = '/usr/lib/x86_64-linux-gnu/libjemalloc.so.2' |
| 139 | + print(command) |
| 140 | + subprocess.check_call(command, cwd=os.environ['OUT'], env=fuzzer_env) |
0 commit comments