• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash -u
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################################################################################
17cd $OUT
18
19if (( $# > 0 )); then
20  FUZZ_TARGETS="$@"
21else
22  FUZZ_TARGETS="$(find . -maxdepth 1 -type f -executable -printf '%P\n')"
23fi
24
25# Timeout for running a single fuzz target.
26if [ -z "$COLLECT_DFT_TIMEOUT" ]; then
27  COLLECT_DFT_TIMEOUT=1h
28fi
29
30# Number of CPUs available, this is needed for running targets in parallel.
31NPROC=$(nproc)
32
33function run_one_target {
34  local target=$1
35  local corpus="/corpus/${target}"
36  local traces="$OUT/${target}_dft"
37
38  # Put the logs in $OUT as well for debugging purposes.
39  local log="$OUT/${target}_dft.log"
40
41  rm -rf $traces && mkdir -p $traces
42
43  timeout $COLLECT_DFT_TIMEOUT dataflow_tracer.py $OUT/$target $corpus $traces &> $log
44  if (( $? != 0 )); then
45    echo "Error occured while collecting data flow traces for $target:"
46    cat $log
47  fi
48}
49
50# Run each fuzz target, write data flow traces into corresponding dir in $OUT.
51for fuzz_target in $FUZZ_TARGETS; do
52  # Skip binaries that do not seem to be fuzz targets.
53  grep "LLVMFuzzerTestOneInput" $fuzz_target > /dev/null 2>&1 || continue
54
55  echo "Running $fuzz_target"
56  run_one_target $fuzz_target &
57
58  # Do not spawn more processes than the number of CPUs available.
59  n_child_proc=$(jobs -rp | wc -l)
60  while [ "$n_child_proc" -eq "$NPROC" ]; do
61    sleep 4
62    n_child_proc=$(jobs -rp | wc -l)
63  done
64done
65
66# Wait for background processes to finish.
67wait
68