Skip to content

Commit

Permalink
Fill binary_name and binary_hash to be consistent with Centipede.
Browse files Browse the repository at this point in the history
This is to ensure that the corpus filenames are the same when running with/without the Centipede binary.

PiperOrigin-RevId: 726541174
  • Loading branch information
xinhaoyuan authored and copybara-github committed Feb 13, 2025
1 parent eb4c69a commit 2cca35c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion centipede/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,6 @@ cc_library(
visibility = PUBLIC_API_VISIBILITY,
deps = [
":environment",
":util",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
Expand Down Expand Up @@ -861,6 +860,7 @@ cc_library(
deps = [
":feature",
":knobs",
":util",
"@com_google_absl//absl/base:no_destructor",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/log",
Expand Down
8 changes: 8 additions & 0 deletions centipede/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <filesystem> // NOLINT
#include <string>
#include <system_error> // NOLINT
#include <vector>
Expand All @@ -33,6 +34,7 @@
#include "absl/time/time.h"
#include "./centipede/feature.h"
#include "./centipede/knobs.h"
#include "./centipede/util.h"
#include "./common/defs.h"
#include "./common/logging.h"
#include "./common/remote_file.h"
Expand Down Expand Up @@ -328,4 +330,10 @@ void Environment::UpdateTimeoutPerBatchIfEqualTo(size_t val) {
<< " sec (see --help for details)";
}

void Environment::UpdateBinaryHashIfEmpty() {
if (binary_hash.empty()) {
binary_hash = HashOfFileContents(coverage_binary);
}
}

} // namespace centipede
2 changes: 2 additions & 0 deletions centipede/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ struct Environment {
// `timeout_per_input` and `batch_size` and updates it. Otherwise, leaves it
// unchanged.
void UpdateTimeoutPerBatchIfEqualTo(size_t val);
// If `binary_hash` is empty, updates it using the file in `coverage_binary`.
void UpdateBinaryHashIfEmpty();
};

} // namespace centipede
Expand Down
7 changes: 3 additions & 4 deletions centipede/environment_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "./centipede/environment.h"
#include "./centipede/util.h"
#include "./common/logging.h"

using ::centipede::Environment;
Expand Down Expand Up @@ -515,11 +514,11 @@ Environment CreateEnvironmentFromFlags(const std::vector<std::string> &argv) {
.riegeli = absl::GetFlag(FLAGS_riegeli),
#endif // CENTIPEDE_DISABLE_RIEGELI
.binary_name = std::filesystem::path(coverage_binary).filename().string(),
.binary_hash = absl::GetFlag(FLAGS_binary_hash).empty()
? HashOfFileContents(coverage_binary)
: absl::GetFlag(FLAGS_binary_hash),
.binary_hash = absl::GetFlag(FLAGS_binary_hash),
};
env_from_flags.UpdateBinaryHashIfEmpty();
env_from_flags.UpdateTimeoutPerBatchIfEqualTo(
Environment::Default().timeout_per_batch);
Expand Down
3 changes: 3 additions & 0 deletions fuzztest/internal/centipede_adaptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <filesystem> // NOLINT
#include <functional>
#include <limits>
#include <memory>
Expand Down Expand Up @@ -224,6 +225,8 @@ centipede::Environment CreateCentipedeEnvironmentFromConfiguration(
"internal_override_total_time_limit=",
total_time_limit);
env.coverage_binary = (*args)[0];
env.binary_name = std::filesystem::path{(*args)[0]}.filename().string();
env.UpdateBinaryHashIfEmpty();
env.exit_on_crash =
// Do shallow testing when running in unit-test mode unless we are replay
// coverage inputs.
Expand Down

0 comments on commit 2cca35c

Please sign in to comment.