Skip to content

Commit eccc615

Browse files
committed
test: verify sanitizer build configuration
Signed-off-by: astandrik <astandrik@yandex-team.ru>
1 parent def38f3 commit eccc615

3 files changed

Lines changed: 76 additions & 1 deletion

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 "$?"
@@ -304,6 +347,7 @@ BUILD_DIR="$BUILD_DIR" scripts/clean.sh
304347
# pass/fail/skip totals aggregate to the same numbers as the sequential run).
305348
# CBM_TEST_SEQUENTIAL=1 restores the single-process runner.
306349
make -j"$NPROC" -f Makefile.cbm "$BUILD_DIR/test-runner" ${MAKE_ARGS[@]+"${MAKE_ARGS[@]}"}
350+
assert_test_runner_build_config "$BUILD_DIR/test-runner" "$EXPECTED_SANITIZED"
307351
if [ "${CBM_TEST_SEQUENTIAL:-0}" = "1" ]; then
308352
make -f Makefile.cbm test ${MAKE_ARGS[@]+"${MAKE_ARGS[@]}"}
309353
else

‎tests/test_main.c‎

Lines changed: 10 additions & 0 deletions
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 "foundation/sanitized.h" /* CBM_SANITIZED — --build-config contract */
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 */
@@ -945,6 +946,15 @@ int main(int argc, char **argv) {
945946
(void)puts("codebase-memory-mcp test-runner");
946947
return 0;
947948
}
949+
if (argc == 2 && strcmp(argv[1], "--build-config") == 0) {
950+
#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
951+
const int test_seams = 1;
952+
#else
953+
const int test_seams = 0;
954+
#endif
955+
(void)printf("sanitized=%d test_seams=%d\n", CBM_SANITIZED, test_seams);
956+
return 0;
957+
}
948958
/* #1830 userns smoke probe -- see the test that spawns it.
949959
*
950960
* The ancestor overflow uid is derived ONCE per process (pthread_once in

0 commit comments

Comments
 (0)