|
| 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 */ |
0 commit comments