Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable crash reporting during fuzz test setup/teardown. #1538

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions fuzztest/internal/centipede_adaptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
20 changes: 9 additions & 11 deletions fuzztest/internal/runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
[&] {
Expand All @@ -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
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 = [&] {
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions fuzztest/internal/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Loading