aboutsummaryrefslogtreecommitdiffhomepage
path: root/infra/base-images/base-runner/collect_dft
blob: 3f6c6899c7e1dbc460bc25b950d6dbe2aa8a6f98 (plain)
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
#!/bin/bash -u
# Copyright 2020 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.
#
################################################################################
cd $OUT

if (( $# > 0 )); then
  FUZZ_TARGETS="$@"
else
  FUZZ_TARGETS="$(find . -maxdepth 1 -type f -executable -printf '%P\n')"
fi

# Timeout for running a single fuzz target.
if [ -z "$COLLECT_DFT_TIMEOUT" ]; then
  COLLECT_DFT_TIMEOUT=1h
fi

# Number of CPUs available, this is needed for running targets in parallel.
NPROC=$(nproc)

function run_one_target {
  local target=$1
  local corpus="/corpus/${target}"
  local traces="$OUT/${target}_dft"

  # Put the logs in $OUT as well for debugging purposes.
  local log="$OUT/${target}_dft.log"

  rm -rf $traces && mkdir -p $traces

  timeout $COLLECT_DFT_TIMEOUT dataflow_tracer.py $OUT/$target $corpus $traces &> $log
  if (( $? != 0 )); then
    echo "Error occured while collecting data flow traces for $target:"
    cat $log
  fi
}

# Run each fuzz target, write data flow traces into corresponding dir in $OUT.
for fuzz_target in $FUZZ_TARGETS; do
  # Skip binaries that do not seem to be fuzz targets.
  grep "LLVMFuzzerTestOneInput" $fuzz_target > /dev/null 2>&1 || continue
  
  echo "Running $fuzz_target"
  run_one_target $fuzz_target &

  # Do not spawn more processes than the number of CPUs available.
  n_child_proc=$(jobs -rp | wc -l)
  while [ "$n_child_proc" -eq "$NPROC" ]; do
    sleep 4
    n_child_proc=$(jobs -rp | wc -l)
  done
done

# Wait for background processes to finish.
wait