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# afl++ configuration options. 19# The 'env|grep' setup ensures we do not trigger the linter. 20# The variables need to be set to "1" here - or before running this script. 21 22# AFL++ settings. 23export AFL_LLVM_MODE_WORKAROUND=0 24export AFL_ENABLE_DICTIONARY=0 25export AFL_ENABLE_CMPLOG=1 26export AFL_LAF_CHANCE=3 27 28# Start compiling afl++. 29echo "Copying precompiled afl++" 30 31# Copy afl++ tools necessary for fuzzing. 32pushd $SRC/aflplusplus > /dev/null 33 34cp -f libAFLDriver.a $LIB_FUZZING_ENGINE 35 36# Some important projects include libraries, copy those even when they don't 37# start with "afl-". Use "sort -u" to avoid a warning about duplicates. 38ls afl-* *.txt *.a *.o *.so | sort -u | xargs cp -t $OUT 39export CC="$SRC/aflplusplus/afl-clang-fast" 40export CXX="$SRC/aflplusplus/afl-clang-fast++" 41 42# Set sane afl++ environment defaults: 43# Be quiet, otherwise this can break some builds. 44export AFL_QUIET=1 45# No leak errors during builds. 46export ASAN_OPTIONS="detect_leaks=0:symbolize=0:detect_odr_violation=0:abort_on_error=1" 47 48# AFL compile option roulette. It is OK if they all happen together. 49 50# 20% chance for CTX-2 coverage instrumentation (Caller conTeXt sensitive 51# edge coverage). 52test $(($RANDOM % 100)) -lt 20 && { 53 export AFL_LLVM_INSTRUMENT=CLASSIC,CTX-2 54 export AFL_ENABLE_CMPLOG=0 55 export AFL_LAF_CHANCE=30 56} 57 58# 40% chance to create a dictionary. 59test $(($RANDOM % 100)) -lt 40 && { 60 export AFL_ENABLE_DICTIONARY=1 61} 62 63# 60% chance to perform CMPLOG/REDQUEEN. 64rm -f "$OUT/afl_cmplog.txt" 65test "$AFL_ENABLE_CMPLOG" = "1" -a $(($RANDOM % 100)) -lt 60 && { 66 export AFL_LLVM_CMPLOG=1 67 touch "$OUT/afl_cmplog.txt" 68} 69 70# 3% chance to perform COMPCOV/LAF_INTEL. 71test $(($RANDOM % 100)) -lt $AFL_LAF_CHANCE && { 72 export AFL_LLVM_LAF_ALL=1 73} 74 75# Create a dictionary if one is wanted. 76test "$AFL_ENABLE_DICTIONARY" = "1" && { 77 export AFL_LLVM_DICT2FILE="$OUT/afl++.dict" 78} 79 80# In case afl-clang-fast ever breaks, this is a workaround: 81test "$AFL_LLVM_MODE_WORKAROUND" = "1" && { 82 export CC=clang 83 export CXX=clang++ 84 WORKAROUND_FLAGS=-fsanitize-coverage=trace-pc-guard 85 # We can still do CMPLOG light: 86 test -e "$OUT/afl_cmplog.txt" && { 87 WORKAROUND_FLAGS="$WORKAROUND_FLAGS",trace-cmp 88 } 89 export CFLAGS="$CFLAGS $WORKAROUND_FLAGS" 90 export CXXFLAGS="$CXXFLAGS $WORKAROUND_FLAGS" 91 unset AFL_LLVM_LAF_ALL 92 unset AFL_LLVM_DICT2FILE 93 unset AFL_ENABLE_DICTIONARY 94 # We need to create a new fuzzer lib however. 95 ar ru libAFLDrivernew.a afl-compiler-rt.o utils/aflpp_driver/aflpp_driver.o 96 cp -f libAFLDrivernew.a $LIB_FUZZING_ENGINE 97} 98 99# Provide a way to document the afl++ options used in this build: 100echo 101echo afl++ target compilation setup: 102env | grep AFL_ | tee "$OUT/afl_options.txt" 103echo 104 105popd > /dev/null 106 107echo " done." 108