diff --git a/fuzztest/internal/centipede_adaptor.cc b/fuzztest/internal/centipede_adaptor.cc index fa7bd4d8..2b1a9652 100644 --- a/fuzztest/internal/centipede_adaptor.cc +++ b/fuzztest/internal/centipede_adaptor.cc @@ -575,6 +575,8 @@ bool CentipedeFuzzerAdaptor::Run(int* argc, char*** argv, RunMode mode, runtime_.SetCurrentTest(&test_, &configuration); if (is_running_property_function_in_this_process) { if (IsSilenceTargetEnabled()) SilenceTargetStdoutAndStderr(); + // TODO(b/393582695): Consider whether we need some kind of reporting + // enabled in the controller mode to handle test setup failures. runtime_.EnableReporter(&fuzzer_impl_.stats_, [] { return absl::Now(); }); } fuzzer_impl_.fixture_driver_->SetUpFuzzTest(); diff --git a/fuzztest/internal/runtime.cc b/fuzztest/internal/runtime.cc index 9a0723b6..f77f06af 100644 --- a/fuzztest/internal/runtime.cc +++ b/fuzztest/internal/runtime.cc @@ -957,6 +957,8 @@ void PopulateLimits(const Configuration& configuration, } bool FuzzTestFuzzerImpl::RunInUnitTestMode(const Configuration& configuration) { + runtime_.SetCurrentTest(&test_, &configuration); + runtime_.EnableReporter(&stats_, [] { return absl::Now(); }); runtime_.SetSkippingRequested(false); fixture_driver_->SetUpFuzzTest(); [&] { @@ -968,8 +970,6 @@ bool FuzzTestFuzzerImpl::RunInUnitTestMode(const Configuration& configuration) { } runtime_.StartWatchdog(); PopulateLimits(configuration, execution_coverage_); - runtime_.EnableReporter(&stats_, [] { return absl::Now(); }); - runtime_.SetCurrentTest(&test_, &configuration); // TODO(sbenzaquen): Currently, some infrastructure code assumes that replay // works in unit test mode, so we support it. However, we would like to @@ -979,7 +979,6 @@ bool FuzzTestFuzzerImpl::RunInUnitTestMode(const Configuration& configuration) { if (ReplayInputsIfAvailable(configuration)) { // If ReplayInputs returns, it means the replay didn't crash. // In replay mode, we only replay. - runtime_.DisableReporter(); return; } @@ -1034,9 +1033,10 @@ bool FuzzTestFuzzerImpl::RunInUnitTestMode(const Configuration& configuration) { break; } } - runtime_.SetCurrentTest(nullptr, nullptr); }(); fixture_driver_->TearDownFuzzTest(); + runtime_.DisableReporter(); + runtime_.SetCurrentTest(nullptr, nullptr); return true; } @@ -1128,6 +1128,9 @@ void FuzzTestFuzzerImpl::MinimizeNonFatalFailureLocally(absl::BitGenRef prng) { bool FuzzTestFuzzerImpl::RunInFuzzingMode(int* /*argc*/, char*** /*argv*/, const Configuration& configuration) { + if (IsSilenceTargetEnabled()) SilenceTargetStdoutAndStderr(); + runtime_.SetCurrentTest(&test_, &configuration); + runtime_.EnableReporter(&stats_, [] { return absl::Now(); }); runtime_.SetSkippingRequested(false); fixture_driver_->SetUpFuzzTest(); const bool success = [&] { @@ -1141,11 +1144,6 @@ bool FuzzTestFuzzerImpl::RunInFuzzingMode(int* /*argc*/, char*** /*argv*/, PopulateLimits(configuration, execution_coverage_); runtime_.SetRunMode(RunMode::kFuzz); - if (IsSilenceTargetEnabled()) SilenceTargetStdoutAndStderr(); - - runtime_.EnableReporter(&stats_, [] { return absl::Now(); }); - runtime_.SetCurrentTest(&test_, &configuration); - if (ReplayInputsIfAvailable(configuration)) { // If ReplayInputs returns, it means the replay didn't crash. // We don't want to actually run the fuzzer so exit now. @@ -1257,14 +1255,14 @@ bool FuzzTestFuzzerImpl::RunInFuzzingMode(int* /*argc*/, char*** /*argv*/, } } - runtime_.SetCurrentTest(nullptr, nullptr); - absl::FPrintF(GetStderr(), "\n[.] Fuzzing was terminated.\n"); runtime_.PrintFinalStatsOnDefaultSink(); absl::FPrintF(GetStderr(), "\n"); return true; }(); fixture_driver_->TearDownFuzzTest(); + runtime_.DisableReporter(); + runtime_.SetCurrentTest(nullptr, nullptr); return success; } diff --git a/fuzztest/internal/runtime.h b/fuzztest/internal/runtime.h index 1b83d888..3ac8dd2f 100644 --- a/fuzztest/internal/runtime.h +++ b/fuzztest/internal/runtime.h @@ -161,6 +161,8 @@ class Runtime { void SetRunMode(RunMode run_mode) { run_mode_ = run_mode; } RunMode run_mode() const { return run_mode_; } + // Enables the crash reporter. + // REQUIRES: `SetCurrentTest()` has been called with non-null arguments. void EnableReporter(const RuntimeStats* stats, absl::Time (*clock_fn)()) { reporter_enabled_ = true; stats_ = stats; @@ -176,7 +178,11 @@ class Runtime { UntypedDomain& domain; }; + // Sets the current test and configuration. + // REQUIRES: Before passing null arguments, the reporter must be disabled by + // calling `DisableReporter()`. void SetCurrentTest(const FuzzTest* test, const Configuration* configuration); + void OnTestIterationStart(const absl::Time& start_time) { current_iteration_start_time_ = start_time; test_iteration_started_ = true;