#Centipede Cleanup `GetTestTempDir` and its usage PiperOrigin-RevId: 602933679
diff --git a/centipede/BUILD b/centipede/BUILD index c6c0511..059081b 100644 --- a/centipede/BUILD +++ b/centipede/BUILD
@@ -1149,7 +1149,6 @@ ":blob_file", ":defs", ":logging", - ":util", "@com_google_absl//absl/log", "@com_google_absl//absl/log:check", "@com_google_absl//absl/strings", @@ -1645,7 +1644,6 @@ "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/log", "@com_google_absl//absl/log:check", - "@com_google_absl//absl/strings", "@com_google_googletest//:gtest_main", ], ) @@ -1759,7 +1757,6 @@ ":test_coverage_util", ":test_util", "@com_google_absl//absl/container:flat_hash_set", - "@com_google_absl//absl/strings", "@com_google_googletest//:gtest_main", ], )
diff --git a/centipede/analyze_corpora_test.cc b/centipede/analyze_corpora_test.cc index 0d22859..e05629f 100644 --- a/centipede/analyze_corpora_test.cc +++ b/centipede/analyze_corpora_test.cc
@@ -48,9 +48,9 @@ // Get pc_table and symbols. bool uses_legacy_trace_pc_instrumentation = {}; BinaryInfo binary_info; - binary_info.InitializeFromSanCovBinary(GetTargetPath(), GetObjDumpPath(), - GetLLVMSymbolizerPath(), - GetTestTempDir()); + binary_info.InitializeFromSanCovBinary( + GetTargetPath(), GetObjDumpPath(), GetLLVMSymbolizerPath(), + GetTestTempDir(test_info_->name()).string()); const auto &pc_table = binary_info.pc_table; EXPECT_FALSE(uses_legacy_trace_pc_instrumentation); const SymbolTable &symbols = binary_info.symbols;
diff --git a/centipede/binary_info_test.cc b/centipede/binary_info_test.cc index 0519cae..7c15f5a 100644 --- a/centipede/binary_info_test.cc +++ b/centipede/binary_info_test.cc
@@ -27,7 +27,9 @@ namespace { TEST(BinaryInfoTest, SerializesAndDeserializesBinaryInfoSuccessfully) { - PCTable input_pcs = {{.pc = 0, .flags = 1}, {.pc = 2, .flags = 3}}; + const std::string temp_dir = GetTestTempDir(test_info_->name()); + + const PCTable input_pcs = {{.pc = 0, .flags = 1}, {.pc = 2, .flags = 3}}; std::string input_symbols = R"(FunctionOne source/location/one.cc:1:0 @@ -39,10 +41,10 @@ std::istringstream input_stream(input_symbols); SymbolTable symbol_table; symbol_table.ReadFromLLVMSymbolizer(input_stream); - BinaryInfo input = {.pc_table = input_pcs, - .symbols = std::move(symbol_table)}; - - auto temp_dir = GetTestTempDir(test_info_->name()); + BinaryInfo input = { + .pc_table = input_pcs, + .symbols = std::move(symbol_table), + }; input.Write(temp_dir); BinaryInfo output; output.Read(temp_dir);
diff --git a/centipede/control_flow_test.cc b/centipede/control_flow_test.cc index 2727aa0..abc927b 100644 --- a/centipede/control_flow_test.cc +++ b/centipede/control_flow_test.cc
@@ -27,7 +27,6 @@ #include "absl/container/flat_hash_map.h" #include "absl/log/check.h" #include "absl/log/log.h" -#include "absl/strings/str_cat.h" #include "./centipede/binary_info.h" #include "./centipede/logging.h" #include "./centipede/pc_info.h" @@ -173,12 +172,6 @@ t3.join(); } -// Returns a path for i-th temporary file. -static std::string GetTempFilePath(size_t i) { - return std::filesystem::path(GetTestTempDir()) - .append(absl::StrCat("coverage_test", i, "-", getpid())); -} - // Returns path to test_fuzz_target. static std::string GetTargetPath() { return GetDataDependencyFilepath("centipede/testing/test_fuzz_target"); @@ -193,13 +186,14 @@ // Tests GetCfTableFromBinary() on test_fuzz_target. TEST(CFTable, GetCfTable) { auto target_path = GetTargetPath(); - std::string tmp_path1 = GetTempFilePath(1); - std::string tmp_path2 = GetTempFilePath(2); + std::string tmp_path1 = GetTempFilePath(test_info_->name(), 1); + std::string tmp_path2 = GetTempFilePath(test_info_->name(), 2); // Load the cf table. BinaryInfo binary_info; binary_info.InitializeFromSanCovBinary( - target_path, GetObjDumpPath(), GetLLVMSymbolizerPath(), GetTestTempDir()); + target_path, GetObjDumpPath(), GetLLVMSymbolizerPath(), + GetTestTempDir(test_info_->name()).string()); const auto &cf_table = binary_info.cf_table; LOG(INFO) << VV(target_path) << VV(tmp_path1) << VV(cf_table.size()); if (cf_table.empty()) { @@ -273,10 +267,11 @@ } } -static void SymbolizeBinary(std::string_view target_path, bool use_trace_pc) { +static void SymbolizeBinary(std::string_view test_dir, + std::string_view target_path, bool use_trace_pc) { BinaryInfo binary_info; - binary_info.InitializeFromSanCovBinary( - target_path, GetObjDumpPath(), GetLLVMSymbolizerPath(), GetTestTempDir()); + binary_info.InitializeFromSanCovBinary(target_path, GetObjDumpPath(), + GetLLVMSymbolizerPath(), test_dir); // Load the pc table. const auto &pc_table = binary_info.pc_table; // Check that it's not empty. @@ -319,14 +314,16 @@ // Tests GetPcTableFromBinary() and SymbolTable on test_fuzz_target. TEST(PCTable, GetPcTableFromBinary_And_SymbolTable_PCTable) { - EXPECT_NO_FATAL_FAILURE( - SymbolizeBinary(GetTargetPath(), /*use_trace_pc=*/false)); + EXPECT_NO_FATAL_FAILURE(SymbolizeBinary( + GetTestTempDir(test_info_->name()).string(), GetTargetPath(), + /*use_trace_pc=*/false)); } // Tests GetPcTableFromBinary() and SymbolTable on test_fuzz_target_trace_pc. TEST(PCTable, GetPcTableFromBinary_And_SymbolTable_TracePC) { - EXPECT_NO_FATAL_FAILURE( - SymbolizeBinary(GetTracePCTargetPath(), /*use_trace_pc=*/true)); + EXPECT_NO_FATAL_FAILURE(SymbolizeBinary( + GetTestTempDir(test_info_->name()).string(), GetTracePCTargetPath(), + /*use_trace_pc=*/true)); } } // namespace
diff --git a/centipede/coverage_test.cc b/centipede/coverage_test.cc index 5410ff8..873156b 100644 --- a/centipede/coverage_test.cc +++ b/centipede/coverage_test.cc
@@ -31,7 +31,6 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" #include "absl/container/flat_hash_set.h" -#include "absl/strings/str_cat.h" #include "./centipede/binary_info.h" #include "./centipede/control_flow.h" #include "./centipede/environment.h" @@ -171,12 +170,6 @@ t2.join(); } -// Returns a path for i-th temporary file. -static std::string GetTempFilePath(size_t i) { - return std::filesystem::path(GetTestTempDir()) - .append(absl::StrCat("coverage_test", i, "-", getpid())); -} - // Returns path to test_fuzz_target. static std::string GetTargetPath() { return GetDataDependencyFilepath("centipede/testing/test_fuzz_target"); @@ -199,9 +192,9 @@ // Get pc_table and symbols. bool uses_legacy_trace_pc_instrumentation = {}; BinaryInfo binary_info; - binary_info.InitializeFromSanCovBinary(GetTargetPath(), GetObjDumpPath(), - GetLLVMSymbolizerPath(), - GetTestTempDir()); + binary_info.InitializeFromSanCovBinary( + GetTargetPath(), GetObjDumpPath(), GetLLVMSymbolizerPath(), + GetTestTempDir(test_info_->name()).string()); const auto &pc_table = binary_info.pc_table; EXPECT_FALSE(uses_legacy_trace_pc_instrumentation); const SymbolTable &symbols = binary_info.symbols; @@ -420,16 +413,17 @@ TEST(Coverage, FunctionFilter) { // Initialize coverage data. BinaryInfo binary_info; - binary_info.InitializeFromSanCovBinary(GetTargetPath(), GetObjDumpPath(), - GetLLVMSymbolizerPath(), - GetTestTempDir()); + binary_info.InitializeFromSanCovBinary( + GetTargetPath(), GetObjDumpPath(), GetLLVMSymbolizerPath(), + GetTestTempDir(test_info_->name()).string()); const PCTable &pc_table = binary_info.pc_table; EXPECT_FALSE(binary_info.uses_legacy_trace_pc_instrumentation); const DsoTable dso_table = {{GetTargetPath(), pc_table.size()}}; SymbolTable symbols; symbols.GetSymbolsFromBinary(pc_table, dso_table, GetLLVMSymbolizerPath(), - GetTempFilePath(0), GetTempFilePath(1)); + GetTempFilePath(test_info_->name(), 0), + GetTempFilePath(test_info_->name(), 1)); // Empty filter. FunctionFilter empty_filter("", symbols); EXPECT_EQ(empty_filter.count(), 0);
diff --git a/centipede/distill_test.cc b/centipede/distill_test.cc index 9c0138c..328caa6 100644 --- a/centipede/distill_test.cc +++ b/centipede/distill_test.cc
@@ -100,7 +100,7 @@ // We need to set at least --binary_hash before `env` is constructed, // so we do this by overriding the flags. absl::FlagSaver flag_saver; - std::string dir = std::filesystem::path(GetTestTempDir()).append(test_name); + std::string dir = GetTestTempDir(test_name); std::filesystem::remove_all(dir); std::filesystem::create_directories(dir); Environment env;
diff --git a/centipede/runner_result_test.cc b/centipede/runner_result_test.cc index 1279c6a..4474a30 100644 --- a/centipede/runner_result_test.cc +++ b/centipede/runner_result_test.cc
@@ -95,8 +95,7 @@ } TEST(ExecutionResult, WriteIntoFileThenRead) { - const std::string temp_file = - std::filesystem::path(GetTestTempDir()).append(test_info_->name()); + const std::string temp_file = GetTestTempDir(test_info_->name()) / "tmp.txt"; std::ofstream output_stream(temp_file, std::ios::out); ASSERT_TRUE(output_stream.is_open());
diff --git a/centipede/seed_corpus_maker_lib_test.cc b/centipede/seed_corpus_maker_lib_test.cc index d788473..4e6a55e 100644 --- a/centipede/seed_corpus_maker_lib_test.cc +++ b/centipede/seed_corpus_maker_lib_test.cc
@@ -103,7 +103,7 @@ } TEST(SeedCorpusMakerLibTest, ResolveConfig) { - const std::string test_dir = fs::canonical(GetTestTempDir()); + const std::string test_dir = GetTestTempDir(test_info_->name()); // `ResolveSeedCorpusConfig()` should use the CWD to resolve relative paths. chdir(test_dir.c_str()); @@ -152,7 +152,7 @@ } TEST(SeedCorpusMakerLibTest, RoundTripWriteReadWrite) { - const fs::path test_dir = fs::canonical(GetTestTempDir()); + const fs::path test_dir = GetTestTempDir(test_info_->name()); // `ResolveSeedCorpusConfig()` should use the CWD to resolve relative paths. chdir(test_dir.c_str());
diff --git a/centipede/test_util.cc b/centipede/test_util.cc index 7a9d037..1096b0a 100644 --- a/centipede/test_util.cc +++ b/centipede/test_util.cc
@@ -25,7 +25,7 @@ namespace centipede { -std::string GetTestTempDir(std::string_view subdir = "") { +std::filesystem::path GetTestTempDir(std::string_view subdir) { std::filesystem::path dir; dir = std::getenv("TEST_TMPDIR"); if (dir.empty()) dir = std::getenv("TMPDIR"); @@ -36,7 +36,11 @@ std::filesystem::create_directories(dir, error); CHECK(!error) << "Failed to create dir: " VV(dir) << error.message(); } - return dir; + return std::filesystem::canonical(dir); +} + +std::string GetTempFilePath(std::string_view subdir, size_t i) { + return GetTestTempDir(subdir) / absl::StrCat("tmp.", getpid(), ".", i); } std::filesystem::path GetTestRunfilesDir() {
diff --git a/centipede/test_util.h b/centipede/test_util.h index 18ce0f5..0158fc3 100644 --- a/centipede/test_util.h +++ b/centipede/test_util.h
@@ -15,14 +15,17 @@ #ifndef THIRD_PARTY_CENTIPEDE_INTERNAL_TEST_UTIL_H_ #define THIRD_PARTY_CENTIPEDE_INTERNAL_TEST_UTIL_H_ +#include <cstddef> #include <filesystem> // NOLINT +#include <memory> #include <string> +#include <string_view> +#include <vector> -#include "absl/strings/str_cat.h" +#include "absl/log/check.h" #include "absl/strings/str_format.h" #include "./centipede/blob_file.h" #include "./centipede/defs.h" -#include "./centipede/util.h" #include "./centipede/logging.h" @@ -42,7 +45,10 @@ // `test_into_->name()`, which returns the name of the test case. // // If the final dir doesn't exist, it gets created. -std::string GetTestTempDir(std::string_view subdir = ""); +std::filesystem::path GetTestTempDir(std::string_view subdir); + +// Returns a path for i-th temporary file. +std::string GetTempFilePath(std::string_view subdir, size_t i); // Returns the root directory filepath for a test's "runfiles". std::filesystem::path GetTestRunfilesDir(); @@ -63,7 +69,7 @@ class TempDir { public: explicit TempDir(std::string_view leaf1, std::string_view leaf2 = "") - : path_{std::filesystem::path(GetTestTempDir()) / leaf1 / leaf2} { + : path_{GetTestTempDir(leaf1) / leaf2} { std::filesystem::remove_all(path_); std::filesystem::create_directories(path_); }