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# Create a directory for instrumented dependencies.
19TOR_DEPS=${SRC}/deps
20mkdir -p $TOR_DEPS
21
22# Build libevent with proper instrumentation.
23cd ${SRC}/libevent
24sh autogen.sh
25./configure --prefix=${TOR_DEPS} --disable-openssl
26make -j$(nproc) clean
27make -j$(nproc) all
28make install
29
30# Build OpenSSL with proper instrumentation.
31cd ${SRC}/openssl
32OPENSSL_CONFIGURE_FLAGS=""
33if [[ $CFLAGS = *sanitize=memory* ]]
34then
35  OPENSSL_CONFIGURE_FLAGS="no-asm"
36fi
37
38./config no-shared --prefix=${TOR_DEPS} \
39    enable-tls1_3 enable-rc5 enable-md2 enable-ec_nistp_64_gcc_128 enable-ssl3 \
40    enable-ssl3-method enable-nextprotoneg enable-weak-ssl-ciphers $CFLAGS \
41    -fno-sanitize=alignment $OPENSSL_CONFIGURE_FLAGS
42
43make -j$(nproc) LDCMD="$CXX $CXXFLAGS"
44make install
45
46# Build zlib with proper instrumentation,
47cd ${SRC}/zlib
48./configure --prefix=${TOR_DEPS}
49make -j$(nproc) clean
50make -j$(nproc) all
51make install
52
53# Build tor and the fuzz targets.
54cd ${SRC}/tor
55
56sh autogen.sh
57
58# We need to run configure with leak-checking disabled, or many of the
59# test functions will fail.
60export ASAN_OPTIONS=detect_leaks=0
61
62./configure --disable-asciidoc --enable-oss-fuzz --disable-memory-sentinels \
63    --with-libevent-dir=${SRC}/deps \
64    --with-openssl-dir=${SRC}/deps \
65    --with-zlib-dir=${SRC}/deps \
66    --disable-gcc-hardening
67
68make clean
69make -j$(nproc) oss-fuzz-fuzzers
70
71TORLIBS="`make show-testing-libs`"
72TORLIBS="$TORLIBS -lm -Wl,-Bstatic -lssl -lcrypto -levent -lz -L${TOR_DEPS}/lib"
73TORLIBS="$TORLIBS -Wl,-Bdynamic"
74
75for fuzzer in src/test/fuzz/*.a; do
76    output="${fuzzer%.a}"
77    output="${output##*lib}"
78    ${CXX} ${CXXFLAGS} -std=c++11 $LIB_FUZZING_ENGINE ${fuzzer} ${TORLIBS} -o ${OUT}/${output}
79
80    corpus_dir="${SRC}/tor-fuzz-corpora/${output#oss-fuzz-}"
81    if [ -d "${corpus_dir}" ]; then
82      set +x
83      zip -q -j ${OUT}/${output}_seed_corpus.zip ${corpus_dir}/*
84      set -x
85    fi
86done
87