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 84bcab2 commit 472d888
Show file tree
Hide file tree
Showing 6 changed files with 33 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
1 change: 1 addition & 0 deletions fuzztest/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ cc_library(
":runtime",
":table_of_recent_compares",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/base:no_destructor",
"@com_google_absl//absl/log",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/random",
Expand Down
18 changes: 18 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 All @@ -43,6 +44,7 @@
#include <vector>

#include "absl/algorithm/container.h"
#include "absl/base/no_destructor.h"
#include "absl/log/log.h"
#include "absl/memory/memory.h"
#include "absl/random/distributions.h"
Expand Down Expand Up @@ -158,6 +160,20 @@ absl::StatusOr<std::vector<std::string>> GetProcessArgs() {
#endif
}

std::string GetSelfBinaryHashForCentipedeEnvironment() {
static absl::NoDestructor<std::string> cached_self_binary_hash{[] {
centipede::Environment env;
const auto args = GetProcessArgs();
FUZZTEST_INTERNAL_CHECK(
args.ok(), absl::StrCat("failed to get the original process args: ",
args.status()));
env.coverage_binary = (*args)[0];
env.UpdateBinaryHashIfEmpty();
return env.binary_hash;
}()};
return *cached_self_binary_hash;
}

std::string ShellEscape(absl::string_view str) {
return absl::StrCat("'", absl::StrReplaceAll(str, {{"'", "'\\''"}}), "'");
}
Expand Down Expand Up @@ -224,6 +240,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();
env.binary_hash = GetSelfBinaryHashForCentipedeEnvironment();
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 472d888

Please sign in to comment.