xref: /aosp_15_r20/external/bc/scripts/fuzz_prep.sh (revision 5a6e848804d15c18a0125914844ee4eb0bda4fcf)
1*5a6e8488SAndroid Build Coastguard Worker#! /bin/sh
2*5a6e8488SAndroid Build Coastguard Worker#
3*5a6e8488SAndroid Build Coastguard Worker# SPDX-License-Identifier: BSD-2-Clause
4*5a6e8488SAndroid Build Coastguard Worker#
5*5a6e8488SAndroid Build Coastguard Worker# Copyright (c) 2018-2024 Gavin D. Howard and contributors.
6*5a6e8488SAndroid Build Coastguard Worker#
7*5a6e8488SAndroid Build Coastguard Worker# Redistribution and use in source and binary forms, with or without
8*5a6e8488SAndroid Build Coastguard Worker# modification, are permitted provided that the following conditions are met:
9*5a6e8488SAndroid Build Coastguard Worker#
10*5a6e8488SAndroid Build Coastguard Worker# * Redistributions of source code must retain the above copyright notice, this
11*5a6e8488SAndroid Build Coastguard Worker#   list of conditions and the following disclaimer.
12*5a6e8488SAndroid Build Coastguard Worker#
13*5a6e8488SAndroid Build Coastguard Worker# * Redistributions in binary form must reproduce the above copyright notice,
14*5a6e8488SAndroid Build Coastguard Worker#   this list of conditions and the following disclaimer in the documentation
15*5a6e8488SAndroid Build Coastguard Worker#   and/or other materials provided with the distribution.
16*5a6e8488SAndroid Build Coastguard Worker#
17*5a6e8488SAndroid Build Coastguard Worker# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*5a6e8488SAndroid Build Coastguard Worker# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*5a6e8488SAndroid Build Coastguard Worker# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*5a6e8488SAndroid Build Coastguard Worker# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*5a6e8488SAndroid Build Coastguard Worker# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*5a6e8488SAndroid Build Coastguard Worker# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*5a6e8488SAndroid Build Coastguard Worker# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*5a6e8488SAndroid Build Coastguard Worker# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*5a6e8488SAndroid Build Coastguard Worker# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*5a6e8488SAndroid Build Coastguard Worker# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*5a6e8488SAndroid Build Coastguard Worker# POSSIBILITY OF SUCH DAMAGE.
28*5a6e8488SAndroid Build Coastguard Worker#
29*5a6e8488SAndroid Build Coastguard Worker
30*5a6e8488SAndroid Build Coastguard Worker# Just print the usage and exit with an error. This can receive a message to
31*5a6e8488SAndroid Build Coastguard Worker# print.
32*5a6e8488SAndroid Build Coastguard Worker# @param 1  A message to print.
33*5a6e8488SAndroid Build Coastguard Workerusage() {
34*5a6e8488SAndroid Build Coastguard Worker	if [ $# -eq 1 ]; then
35*5a6e8488SAndroid Build Coastguard Worker		printf '%s\n\n' "$1"
36*5a6e8488SAndroid Build Coastguard Worker	fi
37*5a6e8488SAndroid Build Coastguard Worker	printf 'usage: %s [-a] [afl_compiler]\n' "$0"
38*5a6e8488SAndroid Build Coastguard Worker	printf '\n'
39*5a6e8488SAndroid Build Coastguard Worker	printf '       If -a is given, then an ASan ready build is created.\n'
40*5a6e8488SAndroid Build Coastguard Worker	printf '       Otherwise, a normal fuzz build is created.\n'
41*5a6e8488SAndroid Build Coastguard Worker	printf '       The ASan-ready build is for running under\n'
42*5a6e8488SAndroid Build Coastguard Worker	printf '       `tests/afl.py --asan`, which checks that there were no\n'
43*5a6e8488SAndroid Build Coastguard Worker	printf '       memory errors in any path found by the fuzzer.\n'
44*5a6e8488SAndroid Build Coastguard Worker	printf '       It might also be useful to run scripts/randmath.py on an\n'
45*5a6e8488SAndroid Build Coastguard Worker	printf '       ASan-ready binary.\n'
46*5a6e8488SAndroid Build Coastguard Worker	exit 1
47*5a6e8488SAndroid Build Coastguard Worker}
48*5a6e8488SAndroid Build Coastguard Worker
49*5a6e8488SAndroid Build Coastguard Workerscript="$0"
50*5a6e8488SAndroid Build Coastguard Workerscriptdir=$(dirname "$script")
51*5a6e8488SAndroid Build Coastguard Worker
52*5a6e8488SAndroid Build Coastguard Worker. "$scriptdir/functions.sh"
53*5a6e8488SAndroid Build Coastguard Worker
54*5a6e8488SAndroid Build Coastguard Workerasan=0
55*5a6e8488SAndroid Build Coastguard Worker
56*5a6e8488SAndroid Build Coastguard Worker# Process command-line arguments.
57*5a6e8488SAndroid Build Coastguard Workerwhile getopts "a" opt; do
58*5a6e8488SAndroid Build Coastguard Worker
59*5a6e8488SAndroid Build Coastguard Worker	case "$opt" in
60*5a6e8488SAndroid Build Coastguard Worker		a) asan=1 ;;
61*5a6e8488SAndroid Build Coastguard Worker		?) usage "Invalid option: $opt" ;;
62*5a6e8488SAndroid Build Coastguard Worker	esac
63*5a6e8488SAndroid Build Coastguard Worker
64*5a6e8488SAndroid Build Coastguard Workerdone
65*5a6e8488SAndroid Build Coastguard Workershift $(($OPTIND - 1))
66*5a6e8488SAndroid Build Coastguard Worker
67*5a6e8488SAndroid Build Coastguard Workerif [ $# -lt 1 ]; then
68*5a6e8488SAndroid Build Coastguard Worker	CC=afl-clang-lto
69*5a6e8488SAndroid Build Coastguard Workerelse
70*5a6e8488SAndroid Build Coastguard Worker	CC="$1"
71*5a6e8488SAndroid Build Coastguard Workerfi
72*5a6e8488SAndroid Build Coastguard Worker
73*5a6e8488SAndroid Build Coastguard Worker# We want this for extra sensitive crashing
74*5a6e8488SAndroid Build Coastguard WorkerAFL_HARDEN=1
75*5a6e8488SAndroid Build Coastguard Worker
76*5a6e8488SAndroid Build Coastguard Workercd "$scriptdir/.."
77*5a6e8488SAndroid Build Coastguard Worker
78*5a6e8488SAndroid Build Coastguard Workerset -e
79*5a6e8488SAndroid Build Coastguard Worker
80*5a6e8488SAndroid Build Coastguard WorkerCFLAGS="-flto -fstack-protector-all -fsanitize=shadow-call-stack -fvisibility=hidden"
81*5a6e8488SAndroid Build Coastguard Worker
82*5a6e8488SAndroid Build Coastguard Workerif [ "$asan" -ne 0 ]; then
83*5a6e8488SAndroid Build Coastguard Worker	CFLAGS="$CFLAGS -fsanitize=address"
84*5a6e8488SAndroid Build Coastguard Workerfi
85*5a6e8488SAndroid Build Coastguard Worker
86*5a6e8488SAndroid Build Coastguard Worker# These are to get better instrumentation.
87*5a6e8488SAndroid Build Coastguard Workerexport AFL_LLVM_LAF_SPLIT_SWITCHES=1
88*5a6e8488SAndroid Build Coastguard Workerexport AFL_LLVM_LAF_TRANSFORM_COMPARES=1
89*5a6e8488SAndroid Build Coastguard Workerexport AFL_LLVM_LAF_SPLIT_COMPARES=1
90*5a6e8488SAndroid Build Coastguard Workerexport AFL_LLVM_LTO_CALLER=1
91*5a6e8488SAndroid Build Coastguard Workerexport AFL_LLVM_LTO_CALLER_DEPTH=5
92*5a6e8488SAndroid Build Coastguard Worker
93*5a6e8488SAndroid Build Coastguard Worker# We want a debug build because asserts are counted as crashes too.
94*5a6e8488SAndroid Build Coastguard WorkerCC="$CC" CFLAGS="$CFLAGS" ./configure.sh -gO3 -z
95*5a6e8488SAndroid Build Coastguard Worker
96*5a6e8488SAndroid Build Coastguard Workermake -j16
97