From 9934f3769a47d898a8543dfdc64486357e8fdb76 Mon Sep 17 00:00:00 2001 From: Erik Burton Date: Wed, 6 Nov 2024 16:25:42 -0800 Subject: [PATCH] fix: fuzzing timeout calculation --- fuzz/fuzz_all_native.py | 26 +++++++++++++++----------- tools/bin/go_core_fuzz | 8 ++++---- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/fuzz/fuzz_all_native.py b/fuzz/fuzz_all_native.py index 27ba4d419bd..b934bf658b7 100755 --- a/fuzz/fuzz_all_native.py +++ b/fuzz/fuzz_all_native.py @@ -6,6 +6,7 @@ import re import subprocess import sys +import time def main(): parser = argparse.ArgumentParser( @@ -22,37 +23,40 @@ 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) for fuzzfn, path in fuzzers.items(): print(f"{fuzzfn} in {path}", file=sys.stderr) + # 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': + if os.getenv('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)) + durations_seconds = [45] 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 = {} diff --git a/tools/bin/go_core_fuzz b/tools/bin/go_core_fuzz index 2c50a612f90..c97b238c2ee 100755 --- a/tools/bin/go_core_fuzz +++ b/tools/bin/go_core_fuzz @@ -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 >= 60 )); 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