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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
Bind tree-sitter runtime to mimalloc in prod build to fix C++ indexing crash (#424)
Martin Vogel0d2da15d4e3a
diff --git a/Makefile.cbm b/Makefile.cbmindex 5948398d7..198b80e6c 100644--- a/Makefile.cbm+++ b/Makefile.cbm@@ -58,7 +58,10 @@ CXXFLAGS_COMMON = -std=c++14 -Wall -Wextra -Werror \-I$(CBM_DIR) -I$(TS_INCLUDE)# Production flags (CFLAGS_EXTRA allows CI to inject -DCBM_VERSION)-CFLAGS_PROD = $(CFLAGS_COMMON) -O2 $(CFLAGS_EXTRA)+# CBM_BIND_TS_ALLOCATOR=1: bind the tree-sitter runtime to mimalloc (#424). Only+# the prod build uses mimalloc (MI_OVERRIDE=1); the test build is CRT+ASan, where+# binding would create an alloc/free mismatch, so the guard is prod-only.+CFLAGS_PROD = $(CFLAGS_COMMON) -O2 -DCBM_BIND_TS_ALLOCATOR=1 $(CFLAGS_EXTRA)CXXFLAGS_PROD = $(CXXFLAGS_COMMON) -O2# Test flags: debug + sanitizers (override SANITIZE= to disable on Windows)diff --git a/internal/cbm/cbm.c b/internal/cbm/cbm.cindex 42d757030..00e54f01d 100644--- a/internal/cbm/cbm.c+++ b/internal/cbm/cbm.c@@ -16,7 +16,8 @@#include "foundation/compat.h"#include "tree_sitter/api.h" // TSParser, TSNode, TSTree, TSInput, TSLanguage, TSPoint, TSParseOptions, TSParseState#include "foundation/constants.h"-#include <stdint.h> // uint32_t, uint64_t, int64_t+#include "mimalloc.h" // mi_malloc/mi_calloc/mi_realloc/mi_free — bind ts runtime allocator (#424)+#include <stdint.h> // uint32_t, uint64_t, int64_t#include <stdlib.h>#include <string.h>#include <ctype.h>@@ -241,6 +242,22 @@ int cbm_init(void) {}enum { CBM_INIT_DONE = 1 };cbm_initialized = CBM_INIT_DONE;+ /* Bind the vendored tree-sitter runtime to the SAME allocator the rest of+ * cbm uses (mimalloc). The runtime otherwise allocates through its+ * overridable ts_current_malloc/free defaults (plain malloc / free). Under+ * the production build's MI_OVERRIDE=1 — particularly the Windows+ * static-MinGW link with --allow-multiple-definition — `malloc` and `free`+ * can resolve to DIFFERENT allocators (mimalloc vs the CRT), so a subtree+ * allocated by mimalloc gets freed by the CRT (or vice-versa), corrupting+ * the heap freelist and crashing mid-parse on a freed (0xffff…) pointer for+ * large templated C++ headers (#424 — scales with parse churn, not syntax).+ * Binding explicitly forces ts allocate+free through one allocator on every+ * platform, eliminating the mismatch class generically. Guarded to the+ * production build (MI_OVERRIDE=1): the test build is CRT + ASan, where+ * binding ts to mimalloc would mismatch ASan/CRT frees. */+#if defined(CBM_BIND_TS_ALLOCATOR) && CBM_BIND_TS_ALLOCATOR+ ts_set_allocator(mi_malloc, mi_calloc, mi_realloc, mi_free);+#endifreturn 0;}diff --git a/tests/test_stack_overflow.c b/tests/test_stack_overflow.cindex 86c1eca2f..75749c799 100644--- a/tests/test_stack_overflow.c+++ b/tests/test_stack_overflow.c@@ -16,6 +16,17 @@#include <stdlib.h>#include <string.h>+/* tree-sitter runtime allocator hooks (ts_runtime/src/alloc.h, TS_PUBLIC) and+ * mimalloc (vendored) — for the #424 allocator-binding regression test. */+extern void *(*ts_current_malloc)(size_t);+extern void *(*ts_current_calloc)(size_t, size_t);+extern void *(*ts_current_realloc)(void *, size_t);+extern void (*ts_current_free)(void *);+extern void *mi_malloc(size_t);+extern void *mi_calloc(size_t, size_t);+extern void *mi_realloc(void *, size_t);+extern void mi_free(void *);+/* ── Helpers ───────────────────────────────────────────────────── */static CBMFileResult *extract(const char *src, CBMLanguage lang, const char *proj,@@ -24,7 +35,6 @@ static CBMFileResult *extract(const char *src, CBMLanguage lang, const char *proreturn r;}-/* ═══════════════════════════════════════════════════════════════════* Test: JavaScript calls exceeding 512 stack cap*@@ -167,7 +177,8 @@ TEST(express_routes_exceed_512) {p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "const express = require('express');\n");p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "const app = express();\n\n");for (int i = 0; i < ROUTE_COUNT; i++) {- p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "app.get('/route_%d', handler_%d);\n", i, i);+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)),+ "app.get('/route_%d', handler_%d);\n", i, i);}p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "\napp.listen(3000);\n");@@ -205,7 +216,8 @@ TEST(ts_imports_exceed_512) {char *p = src;for (int i = 0; i < IMPORT_COUNT; i++) {- p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "import { mod_%d } from './module_%d';\n", i, i);+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)),+ "import { mod_%d } from './module_%d';\n", i, i);}p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "console.log('done');\n");@@ -301,6 +313,82 @@ TEST(yaml_vars_exceed_256) {PASS();}+/* ═══════════════════════════════════════════════════════════════════+ * Test: tree-sitter runtime allocator is bound to cbm's allocator (#424)+ *+ * cbm_init() must bind the vendored tree-sitter runtime to mimalloc via+ * ts_set_allocator(). Otherwise the runtime allocates through its overridable+ * ts_current_malloc/free defaults (plain malloc/free); under the production+ * MI_OVERRIDE=1 build (esp. Windows static-MinGW + --allow-multiple-definition)+ * ts_malloc and ts_free can resolve to different allocators, corrupting the+ * heap and crashing mid-parse on large templated C++ headers. Asserting the+ * binding reproduces the mismatch CONDITION deterministically on every+ * platform: RED before the fix (ts_current_free == plain free), GREEN after.+ * ═══════════════════════════════════════════════════════════════════ */++TEST(ts_allocator_bound_to_mimalloc_issue424) {+ cbm_init();+#if defined(CBM_BIND_TS_ALLOCATOR) && CBM_BIND_TS_ALLOCATOR+ /* Production build: cbm_init must have bound the ts runtime to mimalloc so+ * ts_malloc and ts_free can never resolve to different allocators (#424). */+ ASSERT_TRUE(ts_current_malloc == mi_malloc);+ ASSERT_TRUE(ts_current_free == mi_free);+#else+ /* Test build (MI_OVERRIDE=0, CRT + ASan): binding is intentionally OFF (it+ * would mismatch ASan/CRT frees). The crash only affects the prod binary;+ * its reproduction runs in the prod smoke job. Independently verify the fix+ * MECHANISM — ts_set_allocator wires the runtime hooks — then restore the+ * CRT defaults so the rest of this build stays allocator-consistent. */+ void *(*save_m)(size_t) = ts_current_malloc;+ void *(*save_c)(size_t, size_t) = ts_current_calloc;+ void *(*save_r)(void *, size_t) = ts_current_realloc;+ void (*save_f)(void *) = ts_current_free;+ ts_set_allocator(mi_malloc, mi_calloc, mi_realloc, mi_free);+ int wired = (ts_current_malloc == mi_malloc) && (ts_current_free == mi_free);+ ts_set_allocator(save_m, save_c, save_r, save_f); /* restore BEFORE asserting */+ ASSERT_TRUE(wired);+ ASSERT_TRUE(ts_current_free == save_f);+#endif+ PASS();+}++/* ═══════════════════════════════════════════════════════════════════+ * Test: large templated C++ header extraction does not crash (#424)+ *+ * Generates a json.hpp-style header (hundreds of templated structs with+ * operator overloads) — the parse path that SEGV'd in #424. Asserts+ * extraction completes. (In the MI_OVERRIDE=0 test build the allocator is+ * consistent so this cannot fault here; it guards extraction on large+ * templated input and addresses the C++ gap in this file. The production-+ * binary crash reproduction runs in the smoke job.)+ * ═══════════════════════════════════════════════════════════════════ */++TEST(cpp_large_templated_header_no_crash_issue424) {+ const int N = 400; /* ~400 templated structs → ~2400 lines */+ size_t buf_sz = (size_t)N * 600;+ char *src = malloc(buf_sz);+ ASSERT_NOT_NULL(src);+ char *p = src;+ p += snprintf(p, buf_sz, "#include <cstddef>\nnamespace ns {\n");+ for (int i = 0; i < N; i++) {+ p += snprintf(p, (size_t)(buf_sz - (size_t)(p - src)),+ "template <typename T> struct Box%d {\n"+ " T value;\n"+ " bool operator<(const Box%d &o) const { return value < o.value; }\n"+ " bool operator==(const Box%d &o) const { return value == o.value; }\n"+ " T get() const { return value; }\n"+ "};\n",+ i, i, i);+ }+ snprintf(p, (size_t)(buf_sz - (size_t)(p - src)), "}\n");++ CBMFileResult *r = extract(src, CBM_LANG_CPP, "test", "templated.hpp");+ ASSERT_NOT_NULL(r); /* no crash is the real assertion */+ cbm_free_result(r);+ free(src);+ PASS();+}+/* ═══════════════════════════════════════════════════════════════════* Suite registration* ═══════════════════════════════════════════════════════════════════ */@@ -308,6 +396,9 @@ TEST(yaml_vars_exceed_256) {SUITE(stack_overflow) {cbm_init();+ RUN_TEST(ts_allocator_bound_to_mimalloc_issue424);+ RUN_TEST(cpp_large_templated_header_no_crash_issue424);+RUN_TEST(js_calls_exceed_512);RUN_TEST(python_calls_exceed_512);RUN_TEST(go_calls_exceed_1024);