-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
compile
executable file
·402 lines (346 loc) · 14.6 KB
/
compile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/bin/bash -eu
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
echo "---------------------------------------------------------------"
sysctl -w vm.mmap_rnd_bits=28
OSS_FUZZ_ON_DEMAND="${OSS_FUZZ_ON_DEMAND:-0}"
# Used for Rust introspector builds
RUST_SANITIZER=$SANITIZER
if [ "$FUZZING_LANGUAGE" = "jvm" ]; then
if [ "$FUZZING_ENGINE" != "libfuzzer" ] && [ "$FUZZING_ENGINE" != "wycheproof" ]; then
echo "ERROR: JVM projects can be fuzzed with libFuzzer or tested with wycheproof engines only."
exit 1
fi
if [ "$SANITIZER" != "address" ] && [ "$SANITIZER" != "coverage" ] && [ "$SANITIZER" != "undefined" ] && [ "$SANITIZER" != "none" ] && [ "$SANITIZER" != "introspector" ]; then
echo "ERROR: JVM projects can be fuzzed with AddressSanitizer or UndefinedBehaviorSanitizer or Introspector only."
exit 1
fi
if [ "$ARCHITECTURE" != "x86_64" ]; then
echo "ERROR: JVM projects can be fuzzed on x86_64 architecture only."
exit 1
fi
fi
if [ "$FUZZING_LANGUAGE" = "rust" ]; then
if [ "$SANITIZER" = "introspector" ]; then
# introspector sanitizer flag will cause cargo build to fail. Rremove it
# temporarily, RUST_SANITIZER will hold the original sanitizer.
export SANITIZER=address
fi
fi
if [ "$FUZZING_LANGUAGE" = "javascript" ]; then
if [ "$FUZZING_ENGINE" != "libfuzzer" ]; then
echo "ERROR: JavaScript projects can be fuzzed with libFuzzer engine only."
exit 1
fi
if [ "$SANITIZER" != "coverage" ] && [ "$SANITIZER" != "none" ]; then
echo "ERROR: JavaScript projects cannot be fuzzed with sanitizers."
exit 1
fi
if [ "$ARCHITECTURE" != "x86_64" ]; then
echo "ERROR: JavaScript projects can be fuzzed on x86_64 architecture only."
exit 1
fi
fi
if [ "$FUZZING_LANGUAGE" = "python" ]; then
if [ "$FUZZING_ENGINE" != "libfuzzer" ]; then
echo "ERROR: Python projects can be fuzzed with libFuzzer engine only."
exit 1
fi
if [ "$SANITIZER" != "address" ] && [ "$SANITIZER" != "undefined" ] && [ "$SANITIZER" != "coverage" ] && [ "$SANITIZER" != "introspector" ]; then
echo "ERROR: Python projects can be fuzzed with AddressSanitizer or UndefinedBehaviorSanitizer or Coverage or Fuzz Introspector only."
exit 1
fi
if [ "$ARCHITECTURE" != "x86_64" ]; then
echo "ERROR: Python projects can be fuzzed on x86_64 architecture only."
exit 1
fi
fi
if [ -z "${SANITIZER_FLAGS-}" ]; then
FLAGS_VAR="SANITIZER_FLAGS_${SANITIZER}"
export SANITIZER_FLAGS=${!FLAGS_VAR-}
fi
if [[ $ARCHITECTURE == "i386" ]]; then
export CFLAGS="-m32 $CFLAGS"
cp -R /usr/i386/lib/* /usr/local/lib
cp -R /usr/i386/include/* /usr/local/include
fi
# Don't use a fuzzing engine with Jazzer which has libFuzzer built-in or with
# FuzzBench which will provide the fuzzing engine.
if [[ $FUZZING_ENGINE != "none" ]] && [[ $FUZZING_LANGUAGE != "jvm" ]] && [[ "${OSS_FUZZ_ON_DEMAND}" == "0" ]] ; then
# compile script might override environment, use . to call it.
. compile_${FUZZING_ENGINE}
fi
if [[ $SANITIZER_FLAGS = *sanitize=memory* ]]
then
# Take all libraries from lib/msan
# export CXXFLAGS_EXTRA="-L/usr/msan/lib $CXXFLAGS_EXTRA"
cp -R /usr/msan/lib/* /usr/local/lib/x86_64-unknown-linux-gnu/
cp -R /usr/msan/include/* /usr/local/include
echo 'Building without MSan instrumented libraries.'
fi
# Coverage flag overrides.
COVERAGE_FLAGS_VAR="COVERAGE_FLAGS_${SANITIZER}"
if [[ -n ${!COVERAGE_FLAGS_VAR+x} ]]
then
export COVERAGE_FLAGS="${!COVERAGE_FLAGS_VAR}"
fi
# Only need the default coverage instrumentation for libFuzzer or honggfuzz.
# Other engines bring their own.
if [ $FUZZING_ENGINE = "none" ] || [ $FUZZING_ENGINE = "afl" ] || [ $FUZZING_ENGINE = "centipede" ] || [ "${OSS_FUZZ_ON_DEMAND}" != "0" ]; then
export COVERAGE_FLAGS=
fi
# Rust does not support sanitizers and coverage flags via CFLAGS/CXXFLAGS, so
# use RUSTFLAGS.
# FIXME: Support code coverage once support is in.
# See https://github.com/rust-lang/rust/issues/34701.
if [ "$RUST_SANITIZER" == "introspector" ]; then
export RUSTFLAGS="-Cdebuginfo=2 -Cforce-frame-pointers"
elif [ "$SANITIZER" != "undefined" ] && [ "$SANITIZER" != "coverage" ] && [ "$SANITIZER" != "none" ] && [ "$ARCHITECTURE" != 'i386' ]; then
export RUSTFLAGS="--cfg fuzzing -Zsanitizer=${SANITIZER} -Cdebuginfo=1 -Cforce-frame-pointers"
else
export RUSTFLAGS="--cfg fuzzing -Cdebuginfo=1 -Cforce-frame-pointers"
fi
if [ "$SANITIZER" = "coverage" ]
then
# link to C++ from comment in f5098035eb1a14aa966c8651d88ea3d64323823d
export RUSTFLAGS="$RUSTFLAGS -Cinstrument-coverage -C link-arg=-lc++"
fi
# Add Rust libfuzzer flags.
# See https://github.com/rust-fuzz/libfuzzer/blob/master/build.rs#L12.
export CUSTOM_LIBFUZZER_PATH="$LIB_FUZZING_ENGINE_DEPRECATED"
export CUSTOM_LIBFUZZER_STD_CXX=c++
export CFLAGS="$CFLAGS $SANITIZER_FLAGS $COVERAGE_FLAGS"
export CXXFLAGS="$CFLAGS $CXXFLAGS_EXTRA"
if [ "$SANITIZER" = "undefined" ]; then
# Disable "function" sanitizer for C code for now, because many projects,
# possibly via legacy C code are affected.
# The projects should be fixed and this workaround be removed in the future.
# TODO(#11778):
# https://github.com/google/oss-fuzz/issues/11778
export CFLAGS="$CFLAGS -fno-sanitize=function"
fi
if [ "$FUZZING_LANGUAGE" = "go" ]; then
# required by Go 1.20
export CXX="${CXX} -lresolv"
fi
if [ "$FUZZING_LANGUAGE" = "python" ]; then
sanitizer_with_fuzzer_lib_dir=`python3 -c "import atheris; import os; print(atheris.path())"`
sanitizer_with_fuzzer_output_lib=$OUT/sanitizer_with_fuzzer.so
if [ "$SANITIZER" = "address" ]; then
cp $sanitizer_with_fuzzer_lib_dir/asan_with_fuzzer.so $sanitizer_with_fuzzer_output_lib
elif [ "$SANITIZER" = "undefined" ]; then
cp $sanitizer_with_fuzzer_lib_dir/ubsan_with_fuzzer.so $sanitizer_with_fuzzer_output_lib
fi
# Disable leak checking as it is unsupported.
export CFLAGS="$CFLAGS -fno-sanitize=function,leak,vptr,"
export CXXFLAGS="$CXXFLAGS -fno-sanitize=function,leak,vptr"
fi
# Copy latest llvm-symbolizer in $OUT for stack symbolization.
cp $(which llvm-symbolizer) $OUT/
# Copy Jazzer to $OUT if needed.
if [ "$FUZZING_LANGUAGE" = "jvm" ]; then
cp $(which jazzer_agent_deploy.jar) $(which jazzer_driver) $(which jazzer_junit.jar) $OUT/
jazzer_driver_with_sanitizer=$OUT/jazzer_driver_with_sanitizer
if [ "$SANITIZER" = "address" ]; then
cat > $jazzer_driver_with_sanitizer << 'EOF'
#!/bin/bash
this_dir=$(dirname "$0")
"$this_dir/jazzer_driver" --asan "$@"
EOF
elif [ "$SANITIZER" = "undefined" ]; then
cat > $jazzer_driver_with_sanitizer << 'EOF'
#!/bin/bash
this_dir=$(dirname "$0")
"$this_dir/jazzer_driver" --ubsan "$@"
EOF
elif [ "$SANITIZER" = "coverage" ] || [ "$SANITIZER" = "introspector" ]; then
# Coverage & introspector builds require no instrumentation.
cp $(which jazzer_driver) $jazzer_driver_with_sanitizer
fi
chmod +x $jazzer_driver_with_sanitizer
# Disable leak checking since the JVM triggers too many false positives.
export CFLAGS="$CFLAGS -fno-sanitize=leak"
export CXXFLAGS="$CXXFLAGS -fno-sanitize=leak"
fi
if [ "$SANITIZER" = "introspector" ] || [ "$RUST_SANITIZER" = "introspector" ]; then
export AR=llvm-ar
export NM=llvm-nm
export RANLIB=llvm-ranlib
export CFLAGS="$CFLAGS -g"
export CXXFLAGS="$CXXFLAGS -g"
export FI_BRANCH_PROFILE=1
export FUZZ_INTROSPECTOR=1
export FUZZ_INTROSPECTOR_AUTO_FUZZ=1
# Move ar and ranlib
mv /usr/bin/ar /usr/bin/old-ar
mv /usr/bin/nm /usr/bin/old-nm
mv /usr/bin/ranlib /usr/bin/old-ranlib
ln -sf /usr/local/bin/llvm-ar /usr/bin/ar
ln -sf /usr/local/bin/llvm-nm /usr/bin/nm
ln -sf /usr/local/bin/llvm-ranlib /usr/bin/ranlib
apt-get install -y libjpeg-dev zlib1g-dev libyaml-dev
python3 -m pip install --upgrade pip setuptools
python3 -m pip install cxxfilt pyyaml beautifulsoup4 lxml soupsieve rust-demangler
python3 -m pip install --prefer-binary matplotlib
# Install Fuzz-Introspector
pushd /fuzz-introspector/src
python3 -m pip install .
popd
if [ "$FUZZING_LANGUAGE" = "python" ]; then
python3 /fuzz-introspector/src/main.py light --language=python
elif [ "$FUZZING_LANGUAGE" = "jvm" ]; then
python3 /fuzz-introspector/src/main.py light --language=jvm
elif [ "$FUZZING_LANGUAGE" = "rust" ]; then
python3 /fuzz-introspector/src/main.py light --language=rust
else
python3 /fuzz-introspector/src/main.py light
fi
rsync -avu --delete "$SRC/inspector/" "$OUT/inspector"
fi
echo "---------------------------------------------------------------"
echo "CC=$CC"
echo "CXX=$CXX"
echo "CFLAGS=$CFLAGS"
echo "CXXFLAGS=$CXXFLAGS"
echo "RUSTFLAGS=$RUSTFLAGS"
echo "---------------------------------------------------------------"
if [ "${OSS_FUZZ_ON_DEMAND}" != "0" ]; then
fuzzbench_build
cp $(which llvm-symbolizer) $OUT/
exit 0
fi
if [[ ! -z "${CAPTURE_REPLAY_SCRIPT-}" ]]; then
# Capture a replaying build script which can be used for replaying the build
# after a vanilla build. This script is meant to be used in a cached
# container.
python3 -m pip install bashlex
python3 /usr/local/bin/bash_parser.py $SRC/build.sh
fi
# Prepare the build command to run the project's build script.
if [[ ! -z "${REPLAY_ENABLED-}" ]]; then
# If this is a replay, then use replay_build.sh. This is expected to be
# running in a cached container where a build has already happened prior.
BUILD_CMD="bash -eux $SRC/replay_build.sh"
else
BUILD_CMD="bash -eux $SRC/build.sh"
fi
# Set +u temporarily to continue even if GOPATH and OSSFUZZ_RUSTPATH are undefined.
set +u
# We need to preserve source code files for generating a code coverage report.
# We need exact files that were compiled, so copy both $SRC and $WORK dirs.
COPY_SOURCES_CMD="cp -rL --parents $SRC $WORK /usr/include /usr/local/include $GOPATH $OSSFUZZ_RUSTPATH /rustc $OUT"
set -u
if [ "$FUZZING_LANGUAGE" = "rust" ]; then
# Copy rust std lib to its path with a hash.
export rustch=`rustc --version --verbose | grep commit-hash | cut -d' ' -f2`
mkdir -p /rustc/$rustch/
export rustdef=`rustup toolchain list | grep default | cut -d' ' -f1`
cp -r /rust/rustup/toolchains/$rustdef/lib/rustlib/src/rust/library/ /rustc/$rustch/
fi
if [ "${BUILD_UID-0}" -ne "0" ]; then
adduser -u $BUILD_UID --disabled-password --gecos '' builder
chown -R builder $SRC $OUT $WORK
su -c "$BUILD_CMD" builder
if [ "$SANITIZER" = "coverage" ]; then
# Some directories have broken symlinks (e.g. honggfuzz), ignore the errors.
su -c "$COPY_SOURCES_CMD" builder 2>/dev/null || true
fi
else
$BUILD_CMD
if [ "$SANITIZER" = "coverage" ]; then
# Some directories have broken symlinks (e.g. honggfuzz), ignore the errors.
$COPY_SOURCES_CMD 2>/dev/null || true
fi
fi
if [ "$SANITIZER" = "introspector" ] || [ "$RUST_SANITIZER" = "introspector" ]; then
unset CXXFLAGS
unset CFLAGS
export G_ANALYTICS_TAG="G-8WTFM1Y62J"
if [ "$FUZZING_LANGUAGE" = "jvm" ]; then
echo "GOING jvm route"
set -x
# Output will be put in /out/
python3 /fuzz-introspector/frontends/java/oss-fuzz-main.py
# Move files temporarily to fit workflow of other languages.
mkdir -p $SRC/my-fi-data
find $OUT/ -name *.data -exec mv {} $SRC/my-fi-data/ \;
find $OUT/ -name *.data.yaml -exec mv {} $SRC/my-fi-data/ \;
elif [ "$FUZZING_LANGUAGE" = "rust" ]; then
echo "GOING rust route"
# Run the rust frontend
pushd /fuzz-introspector/frontends/rust/rust_function_analyser
cargo run -- $SRC
# Move files temporarily to fix workflow of other languages.
mkdir -p $SRC/my-fi-data
find ./ -name "*.data" -exec mv {} $SRC/my-fi-data/ \;
find ./ -name "*.data.yaml" -exec mv {} $SRC/my-fi-data/ \;
popd
# Restore the sanitizer flag for rust
export SANITIZER="introspector"
fi
mkdir -p $SRC/inspector
find $SRC/ -name "fuzzerLogFile-*.data" -exec cp {} $SRC/inspector/ \;
find $SRC/ -name "fuzzerLogFile-*.data.yaml" -exec cp {} $SRC/inspector/ \;
find $SRC/ -name "fuzzerLogFile-*.data.debug_*" -exec cp {} $SRC/inspector/ \;
find $SRC/ -name "allFunctionsWithMain-*.yaml" -exec cp {} $SRC/inspector/ \;
# Move coverage report.
if [ -d "$OUT/textcov_reports" ]
then
find $OUT/textcov_reports/ -name "*.covreport" -exec cp {} $SRC/inspector/ \;
find $OUT/textcov_reports/ -name "*.json" -exec cp {} $SRC/inspector/ \;
fi
cd $SRC/inspector
# Make fuzz-introspector HTML report.
REPORT_ARGS="--name=$PROJECT_NAME"
# Only pass coverage_url when COVERAGE_URL is set (in cloud builds)
if [[ ! -z "${COVERAGE_URL+x}" ]]; then
REPORT_ARGS="$REPORT_ARGS --coverage_url=${COVERAGE_URL}"
fi
# Do different things depending on languages
if [ "$FUZZING_LANGUAGE" = "python" ]; then
echo "GOING python route"
set -x
REPORT_ARGS="$REPORT_ARGS --target_dir=$SRC/inspector"
REPORT_ARGS="$REPORT_ARGS --language=python"
python3 /fuzz-introspector/src/main.py report $REPORT_ARGS
rsync -avu --delete "$SRC/inspector/" "$OUT/inspector"
elif [ "$FUZZING_LANGUAGE" = "jvm" ]; then
echo "GOING jvm route"
set -x
find $OUT/ -name "jacoco.xml" -exec cp {} $SRC/inspector/ \;
REPORT_ARGS="$REPORT_ARGS --target_dir=$SRC/inspector"
REPORT_ARGS="$REPORT_ARGS --language=jvm"
python3 /fuzz-introspector/src/main.py report $REPORT_ARGS
rsync -avu --delete "$SRC/inspector/" "$OUT/inspector"
elif [ "$FUZZING_LANGUAGE" = "rust" ]; then
echo "GOING rust route"
REPORT_ARGS="$REPORT_ARGS --target_dir=$SRC/inspector"
REPORT_ARGS="$REPORT_ARGS --language=rust"
python3 /fuzz-introspector/src/main.py report $REPORT_ARGS
rsync -avu --delete "$SRC/inspector/" "$OUT/inspector"
else
# C/C++
# Correlate fuzzer binaries to fuzz-introspector's raw data
python3 /fuzz-introspector/src/main.py correlate --binaries_dir=$OUT/
# Generate fuzz-introspector HTML report, this generates
# the file exe_to_fuzz_introspector_logs.yaml
REPORT_ARGS="$REPORT_ARGS --target_dir=$SRC/inspector"
# Use the just-generated correlation file
REPORT_ARGS="$REPORT_ARGS --correlation_file=exe_to_fuzz_introspector_logs.yaml"
python3 /fuzz-introspector/src/main.py report $REPORT_ARGS
rsync -avu --delete "$SRC/inspector/" "$OUT/inspector"
fi
fi