Skip to content

Commit 463de27

Browse files
committed
test(repro): make the board robust to RED-test early-return leaks
A reproduction fails its assertion and returns before cleanup, so LeakSanitizer flagged benign harness leaks on every red store-level test and _exit()'d, swallowing the unflushed summary — repro.sh then misread it as a build failure (Linux only; macOS/Windows have no LSan). Fixes: - repro.sh: export ASAN_OPTIONS=detect_leaks=0 for the board run. Leak-cleanliness is not the board's signal (the RED rows are); the #581 leak bug gets a dedicated RSS-growth test. ASan's real checks (use-after-free, overflow) stay enabled. - repro_main.c: setvbuf(stdout, _IONBF) so the summary + RED rows survive any abnormal _exit (sanitizer or crash). - add tests/repro/repro_harness.h: shared multi-file index + store-query + fork crash-detector helpers (ported from the proven test_lang_contract.c harness) for the cross-file / store-level / crash reproduction waves. Signed-off-by: Martin Vogel <[email protected]>
1 parent 8423b0b commit 463de27

3 files changed

Lines changed: 178 additions & 0 deletions

File tree

‎scripts/repro.sh‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ print_env "repro.sh"
3838
verify_compiler "$CC"
3939

4040
OUT="$ROOT/repro-out.txt"
41+
# A RED reproduction fails its assertion and returns EARLY — before any cleanup —
42+
# so LeakSanitizer would flag benign harness leaks on every red store-level test
43+
# and abort. The board's signal is the FAIL rows, not leak-cleanliness (the leak
44+
# BUG #581 gets a dedicated RSS-growth test, not LSan). Disable leak detection
45+
# only; ASan's real checks (use-after-free, overflow) stay ON.
46+
export ASAN_OPTIONS="detect_leaks=0${ASAN_OPTIONS:+:$ASAN_OPTIONS}"
47+
4148
# test-repro both builds and runs the runner; tolerate its non-zero (red) exit.
4249
set +e
4350
$ARCH_PREFIX make -j"$NPROC" -f Makefile.cbm test-repro $MAKE_ARGS 2>&1 | tee "$OUT"

‎tests/repro/repro_harness.h‎

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* repro_harness.h — Shared helpers for cross-file / store-level / crash bug
3+
* reproductions (TIER A multi-file, TIER B crashes).
4+
*
5+
* Ported faithfully from the proven static harness in tests/test_lang_contract.c
6+
* so cross-file repro files don't each re-derive it. Header-only (static inline)
7+
* — each TU gets its own copy; no link conflicts. Include AFTER test_framework.h.
8+
*
9+
* Single-file extraction bugs do NOT need this — use cbm_extract_file directly
10+
* (see repro_extraction.c). Use this when the bug only appears once a fixture is
11+
* indexed through the full production pipeline (CALLS/IMPORTS/HTTP_CALLS edges,
12+
* cross-file/cross-package resolution, Route minting, dedup/upsert, etc.).
13+
*/
14+
#ifndef REPRO_HARNESS_H
15+
#define REPRO_HARNESS_H
16+
17+
#include <foundation/compat.h>
18+
#include "test_helpers.h" /* th_rmtree */
19+
#include "cbm.h"
20+
#include <mcp/mcp.h>
21+
#include <store/store.h>
22+
23+
#include <string.h>
24+
#include <stdlib.h>
25+
#include <stdio.h>
26+
#include <unistd.h>
27+
#if !defined(_WIN32)
28+
#include <sys/wait.h> /* fork/waitpid crash isolation — POSIX only */
29+
#endif
30+
31+
typedef struct {
32+
char tmpdir[256];
33+
char dbpath[512];
34+
char *project;
35+
cbm_mcp_server_t *srv;
36+
} RProj;
37+
38+
typedef struct {
39+
const char *name; /* relative filename, may include '/' for subdirs */
40+
const char *content;
41+
} RFile;
42+
43+
static inline void rh_to_fwd_slashes(char *p) {
44+
for (; *p; p++) {
45+
if (*p == '\\')
46+
*p = '/';
47+
}
48+
}
49+
50+
/* Index lp->tmpdir (already populated) via the production index_repository flow
51+
* and open the resulting graph DB (NULL on failure). */
52+
static inline cbm_store_t *rh_open_indexed(RProj *lp) {
53+
lp->project = cbm_project_name_from_path(lp->tmpdir);
54+
if (!lp->project)
55+
return NULL;
56+
const char *home = getenv("HOME");
57+
if (!home)
58+
home = "/tmp";
59+
char cache_dir[512];
60+
snprintf(cache_dir, sizeof(cache_dir), "%s/.cache/codebase-memory-mcp", home);
61+
cbm_mkdir(cache_dir);
62+
snprintf(lp->dbpath, sizeof(lp->dbpath), "%s/%s.db", cache_dir, lp->project);
63+
unlink(lp->dbpath);
64+
lp->srv = cbm_mcp_server_new(NULL);
65+
if (!lp->srv)
66+
return NULL;
67+
char args[700];
68+
snprintf(args, sizeof(args), "{\"repo_path\":\"%s\"}", lp->tmpdir);
69+
char *resp = cbm_mcp_handle_tool(lp->srv, "index_repository", args);
70+
if (resp)
71+
free(resp);
72+
return cbm_store_open_path(lp->dbpath);
73+
}
74+
75+
/* Write each fixture file into a fresh temp project, index it via the MCP
76+
* production flow, and open the resulting graph DB. Returns store (NULL on fail). */
77+
static inline cbm_store_t *rh_index_files(RProj *lp, const RFile *files, int nfiles) {
78+
memset(lp, 0, sizeof(*lp));
79+
snprintf(lp->tmpdir, sizeof(lp->tmpdir), "/tmp/cbm_repro_XXXXXX");
80+
if (!cbm_mkdtemp(lp->tmpdir))
81+
return NULL;
82+
rh_to_fwd_slashes(lp->tmpdir);
83+
for (int i = 0; i < nfiles; i++) {
84+
char path[700];
85+
snprintf(path, sizeof(path), "%s/%s", lp->tmpdir, files[i].name);
86+
char *slash = strrchr(path, '/');
87+
if (slash && slash > path + strlen(lp->tmpdir)) {
88+
*slash = '\0';
89+
cbm_mkdir_p(path, 0755);
90+
*slash = '/';
91+
}
92+
FILE *f = fopen(path, "wb"); /* binary: keep "\n" exact */
93+
if (!f)
94+
return NULL;
95+
fputs(files[i].content, f);
96+
fclose(f);
97+
}
98+
return rh_open_indexed(lp);
99+
}
100+
101+
static inline cbm_store_t *rh_index(RProj *lp, const char *filename, const char *content) {
102+
RFile f = {filename, content};
103+
return rh_index_files(lp, &f, 1);
104+
}
105+
106+
static inline void rh_cleanup(RProj *lp, cbm_store_t *store) {
107+
if (store)
108+
cbm_store_close(store);
109+
if (lp->srv) {
110+
cbm_mcp_server_free(lp->srv);
111+
lp->srv = NULL;
112+
}
113+
free(lp->project);
114+
lp->project = NULL;
115+
th_rmtree(lp->tmpdir);
116+
unlink(lp->dbpath);
117+
char wal[600], shm[600];
118+
snprintf(wal, sizeof(wal), "%s-wal", lp->dbpath);
119+
unlink(wal);
120+
snprintf(shm, sizeof(shm), "%s-shm", lp->dbpath);
121+
unlink(shm);
122+
}
123+
124+
/* Count edges of a given type in the project graph. Returns -1 on query error. */
125+
static inline int rh_count_edges(cbm_store_t *store, const char *project, const char *edge) {
126+
return store ? cbm_store_count_edges_by_type(store, project, edge) : -1;
127+
}
128+
129+
/* Count nodes carrying `label`. Returns -1 on query error. */
130+
static inline int rh_count_label(cbm_store_t *store, const char *project, const char *label) {
131+
cbm_node_t *nodes = NULL;
132+
int count = 0;
133+
if (cbm_store_find_nodes_by_label(store, project, label, &nodes, &count) != CBM_STORE_OK)
134+
return -1;
135+
cbm_store_free_nodes(nodes, count);
136+
return count;
137+
}
138+
139+
/* TIER B: returns true if cbm_extract_file CRASHES (signal) on `content`.
140+
* Runs in a forked child so the crash doesn't take down the repro runner. */
141+
static inline bool rh_extract_crashes(const char *content, CBMLanguage lang, const char *relpath) {
142+
#if defined(_WIN32)
143+
CBMFileResult *r =
144+
cbm_extract_file(content, (int)strlen(content), lang, "repro", relpath, 0, NULL, NULL);
145+
if (r)
146+
cbm_free_result(r);
147+
return false;
148+
#else
149+
fflush(NULL);
150+
pid_t pid = fork();
151+
if (pid < 0)
152+
return false;
153+
if (pid == 0) {
154+
CBMFileResult *r =
155+
cbm_extract_file(content, (int)strlen(content), lang, "repro", relpath, 0, NULL, NULL);
156+
if (r)
157+
cbm_free_result(r);
158+
_exit(0);
159+
}
160+
int status = 0;
161+
(void)waitpid(pid, &status, 0);
162+
return WIFSIGNALED(status);
163+
#endif
164+
}
165+
166+
#endif /* REPRO_HARNESS_H */

‎tests/repro/repro_main.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ extern void suite_repro_issue521(void);
3434
extern void suite_repro_issue382(void);
3535

3636
int main(void) {
37+
/* Unbuffered: a reproduction may crash/_exit (or a sanitizer may _exit on a
38+
* leak) before stdio flushes — keep every printed line so the summary and the
39+
* RED rows always reach the board even on an abnormal exit. */
40+
setvbuf(stdout, NULL, _IONBF, 0);
41+
3742
printf("\n");
3843
printf("════════════════════════════════════════════════════════════\n");
3944
printf(" CUMULATIVE BUG-REPRODUCTION SUITE\n");

0 commit comments

Comments
 (0)