Skip to content

Commit

Permalink
fix: fuzzing timeout calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
erikburt committed Nov 7, 2024
1 parent 09497d3 commit aea4fa7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
40 changes: 26 additions & 14 deletions fuzz/fuzz_all_native.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import subprocess
import sys
import time

def main():
parser = argparse.ArgumentParser(
Expand All @@ -22,37 +23,48 @@ def main():

# use float for remaining_seconds so we can represent infinity
if args.seconds:
remaining_seconds = float(args.seconds)
total_time = float(args.seconds)
else:
remaining_seconds = float("inf")
total_time = float("inf")

start_time = time.time()
remaining_seconds = total_time

fuzzers = discover_fuzzers(args.go_module_root)
print(f"🐝 Discovered fuzzers:", file=sys.stderr)
num_fuzzers = len(fuzzers)
print(f"🐝 Discovered {num_fuzzers} fuzzers:", file=sys.stderr)
for fuzzfn, path in fuzzers.items():
print(f"{fuzzfn} in {path}", file=sys.stderr)

if num_fuzzers == 0:
print(f"No fuzzers found, this is likely an error. Exiting.")
exit(1)

# run forever or until --seconds, with increasingly longer durations per fuzz run
durations_seconds = itertools.chain([5, 10, 30, 90, 270], itertools.repeat(600))
if args.ci:
if env.GITHUB_EVENT_NAME == 'scheduled':
durations_seconds = [60]
else:
duration_seconds = [45]
else:
# run forever or until --seconds, with increasingly longer durations per fuzz run
durations_seconds = itertools.chain([5, 10, 30, 90, 270], itertools.repeat(600))
# In CI - default to 60s fuzzes for scheduled runs, and 45 seconds for everything else
durations_seconds = [60] if os.getenv('GITHUB_EVENT_NAME') == 'scheduled' else [45]
if args.seconds:
# However, if seconds was specified, evenly divide total time among all fuzzers
# leaving a 5 second buffer for processing/building time between fuzz runs
actual_fuzz_time = total_time - (num_fuzzers * 5)
durations_seconds = [ actual_fuzz_time / num_fuzzers ]

for duration_seconds in durations_seconds:
print(f"🐝 Running each fuzzer for {duration_seconds}s before switching to next fuzzer", file=sys.stderr)
for fuzzfn, path in fuzzers.items():
elapsed_time = time.time() - start_time
remaining_seconds = total_time - elapsed_time

if remaining_seconds <= 0:
print(f"🐝 Time budget of {args.seconds}s is exhausted. Exiting.", file=sys.stderr)
return

next_duration_seconds = min(remaining_seconds, duration_seconds)
remaining_seconds -= next_duration_seconds

print(f"🐝 Running {fuzzfn} in {path} for {next_duration_seconds}s before switching to next fuzzer", file=sys.stderr)
print(f"🐝 Running {fuzzfn} in {path} for {next_duration_seconds}s (Elapsed: {elapsed_time:.2f}s, Remaining: {remaining_seconds:.2f}s)", file=sys.stderr)
run_fuzzer(fuzzfn, path, next_duration_seconds, args.go_module_root)
print(f"🐝 Completed running {fuzzfn} in {path} for {next_duration_seconds}s. Total remaining time is {remaining_seconds}s", file=sys.stderr)
print(f"🐝 Completed running {fuzzfn} in {path} for {next_duration_seconds}s.", file=sys.stderr)

def discover_fuzzers(go_module_root):
fuzzers = {}
Expand Down
8 changes: 4 additions & 4 deletions tools/bin/go_core_fuzz
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ OUTPUT_FILE=${OUTPUT_FILE:-"./output.txt"}
FUZZ_TIMEOUT_MINUTES=${FUZZ_TIMEOUT_MINUTES:-"6"}

TOTAL_SECONDS=$((FUZZ_TIMEOUT_MINUTES * 60))
if (( TOTAL_SECONDS >= 180 )); then
# Allow for a 120 second buffer between the timeout, and fuzz test runtime
FUZZ_SECONDS=$((TOTAL_SECONDS - 120))
if (( TOTAL_SECONDS >= 120 )); then
# Allow for a 60 second buffer between the timeout, and fuzz test runtime
FUZZ_SECONDS=$((TOTAL_SECONDS - 60))
else
echo "Increase FUZZ_TIMEOUT_MINUTES to >=3, received $FUZZ_TIMEOUT_MINUTES"
echo "Increase FUZZ_TIMEOUT_MINUTES to >=2, received $FUZZ_TIMEOUT_MINUTES"
exit 1
fi

Expand Down

0 comments on commit aea4fa7

Please sign in to comment.