• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
25
26# Start compiling afl++.
27echo "Copying precompiled afl++"
28
29# Copy afl++ tools necessary for fuzzing.
30pushd $SRC/aflplusplus > /dev/null
31
32cp -f libAFLDriver.a $LIB_FUZZING_ENGINE
33
34# Some important projects include libraries, copy those even when they don't
35# start with "afl-". Use "sort -u" to avoid a warning about duplicates.
36ls afl-* *.txt *.a *.o *.so | sort -u | xargs cp -t $OUT
37export CC="$SRC/aflplusplus/afl-clang-fast"
38export CXX="$SRC/aflplusplus/afl-clang-fast++"
39
40# Set sane afl++ environment defaults:
41# Be quiet, otherwise this can break some builds.
42export AFL_QUIET=1
43# No leak errors during builds.
44export ASAN_OPTIONS="detect_leaks=0:symbolize=0:detect_odr_violation=0:abort_on_error=1"
45
46# AFL compile option roulette. It is OK if they all happen together.
47
48# 40% chance to perform CMPLOG
49rm -f "$OUT/afl_cmplog.txt"
50test $(($RANDOM % 10)) -lt 4 && {
51  export AFL_LLVM_CMPLOG=1
52  touch "$OUT/afl_cmplog.txt"
53}
54
55# 10% chance to perform LAF_INTEL
56test $(($RANDOM % 10)) -lt 1 && {
57  export AFL_LLVM_LAF_ALL=1
58}
59
60# If the targets wants a dictionary - then create one.
61test "$AFL_ENABLE_DICTIONARY" = "1" && {
62  export AFL_LLVM_DICT2FILE="$OUT/afl++.dict"
63}
64
65# In case afl-clang-fast ever breaks, this is a workaround:
66test "$AFL_LLVM_MODE_WORKAROUND" = "1" && {
67  export CC=clang
68  export CXX=clang++
69  WORKAROUND_FLAGS=-fsanitize-coverage=trace-pc-guard
70  # We can still do CMPLOG light:
71  test -e "$OUT/afl_cmplog.txt" && {
72    WORKAROUND_FLAGS="$WORKAROUND_FLAGS",trace-cmp
73  }
74  export CFLAGS="$CFLAGS $WORKAROUND_FLAGS"
75  export CXXFLAGS="$CXXFLAGS $WORKAROUND_FLAGS"
76  unset AFL_LLVM_LAF_ALL
77  unset AFL_LLVM_DICT2FILE
78  unset AFL_ENABLE_DICTIONARY
79  # We need to create a new fuzzer lib however.
80  ar ru libAFLDrivernew.a afl-compiler-rt.o utils/aflpp_driver/aflpp_driver.o
81  cp -f libAFLDrivernew.a $LIB_FUZZING_ENGINE
82}
83
84# Provide a way to document the afl++ options used in this build:
85echo
86echo afl++ target compilation setup:
87env | grep AFL_ | tee "$OUT/afl_options.txt"
88echo
89
90popd > /dev/null
91
92echo " done."
93