• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# Ignore memory leaks from python scripts invoked in the build
4export ASAN_OPTIONS="detect_leaks=0"
5export MSAN_OPTIONS="halt_on_error=0:exitcode=0:report_umrs=0"
6
7# Remove -pthread from CFLAGS, this trips up ./configure
8# which thinks pthreads are available without any CLI flags
9CFLAGS=${CFLAGS//"-pthread"/}
10
11FLAGS=()
12case $SANITIZER in
13  address)
14    FLAGS+=("--with-address-sanitizer")
15    ;;
16  memory)
17    FLAGS+=("--with-memory-sanitizer")
18    # installing ensurepip takes a while with MSAN instrumentation, so
19    # we disable it here
20    FLAGS+=("--without-ensurepip")
21    # -msan-keep-going is needed to allow MSAN's halt_on_error to function
22    FLAGS+=("CFLAGS=-mllvm -msan-keep-going=1")
23    ;;
24  undefined)
25    FLAGS+=("--with-undefined-behavior-sanitizer")
26    ;;
27esac
28./configure "${FLAGS[@]:-}" --prefix $OUT
29
30# We use altinstall to avoid having the Makefile create symlinks
31make -j$(nproc) altinstall
32
33FUZZ_DIR=Modules/_xxtestfuzz
34for fuzz_test in $(cat $FUZZ_DIR/fuzz_tests.txt)
35do
36  # Build (but don't link) the fuzzing stub with a C compiler
37  $CC $CFLAGS $($OUT/bin/python*-config --cflags) $FUZZ_DIR/fuzzer.c \
38    -D _Py_FUZZ_ONE -D _Py_FUZZ_$fuzz_test -c -Wno-unused-function \
39    -o $WORK/$fuzz_test.o
40  # Link with C++ compiler to appease libfuzzer
41  $CXX $CXXFLAGS -rdynamic $WORK/$fuzz_test.o -o $OUT/$fuzz_test \
42    $LIB_FUZZING_ENGINE $($OUT/bin/python*-config --ldflags --embed)
43
44  # Zip up and copy any seed corpus
45  if [ -d "${FUZZ_DIR}/${fuzz_test}_corpus" ]; then
46    zip -j "${OUT}/${fuzz_test}_seed_corpus.zip" ${FUZZ_DIR}/${fuzz_test}_corpus/*
47  fi
48  # Copy over the dictionary for this test
49  if [ -e "${FUZZ_DIR}/dictionaries/${fuzz_test}.dict" ]; then
50    cp "${FUZZ_DIR}/dictionaries/${fuzz_test}.dict" "$OUT/${fuzz_test}.dict"
51  fi
52done
53
54# A little bit hacky but we have to copy $OUT/include to
55# $OUT/$OUT/include as the coverage build needs all source
56# files used in execution and expects it to be there.
57#   See projects/tensorflow/build.sh for prior art
58if [ "$SANITIZER" = "coverage" ]
59then
60  mkdir -p $OUT/$OUT
61  cp -r $OUT/include $OUT/$OUT/
62fi
63