Skip to content

Commit 887401c

Browse files
authored
Update ElfReaderTest's nm usage to be hermetic (#1980)
Summary: Update `ElfReaderTest`'s nm usage to be hermetic This was feedback from #1976. See [this comment](#1976 (comment)) for the previous discussion. Relevant Issues: N/A Type of change: /kind cleanup Test Plan: Elf reader tests pass with updates --------- Signed-off-by: Dom Del Nano <ddelnano@gmail.com>
1 parent 33f0daf commit 887401c

6 files changed

Lines changed: 47 additions & 7 deletions

File tree

bazel/cc_toolchains/clang/toolchain_files.BUILD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ filegroup(
5959
visibility = ["//visibility:public"],
6060
)
6161

62+
filegroup(
63+
name = "toolchain_nm_files",
64+
srcs = [":nm"],
65+
visibility = ["//visibility:public"],
66+
)
67+
6268
filegroup(
6369
name = "toolchain_strip_files",
6470
srcs = [":strip"],
@@ -110,6 +116,7 @@ filegroup(
110116
"as",
111117
"cov",
112118
"objcopy",
119+
"nm",
113120
"strip",
114121
"dwp",
115122
]
@@ -124,6 +131,7 @@ filegroup(
124131
":toolchain_dwp_files",
125132
":toolchain_linker_files",
126133
":toolchain_objcopy_files",
134+
":toolchain_nm_files",
127135
":toolchain_strip_files",
128136
],
129137
visibility = ["//visibility:public"],

src/stirling/obj_tools/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pl_cc_test(
5353
"//src/stirling/obj_tools/testdata/cc:test_exe_debug_target",
5454
"//src/stirling/obj_tools/testdata/cc:test_exe_debuglink_target",
5555
"//src/stirling/obj_tools/testdata/go:test_go_1_19_binary",
56+
"//src/stirling/obj_tools/testdata/go:test_go_1_19_nm_output",
5657
],
5758
deps = [
5859
":cc_library",

src/stirling/obj_tools/elf_reader_test.cc

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ StatusOr<Section> ObjdumpSectionNameToAddr(const std::string& path,
8989
return section;
9090
}
9191

92-
StatusOr<int64_t> NmSymbolNameToAddr(const std::string& path, const std::string& symbol_name) {
92+
StatusOr<int64_t> NmSymbolNameToAddr(const std::string& nm_output_path,
93+
const std::string& symbol_name) {
9394
// Extract the address from nm as the gold standard.
9495
int64_t symbol_addr = -1;
95-
std::string nm_out = px::Exec(absl::StrCat("nm ", path)).ValueOrDie();
96+
PX_ASSIGN_OR_RETURN(auto nm_out, px::ReadFileToString(nm_output_path));
9697
std::vector<absl::string_view> nm_out_lines = absl::StrSplit(nm_out, '\n');
9798
for (auto& line : nm_out_lines) {
9899
if (line.find(symbol_name) != std::string::npos) {
@@ -163,8 +164,9 @@ TEST(ElfReaderTest, ListFuncSymbolsSuffixMatch) {
163164

164165
TEST(ElfReaderTest, SymbolAddress) {
165166
const std::string path = kTestExeFixture.Path().string();
167+
const std::string nm_output_path = kTestExeFixture.NmOutputPath().string();
166168
const std::string kSymbolName = "CanYouFindThis";
167-
ASSERT_OK_AND_ASSIGN(const int64_t symbol_addr, NmSymbolNameToAddr(path, kSymbolName));
169+
ASSERT_OK_AND_ASSIGN(const int64_t symbol_addr, NmSymbolNameToAddr(nm_output_path, kSymbolName));
168170

169171
// Actual tests of SymbolAddress begins here.
170172

@@ -195,8 +197,9 @@ TEST(ElfReaderTest, VirtualAddrToBinaryAddr) {
195197

196198
TEST(ElfReaderTest, AddrToSymbol) {
197199
const std::string path = kTestExeFixture.Path().string();
200+
const std::string nm_output_path = kTestExeFixture.NmOutputPath().string();
198201
const std::string kSymbolName = "CanYouFindThis";
199-
ASSERT_OK_AND_ASSIGN(const int64_t symbol_addr, NmSymbolNameToAddr(path, kSymbolName));
202+
ASSERT_OK_AND_ASSIGN(const int64_t symbol_addr, NmSymbolNameToAddr(nm_output_path, kSymbolName));
200203

201204
ASSERT_OK_AND_ASSIGN(std::unique_ptr<ElfReader> elf_reader, ElfReader::Create(path));
202205

@@ -216,8 +219,9 @@ TEST(ElfReaderTest, AddrToSymbol) {
216219

217220
TEST(ElfReaderTest, InstrAddrToSymbol) {
218221
const std::string path = kTestExeFixture.Path().string();
222+
const std::string nm_output_path = kTestExeFixture.NmOutputPath().string();
219223
const std::string kSymbolName = "CanYouFindThis";
220-
ASSERT_OK_AND_ASSIGN(const int64_t kSymbolAddr, NmSymbolNameToAddr(path, kSymbolName));
224+
ASSERT_OK_AND_ASSIGN(const int64_t kSymbolAddr, NmSymbolNameToAddr(nm_output_path, kSymbolName));
221225

222226
ASSERT_OK_AND_ASSIGN(std::unique_ptr<ElfReader> elf_reader, ElfReader::Create(path));
223227

@@ -300,12 +304,15 @@ TEST(ElfReaderTest, FuncByteCode) {
300304
TEST(ElfReaderTest, GolangAppRuntimeBuildVersion) {
301305
const std::string kPath =
302306
px::testing::BazelRunfilePath("src/stirling/obj_tools/testdata/go/test_go_1_19_binary");
307+
const std::string kGoBinNmOutput =
308+
px::testing::BazelRunfilePath("src/stirling/obj_tools/testdata/go/test_go_1_19_nm_output");
303309
ASSERT_OK_AND_ASSIGN(std::unique_ptr<ElfReader> elf_reader, ElfReader::Create(kPath));
304310
ASSERT_OK_AND_ASSIGN(ElfReader::SymbolInfo symbol,
305311
elf_reader->SearchTheOnlySymbol("runtime.buildVersion"));
306312
// Coverage build might alter the resultant binary.
307313
#ifndef PL_COVERAGE
308-
ASSERT_OK_AND_ASSIGN(auto expected_addr, NmSymbolNameToAddr(kPath, "runtime.buildVersion"));
314+
ASSERT_OK_AND_ASSIGN(auto expected_addr,
315+
NmSymbolNameToAddr(kGoBinNmOutput, "runtime.buildVersion"));
309316
EXPECT_EQ(symbol.address, expected_addr);
310317
#endif
311318
EXPECT_EQ(symbol.size, 16) << "Symbol table entry size should be 16";

src/stirling/obj_tools/testdata/cc/BUILD.bazel

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,21 @@ cc_clang_binary(
4141
],
4242
)
4343

44+
genrule(
45+
name = "test_exe_nm_output_target",
46+
srcs = [":test_exe"],
47+
outs = ["test_exe_nm_output"],
48+
cmd = "$(NM) $(location :test_exe) > $(location test_exe_nm_output)",
49+
toolchains = ["@bazel_tools//tools/cpp:current_cc_toolchain"],
50+
)
51+
4452
cc_library(
4553
name = "test_exe_fixture",
4654
hdrs = ["test_exe_fixture.h"],
47-
data = [":test_exe"],
55+
data = [
56+
":test_exe",
57+
":test_exe_nm_output",
58+
],
4859
deps = ["//src/common/exec:cc_library"],
4960
)
5061

src/stirling/obj_tools/testdata/cc/test_exe_fixture.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ namespace obj_tools {
3131
class TestExeFixture {
3232
public:
3333
static constexpr char kTestExePath[] = "src/stirling/obj_tools/testdata/cc/test_exe_/test_exe";
34+
static constexpr char kTestExeNmOutputPath[] =
35+
"src/stirling/obj_tools/testdata/cc/test_exe_nm_output";
3436

3537
const std::filesystem::path& Path() const { return test_exe_path_; }
38+
const std::filesystem::path& NmOutputPath() const { return test_exe_nm_output_path_; }
3639

3740
Status Run() const {
3841
auto stdout_or = Exec(test_exe_path_);
@@ -41,6 +44,8 @@ class TestExeFixture {
4144

4245
private:
4346
const std::filesystem::path test_exe_path_ = testing::BazelRunfilePath(kTestExePath);
47+
const std::filesystem::path test_exe_nm_output_path_ =
48+
testing::BazelRunfilePath(kTestExeNmOutputPath);
4449
};
4550

4651
} // namespace obj_tools

src/stirling/obj_tools/testdata/go/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ pl_go_binary(
4747
for sdk_version in pl_supported_go_sdk_versions
4848
]
4949

50+
genrule(
51+
name = "test_go_1_19_nm_output_target",
52+
srcs = [":test_go_1_19_binary"],
53+
outs = ["test_go_1_19_nm_output"],
54+
cmd = "$(NM) $(location :test_go_1_19_binary) > $(location test_go_1_19_nm_output)",
55+
toolchains = ["@bazel_tools//tools/cpp:current_cc_toolchain"],
56+
)
57+
5058
filegroup(
5159
name = "test_binaries",
5260
testonly = True,

0 commit comments

Comments
 (0)