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.

wip: audit allocator ownership per size class

Martin Vogel30327de46f20
diff --git a/src/foundation/mem.c b/src/foundation/mem.c
index 4de336913..1c5209ab6 100644
--- a/src/foundation/mem.c
+++ b/src/foundation/mem.c
@@ -219,17 +219,35 @@ void cbm_mem_init_with_cap(double ram_fraction, size_t hard_cap_bytes) {
* for months (#581). Probe it once, out loud: a real malloc asked whether
* mimalloc owns it. Anything but true means the tuning here is decoration
* and freed pages will not come back. */
- void *ownership_probe = malloc(CBM_SZ_64);
- if (ownership_probe) {
- bool owned = mi_is_in_heap_region(ownership_probe);
- free(ownership_probe);
- if (!owned) {
- cbm_log_warn("mem.allocator.not_owned", "detail",
- "malloc is not served by mimalloc: purge/reclaim options "
- "have no effect and freed pages will stay committed");
- } else {
- cbm_log_info("mem.allocator.owned", "detail", "malloc is served by mimalloc");
+ cbm_mem_ownership_audit_t audit;
+ cbm_mem_audit_ownership(&audit);
+ if (!audit.all_owned) {
+ /* Name the classes that escaped, because "not owned" is actionable
+ * only if you know WHICH sizes. A class listed here either bypasses
+ * the override or is misclassified by the routing predicate, and both
+ * mean freed pages will not come back. */
+ char classes[CBM_SZ_256];
+ int length = 0;
+ for (int i = 0; i < CBM_MEM_OWNERSHIP_CLASSES && length >= 0 &&
+ (size_t)length < sizeof(classes);
+ i++) {
+ if (audit.allocated[i] && !audit.owned[i]) {
+ int written = snprintf(classes + length, sizeof(classes) - (size_t)length,
+ "%s%zu", length ? "," : "", audit.probe_bytes[i]);
+ if (written < 0) {
+ break;
+ }
+ length += written;
+ }
}
+ char owned_str[CBM_SZ_32];
+ snprintf(owned_str, sizeof(owned_str), "%d/%d", audit.owned_count, audit.probed_count);
+ cbm_log_warn("mem.allocator.not_owned", "owned_classes", owned_str, "unowned_bytes",
+ length > 0 ? classes : "?", "detail",
+ "allocations in these size classes are not allocator-owned: purge and "
+ "reclaim options do not apply to them and freed pages stay committed");
+ } else {
+ cbm_log_info("mem.allocator.owned", "classes", "all");
}
/* CBM_MEM_BUDGET_MB env override (memory analogue of CBM_WORKERS).
@@ -450,6 +468,54 @@ bool cbm_mem_map_collect(cbm_mem_map_t *out) {
return true;
}
+/* ── Allocator ownership audit (see mem.h) ─────────────────────────── */
+
+static atomic_size_t g_foreign_pointers = ATOMIC_VAR_INIT(0);
+static atomic_size_t g_owned_pointers = ATOMIC_VAR_INIT(0);
+
+void cbm_mem_record_pointer_ownership(bool owned) {
+ atomic_fetch_add_explicit(owned ? &g_owned_pointers : &g_foreign_pointers, 1,
+ memory_order_relaxed);
+}
+
+size_t cbm_mem_foreign_pointer_count(void) {
+ return atomic_load_explicit(&g_foreign_pointers, memory_order_relaxed);
+}
+
+size_t cbm_mem_owned_pointer_count(void) {
+ return atomic_load_explicit(&g_owned_pointers, memory_order_relaxed);
+}
+
+void cbm_mem_audit_ownership(cbm_mem_ownership_audit_t *out) {
+ if (!out) {
+ return;
+ }
+ memset(out, 0, sizeof(*out));
+ /* Span the classes an allocator treats differently: small (bins), page
+ * scale, medium, large, and past the point where allocators typically stop
+ * using their own pages and go to the OS directly — the class most likely
+ * to escape both an override and an ownership predicate. */
+ static const size_t PROBES[CBM_MEM_OWNERSHIP_CLASSES] = {
+ 64, 4096, 65536, 262144, 1048576, 8388608,
+ };
+ for (int i = 0; i < CBM_MEM_OWNERSHIP_CLASSES; i++) {
+ out->probe_bytes[i] = PROBES[i];
+ void *block = malloc(PROBES[i]);
+ if (!block) {
+ continue;
+ }
+ ((char *)block)[0] = 1; /* touch it so it is really backed */
+ out->allocated[i] = true;
+ out->probed_count++;
+ out->owned[i] = mi_is_in_heap_region(block);
+ if (out->owned[i]) {
+ out->owned_count++;
+ }
+ free(block);
+ }
+ out->all_owned = out->probed_count > 0 && out->owned_count == out->probed_count;
+}
+
/* ── Phase map (see mem.h for how to read it, and its limits) ──────── */
enum { MEM_PHASE_SLOTS = 24 };
diff --git a/src/foundation/mem.h b/src/foundation/mem.h
index ad5760d1a..5a8ac8946 100644
--- a/src/foundation/mem.h
+++ b/src/foundation/mem.h
@@ -133,6 +133,46 @@ size_t cbm_mem_map_bucket_limit(int bucket);
* that the walk contributed nothing rather than reading 0 as "no leak". */
bool cbm_mem_map_collect(cbm_mem_map_t *out);
+/* ── Allocator ownership audit ───────────────────────────────────────
+ *
+ * "Does the allocator own our memory" is not one question, it is one per size
+ * class. An override can capture small allocations while large or huge ones go
+ * elsewhere, and the predicate used to route frees can itself be wrong for a
+ * class — which silently hands real allocator blocks to the wrong deallocator.
+ * That is a leak at best and undefined behaviour at worst, so it has to be
+ * measured across the size range rather than probed once.
+ *
+ * Verified at startup and assertable in tests, so an unowned class is a loud
+ * failure instead of a slow ratchet nobody attributes for months (#581).
+ */
+enum { CBM_MEM_OWNERSHIP_CLASSES = 6 };
+
+typedef struct {
+ size_t probe_bytes[CBM_MEM_OWNERSHIP_CLASSES]; /* size probed per class */
+ bool owned[CBM_MEM_OWNERSHIP_CLASSES]; /* predicate said "ours" */
+ bool allocated[CBM_MEM_OWNERSHIP_CLASSES]; /* the probe itself worked */
+ int owned_count;
+ int probed_count;
+ bool all_owned; /* every successfully probed class came back owned */
+} cbm_mem_ownership_audit_t;
+
+/* Allocate one block per size class, ask the routing predicate whether the
+ * allocator owns it, then free it. Pure measurement: mutates no globals. */
+void cbm_mem_audit_ownership(cbm_mem_ownership_audit_t *out);
+
+/* Foreign-pointer accounting from the allocation interposer: how many pointers
+ * reaching our deallocators were classified as NOT allocator-owned. On a
+ * correctly wired build this stays at or near zero. A climbing count means
+ * either an un-intercepted allocation path or — as seen while landing the
+ * Windows interposer — a predicate misclassifying real allocator blocks.
+ * Either way it is a defect signal, which is why soak/smoke can gate on it.
+ * Both read zero on platforms with no interposer. */
+size_t cbm_mem_foreign_pointer_count(void);
+size_t cbm_mem_owned_pointer_count(void);
+
+/* Interposer hook: classify one pointer arriving at a deallocator. */
+void cbm_mem_record_pointer_ownership(bool owned);
+
/* ── Phase map: WHICH code path does the memory stay in? ─────────────
*
* The allocator walk answers "how much and what size"; it cannot answer "who".