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
This commit is contained in:
Christian Blichmann 2022-02-11 07:19:01 -08:00 committed by Copybara-Service
parent 1b816e9dce
commit 36d0f928c6
2 changed files with 13 additions and 10 deletions

View File

@ -177,8 +177,10 @@ absl::StatusOr<SymbolMap> 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) {

View File

@ -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<uint64_t>(ExportedFunctionName);
auto function_entry =
uint64_t function_address = reinterpret_cast<uint64_t>(&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<std::string> imported_libraries = {"libc.so.6"};
EXPECT_THAT(elf.imported_libraries(), Eq(imported_libraries));
EXPECT_THAT(elf.imported_libraries(), ElementsAre("libc.so.6"));
}
} // namespace