Skip to content

Commit 4f7db18

Browse files
authored
Merge pull request DeusData#1477 from DeusData/fix/linux-allocator-override
fix(mem): route ordinary malloc through mimalloc on Linux (DeusData#1360)
2 parents ab43e75 + 452372e commit 4f7db18

4 files changed

Lines changed: 155 additions & 13 deletions

File tree

‎Makefile.cbm‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ endif
7575
# CBM_BIND_TS_ALLOCATOR=1: bind the tree-sitter runtime to mimalloc (#424). Only
7676
# the prod build uses mimalloc (MI_OVERRIDE=1); the test build is CRT+ASan, where
7777
# binding would create an alloc/free mismatch, so the guard is prod-only.
78-
CFLAGS_PROD = $(CFLAGS_COMMON) -O2 -DCBM_BIND_TS_ALLOCATOR=1 $(TEST_SEAM_DEFINE) $(CFLAGS_EXTRA)
78+
CFLAGS_PROD = $(CFLAGS_COMMON) -O2 -DCBM_BIND_TS_ALLOCATOR=1 $(TEST_SEAM_DEFINE) \
79+
$(GLOBAL_OVERRIDE_DEFINE) $(CFLAGS_EXTRA)
7980
CXXFLAGS_PROD = $(CXXFLAGS_COMMON) -O2
8081

8182
# Test flags: debug + sanitizers (override SANITIZE= to disable on Windows)
@@ -386,9 +387,34 @@ UI_SRCS = \
386387
# derives it from the MI_OVERRIDE option); MI_OVERRIDE alone is not read by the
387388
# source — it is kept here only as this project's prod/test marker.
388389
MIMALLOC_SRC = vendored/mimalloc/src/static.c
390+
# Linux belongs on this list too (#1360). The "Unix relies on static-link-order
391+
# override" strategy described above never actually worked: mimalloc defines
392+
# malloc/free ONLY when MI_MALLOC_OVERRIDE is set (it gates alloc-override.c),
393+
# so with the define off there were no strong symbols for the link order to
394+
# prefer. Measured on the shipped v0.9.1-rc.1 artifacts — linux-arm64 glibc AND
395+
# musl-static both report 0/6 allocator-owned size classes, i.e. ordinary malloc
396+
# was going to libc and every purge/reclaim option set in cbm_mem_init applied
397+
# only to the bound sqlite/tree-sitter populations.
398+
#
399+
# The macOS objection does not transfer. That abort is specific to the two-level
400+
# namespace, where this binary's free becomes mi_free while system libraries keep
401+
# allocating from the system allocator, so a pointer crossing the boundary hits
402+
# "mi_free: invalid pointer". ELF is a flat namespace: interposition is
403+
# process-wide and all-or-nothing, which is the configuration mimalloc is
404+
# normally deployed in. macOS therefore stays off, deliberately and permanently.
405+
#
406+
# TWO defines, switched together in ONE place so they cannot drift:
407+
# MI_MALLOC_OVERRIDE reaches the mimalloc translation unit and turns the
408+
# override on; CBM_MEM_GLOBAL_OVERRIDE reaches OUR sources and tells the startup
409+
# audit what to expect. CBM_MEM_GLOBAL_OVERRIDE goes only into CFLAGS_PROD,
410+
# because MIMALLOC_CFLAGS_TEST compiles mimalloc with -DMI_OVERRIDE=0 and no
411+
# override define at all — a test binary has no global override BY CONSTRUCTION
412+
# and must never be told to expect one, or it warns on every run.
389413
MIMALLOC_OVERRIDE_DEFINE :=
390-
ifeq ($(IS_MINGW),yes)
414+
GLOBAL_OVERRIDE_DEFINE :=
415+
ifneq ($(filter yes,$(IS_MINGW) $(IS_LINUX)),)
391416
MIMALLOC_OVERRIDE_DEFINE := -DMI_MALLOC_OVERRIDE=1
417+
GLOBAL_OVERRIDE_DEFINE := -DCBM_MEM_GLOBAL_OVERRIDE=1
392418
endif
393419
MIMALLOC_CFLAGS = -std=c11 -O2 -w \
394420
-Ivendored/mimalloc/include \

‎scripts/smoke-test.sh‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,58 @@ if ! echo "$OUTPUT" | grep -qE 'v?[0-9]+\.[0-9]+|dev'; then
187187
fi
188188
echo "OK"
189189

190+
echo ""
191+
echo "=== Phase 1b: allocator override matches this platform's contract ==="
192+
# Asserts the SHIPPED binary's actual allocator wiring, per platform:
193+
#
194+
# Windows, Linux -> ordinary malloc MUST reach mimalloc (all size classes
195+
# owned). If it does not, every purge/reclaim option in
196+
# cbm_mem_init is decoration and freed pages stay committed
197+
# — that is #581, which hid in production for months
198+
# precisely because nothing asserted it on a real artifact.
199+
# macOS -> it MUST NOT. Enabling the override there aborts on the
200+
# first pointer crossing the two-level-namespace boundary
201+
# ("mi_free: invalid pointer"), so "owned" here would mean
202+
# we shipped a binary that crashes on index.
203+
#
204+
# Both directions fail. A silent flip either way is a release blocker, which is
205+
# why this lives in smoke (real artifact, all platforms) and not only in a unit
206+
# test built from source.
207+
ALLOC_LOG=$("$BINARY" cli list_projects 2>&1 >/dev/null || true)
208+
case "$(uname -s)" in
209+
Darwin)
210+
if echo "$ALLOC_LOG" | grep -q 'mem.allocator.not_owned'; then
211+
echo "FAIL: macOS emitted the not_owned WARNING; expected the by-design"
212+
echo " bound-populations INFO line (see #1360)"
213+
exit 1
214+
fi
215+
if echo "$ALLOC_LOG" | grep -q 'mem.allocator.owned'; then
216+
echo "FAIL: macOS reports ordinary malloc as allocator-owned. The override"
217+
echo " must stay OFF here: under the two-level namespace it aborts"
218+
echo " with 'mi_free: invalid pointer' on the first crossing pointer."
219+
exit 1
220+
fi
221+
echo "OK: macOS serves ordinary malloc from the system allocator, no warning"
222+
;;
223+
MINGW*|MSYS*|CYGWIN*|Linux)
224+
if echo "$ALLOC_LOG" | grep -q 'mem.allocator.not_owned'; then
225+
echo "FAIL: ordinary malloc does NOT reach mimalloc on $(uname -s)."
226+
echo " Allocator tuning is inert and freed pages will stay committed (#581/#1360)."
227+
echo "$ALLOC_LOG" | grep 'mem.allocator' | head -2
228+
exit 1
229+
fi
230+
if echo "$ALLOC_LOG" | grep -q 'mem.allocator.bound_populations_only'; then
231+
echo "FAIL: $(uname -s) reports bound-populations-only; the global override"
232+
echo " is expected to be compiled in on this platform (#1360)."
233+
exit 1
234+
fi
235+
echo "OK: ordinary malloc reaches the allocator on $(uname -s)"
236+
;;
237+
*)
238+
echo "SKIP: no allocator contract defined for $(uname -s)"
239+
;;
240+
esac
241+
190242
if [ "$SMOKE_MODE" != "--agent-config-only" ]; then
191243
echo ""
192244
echo "=== Phase 2: index test project ==="

‎src/foundation/mem.c‎

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,32 @@
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 "

‎src/foundation/mem_override_posix.c‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
/*
22
* mem_override_posix.c — allocation-site visibility on POSIX.
33
*
4-
* On POSIX the build links mimalloc's object first, so its strong malloc/free
5-
* win and every allocation already lands in mimalloc — nothing needs
6-
* redirecting for correctness. What is missing is a place to OBSERVE those
7-
* allocations: the Windows side gets it for free from the --wrap interposer it
8-
* needs anyway, and without an equivalent here the profiler could only ever
9-
* describe Windows, which makes the platform comparison — the one measurement
10-
* that actually isolates #581 — impossible.
4+
* What is missing on POSIX is a place to OBSERVE allocations: the Windows side
5+
* gets it for free from the --wrap interposer it needs anyway, and without an
6+
* equivalent here the profiler could only ever describe Windows, which makes the
7+
* platform comparison — the one measurement that actually isolates #581 —
8+
* impossible.
9+
*
10+
* CORRECTION (#1360). This header previously claimed that "on POSIX the build
11+
* links mimalloc's object first, so its strong malloc/free win and every
12+
* allocation already lands in mimalloc". That was never true, and believing it
13+
* inverts the meaning of the startup ownership audit. mimalloc emits those
14+
* strong malloc/free definitions only when MI_MALLOC_OVERRIDE is set — the
15+
* define its source actually gates alloc-override.c on — while MI_OVERRIDE,
16+
* which the build sets everywhere, is this project's own prod/test marker that
17+
* mimalloc never reads. With the override body compiled out there were no
18+
* strong symbols for link order to prefer, so ordinary malloc went to libc. The
19+
* shipped v0.9.1-rc.1 binaries confirm it: linux-arm64 (glibc AND musl-static)
20+
* and darwin-arm64 all reported 0/6 allocator-owned size classes.
21+
*
22+
* Linux PROD builds now set MI_MALLOC_OVERRIDE (see Makefile.cbm), so the strong
23+
* symbols exist there and ordinary malloc genuinely reaches mimalloc. macOS
24+
* stays off permanently — its two-level namespace would split this binary's
25+
* free from the system libraries' malloc — and test builds stay off everywhere
26+
* by construction, which is why this observation shim remains the only way to
27+
* see POSIX allocation sites.
1128
*
1229
* So this file exists purely for symmetry of measurement. Each wrapper forwards
1330
* to the same mi_* entry point the symbol would have reached on its own and

0 commit comments

Comments
 (0)