diff --git a/scripts/test.sh b/scripts/test.sh index 2b0e28774..995d6755c 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -245,6 +245,23 @@ assert_test_runner_build_config() { print_env "test.sh" +# TEMPORARY sha256 diagnostic (drop before merge): every full-file fingerprint +# the test runner computes appends one "CBM-SHA256-DIAG" line to this file +# (backend, bytes, read vs compute ms, CPU facts; the daemon_bootstrap suite adds +# one selected-vs-portable comparison). Printed at exit, whatever the verdict, +# so the hosted job log carries it; the trap never changes the exit status. +CBM_SHA256_DIAG_FILE="${CBM_SHA256_DIAG_FILE:-${TMPDIR:-/tmp}/cbm-sha256-diag-$$.txt}" +export CBM_SHA256_DIAG_FILE +rm -f "$CBM_SHA256_DIAG_FILE" +sha256_diag_dump() { + if [ -s "$CBM_SHA256_DIAG_FILE" ]; then + echo "=== TEMPORARY sha256 diag: $(wc -l < "$CBM_SHA256_DIAG_FILE" | tr -d ' ') line(s) ===" + head -n 600 "$CBM_SHA256_DIAG_FILE" || true + fi + rm -f "$CBM_SHA256_DIAG_FILE" || true +} +trap sha256_diag_dump EXIT + # ── TSan mode (--tsan): the data-race gate ── # One entry for every venue: CI's tsan jobs and the compose test-tsan service # both run this instead of carrying their own make invocation. diff --git a/src/daemon/service.c b/src/daemon/service.c index 13413be2f..f4d78228c 100644 --- a/src/daemon/service.c +++ b/src/daemon/service.c @@ -12,6 +12,9 @@ #include #include +#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS +#include /* TEMPORARY diagnostic (drop before merge) */ +#endif #include #include #include @@ -355,6 +358,176 @@ static bool fd_regular(int fd, struct stat *status_out) { return true; } +#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS +/* TEMPORARY diagnostic (drop before merge). When the test runner's main read + * CBM_SHA256_DIAG_FILE (before any thread starts), every full-file fingerprint + * appends one "CBM-SHA256-DIAG kind=hash ..." line there: backend, bytes, wall + * ms split into read (pread) and compute (SHA-256), and the CPU facts. + * scripts/test.sh prints the file at exit, so the hosted job log carries the + * lines whatever the verdict. A file, not stderr: tests compare the stderr of + * child processes, and a verdict must not change. Test runner only. */ +static char g_fingerprint_diag_path[1024]; +static _Atomic bool g_fingerprint_diag_on = false; +static _Atomic bool g_fingerprint_diag_compared = false; + +void cbm_daemon_fingerprint_diag_init_for_testing(void) { + const char *path = getenv("CBM_SHA256_DIAG_FILE"); + size_t length = path ? strlen(path) : 0; + if (length == 0 || length >= sizeof(g_fingerprint_diag_path)) { + return; + } + memcpy(g_fingerprint_diag_path, path, length + 1); + atomic_store_explicit(&g_fingerprint_diag_on, true, memory_order_release); + cbm_sha256_diag_enable_for_testing(true); +} + +static bool fingerprint_diag_on(void) { + return atomic_load_explicit(&g_fingerprint_diag_on, memory_order_acquire); +} + +static double fingerprint_diag_now_ms(void) { + struct timespec now; + if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) { + return 0.0; + } + return (double)now.tv_sec * 1e3 + (double)now.tv_nsec / 1e6; +} + +static void fingerprint_diag_emit(const char *line) { + char cpu[384]; + cbm_sha256_diag_cpu_for_testing(cpu, sizeof(cpu)); + char text[1024]; + int length = + snprintf(text, sizeof(text), "CBM-SHA256-DIAG %s pid=%ld %s\n", line, (long)getpid(), cpu); + if (length <= 0) { + return; + } + size_t size = (size_t)length < sizeof(text) ? (size_t)length : sizeof(text) - 1; + int fd = open(g_fingerprint_diag_path, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0600); + if (fd < 0) { + return; + } + ssize_t written = write(fd, text, size); + (void)written; + (void)close(fd); +} + +typedef struct { + bool on; + double start_ms; + double read_ms; + double compute_ms; +} fingerprint_diag_t; + +static void fingerprint_diag_begin(fingerprint_diag_t *diag) { + diag->on = fingerprint_diag_on(); + diag->read_ms = 0.0; + diag->compute_ms = 0.0; + diag->start_ms = diag->on ? fingerprint_diag_now_ms() : 0.0; + if (diag->on) { + cbm_sha256_diag_reset_for_testing(); + } +} + +static double fingerprint_diag_stamp(const fingerprint_diag_t *diag) { + return diag->on ? fingerprint_diag_now_ms() : 0.0; +} + +static void fingerprint_diag_end(const fingerprint_diag_t *diag, long long bytes, bool ok) { + if (!diag->on) { + return; + } + uint64_t calls = 0; + uint64_t dirty = 0; + uint64_t xinuse_or = 0; + cbm_sha256_diag_take_for_testing(&calls, &dirty, &xinuse_or); + char line[512]; + (void)snprintf(line, sizeof(line), + "kind=hash ok=%d bytes=%lld backend=%s wall_ms=%.1f read_ms=%.1f " + "compute_ms=%.1f x86_block_calls=%llu x86_dirty_upper_calls=%llu " + "xinuse_or=0x%llx", + ok, bytes, cbm_sha256_backend_name_for_testing(), + fingerprint_diag_now_ms() - diag->start_ms, diag->read_ms, diag->compute_ms, + (unsigned long long)calls, (unsigned long long)dirty, + (unsigned long long)xinuse_or); + fingerprint_diag_emit(line); +} + +/* At most once per process: hash `path` once, feeding every chunk to the + * selected backend and to the portable transform, so both are timed on the + * same bytes and the same CPU. The test runner calls it only in the + * daemon_bootstrap suite process, before any test runs (single-threaded), so + * it adds about one hash outside every deadline. */ +void cbm_daemon_fingerprint_diag_compare_for_testing(const char *path) { + if (!fingerprint_diag_on() || !path || + atomic_exchange_explicit(&g_fingerprint_diag_compared, true, memory_order_acq_rel)) { + return; + } + int fd = open(path, O_RDONLY | O_CLOEXEC); + struct stat status; + if (fd < 0 || fstat(fd, &status) != 0 || !S_ISREG(status.st_mode)) { + if (fd >= 0) { + (void)close(fd); + } + return; + } + const char *backend = cbm_sha256_backend_name_for_testing(); + cbm_sha256_ctx selected; + cbm_sha256_ctx portable; + cbm_sha256_init(&selected); + cbm_sha256_init(&portable); + unsigned char buffer[DAEMON_SERVICE_IO_CAP]; + double read_ms = 0.0; + double selected_ms = 0.0; + double portable_ms = 0.0; + double start_ms = fingerprint_diag_now_ms(); + off_t offset = 0; + bool ok = true; + cbm_sha256_diag_reset_for_testing(); + while (ok && offset < status.st_size) { + off_t remaining = status.st_size - offset; + size_t request = remaining < (off_t)sizeof(buffer) ? (size_t)remaining : sizeof(buffer); + double t0 = fingerprint_diag_now_ms(); + ssize_t count = pread(fd, buffer, request, offset); + double t1 = fingerprint_diag_now_ms(); + read_ms += t1 - t0; + if (count <= 0) { + ok = count < 0 && errno == EINTR; + continue; + } + cbm_sha256_update(&selected, buffer, (size_t)count); + double t2 = fingerprint_diag_now_ms(); + cbm_sha256_force_portable_for_testing(true); + cbm_sha256_update(&portable, buffer, (size_t)count); + cbm_sha256_force_portable_for_testing(false); + double t3 = fingerprint_diag_now_ms(); + selected_ms += t2 - t1; + portable_ms += t3 - t2; + offset += (off_t)count; + } + (void)close(fd); + uint8_t selected_digest[CBM_SHA256_DIGEST_LEN]; + uint8_t portable_digest[CBM_SHA256_DIGEST_LEN]; + cbm_sha256_final(&selected, selected_digest); + cbm_sha256_final(&portable, portable_digest); + uint64_t calls = 0; + uint64_t dirty = 0; + uint64_t xinuse_or = 0; + cbm_sha256_diag_take_for_testing(&calls, &dirty, &xinuse_or); + char line[512]; + (void)snprintf(line, sizeof(line), + "kind=compare ok=%d bytes=%lld backend=%s wall_ms=%.1f read_ms=%.1f " + "selected_compute_ms=%.1f portable_compute_ms=%.1f digests_match=%d " + "x86_block_calls=%llu x86_dirty_upper_calls=%llu xinuse_or=0x%llx", + ok, (long long)status.st_size, backend, fingerprint_diag_now_ms() - start_ms, + read_ms, selected_ms, portable_ms, + memcmp(selected_digest, portable_digest, sizeof(selected_digest)) == 0, + (unsigned long long)calls, (unsigned long long)dirty, + (unsigned long long)xinuse_or); + fingerprint_diag_emit(line); +} +#endif + bool cbm_daemon_build_fingerprint_native_file(uintptr_t native_file, char out[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]) { if (!out) { @@ -375,12 +548,26 @@ bool cbm_daemon_build_fingerprint_native_file(uintptr_t native_file, unsigned char buffer[DAEMON_SERVICE_IO_CAP]; off_t offset = 0; bool ok = true; +#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS + fingerprint_diag_t diag; /* TEMPORARY diagnostic (drop before merge) */ + fingerprint_diag_begin(&diag); +#endif while (offset < before.st_size) { off_t remaining = before.st_size - offset; size_t request = remaining < (off_t)sizeof(buffer) ? (size_t)remaining : sizeof(buffer); +#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS + double diag_read_start = fingerprint_diag_stamp(&diag); +#endif ssize_t count = pread(fd, buffer, request, offset); +#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS + double diag_read_end = fingerprint_diag_stamp(&diag); + diag.read_ms += diag_read_end - diag_read_start; +#endif if (count > 0) { cbm_sha256_update(&context, buffer, (size_t)count); +#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS + diag.compute_ms += fingerprint_diag_stamp(&diag) - diag_read_end; +#endif offset += (off_t)count; } else if (count == 0) { ok = false; @@ -394,6 +581,9 @@ bool cbm_daemon_build_fingerprint_native_file(uintptr_t native_file, ok = ok && fstat(fd, &after) == 0 && before.st_dev == after.st_dev && before.st_ino == after.st_ino && before.st_size == after.st_size && offset == after.st_size; +#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS + fingerprint_diag_end(&diag, (long long)before.st_size, ok); +#endif if (!ok) { return false; } @@ -1006,6 +1196,15 @@ static bool windows_log_append(const char *log_path, const char *record, size_t return ok; } +#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS +/* TEMPORARY diagnostic (drop before merge): POSIX-only; no-ops on Windows. */ +void cbm_daemon_fingerprint_diag_init_for_testing(void) {} + +void cbm_daemon_fingerprint_diag_compare_for_testing(const char *path) { + (void)path; +} +#endif + #endif /* _WIN32 */ bool cbm_daemon_conflict_log_append(const char *log_path, const cbm_daemon_conflict_t *conflict, diff --git a/src/daemon/service_internal.h b/src/daemon/service_internal.h index 750b72f7c..03eaff771 100644 --- a/src/daemon/service_internal.h +++ b/src/daemon/service_internal.h @@ -32,4 +32,14 @@ typedef void (*cbm_daemon_conflict_log_test_hook_fn)(void *context, void cbm_daemon_conflict_log_set_test_hook(cbm_daemon_conflict_log_test_hook_fn hook, void *context); +#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS +/* TEMPORARY diagnostic (drop before merge): read CBM_SHA256_DIAG_FILE once, + * before any thread starts; then every full-file fingerprint appends one + * "CBM-SHA256-DIAG kind=hash" line to it. The compare call (at most once per + * process) times the selected backend against the portable transform on the + * same bytes. POSIX only; no-ops on Windows. */ +void cbm_daemon_fingerprint_diag_init_for_testing(void); +void cbm_daemon_fingerprint_diag_compare_for_testing(const char *path); +#endif + #endif /* CBM_DAEMON_SERVICE_INTERNAL_H */ diff --git a/src/foundation/sha256.c b/src/foundation/sha256.c index 1f1b39391..7b3c66917 100644 --- a/src/foundation/sha256.c +++ b/src/foundation/sha256.c @@ -13,6 +13,9 @@ #include #include #include +#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS +#include /* TEMPORARY diagnostic (drop before merge): snprintf */ +#endif /* The ARM path needs a way to ask the OS whether the CPU has SHA2; on any * other aarch64 OS the portable transform is used. */ @@ -66,7 +69,30 @@ static const uint32_t K[64] = { #define SIG0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ ((x) >> 3)) #define SIG1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ ((x) >> 10)) -static void sha256_transform(cbm_sha256_ctx *c, const uint8_t *data) { +/* WHY no_sanitize: sanitized test runners fingerprint their own ~800 MB image + * (and copies of it) through this transform whenever the CPU has no SHA + * instructions. ASan + UBSan checks on these fixed-size rounds made one such + * hash 5.3x slower with GCC 13 on x86-64 (15.6 s vs 3.0 s measured; ~72-92 s + * on hosted runners without SHA-NI, past the 60 s copied-image exchange + * budget) and 2.4x slower on arm64, so a test verdict depended on which CPU + * the runner drew. Every access here is a compile-time-bounded index into the + * context or into one 64-byte block whose bounds the callers establish; + * cbm_sha256_update/final, the block loop and the hardware backends stay + * fully instrumented. Equality with the hardware backends is proven by + * cli_sha256_hardware_matches_portable_issue2441 and the NIST vectors. + * Release (unsanitized) builds are unaffected: the attribute only withholds + * instrumentation that -fsanitize would add. Spelled through a macro so the + * attribute sits on the signature line: an attribute on its own line between + * preprocessor guards is laid out differently by clang-format 20 (CI) and + * Homebrew's newer release (same reason as NO_BUILTIN_MEM in + * mem_override_libc.c). */ +#if defined(__clang__) || defined(__GNUC__) +#define SHA256_NO_SANITIZE __attribute__((no_sanitize("address", "undefined"))) +#else +#define SHA256_NO_SANITIZE +#endif + +SHA256_NO_SANITIZE static void sha256_transform(cbm_sha256_ctx *c, const uint8_t *data) { /* Scratch comes from the context, not this frame: a 256-byte local here * is fake-stacked by ASan's use-after-return mode on every 64-byte block * (see the note on cbm_sha256_ctx::sched). Identical values, allocated @@ -210,6 +236,55 @@ static bool sha256_cpu_has_x86(void) { } #endif +#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS +/* TEMPORARY diagnostic (drop before merge); see sha256.h. */ +static _Atomic bool g_sha256_diag = false; +static _Thread_local uint64_t g_sha256_diag_calls; +static _Thread_local uint64_t g_sha256_diag_dirty; +static _Thread_local uint64_t g_sha256_diag_xinuse_or; + +#if defined(SHA256_HW_X86) +static _Atomic int g_sha256_diag_xinuse_readable = -1; + +/* XGETBV with ECX=1 (XINUSE) needs OSXSAVE and CPUID.(0DH,1):EAX[2]. */ +static bool sha256_diag_xinuse_readable(void) { + int readable = atomic_load_explicit(&g_sha256_diag_xinuse_readable, memory_order_relaxed); + if (readable < 0) { + unsigned int eax = 0; + unsigned int ebx = 0; + unsigned int ecx = 0; + unsigned int edx = 0; + bool osxsave = __get_cpuid(1, &eax, &ebx, &ecx, &edx) && (ecx & (1U << 27)) != 0; + bool ecx1 = + osxsave && __get_cpuid_count(0xD, 1, &eax, &ebx, &ecx, &edx) && (eax & (1U << 2)) != 0; + readable = ecx1 ? 1 : 0; + atomic_store_explicit(&g_sha256_diag_xinuse_readable, readable, memory_order_relaxed); + } + return readable == 1; +} + +/* XINUSE bit 2 = AVX upper YMM, 5 = opmask, 6 = ZMM_Hi256, 7 = Hi16_ZMM: any of + * them in use when legacy-SSE code runs is the Intel SSE/AVX transition case. */ +static void sha256_diag_sample_x86(void) { + if (!atomic_load_explicit(&g_sha256_diag, memory_order_relaxed)) { + return; + } + g_sha256_diag_calls++; + if (!sha256_diag_xinuse_readable()) { + return; + } + uint32_t lo = 0; + uint32_t hi = 0; + __asm__ __volatile__("xgetbv" : "=a"(lo), "=d"(hi) : "c"(1U)); + uint64_t xinuse = ((uint64_t)hi << 32) | lo; + g_sha256_diag_xinuse_or |= xinuse; + if ((xinuse & 0xE4U) != 0) { + g_sha256_diag_dirty++; + } +} +#endif +#endif + typedef enum { SHA256_BACKEND_UNPROBED = 0, SHA256_BACKEND_PORTABLE, @@ -255,6 +330,9 @@ static void sha256_blocks(cbm_sha256_ctx *c, const uint8_t *data, size_t blocks) #endif #if defined(SHA256_HW_X86) case SHA256_BACKEND_X86: +#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS + sha256_diag_sample_x86(); /* TEMPORARY diagnostic (drop before merge) */ +#endif sha256_blocks_x86(c, data, blocks); return; #endif @@ -279,6 +357,88 @@ const char *cbm_sha256_backend_name_for_testing(void) { void cbm_sha256_force_portable_for_testing(bool force) { atomic_store_explicit(&g_sha256_force_portable, force, memory_order_relaxed); } + +/* TEMPORARY diagnostic (drop before merge); see sha256.h. */ +void cbm_sha256_diag_enable_for_testing(bool enable) { + atomic_store_explicit(&g_sha256_diag, enable, memory_order_relaxed); +} + +void cbm_sha256_diag_reset_for_testing(void) { + g_sha256_diag_calls = 0; + g_sha256_diag_dirty = 0; + g_sha256_diag_xinuse_or = 0; +} + +void cbm_sha256_diag_take_for_testing(uint64_t *x86_calls, uint64_t *dirty_upper_calls, + uint64_t *xinuse_or) { + if (x86_calls) { + *x86_calls = g_sha256_diag_calls; + } + if (dirty_upper_calls) { + *dirty_upper_calls = g_sha256_diag_dirty; + } + if (xinuse_or) { + *xinuse_or = g_sha256_diag_xinuse_or; + } +} + +/* SHA256RNDS2/MSG1/MSG2 have no VEX form at all; this names the encoding of + * the SSE shuffles and loads around them, fixed when this file was compiled. */ +#if defined(__AVX__) +#define SHA256_DIAG_SSE_ENCODING "vex" +#else +#define SHA256_DIAG_SSE_ENCODING "legacy-sse" +#endif + +void cbm_sha256_diag_cpu_for_testing(char *out, size_t cap) { + if (!out || cap == 0) { + return; + } +#if defined(SHA256_HW_X86) + unsigned int eax = 0; + unsigned int ebx = 0; + unsigned int ecx = 0; + unsigned int edx = 0; + char vendor[13] = "unknown"; + if (__get_cpuid(0, &eax, &ebx, &ecx, &edx)) { + memcpy(vendor, &ebx, 4); + memcpy(vendor + 4, &edx, 4); + memcpy(vendor + 8, &ecx, 4); + vendor[12] = '\0'; + } + unsigned int family = 0; + unsigned int model = 0; + unsigned int stepping = 0; + bool avx = false; + bool osxsave = false; + if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) { + unsigned int base_family = (eax >> 8) & 0xFU; + unsigned int base_model = (eax >> 4) & 0xFU; + family = base_family == 0xFU ? base_family + ((eax >> 20) & 0xFFU) : base_family; + model = base_family == 0x6U || base_family == 0xFU + ? (((eax >> 16) & 0xFU) << 4) | base_model + : base_model; + stepping = eax & 0xFU; + avx = (ecx & (1U << 28)) != 0; + osxsave = (ecx & (1U << 27)) != 0; + } + bool sha = false; + bool avx512f = false; + if (__get_cpuid_count(7, 0, &eax, &ebx, &ecx, &edx)) { + sha = (ebx & (1U << 29)) != 0; + avx512f = (ebx & (1U << 16)) != 0; + } + (void)snprintf(out, cap, + "cpu_vendor=%s family=0x%x model=0x%x stepping=%u cpuid_sha=%d avx=%d " + "avx512f=%d osxsave=%d xinuse_readable=%d shani_sse_encoding=%s", + vendor, family, model, stepping, sha, avx, avx512f, osxsave, + sha256_diag_xinuse_readable(), SHA256_DIAG_SSE_ENCODING); +#elif defined(SHA256_HW_ARM) + (void)snprintf(out, cap, "cpu_arch=arm64 cpu_sha2=%d", sha256_cpu_has_arm()); +#else + (void)snprintf(out, cap, "cpu_arch=other"); +#endif +} #endif void cbm_sha256_init(cbm_sha256_ctx *c) { diff --git a/src/foundation/sha256.h b/src/foundation/sha256.h index 23fb3b8c9..a3d3d3220 100644 --- a/src/foundation/sha256.h +++ b/src/foundation/sha256.h @@ -48,6 +48,21 @@ const char *cbm_sha256_backend_name_for_testing(void); /* Force the portable transform (true) or return to the probed backend (false), * so a test can hash the same input both ways. */ void cbm_sha256_force_portable_for_testing(bool force); + +/* TEMPORARY diagnostic (drop before merge): per-hash CPU facts for the hosted + * x86 runners whose full-image fingerprint is ~40x slower on Intel than on + * AMD. Enabled once per process (before any thread starts); when enabled, the + * x86 SHA-NI block path samples XINUSE (XGETBV ECX=1) before each call, per + * thread, to show whether dirty upper YMM/ZMM state precedes the legacy-SSE + * SHA-NI code. Reset and take refer to the calling thread's counters. */ +void cbm_sha256_diag_enable_for_testing(bool enable); +void cbm_sha256_diag_reset_for_testing(void); +void cbm_sha256_diag_take_for_testing(uint64_t *x86_calls, uint64_t *dirty_upper_calls, + uint64_t *xinuse_or); +/* One "key=value ..." summary of the CPU: vendor/family/model/stepping, the + * CPUID SHA/AVX/AVX-512F bits, XINUSE readability and the SSE encoding the + * SHA-NI path was compiled with. */ +void cbm_sha256_diag_cpu_for_testing(char *out, size_t cap); #endif #endif /* CBM_SHA256_H */ diff --git a/tests/test_daemon_runtime.c b/tests/test_daemon_runtime.c index c34c45f5b..666bb0d72 100644 --- a/tests/test_daemon_runtime.c +++ b/tests/test_daemon_runtime.c @@ -5019,6 +5019,8 @@ TEST(daemon_runtime_mac_fast_path_rejects_foreign_main_image_mapping_active) { ASSERT_TRUE(fingerprint_differs); ASSERT_TRUE(started); ASSERT_TRUE(foreign_ran); + /* 26 = the daemon answered with a rejection. A probe that got no answer + * inside the exchange budget exits 30 and must not pass here. */ ASSERT_EQ(foreign_exit, 26); PASS(); } @@ -5092,6 +5094,8 @@ TEST(daemon_runtime_copied_image_fallback_accepts_identical_and_rejects_changed) ASSERT_TRUE(changed_copied); ASSERT_TRUE(changed_bytes); ASSERT_TRUE(changed_ran); + /* 26 = the daemon answered with a rejection; no answer inside the + * exchange budget exits 30 and fails here instead of passing. */ ASSERT_EQ(changed_exit, 26); #endif PASS(); diff --git a/tests/test_main.c b/tests/test_main.c index 4b3716c50..f6dbbf441 100644 --- a/tests/test_main.c +++ b/tests/test_main.c @@ -24,6 +24,7 @@ int tf_deselected_count = 0; #include "daemon/runtime.h" /* bounded worker response probe */ #include "daemon/ipc.h" /* Windows private-lock re-exec probe */ #include "daemon/version_cohort.h" /* Windows crash-turnover re-exec probe */ +#include "daemon/service_internal.h" /* TEMPORARY sha256 diagnostic (drop before merge) */ #include "mcp/index_supervisor.h" /* cbm_index_set_worker_role */ #include "mcp/mcp.h" /* cbm_mcp_handle_tool — act as a real worker */ #include "ui/http_server.h" /* deleted-self executable probe */ @@ -711,7 +712,11 @@ static int tf_maybe_run_runtime_hello_client(int argc, char **argv) { !client || cbm_daemon_runtime_client_close(client, TF_RUNTIME_IMAGE_EXCHANGE_TIMEOUT_MS); cbm_daemon_ipc_endpoint_free(endpoint); if (!accepted) { - return 26; + /* 26 only when the daemon ANSWERED and refused. No answer inside the + * exchange budget (the daemon still hashing this copy, or I/O) is 30: + * a slow peer-image hash must fail loudly as itself, never satisfy a + * rejection assertion by timing out. */ + return result.status == CBM_DAEMON_RUNTIME_CONNECT_ERROR ? 30 : 26; } return closed ? 0 : 27; } @@ -741,7 +746,10 @@ static int tf_maybe_run_runtime_activation_client(int argc, char **argv) { endpoint, &identity, (cbm_daemon_runtime_activation_action_t)action_value, TF_RUNTIME_IMAGE_EXCHANGE_TIMEOUT_MS, &result); cbm_daemon_ipc_endpoint_free(endpoint); - return exchanged && result.accepted ? 0 : 29; + if (!exchanged) { + return 31; /* no answer inside the exchange budget; see the HELLO probe */ + } + return result.accepted ? 0 : 29; } /* macOS adversarial HELLO probe: execute this mode from a foreign process @@ -2264,6 +2272,9 @@ static void tf_msan_mi_output(const char *msg, void *arg) { #endif int main(int argc, char **argv) { + /* TEMPORARY diagnostic (drop before merge): read CBM_SHA256_DIAG_FILE here, + * before any role or thread, so exec'd roles record their hashes too. */ + cbm_daemon_fingerprint_diag_init_for_testing(); #ifdef TF_MSAN_MIMALLOC_OUTPUT mi_register_output(tf_msan_mi_output, NULL); tf_msan_mi_output_live = true; @@ -2390,6 +2401,15 @@ int main(int argc, char **argv) { } if (!g_list_only) { printf("\n codebase-memory-mcp C test suite\n"); + /* TEMPORARY diagnostic (drop before merge): once, in the daemon_bootstrap + * suite process only, before any test runs: time the selected SHA-256 + * backend against the portable transform on this runner's own image. */ + for (int i = 1; i < argc; i++) { + if (argv[i] && strcmp(argv[i], "daemon_bootstrap") == 0) { + cbm_daemon_fingerprint_diag_compare_for_testing(cbm_test_program_path); + break; + } + } } /* Runner isolation first: nothing below may reach the developer's daemon. */