From 36d0f928c6e78747ffb122372d4d94d97fd0a193 Mon Sep 17 00:00:00 2001 From: Christian Blichmann Date: Fri, 11 Feb 2022 07:19:01 -0800 Subject: [PATCH] Apply page offset during stack unwinding/symbolization This fixes a couple of tests in the open source version of the code. Internally, since we are using a different ELF loader, the page offset will always be zero. Hence we never notices this was broken. PiperOrigin-RevId: 427996428 Change-Id: I44c5b5610b074cf69b9f0c5eeb051be50923e351 --- sandboxed_api/sandbox2/unwind/unwind.cc | 6 ++++-- sandboxed_api/sandbox2/util/minielf_test.cc | 17 +++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/sandboxed_api/sandbox2/unwind/unwind.cc b/sandboxed_api/sandbox2/unwind/unwind.cc index ddeaba0..e62a458 100644 --- a/sandboxed_api/sandbox2/unwind/unwind.cc +++ b/sandboxed_api/sandbox2/unwind/unwind.cc @@ -177,8 +177,10 @@ absl::StatusOr LoadSymbolsMap(pid_t pid) { for (const ElfFile::Symbol& symbol : elf->symbols()) { if (elf->position_independent()) { - if (symbol.address < entry.end - entry.start) { - addr_to_symbol[symbol.address + entry.start] = symbol.name; + if (symbol.address >= entry.pgoff && + symbol.address - entry.pgoff < entry.end - entry.start) { + addr_to_symbol[symbol.address + entry.start - entry.pgoff] = + symbol.name; } } else { if (symbol.address >= entry.start && symbol.address < entry.end) { diff --git a/sandboxed_api/sandbox2/util/minielf_test.cc b/sandboxed_api/sandbox2/util/minielf_test.cc index 1e973c3..cccd97f 100644 --- a/sandboxed_api/sandbox2/util/minielf_test.cc +++ b/sandboxed_api/sandbox2/util/minielf_test.cc @@ -26,13 +26,14 @@ #include "sandboxed_api/util/file_helpers.h" #include "sandboxed_api/util/status_matchers.h" -extern "C" void ExportedFunctionName() { +extern "C" void ExportedFunction() { // Don't do anything - used to generate a symbol. } namespace file = ::sapi::file; using ::sapi::GetTestSourcePath; using ::sapi::IsOk; +using ::testing::ElementsAre; using ::testing::Eq; using ::testing::IsTrue; using ::testing::Ne; @@ -65,19 +66,20 @@ TEST(MinielfTest, SymbolResolutionWorks) { ParseProcMaps(maps_buffer)); // Find maps entry that covers this entry. - uint64_t function_address = reinterpret_cast(ExportedFunctionName); - auto function_entry = + uint64_t function_address = reinterpret_cast(&ExportedFunction); + auto entry = absl::c_find_if(maps, [function_address](const MapsEntry& entry) { return entry.start <= function_address && entry.end > function_address; }); - ASSERT_THAT(function_entry, Ne(maps.end())); - function_address -= function_entry->start; + ASSERT_THAT(entry, Ne(maps.end())); auto function_symbol = absl::c_find_if(elf.symbols(), [](const ElfFile::Symbol& symbol) { - return symbol.name == "ExportedFunctionName"; + return symbol.name == "ExportedFunction"; }); ASSERT_THAT(function_symbol, Ne(elf.symbols().end())); + + function_address -= entry->start - entry->pgoff; EXPECT_THAT(function_symbol->address, Eq(function_address)); } @@ -86,8 +88,7 @@ TEST(MinielfTest, ImportedLibraries) { ElfFile elf, ElfFile::ParseFromFile( GetTestSourcePath("sandbox2/util/testdata/hello_world"), ElfFile::kLoadImportedLibraries)); - std::vector imported_libraries = {"libc.so.6"}; - EXPECT_THAT(elf.imported_libraries(), Eq(imported_libraries)); + EXPECT_THAT(elf.imported_libraries(), ElementsAre("libc.so.6")); } } // namespace