Skip to content

Commit

Permalink
Eliminate some C++17 incompatibilities.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 719349128
  • Loading branch information
fniksic authored and copybara-github committed Jan 24, 2025
1 parent b1d0067 commit 5e60cae
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 88 deletions.
11 changes: 6 additions & 5 deletions centipede/batch_fuzz_example/customized_centipede.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,12 @@ bool CustomizedCallbacks::Execute(std::string_view binary,
}

// Execute.
Command cmd{env_.binary,
{.args = std::move(args),
.env_add = std::move(env),
.stdout_file = tmp_log_filepath,
.stderr_file = tmp_log_filepath}};
Command::Options cmd_options;
cmd_options.args = std::move(args);
cmd_options.env_add = std::move(env);
cmd_options.stdout_file = tmp_log_filepath;
cmd_options.stderr_file = tmp_log_filepath;
Command cmd{env_.binary, std::move(cmd_options)};
const int retval = cmd.Execute();

std::string tmp_log;
Expand Down
13 changes: 7 additions & 6 deletions centipede/binary_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <sstream>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "absl/log/check.h"
Expand Down Expand Up @@ -57,12 +58,12 @@ void BinaryInfo::InitializeFromSanCovBinary(
ScopedFile log_path(tmp_dir_path, "binary_info_log_tmp");
LOG(INFO) << __func__ << ": tmp_dir: " << tmp_dir;

Command cmd(binary_path_with_args,
{.env_add = {absl::StrCat(
"CENTIPEDE_RUNNER_FLAGS=:dump_binary_info:arg1=",
pc_table_path.path(), ":arg2=", cf_table_path.path(),
":arg3=", dso_table_path.path(), ":")},
.stdout_file = std::string(log_path.path())});
Command::Options cmd_options;
cmd_options.env_add = {absl::StrCat(
"CENTIPEDE_RUNNER_FLAGS=:dump_binary_info:arg1=", pc_table_path.path(),
":arg2=", cf_table_path.path(), ":arg3=", dso_table_path.path(), ":")};
cmd_options.stdout_file = std::string(log_path.path());
Command cmd(binary_path_with_args, std::move(cmd_options));
int exit_code = cmd.Execute();
if (exit_code != EXIT_SUCCESS) {
LOG(INFO) << __func__ << ": exit_code: " << exit_code;
Expand Down
29 changes: 18 additions & 11 deletions centipede/centipede.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,13 @@ Centipede::Centipede(const Environment &env, CentipedeCallbacks &user_callbacks,
stats_(stats),
input_filter_path_(std::filesystem::path(TemporaryLocalDirPath())
.append("filter-input")),
input_filter_cmd_(env_.input_filter, {.args = {input_filter_path_},
.stdout_file = "/dev/null",
.stderr_file = "/dev/null"}),
input_filter_cmd_([&] {
Command::Options cmd_options;
cmd_options.args = {input_filter_path_};
cmd_options.stdout_file = "/dev/null";
cmd_options.stderr_file = "/dev/null";
return Command{env_.input_filter, std::move(cmd_options)};
}()),
rusage_profiler_(
/*scope=*/perf::RUsageScope::ThisProcess(),
/*metrics=*/env.DumpRUsageTelemetryInThisShard()
Expand Down Expand Up @@ -555,18 +559,21 @@ void Centipede::GenerateSourceBasedCoverageReport(
merge_arguments.push_back(raw_profile);
}

Command merge_command("llvm-profdata", {.args = std::move(merge_arguments)});
Command::Options merge_cmd_options;
merge_cmd_options.args = std::move(merge_arguments);
Command merge_command("llvm-profdata", std::move(merge_cmd_options));
if (merge_command.Execute() != EXIT_SUCCESS) {
LOG(ERROR) << "Failed to run command " << merge_command.ToString();
return;
}

Command generate_report_command(
"llvm-cov",
{.args = {"show", "-format=html",
absl::StrCat("-output-dir=", report_path),
absl::StrCat("-instr-profile=", indexed_profile_path),
env_.clang_coverage_binary}});
Command::Options generate_report_cmd_options;
generate_report_cmd_options.args = {
"show", "-format=html", absl::StrCat("-output-dir=", report_path),
absl::StrCat("-instr-profile=", indexed_profile_path),
env_.clang_coverage_binary};
Command generate_report_command("llvm-cov",
std::move(generate_report_cmd_options));
if (generate_report_command.Execute() != EXIT_SUCCESS) {
LOG(ERROR) << "Failed to run command "
<< generate_report_command.ToString();
Expand Down Expand Up @@ -762,7 +769,7 @@ void Centipede::FuzzingLoop() {
? corpus_.WeightedRandom(rng_())
: corpus_.UniformRandom(rng_());
mutation_inputs.push_back(
{.data = corpus_record.data, .metadata = &corpus_record.metadata});
MutationInputRef{corpus_record.data, &corpus_record.metadata});
}

user_callbacks_.Mutate(mutation_inputs, batch_size, mutants);
Expand Down
43 changes: 23 additions & 20 deletions centipede/centipede_callbacks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,15 @@ Command &CentipedeCallbacks::GetOrCreateCommandForBinary(
env_.timeout_per_batch == 0
? absl::InfiniteDuration()
: absl::Seconds(env_.timeout_per_batch) + absl::Seconds(5);
Command &cmd = commands_.emplace_back(
Command{binary,
{.env_add = std::move(env),
.env_remove = EnvironmentVariablesToUnset(),
.stdout_file = execute_log_path_,
.stderr_file = execute_log_path_,
.timeout = amortized_timeout,
.temp_file_path = temp_input_file_path_}});
Command::Options cmd_options;
cmd_options.env_add = std::move(env);
cmd_options.env_remove = EnvironmentVariablesToUnset();
cmd_options.stdout_file = execute_log_path_;
cmd_options.stderr_file = execute_log_path_;
cmd_options.timeout = amortized_timeout;
cmd_options.temp_file_path = temp_input_file_path_;
Command &cmd =
commands_.emplace_back(Command{binary, std::move(cmd_options)});
if (env_.fork_server) cmd.StartForkServer(temp_dir_, Hash(binary));

return cmd;
Expand Down Expand Up @@ -261,12 +262,13 @@ bool CentipedeCallbacks::GetSeedsViaExternalBinary(
absl::StrAppend(&centipede_runner_flags,
"dl_path_suffix=", env_.runner_dl_path_suffix, ":");
}
Command cmd{binary,
{.env_add = {std::move(centipede_runner_flags)},
.env_remove = EnvironmentVariablesToUnset(),
.stdout_file = execute_log_path_,
.stderr_file = execute_log_path_,
.temp_file_path = temp_input_file_path_}};
Command::Options cmd_options;
cmd_options.env_add = {std::move(centipede_runner_flags)};
cmd_options.env_remove = EnvironmentVariablesToUnset();
cmd_options.stdout_file = execute_log_path_;
cmd_options.stderr_file = execute_log_path_;
cmd_options.temp_file_path = temp_input_file_path_;
Command cmd{binary, std::move(cmd_options)};
const int retval = cmd.Execute();

std::vector<std::string> seed_input_filenames;
Expand Down Expand Up @@ -302,12 +304,13 @@ bool CentipedeCallbacks::GetSerializedTargetConfigViaExternalBinary(
absl::StrAppend(&centipede_runner_flags,
"dl_path_suffix=", env_.runner_dl_path_suffix, ":");
}
Command cmd{binary,
{.env_add = {std::move(centipede_runner_flags)},
.env_remove = EnvironmentVariablesToUnset(),
.stdout_file = execute_log_path_,
.stderr_file = execute_log_path_,
.temp_file_path = temp_input_file_path_}};
Command::Options cmd_options;
cmd_options.env_add = {std::move(centipede_runner_flags)};
cmd_options.env_remove = EnvironmentVariablesToUnset();
cmd_options.stdout_file = execute_log_path_;
cmd_options.stderr_file = execute_log_path_;
cmd_options.temp_file_path = temp_input_file_path_;
Command cmd{binary, std::move(cmd_options)};
const bool is_success = cmd.Execute() == 0;

if (is_success) {
Expand Down
100 changes: 70 additions & 30 deletions centipede/command_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <filesystem> // NOLINT
#include <string>
#include <string_view>
#include <utility>

#include "gtest/gtest.h"
#include "absl/log/log.h"
Expand All @@ -35,21 +36,45 @@ namespace {

TEST(CommandTest, ToString) {
EXPECT_EQ(Command("x").ToString(), "env \\\nx");
EXPECT_EQ(Command("path", {.args = {"arg1", "arg2"}}).ToString(),
"env \\\npath \\\narg1 \\\narg2");
EXPECT_EQ(Command("x", {.env_add = {"K1=V1", "K2=V2"}, .env_remove = {"K3"}})
.ToString(),
"env \\\n-u K3 \\\nK1=V1 \\\nK2=V2 \\\nx");
EXPECT_EQ(Command("x", {.stdout_file = "out"}).ToString(),
"env \\\nx \\\n> out");
EXPECT_EQ(Command("x", {.stderr_file = "err"}).ToString(),
"env \\\nx \\\n2> err");
EXPECT_EQ(
Command("x", {.stdout_file = "out", .stderr_file = "err"}).ToString(),
"env \\\nx \\\n> out \\\n2> err");
EXPECT_EQ(
Command("x", {.stdout_file = "out", .stderr_file = "out"}).ToString(),
"env \\\nx \\\n> out \\\n2>&1");
{
Command::Options cmd_options;
cmd_options.args = {"arg1", "arg2"};
EXPECT_EQ(Command("path", std::move(cmd_options)).ToString(),
"env \\\npath \\\narg1 \\\narg2");
}
{
Command::Options cmd_options;
cmd_options.env_add = {"K1=V1", "K2=V2"};
cmd_options.env_remove = {"K3"};
EXPECT_EQ(Command("x", std::move(cmd_options)).ToString(),
"env \\\n-u K3 \\\nK1=V1 \\\nK2=V2 \\\nx");
}
{
Command::Options cmd_options;
cmd_options.stdout_file = "out";
EXPECT_EQ(Command("x", std::move(cmd_options)).ToString(),
"env \\\nx \\\n> out");
}
{
Command::Options cmd_options;
cmd_options.stderr_file = "err";
EXPECT_EQ(Command("x", std::move(cmd_options)).ToString(),
"env \\\nx \\\n2> err");
}
{
Command::Options cmd_options;
cmd_options.stdout_file = "out";
cmd_options.stderr_file = "err";
EXPECT_EQ(Command("x", std::move(cmd_options)).ToString(),
"env \\\nx \\\n> out \\\n2> err");
}
{
Command::Options cmd_options;
cmd_options.stdout_file = "out";
cmd_options.stderr_file = "out";
EXPECT_EQ(Command("x", cmd_options).ToString(),
"env \\\nx \\\n> out \\\n2>&1");
}
}

TEST(CommandTest, Execute) {
Expand Down Expand Up @@ -79,8 +104,10 @@ TEST(CommandDeathTest, Execute) {
}

TEST(CommandTest, InputFileWildCard) {
Command cmd("foo bar @@ baz",
{.timeout = absl::Seconds(2), .temp_file_path = "TEMP_FILE"});
Command::Options cmd_options;
cmd_options.timeout = absl::Seconds(2);
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");
}

Expand All @@ -94,8 +121,11 @@ TEST(CommandTest, ForkServer) {
{
const std::string input = "success";
const std::string log = std::filesystem::path{test_tmpdir} / input;
Command cmd(helper,
{.args = {input}, .stdout_file = log, .stderr_file = log});
Command::Options cmd_options;
cmd_options.args = {input};
cmd_options.stdout_file = log;
cmd_options.stderr_file = log;
Command cmd(helper, std::move(cmd_options));
EXPECT_TRUE(cmd.StartForkServer(test_tmpdir, "ForkServer"));
EXPECT_EQ(cmd.Execute(), EXIT_SUCCESS);
std::string log_contents;
Expand All @@ -106,8 +136,11 @@ TEST(CommandTest, ForkServer) {
{
const std::string input = "fail";
const std::string log = std::filesystem::path{test_tmpdir} / input;
Command cmd(helper,
{.args = {input}, .stdout_file = log, .stderr_file = log});
Command::Options cmd_options;
cmd_options.args = {input};
cmd_options.stdout_file = log;
cmd_options.stderr_file = log;
Command cmd(helper, std::move(cmd_options));
EXPECT_TRUE(cmd.StartForkServer(test_tmpdir, "ForkServer"));
EXPECT_EQ(cmd.Execute(), EXIT_FAILURE);
std::string log_contents;
Expand All @@ -118,8 +151,11 @@ TEST(CommandTest, ForkServer) {
{
const std::string input = "ret42";
const std::string log = std::filesystem::path{test_tmpdir} / input;
Command cmd(helper,
{.args = {input}, .stdout_file = log, .stderr_file = log});
Command::Options cmd_options;
cmd_options.args = {input};
cmd_options.stdout_file = log;
cmd_options.stderr_file = log;
Command cmd(helper, std::move(cmd_options));
EXPECT_TRUE(cmd.StartForkServer(test_tmpdir, "ForkServer"));
EXPECT_EQ(cmd.Execute(), 42);
std::string log_contents;
Expand All @@ -130,8 +166,11 @@ TEST(CommandTest, ForkServer) {
{
const std::string input = "abort";
const std::string log = std::filesystem::path{test_tmpdir} / input;
Command cmd(helper,
{.args = {input}, .stdout_file = log, .stderr_file = log});
Command::Options cmd_options;
cmd_options.args = {input};
cmd_options.stdout_file = log;
cmd_options.stderr_file = log;
Command cmd(helper, std::move(cmd_options));
EXPECT_TRUE(cmd.StartForkServer(test_tmpdir, "ForkServer"));
// WTERMSIG() needs an lvalue on some platforms.
const int ret = cmd.Execute();
Expand All @@ -144,11 +183,12 @@ TEST(CommandTest, ForkServer) {
{
const std::string input = "hang";
const std::string log = std::filesystem::path{test_tmpdir} / input;
constexpr auto kTimeout = absl::Seconds(2);
Command cmd(helper, {.args = {input},
.stdout_file = log,
.stderr_file = log,
.timeout = kTimeout});
Command::Options cmd_options;
cmd_options.args = {input};
cmd_options.stdout_file = log;
cmd_options.stderr_file = log;
cmd_options.timeout = absl::Seconds(2);
Command cmd(helper, std::move(cmd_options));
ASSERT_TRUE(cmd.StartForkServer(test_tmpdir, "ForkServer"));
EXPECT_EQ(cmd.Execute(), EXIT_FAILURE);
std::string log_contents;
Expand Down
11 changes: 6 additions & 5 deletions centipede/control_flow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ PCTable GetPcTableFromBinaryWithTracePC(std::string_view binary_path,
std::string_view objdump_path,
std::string_view tmp_path) {
const std::string stderr_path = absl::StrCat(tmp_path, ".log");
Command cmd(objdump_path, {.args = {"-d", std::string(binary_path)},
.stdout_file = std::string(tmp_path),
.stderr_file = stderr_path});
Command::Options cmd_options;
cmd_options.args = {"-d", std::string(binary_path)};
cmd_options.stdout_file = std::string(tmp_path);
cmd_options.stderr_file = stderr_path;
Command cmd(objdump_path, std::move(cmd_options));
int exit_code = cmd.Execute();
if (exit_code != EXIT_SUCCESS) {
std::string log_text;
Expand Down Expand Up @@ -134,8 +136,7 @@ DsoTable ReadDsoTableFromFile(std::string_view file_path) {
const std::vector<std::string> tokens =
absl::StrSplit(line, ' ', absl::SkipEmpty());
CHECK_EQ(tokens.size(), 2) << VV(line);
result.push_back(
{.path = tokens[0], .num_instrumented_pcs = std::stoul(tokens[1])});
result.push_back(DsoInfo{tokens[0], std::stoul(tokens[1])});
}
return result;
}
Expand Down
20 changes: 10 additions & 10 deletions centipede/symbol_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,16 @@ void SymbolTable::GetSymbolsFromOneDso(absl::Span<const PCInfo> pc_infos,
// Run the symbolizer.
LOG(INFO) << "Symbolizing " << pc_infos.size() << " PCs from "
<< dso_basename;
Command cmd{symbolizer_path,
{.args =
{
"--no-inlines",
"-e",
std::string(dso_path),
"<",
std::string(pcs_file.path()),
},
.stdout_file = std::string(symbols_file.path())}};
Command::Options cmd_options;
cmd_options.args = {
"--no-inlines",
"-e",
std::string(dso_path),
"<",
std::string(pcs_file.path()),
};
cmd_options.stdout_file = std::string(symbols_file.path());
Command cmd{symbolizer_path, std::move(cmd_options)};
int exit_code = cmd.Execute();
if (exit_code != EXIT_SUCCESS) {
LOG(ERROR) << "Symbolization failed, debug symbols will not be used: "
Expand Down
5 changes: 4 additions & 1 deletion centipede/symbol_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ class SymbolTable {
return absl::StrCat(file, line_str, col_str);
}

friend bool operator==(const Entry &, const Entry &) = default;
friend bool operator==(const Entry &lhs, const Entry &rhs) {
return lhs.func == rhs.func && lhs.file == rhs.file &&
lhs.line == rhs.line && lhs.col == rhs.col;
}
};

SymbolTable() = default;
Expand Down

0 comments on commit 5e60cae

Please sign in to comment.