skip to content

How would you design the CI sanitizer matrix for a large multithreaded C++ server built with GCC, and what does each job cost?

level: principalimportance: should knowfreq 38%

answer

  1. one question per job
  2. thread stands alone
  3. fail on the first report
  4. 2x versus 5x-15x
  5. sanitizers and -Werror do not mix

basics

~20 s

Separate jobs, because thread cannot share a build with address or leak: ASan with UBSan and leak checks on every change, TSan on the concurrency tests, a hardened release build, all failing on the first report. Run the slow suites nightly.

solid answer

~40 s

I would run one job per question. **ASan+UBSan** (`-fsanitize=address,undefined -fno-sanitize-recover=all`, leak detection on) on every merge request, because it finds the most bugs per CPU-minute at about 2x. **TSan** on the concurrency and integration tests, on every change if the suite is small, nightly if not, since it is about 5x-15x slower and cannot share a build with ASan or LSan. A **`-fhardened` release build** runs the normal suite, so ship-time checks are tested too. All sanitizer jobs fail on the first report, use `-g -O1`, skip `-Werror` (GCC's manual recommends against combining them), and keep suppression files reviewed like code. The trade-offs to own are CI cost, flaky TSan reports in uninstrumented code, and who triages a red nightly.

code

bash · 9 lines
bash
# Memory job: fails on the first ASan or UBSan error
g++ -std=c++23 -g -O1 -fno-omit-frame-pointer \
    -fsanitize=address,undefined -fno-sanitize-recover=all \
    tests/*.cpp src/*.cpp -o tests_asan
./tests_asan

# Race job: its own build, because thread cannot join address or leak
g++ -std=c++23 -g -O1 -fsanitize=thread tests/*.cpp src/*.cpp -o tests_tsan
TSAN_OPTIONS=halt_on_error=1 ./tests_tsan

go deeper

for a junior

Know that different sanitizers run as different builds in CI and that a report should fail the job.

for a middle

Explain which sanitizers can share a build, and why UBSan needs -fno-sanitize-recover to fail a CI job.

for a senior

Lay out the jobs, flags and cadence, keep -Werror and sanitizers apart, and handle uninstrumented dependencies and suppressions.

for a principal

Own the trade-off between detection latency and CI cost, the triage ownership, and whether Clang-only sanitizers justify a second compiler.

## The decision Sanitizers find bugs tests alone miss, but they cost build time, CI machines and triage effort, and they cannot all run in one binary. A lead has to decide **which sanitizers run, on which tests, how often, and what a report does to the pipeline**. There is no single right matrix; it depends on the codebase's concurrency, its test suite size and the team's tolerance for slow CI. ## Constraints from GCC - `-fsanitize=thread` "cannot be combined with `-fsanitize=address`, `-fsanitize=leak`", and ASan cannot combine with `hwaddress`. - UBSan's recovery is on by default; without `-fno-sanitize-recover`, the exit code "may indicate success even when errors have been reported". - The manual warns that sanitizers raise false-positive warnings, "most notably those around `-Wmaybe-uninitialized`", and recommends against combining `-Werror` with them. - GCC has no MemorySanitizer; uninitialized-read detection needs Clang's `-fsanitize=memory`, or the zeroing in `-ftrivial-auto-var-init` as a mitigation rather than a detector. ## A starting matrix | Job | Flags | Tests | Cadence | Cost (Clang docs) | |---|---|---|---|---| | Memory | `-fsanitize=address,undefined -fno-sanitize-recover=all -g -O1 -fno-omit-frame-pointer` | unit + integration | every change | ASan about 2x; UBSan small | | Races | `-fsanitize=thread -g -O1` | concurrency + load replay | every change if small, else nightly | 5x-15x time, 5x-10x memory | | Hardened | `-O2 -fhardened` | full suite | every change | small | | Warnings | `-O2 -Wall -Wextra -Werror` | none (build only) | every change | compile only | ## Policies that make it work 1. **Fail on the first report.** `-fno-sanitize-recover=all` for UBSan; ASan already halts by default; set `halt_on_error=1` in `TSAN_OPTIONS` if the team wants TSan to stop at once rather than report and continue. 2. **Keep `-Werror` out of sanitizer jobs** and run the warning gate in its own optimized build. 3. **Instrument the whole program.** Clang's TSan docs warn that pre-compiled, uninstrumented libraries can hide races or cause false positives; build third-party dependencies with the same flags where possible. 4. **Treat suppression files as code**: each entry has an owner, a reason and a review date. 5. **Name a triage owner** for nightly failures, or red nightlies become background noise. ## The trade-offs to own - **Coverage versus CI minutes.** Running TSan on every change catches races at the commit that introduced them, but its slowdown can double pipeline time. Nightly runs are cheaper and make bisecting harder. - **Signal versus noise.** Uninstrumented code makes TSan noisy; a team that ignores red TSan jobs gets less value than one with a smaller, trusted job. - **Compiler coverage.** If part of the fleet builds with Clang, adding a Clang MemorySanitizer job covers uninitialized reads that no GCC job can find, at the price of instrumenting every dependency. - **Ship-time protection.** Sanitizers do not run in production. A hardened release build is what protects users; testing it in CI catches assertion failures before customers do. ## Signs the matrix needs to change - A production crash that a sanitizer job could have caught but did not run on that code path: widen the tests in the matrix. - TSan jobs regularly timing out: split them or move to nightly with a named owner. - Suppression files growing faster than bugs are fixed: stop and burn them down.

  • Why not run every sanitizer on every change?
    Because ThreadSanitizer cannot share a build with AddressSanitizer or LeakSanitizer, every extra sanitizer is another full build and test run, and TSan alone is documented at about 5x-15x slower. Many teams run the cheap ASan+UBSan job on every change and TSan on the concurrency tests or nightly, accepting later detection for a faster pipeline.
  • How do you keep a sanitizer job trusted?
    Make it fail on the first report, instrument dependencies so reports are real, and keep suppressions small, owned and reviewed. A job that is often red for reasons nobody fixes trains the team to ignore it, which is worse than a smaller job that is always taken seriously.

  • One build with every sanitizer flag covers the whole matrix
  • Add -Werror to sanitizer builds to keep them strict
  • UBSan jobs fail automatically on any report
  • GCC's sanitizers include one for uninitialized reads
  • Sanitizer jobs make a hardened release build unnecessary