3636#include <unistd.h>
3737#endif
3838
39+ /* Does THIS build ask mimalloc to replace ordinary malloc process-wide?
40+ *
41+ * Set by Makefile.cbm alongside MI_MALLOC_OVERRIDE itself — the two are
42+ * switched together in one place, because MI_MALLOC_OVERRIDE reaches only the
43+ * mimalloc translation unit and this one needs the same answer. Do NOT infer it
44+ * from the platform here: the override is compiled into PROD builds only
45+ * (MIMALLOC_CFLAGS_TEST uses -DMI_OVERRIDE=0 and no override define), so a
46+ * Linux test binary genuinely has no global override, and a platform-based
47+ * guess would make every test run warn about a build that is correct.
48+ *
49+ * macOS never gets it, and that is not a defect to warn about: enabling the
50+ * override there compiles alloc-override.c's forwarding definitions, and under
51+ * the two-level namespace this binary's free becomes mi_free while system
52+ * libraries keep allocating from the system allocator, so the first pointer
53+ * crossing that boundary aborts with "mi_free: invalid pointer". ELF's flat
54+ * namespace has no such split, which is why Linux can have it and macOS cannot.
55+ *
56+ * The shipped artifact's actual wiring is pinned by scripts/smoke-test.sh
57+ * Phase 1b, which fails in BOTH directions on the real binary — the only place
58+ * this can be checked honestly, since a from-source test build never has it. */
59+ #if defined(CBM_MEM_GLOBAL_OVERRIDE )
60+ #define CBM_MEM_EXPECT_GLOBAL_OVERRIDE 1
61+ #else
62+ #define CBM_MEM_EXPECT_GLOBAL_OVERRIDE 0
63+ #endif
64+
3965#ifdef _WIN32
4066/* mimalloc internals. Not in the public header, but the allocator is compiled
4167 * into this binary as a unity object, so the symbols are present.
@@ -284,10 +310,33 @@ void cbm_mem_init_with_cap(double ram_fraction, size_t hard_cap_bytes) {
284310 * and nothing checked, so a long-lived daemon ratcheted committed memory
285311 * for months (#581). Probe it once, out loud: a real malloc asked whether
286312 * mimalloc owns it. Anything but true means the tuning here is decoration
287- * and freed pages will not come back. */
313+ * and freed pages will not come back.
314+ *
315+ * ...but "anything but true" only means that where the build actually ASKED
316+ * for a global override: prod builds on Windows and Linux. Everywhere else —
317+ * macOS, and every test binary on any platform — the override body is
318+ * compiled out, so ordinary malloc is SUPPOSED to reach libc and the probe
319+ * is answering a question this build never posed. Same measurement,
320+ * different meaning per build config, hence the split below. */
288321 cbm_mem_ownership_audit_t audit ;
289322 cbm_mem_audit_ownership (& audit );
290- if (!audit .all_owned ) {
323+ char owned_str [CBM_SZ_32 ];
324+ snprintf (owned_str , sizeof (owned_str ), "%d/%d" , audit .owned_count , audit .probed_count );
325+ if (!audit .all_owned && !CBM_MEM_EXPECT_GLOBAL_OVERRIDE ) {
326+ /* Expected: this build never asked mimalloc to replace ordinary malloc,
327+ * so ordinary malloc reaching libc is the design, not a fault. Say what
328+ * IS allocator-served instead, because the interesting question here is
329+ * "are the bound populations bound", not "did the override fire".
330+ *
331+ * Reported at INFO deliberately. A warning that fires on every run of a
332+ * correctly configured build is not a tripwire, it is background noise —
333+ * it trained readers to ignore the one line that catches #581, and cost
334+ * a user the time to file and self-close #1360. */
335+ cbm_log_info ("mem.allocator.bound_populations_only" , "owned_classes" , owned_str ,
336+ "populations" , "sqlite,tree_sitter" , "detail" ,
337+ "ordinary malloc is served by the system allocator in this build by "
338+ "design; allocator tuning applies to the bound populations" );
339+ } else if (!audit .all_owned ) {
291340 /* Name the classes that escaped, because "not owned" is actionable
292341 * only if you know WHICH sizes. A class listed here either bypasses
293342 * the override or is misclassified by the routing predicate, and both
@@ -306,8 +355,6 @@ void cbm_mem_init_with_cap(double ram_fraction, size_t hard_cap_bytes) {
306355 length += written ;
307356 }
308357 }
309- char owned_str [CBM_SZ_32 ];
310- snprintf (owned_str , sizeof (owned_str ), "%d/%d" , audit .owned_count , audit .probed_count );
311358 cbm_log_warn ("mem.allocator.not_owned" , "owned_classes" , owned_str , "unowned_bytes" ,
312359 length > 0 ? classes : "?" , "detail" ,
313360 "allocations in these size classes are not allocator-owned: purge and "
0 commit comments