Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions centipede/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -991,8 +991,6 @@ cc_library(
":runner_result",
":shared_memory_blob_sequence",
"@abseil-cpp//absl/base:nullability",
"@abseil-cpp//absl/random",
"@abseil-cpp//absl/random:bit_gen_ref",
"@com_google_fuzztest//common:defs",
],
)
Expand Down
23 changes: 20 additions & 3 deletions centipede/centipede_callbacks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <algorithm>
#include <cerrno>
#include <cmath>
#include <cstddef>
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -194,8 +195,11 @@ class CentipedeCallbacks::PersistentModeServer {
int poll_ret = -1;
do {
poll_fd = {fd, static_cast<short>(event)};
const int poll_timeout_ms = static_cast<int>(absl::ToInt64Milliseconds(
std::max(deadline - absl::Now(), kPollMinimalTimeout)));
const int poll_timeout_ms =
deadline == absl::InfiniteFuture()
? -1
: static_cast<int>(std::ceil(absl::ToDoubleMilliseconds(
std::max(deadline - absl::Now(), kPollMinimalTimeout))));
poll_ret = poll(&poll_fd, 1, poll_timeout_ms);
} while (poll_ret < 0 && errno == EINTR);
if (poll_ret == 1 && (poll_fd.revents & (event | POLLHUP)) == event) {
Expand Down Expand Up @@ -343,7 +347,6 @@ std::string CentipedeCallbacks::ConstructRunnerFlags(
std::vector<std::string> flags = {
"CENTIPEDE_RUNNER_FLAGS=",
absl::StrCat("timeout_per_input=", env_.timeout_per_input),
absl::StrCat("timeout_per_batch=", env_.timeout_per_batch),
absl::StrCat("address_space_limit_mb=", env_.address_space_limit_mb),
absl::StrCat("rss_limit_mb=", env_.rss_limit_mb),
absl::StrCat("stack_limit_kb=", env_.stack_limit_kb),
Expand Down Expand Up @@ -565,8 +568,12 @@ int CentipedeCallbacks::ExecuteCentipedeSancovBinaryWithShmem(
}

// Run.
const auto batch_start_time = absl::Now();
const int exit_code = RunBatchForBinary(binary);
inputs_blobseq_.ReleaseSharedMemory(); // Inputs are already consumed.
const bool batch_timed_out =
env_.timeout_per_batch > 0 &&
absl::Now() - batch_start_time > absl::Seconds(env_.timeout_per_batch);

// Get results.
batch_result.exit_code() = exit_code;
Expand All @@ -591,6 +598,16 @@ int CentipedeCallbacks::ExecuteCentipedeSancovBinaryWithShmem(

if (env_.print_runner_log) PrintExecutionLog();

if (batch_timed_out) {
FUZZTEST_LOG(INFO)
<< "Batch timeout detected. Replacing the original exit code: "
<< exit_code
<< ", failure description: " << batch_result.failure_description();
batch_result.failure_description() = kExecutionFailurePerBatchTimeout;
batch_result.exit_code() = EXIT_FAILURE;
return EXIT_FAILURE;
}

// TODO: b/467103298 - Handle failures when the exit code is zero, e.g., when
// the target exits via `std::_Exit(0)`.
if (exit_code != EXIT_SUCCESS) {
Expand Down
42 changes: 21 additions & 21 deletions centipede/centipede_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class CentipedeMock : public CentipedeCallbacks {
// i-th element is the number of bytes with the value 'i' in the input.
// `counters` is converted to FeatureVec and added to
// `batch_result.results()`.
for (auto &input : inputs) {
for (auto& input : inputs) {
ByteArray counters(256);
for (uint8_t byte : input) {
counters[byte]++;
Expand Down Expand Up @@ -496,7 +496,7 @@ TEST_F(CentipedeWithTemporaryLocalDir, MutateViaExternalBinary) {
// Must contain normal mutants, but not the ones from crossover.
const auto mutant_data = GetDataFromMutants(result.mutants());
EXPECT_THAT(mutant_data, IsSupersetOf(some_of_expected_mutants));
for (const auto &crossover_mutant : expected_crossover_mutants) {
for (const auto& crossover_mutant : expected_crossover_mutants) {
EXPECT_THAT(mutant_data, Not(Contains(crossover_mutant)));
}
}
Expand Down Expand Up @@ -525,7 +525,7 @@ class MergeMock : public CentipedeCallbacks {
std::vector<Mutant> Mutate(absl::Span<const MutationInputRef> inputs,
size_t num_mutants) override {
std::vector<Mutant> mutants(num_mutants);
for (auto &mutant : mutants) {
for (auto& mutant : mutants) {
mutant.data.resize(1);
mutant.data[0] = ++number_of_mutations_;
mutant.origin = Mutant::kOriginNone;
Expand Down Expand Up @@ -611,7 +611,7 @@ class FunctionFilterMock : public CentipedeCallbacks {
// Sets the inputs to one of 3 pre-defined values.
std::vector<Mutant> Mutate(absl::Span<const MutationInputRef> inputs,
size_t num_mutants) override {
for (auto &input : inputs) {
for (auto& input : inputs) {
if (!seed_inputs_.contains(input.data)) {
observed_inputs_.insert(input.data);
}
Expand All @@ -628,8 +628,8 @@ class FunctionFilterMock : public CentipedeCallbacks {
// Returns one of 3 pre-defined values, that trigger different code paths in
// the test target.
static ByteArray GetMutant(size_t idx) {
const char *mutants[3] = {"func1", "func2-A", "foo"};
const char *mutant = mutants[idx % 3];
const char* mutants[3] = {"func1", "func2-A", "foo"};
const char* mutant = mutants[idx % 3];
return {mutant, mutant + strlen(mutant)};
}

Expand All @@ -646,7 +646,7 @@ class FunctionFilterMock : public CentipedeCallbacks {
// Runs a short fuzzing session with the provided `function_filter`.
// Returns a sorted array of observed inputs.
static std::vector<ByteArray> RunWithFunctionFilter(
std::string_view function_filter, const TempDir &tmp_dir) {
std::string_view function_filter, const TempDir& tmp_dir) {
Environment env;
env.workdir = tmp_dir.path();
env.seed = 1; // make the runs predictable.
Expand Down Expand Up @@ -717,9 +717,9 @@ class ExtraBinariesMock : public CentipedeCallbacks {
bool Execute(std::string_view binary, absl::Span<const ByteSpan> inputs,
BatchResult& batch_result) override {
bool res = true;
for (const auto &input : inputs) {
for (const auto& input : inputs) {
if (input.size() != 1) continue;
for (const Crash &crash : crashes_) {
for (const Crash& crash : crashes_) {
if (binary == crash.binary && input[0] == crash.input) {
batch_result.exit_code() = EXIT_FAILURE;
batch_result.failure_description() = crash.description;
Expand All @@ -736,7 +736,7 @@ class ExtraBinariesMock : public CentipedeCallbacks {
std::vector<Mutant> Mutate(absl::Span<const MutationInputRef> inputs,
size_t num_mutants) override {
std::vector<Mutant> mutants(num_mutants);
for (auto &mutant : mutants) {
for (auto& mutant : mutants) {
mutant.data.resize(1);
mutant.data[0] = ++number_of_mutations_;
mutant.origin = Mutant::kOriginNone;
Expand All @@ -754,20 +754,20 @@ struct FileAndContents {
std::string file;
std::string contents;

bool operator==(const FileAndContents &other) const {
bool operator==(const FileAndContents& other) const {
return file == other.file && contents == other.contents;
}

template <typename Sink>
friend void AbslStringify(Sink &sink, const FileAndContents &f) {
friend void AbslStringify(Sink& sink, const FileAndContents& f) {
absl::Format(&sink, "FileAndContents{%s, \"%s\"}", f.file, f.contents);
}
};

MATCHER_P(HasFilesWithContents, expected_files_and_contents, "") {
const std::string &dir_path = arg;
const std::string& dir_path = arg;
std::vector<FileAndContents> files_and_contents;
for (const auto &dir_ent : std::filesystem::directory_iterator(dir_path)) {
for (const auto& dir_ent : std::filesystem::directory_iterator(dir_path)) {
auto file_and_contents = FileAndContents{dir_ent.path().filename()};
ReadFromLocalFile(dir_ent.path().c_str(), file_and_contents.contents);
files_and_contents.push_back(std::move(file_and_contents));
Expand Down Expand Up @@ -843,7 +843,7 @@ class UndetectedCrashingInputMock : public CentipedeCallbacks {
if (!first_pass_) {
num_inputs_triaged_ += inputs.size();
}
for (const auto &input : inputs) {
for (const auto& input : inputs) {
FUZZTEST_CHECK_EQ(input.size(), 1); // By construction in `Mutate()`.
// The contents of each mutant is its sequential number.
if (input[0] == crashing_input_idx_) {
Expand Down Expand Up @@ -933,7 +933,7 @@ TEST(Centipede, UndetectedCrashingInput) {
absl::StrCat("crashing_batch-", crashing_input_hash);
EXPECT_TRUE(std::filesystem::exists(crashes_dir_path)) << crashes_dir_path;
std::vector<std::string> found_crash_file_names;
for (auto const &dir_ent :
for (auto const& dir_ent :
std::filesystem::directory_iterator(crashes_dir_path)) {
found_crash_file_names.push_back(dir_ent.path().filename());
}
Expand Down Expand Up @@ -1400,14 +1400,14 @@ TEST_F(CentipedeWithTemporaryLocalDir, EngineWorksInWorkerMode) {
env.test_name = "some_test";
env.populate_binary_info = false;
env.fork_server = false;
env.persistent_mode = false;
env.persistent_mode = true;
env.exit_on_crash = true;
env.stop_at = absl::Now() + absl::Seconds(10);
fuzztest::internal::DefaultCallbacksFactory<
fuzztest::internal::CentipedeDefaultCallbacks>
callbacks;
EXPECT_DEATH(
[&] { std::exit(CentipedeMain(env, callbacks)); }(),
ContainsRegex("Failure *: INPUT FAILURE: some_failure_description"));
EXPECT_DEATH([&] { std::exit(CentipedeMain(env, callbacks)); }(),
ContainsRegex("Failure *: some_failure_description"));
}

TEST_F(CentipedeWithTemporaryLocalDir, EngineWorksInStandaloneMode) {
Expand All @@ -1427,7 +1427,7 @@ TEST_F(CentipedeWithTemporaryLocalDir, EngineWorksInStandaloneMode) {
const int status = std::system(test_command.c_str());
std::exit(WEXITSTATUS(status));
}(),
ContainsRegex("Failure *: INPUT FAILURE: some_failure_description"));
ContainsRegex("Failure *: some_failure_description"));
}

} // namespace
Expand Down
12 changes: 8 additions & 4 deletions centipede/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include <algorithm>
#include <atomic>
#include <cmath>
#include <csignal>
#include <cstdint>
#include <cstdlib>
Expand Down Expand Up @@ -146,8 +147,11 @@ struct Command::ForkServerProps {
/*fd=*/pipe_[1],
/*events=*/POLLIN,
};
const int poll_timeout_ms = static_cast<int>(absl::ToInt64Milliseconds(
std::max(deadline - absl::Now(), absl::Milliseconds(1))));
const int poll_timeout_ms =
deadline == absl::InfiniteFuture()
? -1
: static_cast<int>(std::ceil(absl::ToDoubleMilliseconds(
std::max(deadline - absl::Now(), absl::Milliseconds(1)))));
poll_ret = poll(&poll_fd, 1, poll_timeout_ms);
// The `poll()` syscall can get interrupted: it sets errno==EINTR in that
// case. We should tolerate that.
Expand Down Expand Up @@ -207,7 +211,7 @@ std::string Command::ToString() const {
ss.reserve(/*env*/ 1 + options_.env_diff.size() + /*path*/ 1 +
/*args*/ options_.args.size() + /*out/err*/ 2);
// env.
ss.push_back("env");
ss.push_back("exec env");
std::vector<std::string> env_to_set;
env_to_set.reserve(options_.env_diff.size());
// Arguments that unset environment variables must appear first.
Expand Down Expand Up @@ -287,7 +291,7 @@ bool Command::StartForkServer(std::string_view temp_dir_path,
{
CENTIPEDE_FORK_SERVER_FIFO0="%s" \
CENTIPEDE_FORK_SERVER_FIFO1="%s" \
exec %s
%s
} &
printf "%%s" $! > "%s"
)sh";
Expand Down
8 changes: 4 additions & 4 deletions centipede/command_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ namespace {
using ::testing::Optional;

TEST(CommandTest, ToString) {
EXPECT_EQ(Command{"x"}.ToString(), "env \\\nx");
EXPECT_EQ(Command{"x"}.ToString(), "exec env \\\nx");
{
Command::Options cmd_options;
cmd_options.args = {"arg1", "arg2"};
EXPECT_EQ((Command{"path", std::move(cmd_options)}.ToString()),
"env \\\npath \\\narg1 \\\narg2");
"exec env \\\npath \\\narg1 \\\narg2");
}
{
Command::Options cmd_options;
cmd_options.env_diff = {"K1=V1", "K2=V2", "-K3"};
EXPECT_EQ((Command{"x", std::move(cmd_options)}.ToString()),
"env \\\n-u K3 \\\nK1=V1 \\\nK2=V2 \\\nx");
"exec env \\\n-u K3 \\\nK1=V1 \\\nK2=V2 \\\nx");
}
}

Expand Down Expand Up @@ -80,7 +80,7 @@ TEST(CommandTest, InputFileWildCard) {
Command::Options cmd_options;
cmd_options.temp_file_path = "TEMP_FILE";
Command cmd{"foo bar @@ baz", std::move(cmd_options)};
EXPECT_EQ(cmd.ToString(), "env \\\nfoo bar TEMP_FILE baz");
EXPECT_EQ(cmd.ToString(), "exec env \\\nfoo bar TEMP_FILE baz");
}

TEST(CommandTest, ForkServer) {
Expand Down
Loading
Loading