Skip to content

Commit

Permalink
Unify the unit-testing and fuzzing API to return whether the test is …
Browse files Browse the repository at this point in the history
…passed.

This is part of the unified execution model.

PiperOrigin-RevId: 716294134
  • Loading branch information
xinhaoyuan authored and copybara-github committed Jan 16, 2025
1 parent 00edfa0 commit 7c4fcba
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
8 changes: 4 additions & 4 deletions fuzztest/internal/centipede_adaptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -526,15 +526,16 @@ CentipedeFuzzerAdaptor::CentipedeFuzzerAdaptor(
"Invalid fixture driver!");
}

void CentipedeFuzzerAdaptor::RunInUnitTestMode(
bool CentipedeFuzzerAdaptor::RunInUnitTestMode(
const Configuration& configuration) {
centipede_fixture_driver_->set_configuration(&configuration);
CentipedeBeginExecutionBatch();
fuzzer_impl_.RunInUnitTestMode(configuration);
CentipedeEndExecutionBatch();
return true;
}

int CentipedeFuzzerAdaptor::RunInFuzzingMode(
bool CentipedeFuzzerAdaptor::RunInFuzzingMode(
int* argc, char*** argv, const Configuration& configuration) {
centipede_fixture_driver_->set_configuration(&configuration);
runtime_.SetRunMode(RunMode::kFuzz);
Expand Down Expand Up @@ -627,13 +628,12 @@ int CentipedeFuzzerAdaptor::RunInFuzzingMode(
return centipede::CentipedeMain(env, factory);
})();
fuzzer_impl_.fixture_driver_->TearDownFuzzTest();
if (result) std::exit(result);
if (print_final_stats) {
absl::FPrintF(GetStderr(), "\n[.] Fuzzing was terminated.\n");
runtime_.PrintFinalStatsOnDefaultSink();
absl::FPrintF(GetStderr(), "\n");
}
return 0;
return result == 0;
}

} // namespace fuzztest::internal
Expand Down
6 changes: 3 additions & 3 deletions fuzztest/internal/centipede_adaptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class CentipedeFuzzerAdaptor : public FuzzTestFuzzer {
public:
CentipedeFuzzerAdaptor(const FuzzTest& test,
std::unique_ptr<UntypedFixtureDriver> fixture_driver);
void RunInUnitTestMode(const Configuration& configuration) override;
int RunInFuzzingMode(int* argc, char*** argv,
const Configuration& configuration) override;
bool RunInUnitTestMode(const Configuration& configuration) override;
bool RunInFuzzingMode(int* argc, char*** argv,
const Configuration& configuration) override;

private:
Runtime& runtime_ = Runtime::instance();
Expand Down
7 changes: 4 additions & 3 deletions fuzztest/internal/googletest_adaptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ class GTest_TestAdaptor : public ::testing::Test {
EXPECT_TRUE(false) << "Death test is not supported.";
#endif
} else {
test->RunInUnitTestMode(configuration_);
EXPECT_TRUE(test->RunInUnitTestMode(configuration_))
<< "Failure(s) found in the unit-test mode.";
}
} else {
// TODO(b/245753736): Consider using `tolerate_failure` when FuzzTest can
// tolerate crashes in fuzzing mode.
ASSERT_EQ(0, test->RunInFuzzingMode(argc_, argv_, configuration_))
<< "Fuzzing failure.";
EXPECT_TRUE(test->RunInFuzzingMode(argc_, argv_, configuration_))
<< "Failure(s) found in the fuzzing mode.";
}
}

Expand Down
21 changes: 11 additions & 10 deletions fuzztest/internal/runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ void PopulateLimits(const Configuration& configuration,
#endif
}

void FuzzTestFuzzerImpl::RunInUnitTestMode(const Configuration& configuration) {
bool FuzzTestFuzzerImpl::RunInUnitTestMode(const Configuration& configuration) {
runtime_.SetSkippingRequested(false);
fixture_driver_->SetUpFuzzTest();
[&] {
Expand Down Expand Up @@ -1014,6 +1014,7 @@ void FuzzTestFuzzerImpl::RunInUnitTestMode(const Configuration& configuration) {
runtime_.SetCurrentTest(nullptr, nullptr);
}();
fixture_driver_->TearDownFuzzTest();
return true;
}

FuzzTestFuzzerImpl::RunResult FuzzTestFuzzerImpl::RunOneInput(
Expand Down Expand Up @@ -1102,16 +1103,16 @@ void FuzzTestFuzzerImpl::MinimizeNonFatalFailureLocally(absl::BitGenRef prng) {
}
}

int FuzzTestFuzzerImpl::RunInFuzzingMode(int* /*argc*/, char*** /*argv*/,
const Configuration& configuration) {
bool FuzzTestFuzzerImpl::RunInFuzzingMode(int* /*argc*/, char*** /*argv*/,
const Configuration& configuration) {
runtime_.SetSkippingRequested(false);
fixture_driver_->SetUpFuzzTest();
const int exit_code = [&] {
const bool success = [&] {
if (runtime_.skipping_requested()) {
absl::FPrintF(GetStderr(),
"[.] Skipping %s per request from the test setup.\n",
test_.full_name());
return 0;
return true;
}
runtime_.StartWatchdog();
PopulateLimits(configuration, execution_coverage_);
Expand All @@ -1125,14 +1126,14 @@ int FuzzTestFuzzerImpl::RunInFuzzingMode(int* /*argc*/, char*** /*argv*/,
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.
return 0;
return true;
}

if (execution_coverage_ == nullptr) {
absl::FPrintF(
GetStderr(),
"\n\n[!] To fuzz, please build with --config=fuzztest.\n\n\n");
return 1;
return false;
}

stats_.total_edges = execution_coverage_->GetCounterMap().size();
Expand All @@ -1144,7 +1145,7 @@ int FuzzTestFuzzerImpl::RunInFuzzingMode(int* /*argc*/, char*** /*argv*/,
GetStderr(),
"[*] Selected %d corpus inputs in minimization mode - exiting.\n",
stats_.useful_inputs);
return 0;
return true;
}

CorpusDatabase corpus_database(configuration);
Expand Down Expand Up @@ -1238,10 +1239,10 @@ int FuzzTestFuzzerImpl::RunInFuzzingMode(int* /*argc*/, char*** /*argv*/,
absl::FPrintF(GetStderr(), "\n[.] Fuzzing was terminated.\n");
runtime_.PrintFinalStatsOnDefaultSink();
absl::FPrintF(GetStderr(), "\n");
return 0;
return true;
}();
fixture_driver_->TearDownFuzzTest();
return exit_code;
return success;
}

} // namespace fuzztest::internal
15 changes: 8 additions & 7 deletions fuzztest/internal/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ namespace internal {
class FuzzTestFuzzer {
public:
virtual ~FuzzTestFuzzer() = default;
virtual void RunInUnitTestMode(const Configuration& configuration) = 0;
// Returns fuzzing mode's exit code. Zero indicates success.
virtual int RunInFuzzingMode(int* argc, char*** argv,
const Configuration& configuration) = 0;
// Returns ture if no error were detected by the FuzzTest, false otherwise.
virtual bool RunInUnitTestMode(const Configuration& configuration) = 0;
// Returns ture if no error were detected by the FuzzTest, false otherwise.
virtual bool RunInFuzzingMode(int* argc, char*** argv,
const Configuration& configuration) = 0;
};

class FuzzTest;
Expand Down Expand Up @@ -280,11 +281,11 @@ class FuzzTestFuzzerImpl : public FuzzTestFuzzer {

private:
// TODO(fniksic): Refactor to reduce code complexity and improve readability.
void RunInUnitTestMode(const Configuration& configuration) override;
bool RunInUnitTestMode(const Configuration& configuration) override;

// TODO(fniksic): Refactor to reduce code complexity and improve readability.
int RunInFuzzingMode(int* argc, char*** argv,
const Configuration& configuration) override;
bool RunInFuzzingMode(int* argc, char*** argv,
const Configuration& configuration) override;

// Use the standard PRNG instead of absl::BitGen because Abseil doesn't
// guarantee seed stability
Expand Down

0 comments on commit 7c4fcba

Please sign in to comment.