Skip to content

Commit 11b662f

Browse files
authored
Merge pull request DeusData#1716 from astandrik/codex/fix-1648-sanitized-build-config
test: verify sanitizer build configuration
2 parents 4b0b5f2 + ff127ea commit 11b662f

3 files changed

Lines changed: 99 additions & 12 deletions

File tree

‎scripts/msan.sh‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,27 @@ ulimit -s 262144 2>/dev/null || true
7979
export CBM_THREAD_STACK_MB="${CBM_THREAD_STACK_MB:-256}"
8080
export LD_LIBRARY_PATH="$MSAN_PREFIX/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
8181

82+
expected_build_config=$'sanitized=1 test_seams=1\n'
83+
build_config_marker="__cbm_build_config_end__"
84+
captured_build_config=""
85+
actual_build_config=""
86+
build_config_probe_ok=0
87+
if captured_build_config="$(./build/msan/test-runner --build-config && printf '%s' "$build_config_marker")"; then
88+
build_config_probe_ok=1
89+
actual_build_config="${captured_build_config%"$build_config_marker"}"
90+
else
91+
actual_build_config="$captured_build_config"
92+
fi
93+
if [ "$build_config_probe_ok" -ne 1 ] ||
94+
[ "$actual_build_config" != "$expected_build_config" ]; then
95+
reported_build_config="${actual_build_config//$'\n'/\\n}"
96+
printf '%s\n' 'ERROR: build config mismatch for build/msan/test-runner' >&2
97+
printf '%s\n' ' expected: sanitized=1 test_seams=1' >&2
98+
printf ' reported: %s\n' "${reported_build_config:-<empty>}" >&2
99+
printf '%s\n' 'Refusing to run suites; check sanitizer flags and CBM_SANITIZED_BUILD wiring.' >&2
100+
exit 1
101+
fi
102+
82103
echo "=== MSan lane: $(clang --version | head -1) ==="
83104

84105
# KNOWN RED — THREE DISTINCT CAUSES, whitelisted per cause (O10)

‎scripts/test.sh‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ source "$ROOT/scripts/path-safety.sh"
145145
MAKE_ARGS=()
146146
BUILD_DIR="build/c"
147147
SANITIZE_GIVEN=0
148+
SANITIZE_VALUE=""
148149
prev_arg=""
149150
for arg in "$@"; do
150151
case "$arg" in
@@ -154,7 +155,11 @@ for arg in "$@"; do
154155
--tsan) ;; # already handled
155156
--suites|--suites=*) ;; # already handled (value skipped via prev_arg below)
156157
BUILD_DIR=*) BUILD_DIR="${arg#BUILD_DIR=}"; MAKE_ARGS+=("$arg") ;;
157-
SANITIZE=*) SANITIZE_GIVEN=1; MAKE_ARGS+=("$arg") ;;
158+
SANITIZE=*)
159+
SANITIZE_GIVEN=1
160+
SANITIZE_VALUE="${arg#SANITIZE=}"
161+
MAKE_ARGS+=("$arg")
162+
;;
158163
*=*)
159164
if [[ "${prev_arg:-}" != "--suites" ]]; then
160165
MAKE_ARGS+=("$arg") # forward any VAR=VAL to make
@@ -174,6 +179,42 @@ if [ "$SANITIZE_GIVEN" -eq 0 ] && [ "${MSYSTEM:-}" = "CLANGARM64" ]; then
174179
MAKE_ARGS+=("SANITIZE=-fsanitize=undefined -fsanitize-trap=undefined -fstack-protector-strong -fno-omit-frame-pointer")
175180
fi
176181

182+
EXPECTED_SANITIZED=1
183+
if [ "$SANITIZE_GIVEN" -eq 1 ]; then
184+
case "$SANITIZE_VALUE" in
185+
*[![:space:]]*) EXPECTED_SANITIZED=1 ;;
186+
*) EXPECTED_SANITIZED=0 ;;
187+
esac
188+
fi
189+
190+
# Refuse to run suites when the built runner disagrees with the lane that
191+
# produced it. Exact output keeps missing, duplicate, or future unhandled
192+
# fields fail-closed instead of silently weakening the gate.
193+
assert_test_runner_build_config() {
194+
local runner="$1"
195+
local expected_sanitized="$2"
196+
local expected="sanitized=$expected_sanitized test_seams=1"
197+
local marker="__cbm_build_config_end__"
198+
local captured=""
199+
local actual=""
200+
local probe_ok=0
201+
202+
if captured="$("$runner" --build-config && printf '%s' "$marker")"; then
203+
probe_ok=1
204+
actual="${captured%"$marker"}"
205+
else
206+
actual="$captured"
207+
fi
208+
if [ "$probe_ok" -ne 1 ] || [ "$actual" != "$expected"$'\n' ]; then
209+
local reported="${actual//$'\n'/\\n}"
210+
printf 'ERROR: build config mismatch for %s\n' "$runner" >&2
211+
printf ' expected: %s\n' "$expected" >&2
212+
printf ' reported: %s\n' "${reported:-<empty>}" >&2
213+
printf '%s\n' 'Refusing to run suites; check sanitizer flags and CBM_SANITIZED_BUILD wiring.' >&2
214+
return 1
215+
fi
216+
}
217+
177218
print_env "test.sh"
178219

179220
# ── TSan mode (--tsan): the data-race gate ──
@@ -182,6 +223,7 @@ print_env "test.sh"
182223
if [ "$TSAN" -eq 1 ]; then
183224
echo "=== test.sh: TSan leg (make test-tsan) ==="
184225
make -j"$NPROC" -f Makefile.cbm "$BUILD_DIR/test-runner-tsan" ${MAKE_ARGS[@]+"${MAKE_ARGS[@]}"}
226+
assert_test_runner_build_config "$BUILD_DIR/test-runner-tsan" 1
185227
make -f Makefile.cbm test-tsan ${MAKE_ARGS[@]+"${MAKE_ARGS[@]}"}
186228
exit "$?"
187229
fi
@@ -192,6 +234,7 @@ fi
192234
if [ -n "$SUITES" ]; then
193235
echo "=== test.sh: ITERATION mode — suites: $SUITES (incremental build) ==="
194236
make -j"$NPROC" -f Makefile.cbm "$BUILD_DIR/test-runner" ${MAKE_ARGS[@]+"${MAKE_ARGS[@]}"}
237+
assert_test_runner_build_config "$BUILD_DIR/test-runner" "$EXPECTED_SANITIZED"
195238
# shellcheck disable=SC2086 # suite list is deliberately word-split
196239
"$BUILD_DIR/test-runner" $SUITES
197240
exit "$?"
@@ -307,6 +350,7 @@ BUILD_DIR="$BUILD_DIR" scripts/clean.sh
307350
# pass/fail/skip totals aggregate to the same numbers as the sequential run).
308351
# CBM_TEST_SEQUENTIAL=1 restores the single-process runner.
309352
make -j"$NPROC" -f Makefile.cbm "$BUILD_DIR/test-runner" ${MAKE_ARGS[@]+"${MAKE_ARGS[@]}"}
353+
assert_test_runner_build_config "$BUILD_DIR/test-runner" "$EXPECTED_SANITIZED"
310354
if [ "${CBM_TEST_SEQUENTIAL:-0}" = "1" ]; then
311355
make -f Makefile.cbm test ${MAKE_ARGS[@]+"${MAKE_ARGS[@]}"}
312356
else

‎tests/test_main.c‎

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ int tf_skip_count = 0;
3333
#include <string.h>
3434
#include <signal.h>
3535
#ifdef _WIN32
36+
#include <fcntl.h>
37+
#include <io.h>
3638
#include <winsock2.h> /* #798 follow-up: socket-isolation re-exec probe */
3739
#include <windows.h>
3840
#else
@@ -921,17 +923,6 @@ extern void suite_dump_verify_io(void);
921923
extern void cbm_kind_in_set_free_cache(void);
922924

923925
int main(int argc, char **argv) {
924-
/* Skip the multi-hundred-MB executable-image hash that computes the exact
925-
* build fingerprint: it is tens of seconds per spawned worker/daemon under
926-
* ASan on constrained CI runners and the sole cause of the daemon-family
927-
* readiness-timeout flakes. Set once here; every forked child and re-exec'd
928-
* worker inherits it, so exact-build match/mismatch still works (a
929-
* mismatch test still passes a DIFFERENT fingerprint via argv). Honoured
930-
* only under CBM_CLI_ENABLE_TEST_API — never in a production binary. */
931-
if (!getenv("CBM_TEST_BUILD_FINGERPRINT")) {
932-
(void)cbm_setenv("CBM_TEST_BUILD_FINGERPRINT",
933-
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", 1);
934-
}
935926
int memory_limit_probe_rc = tf_maybe_run_windows_memory_limit_probe(argc, argv);
936927
if (memory_limit_probe_rc >= 0) {
937928
return memory_limit_probe_rc;
@@ -946,6 +937,37 @@ int main(int argc, char **argv) {
946937
(void)puts("codebase-memory-mcp test-runner");
947938
return 0;
948939
}
940+
if (argc == 2 && strcmp(argv[1], "--build-config") == 0) {
941+
#ifdef _WIN32
942+
if (_setmode(cbm_fileno(stdout), _O_BINARY) == -1) {
943+
fprintf(stderr, "failed to set build-config stdout to binary mode\n");
944+
return 2;
945+
}
946+
#endif
947+
#if defined(CBM_SANITIZED_BUILD) && CBM_SANITIZED_BUILD
948+
const int sanitized = 1;
949+
#else
950+
const int sanitized = 0;
951+
#endif
952+
#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
953+
const int test_seams = 1;
954+
#else
955+
const int test_seams = 0;
956+
#endif
957+
(void)printf("sanitized=%d test_seams=%d\n", sanitized, test_seams);
958+
return 0;
959+
}
960+
/* Skip the multi-hundred-MB executable-image hash that computes the exact
961+
* build fingerprint: it is tens of seconds per spawned worker/daemon under
962+
* ASan on constrained CI runners and the sole cause of the daemon-family
963+
* readiness-timeout flakes. Set once here; every forked child and re-exec'd
964+
* worker inherits it, so exact-build match/mismatch still works (a
965+
* mismatch test still passes a DIFFERENT fingerprint via argv). Honoured
966+
* only under CBM_CLI_ENABLE_TEST_API — never in a production binary. */
967+
if (!getenv("CBM_TEST_BUILD_FINGERPRINT")) {
968+
(void)cbm_setenv("CBM_TEST_BUILD_FINGERPRINT",
969+
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", 1);
970+
}
949971
/* #1830 userns smoke probe -- see the test that spawns it.
950972
*
951973
* The ancestor overflow uid is derived ONCE per process (pthread_once in

0 commit comments

Comments
 (0)