Skip to content

Commit 533eee0

Browse files
committed
fix(test): isolate the C test runners' daemon runtime
Why: the test runner isolated HOME and CBM_CACHE_DIR but not CBM_RUNTIME_DIR, and neither of the first two moves the daemon rendezvous. Its key is a constant product hash (cbm_daemon_rendezvous_key, "c888acc3ae367a1e"), and with no explicit parent and CBM_RUNTIME_DIR unset cbm_daemon_bootstrap_endpoint_new falls back to the account-wide default parent (/private/tmp on macOS, /tmp on Linux, LocalAppData on Windows). Every endpoint a test resolved without an explicit parent was therefore byte-identical to the developer's live daemon socket (/private/tmp/cbm-daemon-<uid>/cbm-c888acc3ae367a1e.sock), whatever HOME said. The only in-runner product caller is CLI activation (cli_activation_production_context_init, reached from cbm_cmd_install, cbm_cmd_uninstall and update). Only a process-global seam kept it off the default: SUITE(cli) sets that seam on entry and clears it on exit. On the default endpoint, activation admits into the live daemon's cohort. When the cohort is ours or busy, or the active cache fingerprint is unreadable, activation prints "Stopping active CBM sessions" and drains them. That is the same class of failure as an old binary's install, which stopped a user daemon and six MCP sessions. One test outside SUITE(cli), or one fixture that drops the variable, was enough to reach it. daemon_bootstrap_runtime_dir_env_relocates_rendezvous did exactly that: it restored by unsetting CBM_RUNTIME_DIR. Fix: - tests/test_main.c: tf_setup_cache_sentinel creates a fresh per-run runtime parent (th_secure_runtime_parent_new "run", under /tmp on POSIX and LocalAppData on Windows) and sets CBM_RUNTIME_DIR to it for the whole run. It replaces any inherited value, the rule scripts/test-runtime.sh applies to the shell harness. The existing atexit cleanup removes the directory. - src/daemon/bootstrap.c, CBM_ENABLE_TEST_SEAMS only: once a test runner arms the guard, an endpoint request with no explicit parent and no CBM_RUNTIME_DIR is refused with a stderr diagnostic instead of resolving the default. No product binary arms it. - tests/repro/repro_main.c arms the same guard. No reproduction needs the rendezvous. - tests/test_daemon_bootstrap.c: the relocation test restores the value it found instead of unsetting it. - tests/test_helpers.h: th_secure_runtime_base() names the anchor that th_secure_runtime_parent_new already used. Proof: - Before, on origin/main 1f9b4db: runner_isolation is RED 3/3 at its first assertion (CBM_RUNTIME_DIR is NULL during the run). - After: green 3/3. The endpoint resolved through the product resolver lies inside /private/tmp/cbm-runtime-run-XXXXXX/, never at /private/tmp/cbm-daemon-<uid>. All 43 activation-guard scopes logged by the suites below are fixture or cli-suite directories, and the guard never fired. The per-run directory is gone after exit, and the live daemon and its runtime directory are unchanged. - Revert: dropping the setenv gives RED (value is NULL). Disarming the guard gives RED (refused_without). Tests: - runner_isolation: runner_isolation_daemon_runtime_dir_is_private, runner_isolation_refuses_default_daemon_runtime - Green on macOS: cli (328), mcp + mcp_mutation_guard (344), daemon_runtime + daemon_application (106), daemon + daemon_version + daemon_frontend + daemon_bootstrap + daemon_ipc (117), runner_isolation + activation_transaction + version_cohort + project_lock + index_supervisor + private_file_lock + lock_registry (73). All skips are existing Windows- or Linux-only platform skips. - make -f Makefile.cbm lint-ci passes. test-repro-runner builds. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 1f9b4db commit 533eee0

6 files changed

Lines changed: 182 additions & 16 deletions

File tree

‎src/daemon/bootstrap.c‎

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,21 +241,49 @@ static const char *bootstrap_runtime_parent_override(char *buffer, size_t capaci
241241
return value && value[0] != '\0' ? value : NULL;
242242
}
243243

244+
/* An explicit parent keeps precedence: it carries the compile-time test seam
245+
* and the lifecycle guards' isolated namespace. NULL means the account-wide
246+
* default rendezvous. */
247+
static const char *bootstrap_runtime_parent_resolve(const char *runtime_parent, char *buffer,
248+
size_t capacity) {
249+
return runtime_parent ? runtime_parent : bootstrap_runtime_parent_override(buffer, capacity);
250+
}
251+
252+
#ifdef CBM_ENABLE_TEST_SEAMS
253+
/* Armed once by a test runner before any suite starts its threads. */
254+
static bool g_bootstrap_default_runtime_forbidden = false;
255+
256+
void cbm_daemon_bootstrap_forbid_default_runtime_for_test(bool forbid) {
257+
g_bootstrap_default_runtime_forbidden = forbid;
258+
}
259+
260+
bool cbm_daemon_bootstrap_default_runtime_refused_for_test(const char *runtime_parent) {
261+
char override_parent[BOOTSTRAP_PATH_CAP];
262+
return g_bootstrap_default_runtime_forbidden &&
263+
!bootstrap_runtime_parent_resolve(runtime_parent, override_parent,
264+
sizeof(override_parent));
265+
}
266+
#endif
267+
244268
cbm_daemon_ipc_endpoint_t *cbm_daemon_bootstrap_endpoint_new(const char *runtime_parent) {
245269
char key[CBM_DAEMON_KEY_SIZE];
246270
if (!cbm_daemon_rendezvous_key(key)) {
247271
return NULL;
248272
}
249-
/* An explicit parent keeps precedence: it carries the compile-time test
250-
* seam and the lifecycle guards' isolated namespace. The override is
251-
* resolved HERE, the one function every product endpoint goes through
252-
* (daemon, MCP client, local CLI, index worker, activation), so no call
253-
* site can silently keep the default. */
273+
/* The override is resolved HERE, the one function every product endpoint
274+
* goes through (daemon, MCP client, local CLI, index worker, activation),
275+
* so no call site can silently keep the default. */
254276
char override_parent[BOOTSTRAP_PATH_CAP];
255277
const char *parent =
256-
runtime_parent
257-
? runtime_parent
258-
: bootstrap_runtime_parent_override(override_parent, sizeof(override_parent));
278+
bootstrap_runtime_parent_resolve(runtime_parent, override_parent, sizeof(override_parent));
279+
#ifdef CBM_ENABLE_TEST_SEAMS
280+
if (!parent && g_bootstrap_default_runtime_forbidden) {
281+
(void)fprintf(stderr, "codebase-memory-mcp: test run refused the default daemon runtime "
282+
"directory (no explicit parent and CBM_RUNTIME_DIR unset): the "
283+
"developer's live daemon listens there\n");
284+
return NULL;
285+
}
286+
#endif
259287
return cbm_daemon_ipc_endpoint_new(key, parent);
260288
}
261289

‎src/daemon/bootstrap.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ bool cbm_daemon_process_role_requires_client(cbm_daemon_process_role_t role);
4545
* ABI values must never create parallel daemon namespaces. */
4646
cbm_daemon_ipc_endpoint_t *cbm_daemon_bootstrap_endpoint_new(const char *runtime_parent);
4747

48+
#ifdef CBM_ENABLE_TEST_SEAMS
49+
/* Test runners only; no product binary arms it. Once armed, an endpoint
50+
* request that names no runtime parent while CBM_RUNTIME_DIR is unset (one
51+
* that would resolve to the account-wide default rendezvous, where the
52+
* developer's live daemon listens) is refused with a diagnostic on stderr. */
53+
void cbm_daemon_bootstrap_forbid_default_runtime_for_test(bool forbid);
54+
/* The same decision cbm_daemon_bootstrap_endpoint_new makes, without
55+
* resolving anything: true when that call would be refused. */
56+
bool cbm_daemon_bootstrap_default_runtime_refused_for_test(const char *runtime_parent);
57+
#endif
58+
4859
/* Cross-platform launch policy for the daemon child. The child is invoked
4960
* directly (never through a shell), with exactly argv[0] plus the one hidden
5061
* internal argument. It is detached from the launching client's lifetime and

‎tests/repro/repro_main.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ int tf_skip_count = 0;
2828
#include "test_framework.h"
2929
#include "repro_runner.h"
3030
#include "foundation/compat.h" /* cbm_setenv — #845 supervisor kill switch */
31+
#include "daemon/bootstrap.h" /* refuse the developer's default daemon rendezvous */
3132

3233
/* Per-suite summary + filter. RUN_SUITE prints a one-line
3334
* "[SUITE] <name> P passed, F failed" report (greppable for which suites still
@@ -131,6 +132,13 @@ int main(void) {
131132
* supervisor gate already ignores unmarked hosts; pin the kill switch too.
132133
* A test that exercises the supervisor must explicitly re-enable it. */
133134
cbm_setenv("CBM_INDEX_SUPERVISOR", "0", 1);
135+
#ifdef CBM_ENABLE_TEST_SEAMS
136+
/* No reproduction needs the daemon rendezvous. Its default is the
137+
* developer's live daemon whatever HOME says, so a case that ever resolves
138+
* an endpoint without an explicit parent or CBM_RUNTIME_DIR is refused
139+
* loudly instead of reaching it. */
140+
cbm_daemon_bootstrap_forbid_default_runtime_for_test(true);
141+
#endif
134142

135143
/* Unbuffered: a reproduction may crash/_exit (or a sanitizer may _exit on a
136144
* leak) before stdio flushes — keep every printed line so the summary and the

‎tests/test_daemon_bootstrap.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,11 @@ TEST(daemon_bootstrap_runtime_dir_env_relocates_rendezvous) {
400400
char relocated_runtime[BOOTSTRAP_TEST_PATH_CAP] = {0};
401401
char explicit_runtime[BOOTSTRAP_TEST_PATH_CAP] = {0};
402402
char unusable[BOOTSTRAP_TEST_PATH_CAP] = {0};
403+
/* The runner isolates the rendezvous for the whole run through this same
404+
* variable, so the test must hand back exactly what it found. */
405+
char previous_value[BOOTSTRAP_TEST_PATH_CAP] = {0};
406+
const char *previous =
407+
cbm_safe_getenv("CBM_RUNTIME_DIR", previous_value, sizeof(previous_value), NULL);
403408
int written = snprintf(override_parent, sizeof(override_parent),
404409
"%s/cbm-bootstrap-runtime-env-XXXXXX", cbm_tmpdir());
405410
if (written <= 0 || written >= (int)sizeof(override_parent) || !cbm_mkdtemp(override_parent)) {
@@ -436,8 +441,10 @@ TEST(daemon_bootstrap_runtime_dir_env_relocates_rendezvous) {
436441
unusable_set ? cbm_daemon_bootstrap_endpoint_new(NULL) : NULL;
437442

438443
/* Restore before asserting: a failed assertion returns immediately, and a
439-
* leaked CBM_RUNTIME_DIR would follow every later suite in this process. */
440-
(void)cbm_unsetenv("CBM_RUNTIME_DIR");
444+
* leaked override, or a dropped run value, would follow every later suite
445+
* in this process. */
446+
bool restored = previous ? cbm_setenv("CBM_RUNTIME_DIR", previous_value, 1) == 0
447+
: cbm_unsetenv("CBM_RUNTIME_DIR") == 0;
441448
cbm_daemon_ipc_endpoint_free(refused);
442449
cbm_daemon_ipc_endpoint_free(relocated);
443450
if (relocated_runtime[0] != '\0') {
@@ -448,6 +455,7 @@ TEST(daemon_bootstrap_runtime_dir_env_relocates_rendezvous) {
448455
}
449456
(void)cbm_rmdir(override_parent);
450457

458+
ASSERT_TRUE(restored);
451459
ASSERT_TRUE(prepared);
452460
ASSERT_TRUE(explicit_started);
453461
ASSERT_TRUE(explicit_canonical);

‎tests/test_helpers.h‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,19 @@ static inline char *th_mktempdir(const char *prefix) {
180180
* leaf made by cbm_mkdtemp() has a private DACL. Keep these security-sensitive
181181
* fixtures under LocalAppData on Windows; preserve the ordinary temporary-root
182182
* behavior on POSIX. */
183+
static inline const char *th_secure_runtime_base(void) {
184+
#ifdef _WIN32
185+
return cbm_app_local_dir();
186+
#else
187+
return cbm_tmpdir();
188+
#endif
189+
}
190+
183191
static inline bool th_secure_runtime_parent_new(char *out, size_t out_cap, const char *tag) {
184192
if (!out || out_cap == 0 || !tag || !tag[0]) {
185193
return false;
186194
}
187-
#ifdef _WIN32
188-
const char *base = cbm_app_local_dir();
189-
#else
190-
const char *base = cbm_tmpdir();
191-
#endif
195+
const char *base = th_secure_runtime_base();
192196
if (!base || !base[0]) {
193197
out[0] = '\0';
194198
return false;

‎tests/test_main.c‎

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ int tf_skip_count = 0;
1818
#include "foundation/mem.h" /* cbm_mem_init — worker budget */
1919
#include "foundation/log.h" /* worker liveness heartbeat probe */
2020
#include "foundation/platform.h" /* cbm_file_exists — blocking-git marker */
21+
#include "daemon/bootstrap.h" /* runner rendezvous isolation */
2122
#include "daemon/runtime.h" /* bounded worker response probe */
2223
#include "daemon/ipc.h" /* Windows private-lock re-exec probe */
2324
#include "daemon/version_cohort.h" /* Windows crash-turnover re-exec probe */
@@ -186,11 +187,15 @@ static int tf_maybe_run_blocking_git_probe(int argc, char **argv) {
186187
* this sentinel and restore it; atexit removes anything left behind by an
187188
* assertion that returns before fixture cleanup. */
188189
static char tf_home_sentinel[512];
190+
static char tf_runtime_sentinel[512];
189191

190192
static void tf_cleanup_cache_sentinel(void) {
191193
if (tf_home_sentinel[0]) {
192194
th_rmtree(tf_home_sentinel);
193195
}
196+
if (tf_runtime_sentinel[0]) {
197+
th_rmtree(tf_runtime_sentinel);
198+
}
194199
}
195200

196201
/* Client home overrides the CLI honours BEFORE $HOME, so redirecting HOME alone
@@ -242,9 +247,108 @@ static bool tf_setup_cache_sentinel(void) {
242247
cbm_unsetenv(tf_client_home_overrides[i]);
243248
}
244249
atexit(tf_cleanup_cache_sentinel);
250+
/* HOME and CBM_CACHE_DIR do not move the daemon rendezvous: its key is a
251+
* constant product hash under the system temp root. Without this, an
252+
* install/uninstall/update test or any endpoint resolved with no explicit
253+
* parent reaches the developer's live daemon. The run always gets a fresh
254+
* private directory; an inherited value is replaced, never reused (the
255+
* rule scripts/test-runtime.sh applies to the shell harness).
256+
* th_secure_runtime_parent_new anchors it where the IPC ancestry check
257+
* accepts it (LocalAppData on Windows). */
258+
if (!th_secure_runtime_parent_new(tf_runtime_sentinel, sizeof(tf_runtime_sentinel), "run")) {
259+
tf_runtime_sentinel[0] = '\0';
260+
return false;
261+
}
262+
if (cbm_setenv("CBM_RUNTIME_DIR", tf_runtime_sentinel, 1) != 0) {
263+
return false;
264+
}
265+
#ifdef CBM_ENABLE_TEST_SEAMS
266+
/* Belt and braces: a fixture that later drops the variable gets a loud
267+
* refusal instead of the default rendezvous. */
268+
cbm_daemon_bootstrap_forbid_default_runtime_for_test(true);
269+
#endif
245270
return true;
246271
}
247272

273+
static bool tf_path_has_parent(const char *path, const char *parent) {
274+
size_t length = parent ? strlen(parent) : 0;
275+
return path && length > 0 && strncmp(path, parent, length) == 0 &&
276+
(path[length] == '/' || path[length] == '\\');
277+
}
278+
279+
/* The daemon rendezvous is not derived from HOME or the cache: its key is a
280+
* constant product hash and its default parent is the system temp root. With
281+
* CBM_RUNTIME_DIR unset, every endpoint a test resolves is therefore the
282+
* developer's live daemon socket (<tmp>/cbm-daemon-<uid>/cbm-<key>.sock).
283+
* Every check runs before anything is resolved, so a run without isolation
284+
* fails here and this test never reaches that socket itself. */
285+
TEST(runner_isolation_daemon_runtime_dir_is_private) {
286+
char runtime_env[sizeof(tf_runtime_sentinel)] = {0};
287+
const char *value = cbm_safe_getenv("CBM_RUNTIME_DIR", runtime_env, sizeof(runtime_env), NULL);
288+
ASSERT_NOT_NULL(value);
289+
ASSERT_TRUE(value[0] != '\0');
290+
/* The fresh per-run directory, never a value inherited from the caller. */
291+
ASSERT_STR_EQ(value, tf_runtime_sentinel);
292+
293+
const char *root = th_secure_runtime_base();
294+
ASSERT_NOT_NULL(root);
295+
char canonical_root[CBM_SZ_4K] = {0};
296+
char canonical_run[CBM_SZ_4K] = {0};
297+
ASSERT_TRUE(cbm_canonical_path(root, canonical_root, sizeof(canonical_root)) != 0);
298+
ASSERT_TRUE(cbm_canonical_path(value, canonical_run, sizeof(canonical_run)) != 0);
299+
/* Strictly below the temp root: the root itself is the default rendezvous
300+
* parent (/tmp on POSIX, LocalAppData on Windows). */
301+
ASSERT_TRUE(tf_path_has_parent(canonical_run, canonical_root));
302+
303+
/* Only now resolve through the product resolver. A NULL parent is what
304+
* every product call site passes: daemon, MCP client, CLI, activation. */
305+
cbm_daemon_ipc_endpoint_t *endpoint = cbm_daemon_bootstrap_endpoint_new(NULL);
306+
const char *resolved = endpoint ? cbm_daemon_ipc_endpoint_runtime_dir(endpoint) : NULL;
307+
char canonical_resolved[CBM_SZ_4K] = {0};
308+
bool resolved_ok = resolved && cbm_canonical_path(resolved, canonical_resolved,
309+
sizeof(canonical_resolved)) != 0;
310+
cbm_daemon_ipc_endpoint_free(endpoint);
311+
ASSERT_TRUE(resolved_ok);
312+
ASSERT_TRUE(tf_path_has_parent(canonical_resolved, canonical_run));
313+
#ifndef _WIN32
314+
char live_default[CBM_SZ_4K];
315+
(void)snprintf(live_default, sizeof(live_default), "%s/cbm-daemon-%lu", canonical_root,
316+
(unsigned long)geteuid());
317+
ASSERT_TRUE(strcmp(canonical_resolved, live_default) != 0);
318+
#endif
319+
PASS();
320+
}
321+
322+
#ifdef CBM_ENABLE_TEST_SEAMS
323+
/* The runner arms the bootstrap guard, so a fixture that drops the variable
324+
* gets a refusal instead of the default rendezvous. Only the decision is
325+
* exercised: no endpoint is ever built while the variable is absent. */
326+
TEST(runner_isolation_refuses_default_daemon_runtime) {
327+
ASSERT_TRUE(tf_runtime_sentinel[0] != '\0');
328+
bool allowed_with_run_dir = !cbm_daemon_bootstrap_default_runtime_refused_for_test(NULL);
329+
330+
(void)cbm_unsetenv("CBM_RUNTIME_DIR");
331+
bool refused_without = cbm_daemon_bootstrap_default_runtime_refused_for_test(NULL);
332+
bool explicit_allowed =
333+
!cbm_daemon_bootstrap_default_runtime_refused_for_test(tf_runtime_sentinel);
334+
/* Restore before asserting: a failed assertion returns immediately. */
335+
bool restored = cbm_setenv("CBM_RUNTIME_DIR", tf_runtime_sentinel, 1) == 0;
336+
337+
ASSERT_TRUE(restored);
338+
ASSERT_TRUE(allowed_with_run_dir);
339+
ASSERT_TRUE(refused_without);
340+
ASSERT_TRUE(explicit_allowed);
341+
PASS();
342+
}
343+
#endif
344+
345+
SUITE(runner_isolation) {
346+
RUN_TEST(runner_isolation_daemon_runtime_dir_is_private);
347+
#ifdef CBM_ENABLE_TEST_SEAMS
348+
RUN_TEST(runner_isolation_refuses_default_daemon_runtime);
349+
#endif
350+
}
351+
248352
/* Fast real-process probes for the async index-supervisor contract. They run
249353
* only in a child admitted by the exact build-bound worker grammar. */
250354
static void tf_index_worker_probe(const char *args_json, const char *response_out) {
@@ -1047,7 +1151,7 @@ int main(int argc, char **argv) {
10471151
* A test that exercises the supervisor must explicitly re-enable it. */
10481152
cbm_setenv("CBM_INDEX_SUPERVISOR", "0", 1);
10491153
if (!tf_setup_cache_sentinel()) {
1050-
fprintf(stderr, "failed to create isolated test cache\n");
1154+
fprintf(stderr, "failed to create isolated test cache and daemon runtime\n");
10511155
return 2;
10521156
}
10531157

@@ -1071,6 +1175,9 @@ int main(int argc, char **argv) {
10711175
printf("\n codebase-memory-mcp C test suite\n");
10721176
}
10731177

1178+
/* Runner isolation first: nothing below may reach the developer's daemon. */
1179+
RUN_SELECTED_SUITE(runner_isolation);
1180+
10741181
/* Foundation */
10751182
RUN_SELECTED_SUITE(arena);
10761183
RUN_SELECTED_SUITE(hash_table);

0 commit comments

Comments
 (0)