|
| 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 */ |
0 commit comments