1#!/bin/bash -eu 2# Copyright 2016 Google Inc. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16################################################################################ 17 18# Fuzzer runner. Appends .options arguments and seed corpus to users args. 19# Usage: $0 <fuzzer_name> <fuzzer_args> 20 21export PATH=$OUT:$PATH 22cd $OUT 23 24DEBUGGER=${DEBUGGER:-} 25 26FUZZER=$1 27shift 28 29CORPUS_DIR=${CORPUS_DIR:-"/tmp/${FUZZER}_corpus"} 30 31SANITIZER=${SANITIZER:-} 32if [ -z $SANITIZER ]; then 33 # If $SANITIZER is not specified (e.g. calling from `reproduce` command), it 34 # is not important and can be set to any value. 35 SANITIZER="default" 36fi 37 38if [[ "$RUN_FUZZER_MODE" = interactive ]]; then 39 FUZZER_OUT="$OUT/${FUZZER}_${FUZZING_ENGINE}_${SANITIZER}_out" 40else 41 FUZZER_OUT="/tmp/${FUZZER}_${FUZZING_ENGINE}_${SANITIZER}_out" 42fi 43 44function get_dictionary() { 45 local options_file="$FUZZER.options" 46 local dict_file="$FUZZER.dict" 47 local dict="" 48 if [[ -f "$options_file" ]]; then 49 dict=$(sed -n 's/^\s*dict\s*=\s*\(.*\)/\1/p' "$options_file" | tail -1) 50 fi 51 if [[ -z "$dict" && -f "$dict_file" ]]; then 52 dict="$dict_file" 53 fi 54 [[ -z "$dict" ]] && return 55 if [[ "$FUZZING_ENGINE" = "libfuzzer" ]]; then 56 printf -- "-dict=%s" "$dict" 57 elif [[ "$FUZZING_ENGINE" = "afl" ]]; then 58 printf -- "-x %s" "$dict" 59 elif [[ "$FUZZING_ENGINE" = "honggfuzz" ]]; then 60 printf -- "--dict %s" "$dict" 61 else 62 printf "Unexpected FUZZING_ENGINE: $FUZZING_ENGINE, ignoring\n" >&2 63 fi 64} 65 66rm -rf $CORPUS_DIR && mkdir -p $CORPUS_DIR 67rm -rf $FUZZER_OUT && mkdir -p $FUZZER_OUT 68 69SEED_CORPUS="${FUZZER}_seed_corpus.zip" 70 71if [ -f $SEED_CORPUS ] && [ -z ${SKIP_SEED_CORPUS:-} ]; then 72 echo "Using seed corpus: $SEED_CORPUS" 73 unzip -d ${CORPUS_DIR}/ $SEED_CORPUS > /dev/null 74fi 75 76OPTIONS_FILE="${FUZZER}.options" 77CUSTOM_LIBFUZZER_OPTIONS="" 78 79if [ -f $OPTIONS_FILE ]; then 80 custom_asan_options=$(parse_options.py $OPTIONS_FILE asan) 81 if [ ! -z $custom_asan_options ]; then 82 export ASAN_OPTIONS="$ASAN_OPTIONS:$custom_asan_options" 83 fi 84 85 custom_msan_options=$(parse_options.py $OPTIONS_FILE msan) 86 if [ ! -z $custom_msan_options ]; then 87 export MSAN_OPTIONS="$MSAN_OPTIONS:$custom_msan_options" 88 fi 89 90 custom_ubsan_options=$(parse_options.py $OPTIONS_FILE ubsan) 91 if [ ! -z $custom_ubsan_options ]; then 92 export UBSAN_OPTIONS="$UBSAN_OPTIONS:$custom_ubsan_options" 93 fi 94 95 CUSTOM_LIBFUZZER_OPTIONS=$(parse_options.py $OPTIONS_FILE libfuzzer) 96fi 97 98if [[ "$FUZZING_ENGINE" = afl ]]; then 99 100 # Set afl++ environment options. 101 export ASAN_OPTIONS="$ASAN_OPTIONS:abort_on_error=1:symbolize=0:detect_odr_violation=0:" 102 export MSAN_OPTIONS="$MSAN_OPTIONS:exit_code=86:symbolize=0" 103 export UBSAN_OPTIONS="$UBSAN_OPTIONS:symbolize=0" 104 export AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES=1 105 export AFL_SKIP_CPUFREQ=1 106 export AFL_NO_AFFINITY=1 107 export AFL_FAST_CAL=1 108 # If $OUT/afl_cmplog.txt is present this means the target was compiled for 109 # CMPLOG. So we have to add the proper parameters to afl-fuzz. `-l 2` is 110 # CMPLOG level 2, which will colorize larger files but not huge files and 111 # not enable transform analysis unless there have been several cycles without 112 # any finds. 113 test -e "$OUT/afl_cmplog.txt" && AFL_FUZZER_ARGS="$AFL_FUZZER_ARGS -l 2 -c $OUT/$FUZZER" 114 # If $OUT/afl++.dict we load it as a dictionary for afl-fuzz. 115 test -e "$OUT/afl++.dict" && AFL_FUZZER_ARGS="$AFL_FUZZER_ARGS -x $OUT/afl++.dict" 116 # Ensure timeout is a bit large than 1sec as some of the OSS-Fuzz fuzzers 117 # are slower than this. 118 AFL_FUZZER_ARGS="$AFL_FUZZER_ARGS -t 5000+" 119 # AFL expects at least 1 file in the input dir. 120 echo input > ${CORPUS_DIR}/input 121 echo afl++ setup: 122 env|grep AFL_ 123 cat "$OUT/afl_options.txt" 124 CMD_LINE="$OUT/afl-fuzz $AFL_FUZZER_ARGS -i $CORPUS_DIR -o $FUZZER_OUT $(get_dictionary) $* -- $OUT/$FUZZER" 125 126elif [[ "$FUZZING_ENGINE" = honggfuzz ]]; then 127 128 # Honggfuzz expects at least 1 file in the input dir. 129 echo input > $CORPUS_DIR/input 130 # --exit_upon_crash: exit whith a first crash seen 131 # -R (report): save report file to this location 132 # -W (working dir): where the crashes go 133 # -v (verbose): don't use VTE UI, just stderr 134 # -z: use software-instrumentation of clang (trace-pc-guard....) 135 # -P: use persistent mode of fuzzing (i.e. LLVMFuzzerTestOneInput) 136 # -f: location of the initial (and destination) file corpus 137 # -n: number of fuzzing threads (and processes) 138 CMD_LINE="$OUT/honggfuzz -n 1 --exit_upon_crash -R /tmp/${FUZZER}_honggfuzz.report -W $FUZZER_OUT -v -z -P -f \"$CORPUS_DIR\" $(get_dictionary) $* -- \"$OUT/$FUZZER\"" 139 140else 141 142 CMD_LINE="$OUT/$FUZZER $FUZZER_ARGS $*" 143 144 if [ -z ${SKIP_SEED_CORPUS:-} ]; then 145 CMD_LINE="$CMD_LINE $CORPUS_DIR" 146 fi 147 148 if [ ! -z $CUSTOM_LIBFUZZER_OPTIONS ]; then 149 CMD_LINE="$CMD_LINE $CUSTOM_LIBFUZZER_OPTIONS" 150 fi 151 152 if [[ ! "$CMD_LINE" =~ "-dict=" ]]; then 153 if [ -f "$FUZZER.dict" ]; then 154 CMD_LINE="$CMD_LINE -dict=$FUZZER.dict" 155 fi 156 fi 157 158 CMD_LINE="$CMD_LINE < /dev/null" 159 160fi 161 162echo $CMD_LINE 163 164# Unset OUT so the fuzz target can't rely on it. 165unset OUT 166 167if [ ! -z "$DEBUGGER" ]; then 168 CMD_LINE="$DEBUGGER $CMD_LINE" 169fi 170 171bash -c "$CMD_LINE" 172 173