[compiler-rt] [msan] Dynamically grow kNumStackOriginDescrs (PR #92826)
Thurston Dang via llvm-commits
llvm-commits at lists.llvm.org
Mon May 20 14:48:29 PDT 2024
https://github.com/thurstond created https://github.com/llvm/llvm-project/pull/92826
StackOriginDescr/StackOriginPC are currently fixed size arrays
and have a FIXME of resizability. This patch dynamically doubles
the size of these arrays as needed (for runtime that is amortized linear
in the size of the array).
Since NumStackOriginDescrs (the number of items currently stored in the array)
is monotonically increasing, there is no need to ever decrease the
allocated size of the array.
Testing notes: the old fixed limit was 1 million variables; this would be an
extremely large test case. By manually capping the maximum size, I found
that param_tls_limit.cpp is the test that is most sensitive to
kNumStackOriginDescrs.
>From b42c106bdc39b3adcae8bfa675a154baf6d60d93 Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Mon, 20 May 2024 21:42:14 +0000
Subject: [PATCH] [msan] Dynamically grow kNumStackOriginDescrs
StackOriginDescr/StackOriginPC are currently fixed size arrays
and have a FIXME of resizability. This patch dynamically doubles
the size of these arrays as needed (for runtime that is amortized linear
in the size of the array).
Since NumStackOriginDescrs (the number of items currently stored in the array)
is monotonically increasing, there is no need to ever decrease the
allocated size of the array.
Testing notes: the old fixed limit was 1 million variables; this would be an
extremely large test case. By manually capping the maximum size, I found
that param_tls_limit.cpp is the test that is most sensitive to
kNumStackOriginDescrs.
---
compiler-rt/lib/msan/msan.cpp | 29 +++++++++++++++++++++++++----
1 file changed, 25 insertions(+), 4 deletions(-)
diff --git a/compiler-rt/lib/msan/msan.cpp b/compiler-rt/lib/msan/msan.cpp
index a2fc27de1901b..e66b5145f83de 100644
--- a/compiler-rt/lib/msan/msan.cpp
+++ b/compiler-rt/lib/msan/msan.cpp
@@ -99,10 +99,10 @@ bool msan_init_is_running;
int msan_report_count = 0;
// Array of stack origins.
-// FIXME: make it resizable.
-static const uptr kNumStackOriginDescrs = 1024 * 1024;
-static const char *StackOriginDescr[kNumStackOriginDescrs];
-static uptr StackOriginPC[kNumStackOriginDescrs];
+// This is resized dynamically by ResizeStackOriginMetadata
+static uptr kNumStackOriginDescrs = 0;
+static char **StackOriginDescr = nullptr;
+static uptr *StackOriginPC = nullptr;
static atomic_uint32_t NumStackOriginDescrs;
void Flags::SetDefaults() {
@@ -282,7 +282,27 @@ void ScopedThreadLocalStateBackup::Restore() {
void UnpoisonThreadLocalState() {
}
+// Grow stack origin metadata structures exponentially if out of space
+static void ResizeStackOriginMetadata(u32 id) {
+ if (id >= kNumStackOriginDescrs) {
+ if (kNumStackOriginDescrs == 0)
+ kNumStackOriginDescrs = 1;
+ else
+ kNumStackOriginDescrs = kNumStackOriginDescrs * 2;
+
+ VReport(2, "Resized stack origin metadata to %lu entries\n",
+ kNumStackOriginDescrs);
+ }
+
+ StackOriginDescr = (char **)InternalRealloc(
+ StackOriginDescr, kNumStackOriginDescrs * sizeof(char *));
+ StackOriginPC = (uptr *)InternalRealloc(StackOriginPC,
+ kNumStackOriginDescrs * sizeof(uptr));
+ // No checks needed - InternalRealloc() will die if out of memory
+}
+
const char *GetStackOriginDescr(u32 id, uptr *pc) {
+ ResizeStackOriginMetadata(id);
CHECK_LT(id, kNumStackOriginDescrs);
if (pc) *pc = StackOriginPC[id];
return StackOriginDescr[id];
@@ -312,6 +332,7 @@ static inline void SetAllocaOrigin(void *a, uptr size, u32 *id_ptr, char *descr,
u32 id = *id_ptr;
if (id == 0 || id == first_timer) {
u32 idx = atomic_fetch_add(&NumStackOriginDescrs, 1, memory_order_relaxed);
+ ResizeStackOriginMetadata(idx);
CHECK_LT(idx, kNumStackOriginDescrs);
StackOriginDescr[idx] = descr;
StackOriginPC[idx] = pc;
More information about the llvm-commits
mailing list