Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(diagnostics): attribute allocations to the calling code

Martin Vogelab088e324c50
diff --git a/internal/cbm/cbm.c b/internal/cbm/cbm.c
index eb4ff346b..aae578540 100644
--- a/internal/cbm/cbm.c
+++ b/internal/cbm/cbm.c
@@ -269,7 +269,7 @@ static TSParser *get_thread_parser(const TSLanguage *ts_lang, CBMLanguage lang)
* (#581). */
static void *cbm_sqlite_malloc(int n) {
void *block = mi_malloc((size_t)n);
- cbm_mem_profile_alloc(block, (size_t)n);
+ cbm_mem_profile_alloc_at(block, (size_t)n, __builtin_return_address(0));
return block;
}
static void cbm_sqlite_free(void *p) {
@@ -281,7 +281,7 @@ static void *cbm_sqlite_realloc(void *p, int n) {
cbm_mem_profile_free(p);
}
void *grown = mi_realloc(p, (size_t)n);
- cbm_mem_profile_alloc(grown, (size_t)n);
+ cbm_mem_profile_alloc_at(grown, (size_t)n, __builtin_return_address(0));
return grown;
}
static int cbm_sqlite_size(void *p) {
@@ -294,12 +294,12 @@ static int cbm_sqlite_roundup(int n) {
* through these, and they too skip the interposer. */
static void *cbm_ts_malloc(size_t n) {
void *block = mi_malloc(n);
- cbm_mem_profile_alloc(block, n);
+ cbm_mem_profile_alloc_at(block, n, __builtin_return_address(0));
return block;
}
static void *cbm_ts_calloc(size_t count, size_t size) {
void *block = mi_calloc(count, size);
- cbm_mem_profile_alloc(block, count * size);
+ cbm_mem_profile_alloc_at(block, count * size, __builtin_return_address(0));
return block;
}
static void *cbm_ts_realloc(void *p, size_t n) {
@@ -307,7 +307,7 @@ static void *cbm_ts_realloc(void *p, size_t n) {
cbm_mem_profile_free(p);
}
void *grown = mi_realloc(p, n);
- cbm_mem_profile_alloc(grown, n);
+ cbm_mem_profile_alloc_at(grown, n, __builtin_return_address(0));
return grown;
}
static void cbm_ts_free(void *p) {
diff --git a/src/foundation/mem.c b/src/foundation/mem.c
index 56975ca1f..459cd36dc 100644
--- a/src/foundation/mem.c
+++ b/src/foundation/mem.c
@@ -880,8 +880,10 @@ void cbm_mem_census_log(const char *tag) {
char prof_live_kb[CBM_SZ_32];
char prof_sites[CBM_SZ_32];
char prof_lost[CBM_SZ_32];
+ char prof_total_kb[CBM_SZ_32];
(void)snprintf(prof_live_kb, sizeof(prof_live_kb), "%zu", profile.live_bytes / CBM_SZ_1K);
(void)snprintf(prof_sites, sizeof(prof_sites), "%zu", profile.sites);
+ (void)snprintf(prof_total_kb, sizeof(prof_total_kb), "%zu", profile.total_bytes / CBM_SZ_1K);
(void)snprintf(prof_lost, sizeof(prof_lost), "%zu",
profile.site_table_full + profile.pointer_table_full +
profile.capture_failed);
@@ -890,7 +892,7 @@ void cbm_mem_census_log(const char *tag) {
(void)cbm_mem_profile_dump(profile_path, tag ? tag : "?");
}
cbm_log_info("mem.census", "at", tag ? tag : "?",
- "prof_live_kb", prof_live_kb, "prof_sites", prof_sites, "prof_lost", prof_lost, "priv_kb", priv_kb, "crt_kb", crt_kb,
+ "prof_live_kb", prof_live_kb, "prof_sites", prof_sites, "prof_total_kb", prof_total_kb, "prof_lost", prof_lost, "priv_kb", priv_kb, "crt_kb", crt_kb,
"crt_busy_kb", busy_kb, "mapped_kb", mapped_kb, "rss_kb", rss_kb, "biggest_kb",
biggest_kb, "regions", regions, "big_regions", big_regions, "heaps", heaps,
"decommit_calls", dc_calls, "decommit_fails", dc_fail, "decommit_err", dc_err,
diff --git a/src/foundation/mem_override_win.c b/src/foundation/mem_override_win.c
index addc9e8c8..43b845dfb 100644
--- a/src/foundation/mem_override_win.c
+++ b/src/foundation/mem_override_win.c
@@ -71,13 +71,13 @@ static inline bool mem_override_is_ours(const void *block) {
void *__wrap_malloc(size_t size) {
void *block = mi_malloc(size);
- cbm_mem_profile_alloc(block, size);
+ cbm_mem_profile_alloc_at(block, size, __builtin_return_address(0));
return block;
}
void *__wrap_calloc(size_t count, size_t size) {
void *block = mi_calloc(count, size);
- cbm_mem_profile_alloc(block, count * size);
+ cbm_mem_profile_alloc_at(block, count * size, __builtin_return_address(0));
return block;
}
@@ -128,7 +128,7 @@ void *__wrap_realloc(void *block, size_t size) {
if (mem_override_is_ours(block)) {
cbm_mem_profile_free(block);
void *grown = mi_realloc(block, size);
- cbm_mem_profile_alloc(grown, size);
+ cbm_mem_profile_alloc_at(grown, size, __builtin_return_address(0));
return grown;
}
/* A CRT block cannot be handed to mi_realloc, and the CRT's realloc must
diff --git a/src/foundation/mem_profile.c b/src/foundation/mem_profile.c
index 9c8e4e003..f0c43b745 100644
--- a/src/foundation/mem_profile.c
+++ b/src/foundation/mem_profile.c
@@ -112,18 +112,22 @@ static uint64_t profile_hash(void *const *frames, int count) {
return hash == 0 ? 1 : hash; /* 0 marks an empty slot */
}
-static int profile_capture(void **frames) {
+static int profile_capture(void **frames, void *caller) {
#ifdef _WIN32
int captured = (int)CaptureStackBackTrace(PROFILE_SKIP_FRAMES, PROFILE_FRAMES, frames, NULL);
if (captured > 0) {
return captured;
}
- /* Fall back to the immediate return address rather than dropping the
- * sample: one frame still separates call sites, and a coarse attribution
- * beats an empty profile that looks like "nothing allocates". */
+ /* Fall back to the caller the hook supplied. Using a return address from
+ * THIS frame would name the profiler, not the allocating code. */
+ if (caller) {
+ frames[0] = caller;
+ return 1;
+ }
frames[0] = __builtin_return_address(0);
return frames[0] != NULL ? 1 : 0;
#else
+ (void)caller;
void *raw[PROFILE_FRAMES + PROFILE_SKIP_FRAMES];
int got = backtrace(raw, PROFILE_FRAMES + PROFILE_SKIP_FRAMES);
int count = got - PROFILE_SKIP_FRAMES;
@@ -169,6 +173,10 @@ static size_t profile_pointer_slot(const void *block) {
}
void cbm_mem_profile_alloc(void *block, size_t size) {
+ cbm_mem_profile_alloc_at(block, size, NULL);
+}
+
+void cbm_mem_profile_alloc_at(void *block, size_t size, void *caller) {
if (!block || g_profile_reentrant) {
return;
}
@@ -178,7 +186,7 @@ void cbm_mem_profile_alloc(void *block, size_t size) {
return;
}
void *frames[PROFILE_FRAMES];
- int count = profile_capture(frames);
+ int count = profile_capture(frames, caller);
if (count <= 0) {
cbm_mutex_lock(&g_profile_mutex);
g_totals.capture_failed++;
diff --git a/src/foundation/mem_profile.h b/src/foundation/mem_profile.h
index 466b9d895..8ed6d1512 100644
--- a/src/foundation/mem_profile.h
+++ b/src/foundation/mem_profile.h
@@ -32,6 +32,14 @@ size_t cbm_mem_profile_threshold(void);
/* Record an allocation / release. Safe to call unconditionally: both return
* immediately when profiling is off or the size is below the threshold. */
void cbm_mem_profile_alloc(void *block, size_t size);
+
+/* Same, but with the caller's return address supplied by the hook itself.
+ * Required on Windows ARM64, where CaptureStackBackTrace returns nothing: a
+ * return address captured inside the capture helper names the helper, so every
+ * allocation collapses onto a handful of hook addresses and the attribution
+ * column becomes useless. Each hook must evaluate __builtin_return_address(0)
+ * in its OWN frame and pass it here. */
+void cbm_mem_profile_alloc_at(void *block, size_t size, void *caller);
void cbm_mem_profile_free(void *block);
typedef struct {