Skip to content

Commit

Permalink
Add an IO function for reading inputs from blob files.
Browse files Browse the repository at this point in the history
Reorganized the runtime to use the new function and removed unused code.

PiperOrigin-RevId: 647754890
  • Loading branch information
fniksic authored and Copybara-Service committed Jun 28, 2024
1 parent 9875339 commit ed1dddd
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 342 deletions.
19 changes: 4 additions & 15 deletions common/blob_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <memory>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -187,10 +186,6 @@ class DefaultBlobFileReader : public BlobFileReader {
riegeli_reader_.Reset(riegeli::kClosed);
#endif // CENTIPEDE_DISABLE_RIEGELI

// Because we fall back to a legacy reader, we can't distinguish between
// an empty blob file and a file that is not a blob file. Once this behavior
// changes (e.g., we get rid of the legacy reader), consider b/349115475 and
// track down places that would benefit from this distinction.
legacy_reader_ = std::make_unique<SimpleBlobFileReader>();
if (absl::Status s = legacy_reader_->Open(path); !s.ok()) {
legacy_reader_ = nullptr;
Expand Down Expand Up @@ -290,13 +285,9 @@ class RiegeliWriter : public BlobFileWriter {
}

absl::Status Write(ByteSpan blob) override {
std::string_view blob_view = AsStringView(blob);
const auto now = absl::Now();
if (!PreWriteFlush(blob.size())) return writer_.status();
if (!writer_.WriteRecord(
absl::string_view{blob_view.data(), blob_view.size()})) {
return writer_.status();
}
if (!writer_.WriteRecord(AsStringView(blob))) return writer_.status();
if (!PostWriteFlush(blob.size())) return writer_.status();
write_duration_ += absl::Now() - now;
if (written_blobs_ + buffered_blobs_ % 10000 == 0)
Expand Down Expand Up @@ -445,13 +436,11 @@ ByteArray PackBytesForAppendFile(ByteSpan blob) {
size_t size = blob.size();
uint8_t size_bytes[sizeof(size)];
std::memcpy(size_bytes, &size, sizeof(size));
res.insert(res.end(), std::begin(kPackBegMagic),
std::begin(kPackBegMagic) + kMagicLen);
res.insert(res.end(), &kPackBegMagic[0], &kPackBegMagic[kMagicLen]);
res.insert(res.end(), hash.begin(), hash.end());
res.insert(res.end(), std::begin(size_bytes), std::end(size_bytes));
res.insert(res.end(), &size_bytes[0], &size_bytes[sizeof(size_bytes)]);
res.insert(res.end(), blob.begin(), blob.end());
res.insert(res.end(), std::begin(kPackEndMagic),
std::begin(kPackEndMagic) + kMagicLen);
res.insert(res.end(), &kPackEndMagic[0], &kPackEndMagic[kMagicLen]);
return res;
}

Expand Down
3 changes: 0 additions & 3 deletions common/remote_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ void RemoteFileGetContents(const std::filesystem::path &path,
// Returns true if `path` exists.
bool RemotePathExists(std::string_view path);

// Returns true if `path` is a directory.
bool RemotePathIsDirectory(std::string_view path);

// Returns the size of the file at `path` in bytes. The file must exist.
int64_t RemoteFileGetSize(std::string_view path);

Expand Down
4 changes: 0 additions & 4 deletions common/remote_file_oss.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ bool RemotePathExists(std::string_view path) {
return std::filesystem::exists(path);
}

bool RemotePathIsDirectory(std::string_view path) {
return std::filesystem::is_directory(path);
}

int64_t RemoteFileGetSize(std::string_view path) {
FILE *f = std::fopen(path.data(), "r");
CHECK(f != nullptr) << VV(path);
Expand Down
6 changes: 2 additions & 4 deletions e2e_tests/functional_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -801,10 +801,8 @@ void ExpectCorpusInputMessageInLogs(absl::string_view logs, int num_inputs) {
HasSubstr(absl::StrFormat("%d inputs to rerun", num_inputs)))
<< logs;
#else
EXPECT_THAT(logs,
HasSubstr(absl::StrFormat(
"In total, loaded %d inputs and ignored 0 invalid inputs",
num_inputs)))
EXPECT_THAT(logs, HasSubstr(absl::StrFormat(
"Parsed %d inputs and ignored 0 inputs", num_inputs)))
<< logs;
#endif
}
Expand Down
18 changes: 2 additions & 16 deletions fuzztest/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ cc_library(
"fuzztest_macros.h",
],
deps = [
":io",
":registration",
":registry",
"@com_google_absl//absl/log:check",
Expand Down Expand Up @@ -118,7 +117,6 @@ cc_library(
":configuration",
":flag_name",
":googletest_adaptor",
":io",
":logging",
":registry",
":runtime",
Expand Down Expand Up @@ -454,7 +452,6 @@ cc_library(
":configuration",
":corpus_database",
":flag_name",
":io",
":registry",
":runtime",
"@com_google_absl//absl/strings",
Expand All @@ -480,15 +477,9 @@ cc_library(
hdrs = ["internal/io.h"],
deps = [
":logging",
"@com_google_absl//absl/functional:function_ref",
"@com_google_absl//absl/hash",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/types:span",
"@com_google_fuzztest//common:blob_file",
"@com_google_fuzztest//common:defs",
"@com_google_fuzztest//common:remote_file",
] + select({
"//conditions:default": [],
}),
Expand All @@ -500,11 +491,7 @@ cc_test(
deps = [
":fuzztest_core",
":io",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_fuzztest//common:blob_file",
"@com_google_fuzztest//common:defs",
"@com_google_googletest//:gtest_main",
],
)
Expand Down Expand Up @@ -595,20 +582,19 @@ cc_library(
":flag_name",
":io",
":logging",
":meta",
":printer",
":registration",
":seed_seq",
":serialization",
":status",
":type_support",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/functional:bind_front",
"@com_google_absl//absl/functional:function_ref",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/random",
"@com_google_absl//absl/random:bit_gen_ref",
"@com_google_absl//absl/random:distributions",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/time",
Expand Down
19 changes: 2 additions & 17 deletions fuzztest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ fuzztest_cc_library(
SRCS
"fuzztest_macros.cc"
DEPS
fuzztest::io
fuzztest::registration
fuzztest::registry
)
Expand All @@ -84,7 +83,6 @@ fuzztest_cc_library(
DEPS
fuzztest::configuration
fuzztest::googletest_adaptor
fuzztest::io
fuzztest::logging
fuzztest::registry
fuzztest::runtime
Expand Down Expand Up @@ -377,7 +375,6 @@ fuzztest_cc_library(
DEPS
fuzztest::configuration
fuzztest::corpus_database
fuzztest::io
fuzztest::registry
fuzztest::runtime
absl::strings
Expand All @@ -404,16 +401,9 @@ fuzztest_cc_library(
SRCS
"internal/io.cc"
DEPS
fuzztest::blob_file
fuzztest::defs
fuzztest::logging
fuzztest::remote_file
absl::function_ref
absl::hash
absl::status
absl::str_format
absl::string_view
absl::span
)

fuzztest_cc_test(
Expand All @@ -422,12 +412,8 @@ fuzztest_cc_test(
SRCS
"internal/io_test.cc"
DEPS
fuzztest::blob_file
fuzztest::defs
fuzztest::fuzztest_core
fuzztest::io
absl::check
absl::status
absl::strings
GTest::gmock_main
)
Expand Down Expand Up @@ -525,20 +511,19 @@ fuzztest_cc_library(
fuzztest::fixture_driver
fuzztest::io
fuzztest::logging
fuzztest::meta
fuzztest::printer
fuzztest::registration
fuzztest::seed_seq
fuzztest::serialization
fuzztest::status
fuzztest::type_support
absl::any_invocable
absl::bind_front
absl::function_ref
absl::check
absl::random_random
absl::random_bit_gen_ref
absl::random_distributions
absl::status
absl::statusor
absl::strings
absl::str_format
absl::time
Expand Down
3 changes: 1 addition & 2 deletions fuzztest/fuzztest_macros.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <cerrno>
#include <cstring>
#include <filesystem> // NOLINT
#include <filesystem>
#include <fstream>
#include <sstream>
#include <string>
Expand All @@ -17,7 +17,6 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "./fuzztest/internal/io.h"

namespace fuzztest {

Expand Down
1 change: 0 additions & 1 deletion fuzztest/init_fuzztest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "./fuzztest/internal/configuration.h"
#include "./fuzztest/internal/flag_name.h"
#include "./fuzztest/internal/googletest_adaptor.h"
#include "./fuzztest/internal/io.h"
#include "./fuzztest/internal/logging.h"
#include "./fuzztest/internal/registry.h"
#include "./fuzztest/internal/runtime.h"
Expand Down
23 changes: 11 additions & 12 deletions fuzztest/internal/centipede_adaptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,8 @@ class CentipedeAdaptorRunnerCallbacks : public centipede::RunnerCallbacks {
}

bool Execute(centipede::ByteSpan input) override {
auto parsed_input =
fuzzer_impl_.TryParse({(char*)input.data(), input.size()});
if (parsed_input.ok()) {
if (auto parsed_input =
fuzzer_impl_.TryParse({(char*)input.data(), input.size()})) {
fuzzer_impl_.RunOneInput({*std::move(parsed_input)});
return true;
}
Expand All @@ -184,12 +183,13 @@ class CentipedeAdaptorRunnerCallbacks : public centipede::RunnerCallbacks {
std::vector<GenericDomainCorpusType> seeds =
fuzzer_impl_.fixture_driver_->GetSeeds();
CorpusDatabase corpus_database(configuration_);
fuzzer_impl_.ForEachInput(
corpus_database.GetCoverageInputsIfAny(fuzzer_impl_.test_.full_name()),
[&](absl::string_view /*file_path*/, std::optional<int> /*blob_idx*/,
FuzzTestFuzzerImpl::Input input) {
seeds.push_back(std::move(input.args));
});
for (const std::string& corpus_file :
corpus_database.GetCoverageInputsIfAny(
fuzzer_impl_.test_.full_name())) {
auto corpus_value = fuzzer_impl_.GetCorpusValueFromFile(corpus_file);
if (!corpus_value) continue;
seeds.push_back(*corpus_value);
}
constexpr int kInitialValuesInSeeds = 32;
for (int i = 0; i < kInitialValuesInSeeds; ++i) {
seeds.push_back(fuzzer_impl_.params_domain_.Init(prng_));
Expand Down Expand Up @@ -226,10 +226,9 @@ class CentipedeAdaptorRunnerCallbacks : public centipede::RunnerCallbacks {
inputs[absl::Uniform<size_t>(prng_, 0, inputs.size())].data;
auto parsed_origin =
fuzzer_impl_.TryParse({(const char*)origin.data(), origin.size()});
if (!parsed_origin.ok()) {
if (!parsed_origin)
parsed_origin = fuzzer_impl_.params_domain_.Init(prng_);
}
auto mutant = FuzzTestFuzzerImpl::Input{*std::move(parsed_origin)};
auto mutant = FuzzTestFuzzerImpl::Input{*parsed_origin};
fuzzer_impl_.MutateValue(mutant, prng_);
mutant_data =
fuzzer_impl_.params_domain_.SerializeCorpus(mutant.args).ToString();
Expand Down
1 change: 0 additions & 1 deletion fuzztest/internal/googletest_adaptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "./fuzztest/internal/configuration.h"
#include "./fuzztest/internal/corpus_database.h"
#include "./fuzztest/internal/flag_name.h"
#include "./fuzztest/internal/io.h"
#include "./fuzztest/internal/registry.h"
#include "./fuzztest/internal/runtime.h"

Expand Down
Loading

0 comments on commit ed1dddd

Please sign in to comment.