1#!/bin/bash -eux
2# Copyright 2020 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# Note: This project creates Rust fuzz targets exclusively
19
20# recipe:
21# -------
22# 1. we list all the fuzzers and save to a file fuzzer_list
23# 2. we build the corpus for each fuzzer
24# 3. we build all the fuzzers
25
26# reset flags of OSS-Fuzz
27export CFLAGS="-O1 -fno-omit-frame-pointer -gline-tables-only -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION"
28export CXXFLAGS_EXTRA="-stdlib=libc++"
29export CXXFLAGS="$CFLAGS $CXXFLAGS_EXTRA"
30
31# correct workdir
32cd $SRC/libra/testsuite/libra-fuzzer
33
34# fetch all dependencies (needed for patching rocksdb)
35cargo fetch
36
37# patch rocksdb to not link libc++ statically
38sed -i "s/link_cpp(&mut build)/build.cpp_link_stdlib(None)/" \
39    /rust/git/checkouts/rust-rocksdb-a9a28e74c6ead8ef/72e45c3/librocksdb_sys/build.rs
40# so now we need to link libc++ at the end
41export RUSTFLAGS="-C link-arg=-L/usr/local/lib -C link-arg=-lc++"
42
43# 1. list fuzzers
44cargo run --bin libra-fuzzer list --no-desc > fuzzer_list
45
46# 2. build corpus and move to $OUT
47cat fuzzer_list | while read -r line
48do
49    cargo run --bin libra-fuzzer generate -n 128 $line
50    zip -r $OUT/"$line"_seed_corpus.zip fuzz/corpus/$line
51    rm -r fuzz/corpus/$line
52done
53
54# rust libfuzzer flags (https://github.com/rust-fuzz/libfuzzer/blob/master/build.rs#L12)
55export CUSTOM_LIBFUZZER_PATH="$LIB_FUZZING_ENGINE_DEPRECATED"
56export CUSTOM_LIBFUZZER_STD_CXX=c++
57# export CUSTOM_LIBFUZZER_STD_CXX=none
58
59# export fuzzing flags
60RUSTFLAGS="$RUSTFLAGS --cfg fuzzing"          # used to change code logic
61RUSTFLAGS="$RUSTFLAGS -Cdebug-assertions"     # to get debug_assert in rust
62RUSTFLAGS="$RUSTFLAGS -Zsanitizer=address"    # address sanitizer (ASAN)
63
64RUSTFLAGS="$RUSTFLAGS -Cdebuginfo=1"
65RUSTFLAGS="$RUSTFLAGS -Cforce-frame-pointers"
66
67RUSTFLAGS="$RUSTFLAGS -Cpasses=sancov"
68RUSTFLAGS="$RUSTFLAGS -Cllvm-args=-sanitizer-coverage-level=4"
69RUSTFLAGS="$RUSTFLAGS -Cllvm-args=-sanitizer-coverage-trace-compares"
70RUSTFLAGS="$RUSTFLAGS -Cllvm-args=-sanitizer-coverage-inline-8bit-counters"
71RUSTFLAGS="$RUSTFLAGS -Cllvm-args=-sanitizer-coverage-trace-geps"
72RUSTFLAGS="$RUSTFLAGS -Cllvm-args=-sanitizer-coverage-prune-blocks=0"
73RUSTFLAGS="$RUSTFLAGS -Cllvm-args=-sanitizer-coverage-pc-table"
74RUSTFLAGS="$RUSTFLAGS -Clink-dead-code"
75RUSTFLAGS="$RUSTFLAGS -Cllvm-args=-sanitizer-coverage-stack-depth"
76RUSTFLAGS="$RUSTFLAGS -Ccodegen-units=1"
77
78export RUSTFLAGS
79
80# 3. build all the fuzzers!
81cat fuzzer_list | while read -r line
82do
83    # build
84    export SINGLE_FUZZ_TARGET="$line"
85    cargo build --manifest-path fuzz/Cargo.toml --bin fuzzer_builder --release --target x86_64-unknown-linux-gnu
86    # move fuzzer to $OUT
87    mv $SRC/libra/target/x86_64-unknown-linux-gnu/release/fuzzer_builder $OUT/$SINGLE_FUZZ_TARGET
88done
89