Re-enable the FuzzTest reporter after each input that reported a failure. The FuzzTest reporter is enabled once per fuzz test, and printing a report disables it so that each crash is reported at most once. A non-fatal failure, such as a GoogleTest assertion failure outside of fuzzing, is reported without terminating the process, which may then run more inputs but can no longer report their failures. In particular, a persistent-mode Centipede runner runs subsequent batches in the same process. For example, when crash deduplication replays a heap buffer overflow after an assertion failure, the overflow is not reported and Centipede falls back to the `unexpected-termination` signature, so the crash does not reproduce with its stored signature. Now `FuzzTestFuzzerImpl::RunOneInput()` keeps the reporter disabled only for the rest of the input that reported a failure, and re-enables it after the input, which also resets the crash type and signature. It also resets the external failure flag before each input, so that the flag reflects only the current input, which makes resetting it in the callers unnecessary. Make `ReplayUpdatesDatabaseWithFlag` check that all crashing inputs of `FuzzTest.FailsInTwoWays` are touched, not just an arbitrary one, which catches the issue whenever the assertion failure is replayed first. Also increase the test's replay time limit, so that the quarter of it dedicated to crash deduplication suffices to replay both crashing inputs in sanitizer builds. PiperOrigin-RevId: 1021309212
diff --git a/e2e_tests/corpus_database_test.cc b/e2e_tests/corpus_database_test.cc index 8dfc3f2..a533147 100644 --- a/e2e_tests/corpus_database_test.cc +++ b/e2e_tests/corpus_database_test.cc
@@ -446,11 +446,13 @@ } } ASSERT_FALSE(crash_files.empty()); - const std::string target_crash = crash_files[0]; - const auto initial_mtime = RemoteFileGetMTime(target_crash); - ASSERT_TRUE(initial_mtime.ok()); - const absl::Time initial_mod_time = *initial_mtime; + absl::flat_hash_map<std::string, absl::Time> initial_mtimes; + for (const std::string& crash_file : crash_files) { + const absl::StatusOr<absl::Time> mtime = RemoteFileGetMTime(crash_file); + ASSERT_TRUE(mtime.ok()) << mtime.status(); + initial_mtimes[crash_file] = *mtime; + } absl::SleepFor(absl::Seconds(2)); @@ -458,19 +460,30 @@ run_options.fuzztest_flags = { {"corpus_database", db_path}, {"replay_corpus", "FuzzTest.FailsInTwoWays"}, - {"replay_corpus_for", "10s"}, + // A quarter of this time is dedicated to replaying the crashing inputs, + // which may require restarting the test binary after a crash, so leave + // enough time for sanitizer builds. + {"replay_corpus_for", "30s"}, {"update_corpus_database", "true"}, }; auto [status, std_out, std_err] = RunBinaryMaybeWithCentipede( GetCorpusDatabaseTestingBinaryPath(), run_options); - const auto final_mtime = RemoteFileGetMTime(target_crash); - ASSERT_TRUE(final_mtime.ok()); - EXPECT_GT(*final_mtime, initial_mod_time) - << "target_crash: " << target_crash << "\nstd_err:\n" - << std_err << "\nstd_out:\n" - << std_out; + // All crashes reproduce with their stored signatures, so all crashing inputs + // should be touched, regardless of the order in which they are replayed. + // TODO(fniksic): Once GoogleTest assertion failures can have distinct + // signatures, also test with multiple non-fatal failures, so that some crash + // is always replayed after a non-fatal failure in the same process. + for (const auto& [crash_file, initial_mtime] : initial_mtimes) { + const absl::StatusOr<absl::Time> final_mtime = + RemoteFileGetMTime(crash_file); + ASSERT_TRUE(final_mtime.ok()) << final_mtime.status(); + EXPECT_GT(*final_mtime, initial_mtime) + << "Crashing input not touched: " << crash_file << "\nstd_err:\n" + << std_err << "\nstd_out:\n" + << std_out; + } } TEST_P(UpdateCorpusDatabaseTest, FuzzingDoesNotUpdateDatabaseWithFlag) {
diff --git a/fuzztest/internal/runtime.cc b/fuzztest/internal/runtime.cc index f2c7fe8..2d4c296 100644 --- a/fuzztest/internal/runtime.cc +++ b/fuzztest/internal/runtime.cc
@@ -1275,7 +1275,6 @@ Input mutation{params_domain_.Init(prng)}; const size_t max_iterations = duration == absl::ZeroDuration() ? 0 : 10000; for (int i = 0; i < max_iterations; ++i) { - runtime_.SetExternalFailureDetected(false); RunOneInput(mutation); if (runtime_.external_failure_detected()) { break; @@ -1309,6 +1308,8 @@ // a non-terminating failure on a previous input (e.g., during local // minimization of non-fatal failures or in unit-test mode). runtime_.ResetCrashType(); + runtime_.SetExternalFailureDetected(false); + const bool reporter_enabled_before_input = runtime_.reporter_enabled(); auto untyped_args = params_domain_.GetValue(input.args); Runtime::Args debug_args{input.args, params_domain_}; runtime_.SetCurrentArgs(&debug_args); @@ -1348,6 +1349,14 @@ runtime_.OnTestIterationEnd(); runtime_.UnsetCurrentArgs(); + // Reporting a failure disables the reporter for the rest of the input, so + // that the input reports at most one failure. A non-fatal failure doesn't + // terminate the process, which may run more inputs (e.g., in the next batch + // of a persistent-mode Centipede runner), so re-enable the reporter, which + // also resets the crash type and signature, to report their failures. + if (reporter_enabled_before_input && !runtime_.reporter_enabled()) { + runtime_.EnableReporter(&stats_, [] { return absl::Now(); }); + } return {new_coverage, run_time}; } @@ -1378,7 +1387,6 @@ if (SerializeIRObject(params_domain_.SerializeCorpus( minimal_non_fatal_counterexample_->args)) != SerializeIRObject(params_domain_.SerializeCorpus(copy.args))) { - runtime_.SetExternalFailureDetected(false); RunOneInput(copy); if (runtime_.external_failure_detected()) { // Found a smaller one, record it and reset the counter. @@ -1483,7 +1491,6 @@ // the failing sample to force a crash. Instead, we could store the // information from the first failure and generate a report manually. runtime_.SetShouldTerminateOnNonFatalFailure(true); - runtime_.SetExternalFailureDetected(false); RunOneInput(*minimal_non_fatal_counterexample_); } };
diff --git a/fuzztest/internal/runtime.h b/fuzztest/internal/runtime.h index 4eb7ed6..033982f 100644 --- a/fuzztest/internal/runtime.h +++ b/fuzztest/internal/runtime.h
@@ -580,6 +580,10 @@ void InitializeCorpus(absl::BitGenRef prng); + // Runs the property function on `input`. Afterwards, + // `runtime_.external_failure_detected()` indicates whether running `input` + // resulted in a non-fatal failure. If the failure was reported, the reporter + // is re-enabled for subsequent inputs. RunResult RunOneInput(const Input& input); bool ShouldStop();