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.

fix(pipeline): module-QN strings must outlive the cross registries they feed

Martin Vogele2f5b6a79840
diff --git a/src/pipeline/pass_lsp_cross.c b/src/pipeline/pass_lsp_cross.c
index 4df3736c9..30f9696d5 100644
--- a/src/pipeline/pass_lsp_cross.c
+++ b/src/pipeline/pass_lsp_cross.c
@@ -1265,9 +1265,14 @@ int cbm_pipeline_pass_lsp_cross(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *
cbm_pxc_free_module_def_index(module_def_index);
free(all_defs);
- for (int i = 0; i < file_count; i++)
- free(def_modules[i]);
- free(def_modules);
+ /* The module-QN strings are borrowed by the shared cross registries in
+ * ctx->seq_cross_arena, which deliberately outlive this pass so that
+ * pass_calls can read borrowed registry strings. Freeing the strings here
+ * while keeping the registries alive was a use-after-free (pass_calls
+ * strcmp on a freed module QN, caught by ASan on the kernel corpus tier).
+ * Ownership transfers to the ctx; released beside the arena. */
+ ctx->seq_cross_def_modules = def_modules;
+ ctx->seq_cross_def_module_count = file_count;
/* Drop the borrowed manifest pointer before its arena dies, so a later
* pass (or a stale thread-local) can never read freed manifest memory. */
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index 824cd72a9..6b5e94d47 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -1075,11 +1075,21 @@ static int run_sequential_pipeline(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,
ctx->result_cache = NULL;
}
/* Release the lsp_cross pass's shared registries only now: resolved_calls
- * borrowed registry-owned strings that the calls pass read above. */
+ * borrowed registry-owned strings that the calls pass read above. The
+ * module-QN strings the registries borrow (parked on the ctx by the pass
+ * for exactly this lifetime) go with them. */
if (ctx->seq_cross_arena_live) {
cbm_arena_destroy(&ctx->seq_cross_arena);
ctx->seq_cross_arena_live = false;
}
+ if (ctx->seq_cross_def_modules) {
+ for (int i = 0; i < ctx->seq_cross_def_module_count; i++) {
+ free(ctx->seq_cross_def_modules[i]);
+ }
+ free(ctx->seq_cross_def_modules);
+ ctx->seq_cross_def_modules = NULL;
+ ctx->seq_cross_def_module_count = 0;
+ }
/* Destroy this thread's TLS parser: the sequential path parses on the
* CALLING thread (usually main), and a parser left alive here was
* allocated in the current tree-sitter allocator epoch. A later
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 102e160a3..38818ea99 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -120,6 +120,14 @@ typedef struct {
* resolve. */
CBMArena seq_cross_arena;
bool seq_cross_arena_live;
+ /* Sequential lsp_cross only: the per-file module-QN strings the collected
+ * defs (and through them the shared cross registries in seq_cross_arena)
+ * borrow. The registries outlive the pass so pass_calls can read borrowed
+ * strings -- these must too. Ownership transfers here at the end of the
+ * pass; released beside the arena. Freeing them at pass end was a
+ * use-after-free first observable on the real-repo corpus tier. */
+ char **seq_cross_def_modules;
+ int seq_cross_def_module_count;
/* ObjectScript $$$macro table built from .inc files in the repo (NULL if
* no ObjectScript include files were found). Owned by pipeline.c. */
diff --git a/tests/test_parallel.c b/tests/test_parallel.c
index 1df6d9276..6f7a2ecc1 100644
--- a/tests/test_parallel.c
+++ b/tests/test_parallel.c
@@ -253,6 +253,13 @@ static cbm_gbuf_t *run_sequential_with_lsp_cross_and_mutator(
cbm_arena_destroy(&ctx.seq_cross_arena);
ctx.seq_cross_arena_live = false;
}
+ if (ctx.seq_cross_def_modules) {
+ for (int i = 0; i < ctx.seq_cross_def_module_count; i++) {
+ free(ctx.seq_cross_def_modules[i]);
+ }
+ free(ctx.seq_cross_def_modules);
+ ctx.seq_cross_def_modules = NULL;
+ }
return gbuf;
}