Reduce /proc/self/maps re-reads on unknown addresses in Symbolize.
`Symbolizer::FindObjFile()` previously cleared `addr_map_` after both iterations
of its lookup loop, causing two `/proc/self/maps` reads on an unmapped address
and leaving `addr_map_` empty so the next `absl::Symbolize()` call (even on a
valid address) had to re-read `/proc/self/maps` and re-open ELF binaries.
1. Fast-reject particular invalid addresses that cannot live in a user-space ELF
mapping or vDSO — `< 4096` (the zeroth page: `nullptr` and near-null values)
and `(addr >> 56) != 0` on 64-bit Linux architectures where user-space
virtual addresses fit in 56 bits (`x86_64`, `aarch64`, `powerpc64`,
`riscv64`) — avoiding `/proc/self/maps` invalidation and vDSO scanning
entirely for those addresses.
2. When `/proc/self/maps` is freshly read inside `FindObjFile()` and the lookup
still misses, keep the refreshed `addr_map_` cached instead of clearing it on
exit.
Note: this eliminates `/proc/self/maps` re-reads only for the fast-rejected
invalid addresses above, not for all unknown addresses. Other unmapped
user-space addresses (e.g. JIT/anonymous code, non-executable data/stack
addresses, or unmapped pointers in the valid user-space range) still miss in
the cached `addr_map_`, call `ClearAddrMap()` (closing cached ELF file
descriptors), and trigger one `/proc/self/maps` re-read per call (down from
two, while leaving `addr_map_` populated afterward; the next uncached valid
lookup still re-opens its ELF file).
On a microbenchmark (`BM_SymbolizeUnknown`), latency drops from ~235 us/call to
~5 ns/call for `nullptr` and `~uintptr_t{0}`, and from ~237 us/call to
~118 us/call (~2x faster) for a slow-path unmapped stack address.
PiperOrigin-RevId: 1029144384
diff --git a/absl/debugging/BUILD.bazel b/absl/debugging/BUILD.bazel
index 265133a..9cfd5a5 100644
--- a/absl/debugging/BUILD.bazel
+++ b/absl/debugging/BUILD.bazel
@@ -141,6 +141,7 @@
"//absl/base",
"//absl/base:config",
"//absl/base:core_headers",
+ "//absl/base:dynamic_annotations",
"//absl/base:malloc_internal",
"//absl/cleanup",
"//absl/log",
diff --git a/absl/debugging/CMakeLists.txt b/absl/debugging/CMakeLists.txt
index 247e82f..868d7df 100644
--- a/absl/debugging/CMakeLists.txt
+++ b/absl/debugging/CMakeLists.txt
@@ -111,6 +111,7 @@
absl::cleanup
absl::config
absl::core_headers
+ absl::dynamic_annotations
absl::log
absl::malloc_internal
absl::memory
diff --git a/absl/debugging/symbolize_elf.inc b/absl/debugging/symbolize_elf.inc
index 07b1167..7b8414e 100644
--- a/absl/debugging/symbolize_elf.inc
+++ b/absl/debugging/symbolize_elf.inc
@@ -1196,13 +1196,38 @@
return true;
}
+// Returns true for addresses that cannot live in a user-space ELF mapping or
+// vDSO (and therefore do not warrant reading or clearing the cached map).
+static bool IsImpossibleUserAddress(const void* addr) {
+ const uintptr_t uaddr = reinterpret_cast<uintptr_t>(addr);
+ // Reject the zeroth page (nullptr and near-null values that frequently appear
+ // in corrupted frames): Linux defaults vm.mmap_min_addr to at least 4096, so
+ // no valid ELF mapping or vDSO resides below 4 KiB.
+ if (uaddr < 4096) return true;
+#if defined(__linux__) && defined(__LP64__) && \
+ (defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__) || \
+ (defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64))
+ // User-space virtual addresses on these 64-bit Linux architectures fit in 56
+ // bits (including x86_64 5-level paging / LA57), so a non-zero top byte
+ // indicates a kernel, non-canonical, or high-byte-tagged address that cannot
+ // match an ELF object mapping or vDSO.
+ // NOTE: If/when we support tagged code pointers (ARM TBI / x86 LAM), this
+ // check will need to be updated to account for the pointer tag.
+ return (uaddr >> 56) != 0;
+#else
+ return false;
+#endif
+}
+
// Find the objfile mapped in address region containing [addr, addr + len).
ObjFile *Symbolizer::FindObjFile(const void *const addr, size_t len) {
+ if (IsImpossibleUserAddress(addr)) return nullptr;
for (int i = 0; i < 2; ++i) {
if (!ok_) return nullptr;
- // Read /proc/self/maps if necessary
- if (!addr_map_read_) {
+ // Read /proc/self/maps if necessary.
+ const bool freshly_read = !addr_map_read_;
+ if (freshly_read) {
addr_map_read_ = true;
if (!ReadAddrMap(RegisterObjFile, this, tmp_buf_, TMP_BUF_SIZE)) {
ok_ = false;
@@ -1229,7 +1254,11 @@
return obj;
}
- // The address mapping may have changed since it was last read. Retry.
+ // If the map was just refreshed above, keep it populated so subsequent
+ // lookups do not re-read /proc/self/maps and re-open ELF files.
+ if (freshly_read) break;
+
+ // The address mapping may have changed since it was last read. Retry once.
ClearAddrMap();
}
return nullptr;
@@ -1497,6 +1526,7 @@
// To keep stack consumption low, we would like this function to not
// get inlined.
const char *Symbolizer::GetUncachedSymbol(const void *pc) {
+ if (IsImpossibleUserAddress(pc)) return nullptr;
ObjFile *const obj = FindObjFile(pc, 1);
ptrdiff_t relocation = 0;
if (obj != nullptr) {
diff --git a/absl/debugging/symbolize_test.cc b/absl/debugging/symbolize_test.cc
index 87cdf6e..2eb6ce6 100644
--- a/absl/debugging/symbolize_test.cc
+++ b/absl/debugging/symbolize_test.cc
@@ -28,6 +28,7 @@
#include "absl/base/attributes.h"
#include "absl/base/casts.h"
#include "absl/base/config.h"
+#include "absl/base/dynamic_annotations.h"
#include "absl/base/internal/direct_mmap.h"
#include "absl/base/internal/low_level_alloc.h"
#include "absl/base/internal/per_thread_tls.h"
@@ -185,6 +186,29 @@
strcmp("static_func()", static_func_symbol) == 0);
EXPECT_TRUE(nullptr == TrySymbolize(nullptr));
+#ifdef ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE
+ // Near-null addresses (< 4096) are fast-rejected.
+ EXPECT_EQ(nullptr, TrySymbolize(reinterpret_cast<void*>(uintptr_t{0x100})));
+ // An untagged non-executable stack address misses once and refreshes the
+ // cached address map; verify subsequent uncached lookup still succeeds.
+ int stack_var = 0;
+ EXPECT_EQ(nullptr, TrySymbolize(absl::HwasanTagPointer(&stack_var, 0)));
+#if defined(__linux__) && defined(__LP64__) && \
+ (defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__) || \
+ (defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64))
+ // Addresses with a non-zero top byte (kernel, non-canonical, or tagged)
+ // fail to symbolize quickly without refreshing the cached address map.
+ EXPECT_EQ(nullptr,
+ TrySymbolize(reinterpret_cast<void*>((uintptr_t{0x01} << 56) | 1)));
+ EXPECT_EQ(nullptr,
+ TrySymbolize(reinterpret_cast<void*>((uintptr_t{0xFE} << 56) | 1)));
+ EXPECT_EQ(nullptr, TrySymbolize(reinterpret_cast<void*>(~uintptr_t{0})));
+#endif
+ // Distinct (uncached) offset exercises `addr_map_` lookup after the misses.
+ EXPECT_STREQ("nonstatic_func",
+ TrySymbolize(reinterpret_cast<void*>(
+ reinterpret_cast<uintptr_t>(&nonstatic_func) + 1)));
+#endif
}
TEST(Symbolize, Truncation) {
@@ -364,8 +388,12 @@
ASSERT_NE(ret, MAP_FAILED);
}
- // Invalidate the symbolization cache so we are forced to rely on the hint.
- absl::Symbolize(nullptr, buf, sizeof(buf));
+ // Refresh the symbolization cache so we are forced to rely on the hint.
+ // Use an untagged stack variable address: addr_map_ only caches executable
+ // file-backed mappings, whereas nullptr, low addresses (< 4096), and
+ // top-byte-tagged pointers are fast-rejected.
+ int stack_var = 0;
+ absl::Symbolize(absl::HwasanTagPointer(&stack_var, 0), buf, sizeof(buf));
// Verify we can still symbolize.
const char *expected[] = {"kPadding0", "kPadding1"};