|
5 | 5 |
|
6 | 6 | # Build script for oss-fuzz integration. |
7 | 7 | # |
8 | | -# This script builds fuzz targets for use with libfuzzer, honggfuzz, or AFL++. |
9 | | -# It follows the oss-fuzz build conventions: |
10 | | -# - Uses environment variables: $CC, $CXX, $CFLAGS, $CXXFLAGS, $LIB_FUZZING_ENGINE, $OUT |
11 | | -# - Produces binaries in $OUT |
12 | | -# - Creates seed corpus archives as fuzz_<target>_seed_corpus.zip |
13 | | -# |
14 | | -# For local testing with libfuzzer: |
15 | | -# export CC=clang |
16 | | -# export CXX=clang++ |
17 | | -# export CFLAGS="-g -fsanitize=fuzzer-no-link,address" |
18 | | -# export CXXFLAGS="-g -fsanitize=fuzzer-no-link,address" |
19 | | -# export LIB_FUZZING_ENGINE="-fsanitize=fuzzer" |
20 | | -# export OUT=./fuzz-out |
21 | | -# ./build-fuzz.sh |
22 | | -# |
23 | | -# For honggfuzz (local): |
24 | | -# export CC=hfuzz-clang |
25 | | -# export CXX=hfuzz-clang++ |
26 | | -# export LIB_FUZZING_ENGINE="" # honggfuzz provides its own |
27 | | -# export OUT=./fuzz-out |
28 | | -# ./build-fuzz.sh |
29 | | -# |
30 | | -# Outputs: |
31 | | -# $OUT/fuzz_tx - Transaction application fuzzer |
32 | | -# $OUT/fuzz_overlay - Overlay message fuzzer |
33 | | -# $OUT/fuzz_tx_seed_corpus.zip - Seed corpus for tx fuzzer |
34 | | -# $OUT/fuzz_overlay_seed_corpus.zip - Seed corpus for overlay fuzzer |
35 | | - |
36 | | -set -eux |
37 | | - |
38 | | -# Get source directory (where this script lives) |
39 | | -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
40 | | -SRC="${SRC:-$SCRIPT_DIR}" |
41 | | - |
42 | | -# Create output directory |
43 | | -: "${OUT:=./fuzz-out}" |
44 | | -mkdir -p "$OUT" |
45 | | - |
46 | | -# Change to source directory |
47 | | -cd "$SRC" |
48 | | - |
49 | | -# Run autogen if configure doesn't exist |
50 | | -if [ ! -f configure ]; then |
51 | | - ./autogen.sh |
| 8 | +# This script is copy of the "build.sh" integration script |
| 9 | +# that OSS-fuzz uses to build fuzz targets for running on their infrastructure |
| 10 | +# very slightly modified to work with our CI (using ccache + cached build dirs) |
| 11 | + |
| 12 | +SRC_DIR="$(pwd)" |
| 13 | +OUT="${OUT:-$SRC_DIR/fuzz-out}" |
| 14 | +if [[ "$OUT" != /* ]]; then |
| 15 | + OUT="$SRC_DIR/$OUT" |
52 | 16 | fi |
| 17 | +mkdir -p "$OUT" |
53 | 18 |
|
54 | | -# Configure with fuzzing support |
55 | | -# The --enable-fuzz flag: |
56 | | -# - Adds FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION define |
57 | | -# - Sets FUZZING_LIBS from LIB_FUZZING_ENGINE |
58 | | -# - Enables building of fuzz_* targets (via ENABLE_FUZZ in Makefile.am) |
59 | | -./configure \ |
60 | | - --enable-fuzz \ |
61 | | - --without-postgres |
62 | | - |
63 | | -# Build fuzz targets using automake rules |
64 | | -# This builds fuzz_tx, fuzz_overlay, and Soroban targets with proper FUZZ_TARGET_NAME defines |
65 | | -make -j"$(nproc)" fuzz-targets |
66 | | - |
67 | | -# Install fuzz targets to $OUT |
68 | | -# Install all fuzz targets (C++ and Soroban) |
69 | | -for target in tx overlay soroban_expr soroban_wasmi; do |
70 | | - if [ -f "src/fuzz_$target" ]; then |
71 | | - cp "src/fuzz_$target" "$OUT/" |
72 | | - fi |
73 | | -done |
74 | | - |
75 | | -# Generate and package seed corpus (if stellar-core supports it) |
76 | | -# For now, create empty corpus directories |
77 | | -mkdir -p corpus/tx corpus/overlay corpus/soroban_expr corpus/soroban_wasmi |
78 | | - |
79 | | -# Create corpus archives (even if empty, oss-fuzz expects them) |
80 | | -# Create corpus archives for all targets (even if empty, oss-fuzz expects them) |
81 | | -for target in tx overlay soroban_expr soroban_wasmi; do |
82 | | - if [ -d "corpus/$target" ] && [ "$(ls -A "corpus/$target" 2>/dev/null)" ]; then |
83 | | - (cd "corpus/$target" && zip -q "$OUT/fuzz_${target}_seed_corpus.zip" *) |
84 | | - else |
85 | | - # Create empty zip if no corpus |
86 | | - touch empty && zip -q "$OUT/fuzz_${target}_seed_corpus.zip" empty && rm empty |
| 19 | +mkdir -p "build-clang-libfuzzer" |
| 20 | +cd "build-clang-libfuzzer" |
| 21 | + |
| 22 | +#### ccache config |
| 23 | +export CCACHE_DIR="$(pwd)/.ccache" |
| 24 | +export CCACHE_COMPRESS=true |
| 25 | +export CCACHE_COMPRESSLEVEL=9 |
| 26 | +# cache size should be large enough for a full build |
| 27 | +export CCACHE_MAXSIZE=800M |
| 28 | +export CCACHE_CPP2=true |
| 29 | + |
| 30 | +# periodically check to see if caches are old and purge them if so |
| 31 | +CACHE_MAX_DAYS=30 |
| 32 | +if [ -d "${CCACHE_DIR}" ] ; then |
| 33 | + if [ -n "$(find ${CCACHE_DIR} -mtime +$CACHE_MAX_DAYS -print -quit)" ] ; then |
| 34 | + echo Purging old cache dirs "${CCACHE_DIR}" ./target "${HOME}/.cargo/registry" "${HOME}/.cargo/git" |
| 35 | + rm -rf "${CCACHE_DIR}" ./target "${HOME}/.cargo/registry" "${HOME}/.cargo/git" |
87 | 36 | fi |
88 | | -done |
| 37 | +fi |
89 | 38 |
|
90 | | -echo "Build complete. Fuzz targets in $OUT:" |
91 | | -ls -la "$OUT" |
| 39 | +ccache -p |
| 40 | +ccache -s |
| 41 | +ccache -z |
| 42 | + |
| 43 | +. "${HOME}/.cargo/env" |
| 44 | +(cd "${SRC_DIR}" && ./autogen.sh) |
| 45 | + |
| 46 | +# NB: the oss-fuzz driver injects sanitizer flags to CFLAGS, CXXFLAGS |
| 47 | +# and RUSTFLAGS. This overlaps with our own support for sanitizers, |
| 48 | +# but not fatally. It does require us to enable the unified rust |
| 49 | +# build. |
| 50 | +"${SRC_DIR}/configure" --enable-fuzz --enable-unified-rust-unsafe-for-production --disable-postgres --enable-ccache |
| 51 | +make -j $(nproc) |
| 52 | +make -C src fuzz-targets |
| 53 | +cp src/fuzz_* "${OUT}" |
0 commit comments