Skip to content

Commit c059396

Browse files
fix(build): give sanitized-build detection one spelling and a backstop
Four places ask "is this binary instrumented?" and each asked it differently. One of them, the C# LSP bench, only recognised ASan, so TSan and MSan measured an instrumented parse against the NATIVE 200ms budget. The other three carried a hand-copied list of `__SANITIZE_*__` macros that nobody kept in sync. That drift is what the TSan gap was made of. CFLAGS_TSAN never passed SANITIZED_DEFINE, and the per-site conditions could not cover for it: they test `__SANITIZE_THREAD__`, which is GCC's spelling. Clang — the compiler that leg uses — announces thread instrumentation through `__has_feature(thread_sanitizer)` only, and no site consulted it. The claim in 0a163d4 that compiler probes could not have helped is true of the probes we had, not of the one clang actually offers. src/foundation/sanitized.h now answers the question once, as CBM_SANITIZED, from two sources with distinct jobs: - CBM_SANITIZED_BUILD from the build system stays the source of truth, and is the ONLY thing that can answer for UBSan and trap-UBSan: undefined-behaviour instrumentation leaves no macro and no __has_feature bit to probe. - The clang and GCC probes are the backstop for the three sanitizers that do announce themselves, so a lane that forgets the define still gets correct budgets instead of native ones on an instrumented binary. Deliberately no #error when a probe fires without the define: promoting leaves the binary correct while the lane gets fixed, and it does not break an out-of-tree `make CFLAGS_EXTRA=-fsanitize=address` that never went near Makefile.cbm. __has_feature is defined away where it does not exist rather than guarded with `#elif defined(__has_feature)`. The guarded form compiles everywhere but fails cppcheck, which walks every configuration and rejects the file with "failed to evaluate #if condition, undefined function-like macro invocation". The define-away idiom is what clang documents and what tests/test_mem.c already uses; `defined(...) && __has_feature(...)` is not an option at all, since && does not spare a preprocessor without the builtin from parsing `0 (0)`. Verified the resolution rather than assuming it (clang 22, -dM -E): native CBM_SANITIZED 0 -DCBM_SANITIZED_BUILD=1 CBM_SANITIZED 1 -fsanitize=address CBM_SANITIZED 1 -fsanitize=thread (linux target) CBM_SANITIZED 1 -fsanitize=memory (linux target) CBM_SANITIZED 1 -fsanitize=undefined CBM_SANITIZED 0 <- define-only, as designed The third row is the one that matters: the TSan failure this header is named after would have self-healed. Also wired the define into the instrumented flag sets of our own code that still lacked it — CXXFLAGS_TSAN (preprocessor.cpp is ours, and CXXFLAGS_TEST already had it), GRAMMAR_CFLAGS_TEST and GRAMMAR_CFLAGS_TSAN. Neither tree can include the header today (no -Isrc), so this is the build system keeping its own promise rather than a behaviour change. Vendored flag sets are untouched: mimalloc, sqlite3, tre, zstd, lz4 and tree-sitter read no macro of ours. Behaviour change worth naming: test_cs_lsp_bench now allows 2000ms on the TSan and MSan lanes instead of 200ms. It loosens a bound that was being applied to an instrumented binary by accident; it never tightens one. Not verified locally: this machine has no POSIX-target compiler, so the POSIX half of subprocess.c was not compiled here. The full Windows test-runner builds clean with -Werror and the subprocess suite is green (14 passed, 17 skipped); clang-format clean; macro matrix as above. Signed-off-by: Mauricio Offermann <mauricio.offermann@gocode.cl>
1 parent 973cde8 commit c059396

6 files changed

Lines changed: 82 additions & 13 deletions

File tree

‎Makefile.cbm‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,15 @@ TSAN_SANITIZE = -fsanitize=thread -fno-omit-frame-pointer
111111
# every sanitized-budget branch compiled to its NATIVE value on this leg —
112112
# exactly the failure the comment above SANITIZED_DEFINE describes for
113113
# trap-UBSan, repeated on a leg that was never wired up.
114+
#
115+
# The same define now goes on EVERY instrumented flag set of our own code, C and
116+
# C++ alike (CXXFLAGS_TSAN here, GRAMMAR_CFLAGS_TEST/TSAN below). Vendored flag
117+
# sets are left alone: mimalloc, sqlite3, tre, zstd, lz4 and tree-sitter read no
118+
# macro of ours.
114119
CFLAGS_TSAN = $(CFLAGS_COMMON) $(EDITOR_TEST_DEFINES) $(KOTLIN_DEDUP_TEST_DEFINE) \
115120
$(CALL_REFERENCE_LOOKUP_TEST_DEFINE) $(INCREMENTAL_TEST_DEFINE) \
116121
-DCBM_SANITIZED_BUILD=1 -g -O1 $(TSAN_SANITIZE)
117-
CXXFLAGS_TSAN = $(CXXFLAGS_COMMON) -g -O1 \
122+
CXXFLAGS_TSAN = $(CXXFLAGS_COMMON) -DCBM_SANITIZED_BUILD=1 -g -O1 \
118123
$(TSAN_SANITIZE)
119124

120125
# Windows needs ws2_32 (Winsock), psapi (GetProcessMemoryInfo), shell32
@@ -707,9 +712,9 @@ BUILD_DIR = build/c
707712
# Grammar + tree-sitter runtime: compiled without -Werror (upstream code has warnings)
708713
GRAMMAR_CFLAGS = -std=c11 -D_DEFAULT_SOURCE -O2 -w -I$(CBM_DIR) -I$(TS_INCLUDE) -I$(TS_SRC)
709714
GRAMMAR_CFLAGS_TEST = -std=c11 -D_DEFAULT_SOURCE -g -O1 -w -I$(CBM_DIR) -I$(TS_INCLUDE) -I$(TS_SRC) \
710-
$(SANITIZE)
715+
$(SANITIZED_DEFINE) $(SANITIZE)
711716
GRAMMAR_CFLAGS_TSAN = -std=c11 -D_DEFAULT_SOURCE -g -O1 -w -I$(CBM_DIR) -I$(TS_INCLUDE) -I$(TS_SRC) \
712-
$(TSAN_SANITIZE)
717+
-DCBM_SANITIZED_BUILD=1 $(TSAN_SANITIZE)
713718

714719
# Object files for grammars + ts_runtime + lsp_all + preprocessor
715720
GRAMMAR_OBJS_TEST = $(patsubst $(CBM_DIR)/%.c,$(BUILD_DIR)/%.o,$(GRAMMAR_SRCS))

‎src/foundation/compat_thread.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "foundation/compat_thread.h"
99

1010
#include "foundation/platform.h"
11+
#include "foundation/sanitized.h" /* CBM_SANITIZED — diagnostic stack floor */
1112

1213
#include <mimalloc.h> /* mi_thread_done at thread exit */
1314

@@ -31,7 +32,7 @@
3132
* exactly the threads that overflow. Diagnostic builds only: the shipping
3233
* binary keeps its fixed, predictable stack sizes. */
3334
static size_t cbm_thread_stack_floor(size_t requested) {
34-
#if defined(CBM_SANITIZED_BUILD) && CBM_SANITIZED_BUILD
35+
#if CBM_SANITIZED
3536
const char *env = getenv("CBM_THREAD_STACK_MB");
3637
if (env && env[0]) {
3738
char *end = NULL;

‎src/foundation/sanitized.h‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* sanitized.h — One spelling of "is this binary instrumented?".
3+
*
4+
* Timing budgets, retry windows and stack floors all have to widen under a
5+
* sanitizer, so several places need to ask this question. Before this header
6+
* they each asked it differently — four sites, four spellings, one of which
7+
* (the C# LSP bench) only recognised ASan and therefore ran a NATIVE 200ms
8+
* budget on a ThreadSanitizer binary. Ask it here instead.
9+
*
10+
* The two sources are deliberate and neither replaces the other:
11+
*
12+
* CBM_SANITIZED_BUILD — set by the build system (Makefile.cbm), and the ONLY
13+
* source that can answer for UBSan and trap-UBSan: undefined-behaviour
14+
* instrumentation leaves no macro and no __has_feature bit behind, so no
15+
* probe can see it. This is why the build system stays the source of truth.
16+
*
17+
* Compiler probes — the backstop for the three sanitizers that DO announce
18+
* themselves, for when a new build lane forgets to pass the define. That is
19+
* not hypothetical: CFLAGS_TSAN never included SANITIZED_DEFINE (it keys off
20+
* $(SANITIZE), which TSan does not use), so every sanitized budget compiled
21+
* to its native value on the one leg they were written for, and
22+
* `subprocess_run_spawn_failure` failed on the very PR meant to fix it.
23+
*
24+
* Both clang and GCC spellings are listed because they disagree: clang reports
25+
* thread and memory instrumentation through __has_feature only, GCC through
26+
* __SANITIZE_*__ only, and __SANITIZE_THREAD__ alone — which is what the old
27+
* per-site conditions used — is invisible on the clang lane that broke.
28+
*
29+
* Deliberately no #error when a probe fires without the define. Promoting
30+
* leaves the binary CORRECT while the build lane is fixed, whereas an error
31+
* would break it, and would also break an out-of-tree
32+
* `make CFLAGS_EXTRA=-fsanitize=address` that never went near Makefile.cbm.
33+
*/
34+
#ifndef CBM_SANITIZED_H
35+
#define CBM_SANITIZED_H
36+
37+
/* Define __has_feature away where it does not exist. `defined(__has_feature) &&
38+
* __has_feature(...)` is NOT a substitute: && does not spare a preprocessor
39+
* that lacks the builtin from parsing the call, and there `__has_feature(x)`
40+
* becomes `0 (0)` — a syntax error rather than a 0. Nesting the arm under
41+
* `#elif defined(__has_feature)` compiles everywhere but breaks cppcheck, which
42+
* walks every configuration and rejects the file outright with "failed to
43+
* evaluate #if condition, undefined function-like macro invocation". This is
44+
* the idiom clang documents, and the one tests/test_mem.c already uses. */
45+
#ifndef __has_feature
46+
#define __has_feature(x) 0
47+
#endif
48+
49+
#if defined(CBM_SANITIZED_BUILD) && CBM_SANITIZED_BUILD
50+
#define CBM_SANITIZED 1
51+
#elif __has_feature(address_sanitizer) || __has_feature(thread_sanitizer) || \
52+
__has_feature(memory_sanitizer)
53+
#define CBM_SANITIZED 1
54+
#elif defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_THREAD__)
55+
#define CBM_SANITIZED 1
56+
#else
57+
#define CBM_SANITIZED 0
58+
#endif
59+
60+
#endif /* CBM_SANITIZED_H */

‎src/foundation/subprocess.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#include "compat.h" /* cbm_nanosleep */
99
#include "compat_fs.h"
1010
#include "log.h"
11-
#include "platform.h" /* cbm_now_ms */
11+
#include "platform.h" /* cbm_now_ms */
12+
#include "sanitized.h" /* CBM_SANITIZED — spawn-retry budget */
1213

1314
#include <stdio.h>
1415
#include <stdatomic.h>
@@ -902,8 +903,7 @@ static cbm_proc_poll_t cbm_subprocess_poll_win(cbm_subprocess_t *process, cbm_pr
902903
* hang for seconds. So the extra patience is scoped to the builds that need it,
903904
* the same way the daemon announce backstop is (test_daemon_frontend.c). Three
904905
* more doublings take the sanitized ceiling to roughly 5s. */
905-
#if defined(CBM_SANITIZED_BUILD) || defined(__SANITIZE_ADDRESS__) || \
906-
defined(__SANITIZE_MEMORY__) || defined(__SANITIZE_THREAD__)
906+
#if CBM_SANITIZED
907907
enum { CBM_SPAWN_RETRY = 2, CBM_SPAWN_RETRY_ATTEMPTS = 9 };
908908
#else
909909
enum { CBM_SPAWN_RETRY = 2, CBM_SPAWN_RETRY_ATTEMPTS = 6 };

‎tests/test_cs_lsp_bench.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*/
2929
#include "test_framework.h"
3030
#include "cbm.h"
31+
#include "foundation/sanitized.h"
3132
#include "lsp/cs_lsp.h"
3233
#include <stdlib.h>
3334
#include <time.h>
@@ -235,10 +236,12 @@ TEST(cslsp_bench_resolution_ratio) {
235236
ASSERT_GTE(resolved * 100, calls * 45);
236237
}
237238

238-
/* Time budget. ASan+UBSan instrumentation slows the parse ~5-10×, so
239-
* scale the budget when a sanitizer is active. Native: 200 ms for a
240-
* ~260-line fixture; sanitized: 2000 ms. */
241-
#if defined(CBM_SANITIZED_BUILD) || defined(__SANITIZE_ADDRESS__)
239+
/* Time budget. Instrumentation slows the parse ~5-10×, so scale the budget
240+
* when a sanitizer is active. Native: 200 ms for a ~260-line fixture;
241+
* sanitized: 2000 ms. The condition used to name ASan specifically, which
242+
* left the TSan and MSan lanes measuring an instrumented parse against the
243+
* native 200 ms. */
244+
#if CBM_SANITIZED
242245
ASSERT(ms < 2000.0);
243246
#else
244247
ASSERT(ms < 200.0);

‎tests/test_daemon_frontend.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "foundation/compat.h"
1212
#include "foundation/compat_thread.h"
1313
#include "foundation/platform.h"
14+
#include "foundation/sanitized.h"
1415

1516
#include <stdint.h>
1617
#include <stdatomic.h>
@@ -842,8 +843,7 @@ static bool frontend_backpressure_run_isolated(bool maintenance) {
842843
* MSan stayed green. Widening the backstop for sanitized builds costs
843844
* nothing when the daemon is healthy: a passing run returns as soon as the
844845
* byte arrives, whatever the ceiling is. */
845-
#if defined(CBM_SANITIZED_BUILD) || defined(__SANITIZE_ADDRESS__) || \
846-
defined(__SANITIZE_MEMORY__) || defined(__SANITIZE_THREAD__)
846+
#if CBM_SANITIZED
847847
const uint64_t announce_backstop_ms = 180000U;
848848
#else
849849
const uint64_t announce_backstop_ms = 30000U;

0 commit comments

Comments
 (0)