Skip to content

Commit 332bea6

Browse files
author
Martin Vogel
committed
Bind sqlite3 and libgit2 to mimalloc explicitly in prod build (#424 follow-up)
Defense-in-depth follow-up to the tree-sitter bind: route sqlite3 and libgit2 allocations through mimalloc explicitly so a correct binary never depends on the fragile MI_OVERRIDE symbol override (where the Windows static-MinGW link can resolve a library's malloc to mimalloc but its free to the CRT, corrupting the heap). Prod-only, guarded by the existing CBM_BIND_TS_ALLOCATOR; the CRT+ASan test build compiles these as no-ops and stays unchanged. New cbm_alloc_init() consolidates all three binds behind a static idempotency guard. main() calls it as its very first statement — before cbm_mcp_server_new opens the in-memory store — because SQLITE_CONFIG_MALLOC must run before the first sqlite3_open*/sqlite3_initialize, else sqlite3_config returns SQLITE_MISUSE silently and the bind is ignored (the return code is asserted). cbm_init() also calls cbm_alloc_init() so pipeline-pass entry points get the binds too; the ts_set_allocator bind moved out of cbm_init into cbm_alloc_init. sqlite3: a static sqlite3_mem_methods backed by mi_malloc/mi_free/mi_realloc, xSize via mi_usable_size, xRoundup to an 8-byte boundary, xInit returning SQLITE_OK, xShutdown a no-op (int-sized sqlite signatures wrapped with size_t casts). libgit2: a static git_allocator (gmalloc/grealloc/gfree → mi_*) installed via git_libgit2_opts(GIT_OPT_SET_ALLOCATOR). The allocator is set BEFORE any git_libgit2_init: git_allocator_global_init keeps a custom allocator if one is already set (git__allocator.gmalloc != git_failalloc_malloc), the pre-init global is the fail-allocator, and there is no allocator reset on git_libgit2_shutdown — so binding once at startup survives pass_githistory's per-call init/shutdown pairs. libgit2 code is additionally guarded by HAVE_LIBGIT2. Test build 5564/0 ASan-clean; prod build clean with CBM_BIND_TS_ALLOCATOR=1; the HAVE_LIBGIT2 branch type-checked against the libgit2 sys/alloc.h struct shape; lint-ci clean.
1 parent bb4af38 commit 332bea6

3 files changed

Lines changed: 148 additions & 18 deletions

File tree

‎internal/cbm/cbm.c‎

Lines changed: 130 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@
1616
#include "foundation/compat.h"
1717
#include "tree_sitter/api.h" // TSParser, TSNode, TSTree, TSInput, TSLanguage, TSPoint, TSParseOptions, TSParseState
1818
#include "foundation/constants.h"
19-
#include "mimalloc.h" // mi_malloc/mi_calloc/mi_realloc/mi_free — bind ts runtime allocator (#424)
20-
#include <stdint.h> // uint32_t, uint64_t, int64_t
19+
#include "mimalloc.h" // mi_malloc/mi_calloc/mi_realloc/mi_free/mi_usable_size — bind 3rd-party allocators (#424)
20+
#if defined(CBM_BIND_TS_ALLOCATOR) && CBM_BIND_TS_ALLOCATOR
21+
#include "sqlite3.h" // sqlite3_mem_methods, sqlite3_config, SQLITE_CONFIG_MALLOC — bind sqlite to mimalloc
22+
#if defined(HAVE_LIBGIT2)
23+
#include <git2.h> // git_allocator, git_libgit2_opts, GIT_OPT_SET_ALLOCATOR — bind libgit2 to mimalloc
24+
#endif
25+
#endif
26+
#include <stdint.h> // uint32_t, uint64_t, int64_t
2127
#include <stdlib.h>
2228
#include <string.h>
2329
#include <ctype.h>
@@ -232,6 +238,122 @@ static TSParser *get_thread_parser(const TSLanguage *ts_lang, CBMLanguage lang)
232238
return tl_parser;
233239
}
234240

241+
// --- Allocator binding (defense-in-depth, #424) ---
242+
243+
/* Bind tree-sitter, sqlite3, and libgit2 to mimalloc explicitly so a correct
244+
* binary does NOT depend on the fragile MI_OVERRIDE symbol override. Under
245+
* MI_OVERRIDE=1 — particularly the Windows static-MinGW link with
246+
* --allow-multiple-definition — `malloc`/`free` can resolve to DIFFERENT
247+
* allocators (mimalloc vs the CRT) inside third-party libs, so a block
248+
* allocated by mimalloc gets freed by the CRT (or vice-versa), corrupting the
249+
* heap freelist (#424). Binding each library through one explicit allocator
250+
* eliminates that mismatch class generically, on every platform.
251+
*
252+
* Guarded to the production build (CBM_BIND_TS_ALLOCATOR=1, which CFLAGS_PROD
253+
* defines alongside MI_OVERRIDE=1). The test build is CRT + ASan, where binding
254+
* to mimalloc would mismatch ASan/CRT frees — there these binds compile to
255+
* no-ops and the build stays unchanged. */
256+
257+
#if defined(CBM_BIND_TS_ALLOCATOR) && CBM_BIND_TS_ALLOCATOR
258+
#include <assert.h>
259+
260+
/* sqlite3 mem methods backed by mimalloc. sqlite's xMalloc/xRealloc/xSize use
261+
* `int` sizes; wrap with size_t casts. xRoundup rounds to an 8-byte boundary
262+
* (sqlite requires 8-byte-aligned roundup, and mimalloc honors that alignment).
263+
* Field order matches struct sqlite3_mem_methods exactly:
264+
* xMalloc, xFree, xRealloc, xSize, xRoundup, xInit, xShutdown, pAppData. */
265+
static void *cbm_sqlite_malloc(int n) {
266+
return mi_malloc((size_t)n);
267+
}
268+
static void cbm_sqlite_free(void *p) {
269+
mi_free(p);
270+
}
271+
static void *cbm_sqlite_realloc(void *p, int n) {
272+
return mi_realloc(p, (size_t)n);
273+
}
274+
static int cbm_sqlite_size(void *p) {
275+
return (int)mi_usable_size(p);
276+
}
277+
static int cbm_sqlite_roundup(int n) {
278+
return (n + 7) & ~7; /* round up to 8-byte boundary */
279+
}
280+
static int cbm_sqlite_meminit(void *appdata) {
281+
(void)appdata;
282+
return SQLITE_OK;
283+
}
284+
static void cbm_sqlite_memshutdown(void *appdata) {
285+
(void)appdata;
286+
}
287+
288+
#if defined(HAVE_LIBGIT2)
289+
/* libgit2 git_allocator backed by mimalloc. The struct (current libgit2) has
290+
* exactly three members: gmalloc(size_t,file,line), grealloc(ptr,size,file,line),
291+
* gfree(ptr). The file/line args are ignored. */
292+
static void *cbm_git_malloc(size_t n, const char *file, int line) {
293+
(void)file;
294+
(void)line;
295+
return mi_malloc(n);
296+
}
297+
static void *cbm_git_realloc(void *ptr, size_t size, const char *file, int line) {
298+
(void)file;
299+
(void)line;
300+
return mi_realloc(ptr, size);
301+
}
302+
static void cbm_git_free(void *ptr) {
303+
mi_free(ptr);
304+
}
305+
#endif /* HAVE_LIBGIT2 */
306+
#endif /* CBM_BIND_TS_ALLOCATOR */
307+
308+
void cbm_alloc_init(void) {
309+
#if defined(CBM_BIND_TS_ALLOCATOR) && CBM_BIND_TS_ALLOCATOR
310+
static int alloc_bound = 0; /* single-threaded startup; plain int is fine */
311+
if (alloc_bound) {
312+
return;
313+
}
314+
alloc_bound = 1;
315+
316+
/* tree-sitter runtime (was previously bound in cbm_init; consolidated here). */
317+
ts_set_allocator(mi_malloc, mi_calloc, mi_realloc, mi_free);
318+
319+
/* sqlite3. SQLITE_CONFIG_MALLOC MUST run before sqlite3_initialize / the
320+
* first sqlite3_open* — otherwise sqlite3_config returns SQLITE_MISUSE
321+
* silently and the binding is ignored. cbm_alloc_init() runs as the very
322+
* first statement of main(), before cbm_mcp_server_new → cbm_store_open*. */
323+
static sqlite3_mem_methods cbm_sqlite_mem = {
324+
cbm_sqlite_malloc, /* xMalloc */
325+
cbm_sqlite_free, /* xFree */
326+
cbm_sqlite_realloc, /* xRealloc */
327+
cbm_sqlite_size, /* xSize */
328+
cbm_sqlite_roundup, /* xRoundup */
329+
cbm_sqlite_meminit, /* xInit */
330+
cbm_sqlite_memshutdown, /* xShutdown */
331+
NULL, /* pAppData */
332+
};
333+
int sqlite_rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &cbm_sqlite_mem);
334+
assert(sqlite_rc == SQLITE_OK && "SQLITE_CONFIG_MALLOC must run before sqlite3_initialize");
335+
(void)sqlite_rc;
336+
337+
#if defined(HAVE_LIBGIT2)
338+
/* libgit2. GIT_OPT_SET_ALLOCATOR MUST be set BEFORE git_libgit2_init():
339+
* libgit2's git_allocator_global_init (run during init) installs the default
340+
* stdalloc only if no custom allocator is set yet
341+
* (`if (git__allocator.gmalloc != git_failalloc_malloc) return 0;`), and the
342+
* pre-init global is the fail-allocator. There is no allocator-reset on
343+
* git_libgit2_shutdown, so binding once here — before pass_githistory's
344+
* per-call git_libgit2_init/shutdown pairs ever run — persists for the whole
345+
* process. git_libgit2_opts(GIT_OPT_SET_ALLOCATOR,...) itself does not
346+
* allocate, so calling it before init is safe. */
347+
static git_allocator cbm_git_alloc = {
348+
cbm_git_malloc, /* gmalloc */
349+
cbm_git_realloc, /* grealloc */
350+
cbm_git_free, /* gfree */
351+
};
352+
git_libgit2_opts(GIT_OPT_SET_ALLOCATOR, &cbm_git_alloc);
353+
#endif /* HAVE_LIBGIT2 */
354+
#endif /* CBM_BIND_TS_ALLOCATOR */
355+
}
356+
235357
// --- Init/Shutdown ---
236358

237359
static int cbm_initialized = 0;
@@ -242,22 +364,12 @@ int cbm_init(void) {
242364
}
243365
enum { CBM_INIT_DONE = 1 };
244366
cbm_initialized = CBM_INIT_DONE;
245-
/* Bind the vendored tree-sitter runtime to the SAME allocator the rest of
246-
* cbm uses (mimalloc). The runtime otherwise allocates through its
247-
* overridable ts_current_malloc/free defaults (plain malloc / free). Under
248-
* the production build's MI_OVERRIDE=1 — particularly the Windows
249-
* static-MinGW link with --allow-multiple-definition — `malloc` and `free`
250-
* can resolve to DIFFERENT allocators (mimalloc vs the CRT), so a subtree
251-
* allocated by mimalloc gets freed by the CRT (or vice-versa), corrupting
252-
* the heap freelist and crashing mid-parse on a freed (0xffff…) pointer for
253-
* large templated C++ headers (#424 — scales with parse churn, not syntax).
254-
* Binding explicitly forces ts allocate+free through one allocator on every
255-
* platform, eliminating the mismatch class generically. Guarded to the
256-
* production build (MI_OVERRIDE=1): the test build is CRT + ASan, where
257-
* binding ts to mimalloc would mismatch ASan/CRT frees. */
258-
#if defined(CBM_BIND_TS_ALLOCATOR) && CBM_BIND_TS_ALLOCATOR
259-
ts_set_allocator(mi_malloc, mi_calloc, mi_realloc, mi_free);
260-
#endif
367+
/* Defense-in-depth allocator binds (idempotent). main() calls cbm_alloc_init
368+
* first; this covers non-main entry points (pipeline passes call cbm_init).
369+
* For sqlite the SQLITE_CONFIG_MALLOC bind only takes effect if it runs
370+
* before sqlite initializes — main() guarantees that ordering; here it is a
371+
* best-effort idempotent re-assert for paths that never hit main(). */
372+
cbm_alloc_init();
261373
return 0;
262374
}
263375

‎internal/cbm/cbm.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,16 @@ typedef struct {
502502

503503
// --- Public API ---
504504

505+
// Bind third-party allocators (tree-sitter, sqlite3, libgit2) to mimalloc as
506+
// defense-in-depth, so they never depend on the fragile MI_OVERRIDE symbol
507+
// override (#424). MUST be called as the very first statement of main(), before
508+
// any sqlite3_open*/sqlite3_initialize (SQLITE_CONFIG_MALLOC returns
509+
// SQLITE_MISUSE once sqlite has initialized) and before any git_libgit2_init.
510+
// Idempotent (static guard); intended for single-threaded startup. cbm_init()
511+
// also calls it so non-main entry points (pipeline passes) still get the binds.
512+
// In the test build (no CBM_BIND_TS_ALLOCATOR) this is a no-op.
513+
void cbm_alloc_init(void);
514+
505515
// Initialize the library. Call once at startup. Returns 0 on success.
506516
int cbm_init(void);
507517

‎src/main.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Watcher runs in a background thread, polling for git changes.
1414
* HTTP UI server (optional) runs in a background thread on localhost.
1515
*/
16+
#include "cbm.h" // cbm_alloc_init — bind 3rd-party allocators to mimalloc before any sqlite/git init
1617
#include "mcp/mcp.h"
1718
#include "watcher/watcher.h"
1819
#include "pipeline/pipeline.h"
@@ -384,6 +385,13 @@ static void setup_signal_handlers(void) {
384385
}
385386

386387
int main(int argc, char **argv) {
388+
/* Defense-in-depth: bind tree-sitter, sqlite3, and libgit2 to mimalloc so a
389+
* correct binary does not rely on the fragile MI_OVERRIDE symbol override
390+
* (#424). MUST be the VERY FIRST statement: SQLITE_CONFIG_MALLOC has to run
391+
* before the first sqlite3_open* (cbm_mcp_server_new → cbm_store_open_memory
392+
* below opens sqlite early), else sqlite3_config returns SQLITE_MISUSE and
393+
* the bind is silently ignored. No-op in the test build. */
394+
cbm_alloc_init();
387395
cbm_profile_init(); /* reads CBM_PROFILE env var, gates all prof macros */
388396
/* CBM_LOG_LEVEL support — distilled from #414 (closes #413). Apply before
389397
* the first log statement so the configured level governs all output. */

0 commit comments

Comments
 (0)