#!/usr/bin/env bash # ThreadSanitizer (ADR 0004, Revision 5): the runtime's unit tests, then # native programs on several threads, with the runtime and Rust's standard # library instrumented. It needs a nightly toolchain with rust-src, and # clang. On the forge, .toboggan/workflows/tsan.yml runs it; locally, # run it alone or with `LOUISE_CI_TSAN=1 scripts/ci.sh`. # # - The runtime is built with `-Zsanitizer=thread -Zbuild-std` and # `--cfg louise_rt_tsan`, which announces fiber switches to TSan (fiber.rs), # and without mimalloc, whose C code is not instrumented. # - Generated C is built with `-fsanitize=thread`, but without TSan's # function entry/exit hooks: they do not pair up across `musttail` calls, # so an endless tail-recursive loop overflows TSan's shadow call stack. # Accesses are still checked; reports show fewer C frames. # - `-g1`, not `-g`: full debug information makes clang's # LiveDebugVariables pass use memory quadratic in a function's size — # msgpack.lse's 13,000-line main needed 6.5 GB, and the forge's OOM # killer stopped clang, which said nothing (the empty "the C compiler # failed:"). Line tables alone still give a report file:line through # the generated `#line` map. # - scripts/tsan.supp lists the suppressions, each with its reason. # - TSan's shadow call stack holds 64Ki frames per fiber, without a bounds # check: deeper recursion through instrumented (runtime) frames silently # corrupts TSan's own memory, which shows as CHECK failures in its # allocator, bogus reports or hangs. Programs that recurse that deep are # skipped (SKIP, with the reason); ASan and the differential tests still # run them. set -euo pipefail cd "$(dirname "$0")/.." toolchain="${LOUISE_TSAN_TOOLCHAIN:-nightly-2026-09-20}" threads="${LOUISE_THREADS:-4}" root="$PWD" target="$root/target/tsan" out="$root/target/tsan-programs" mkdir -p "$out" "$root/target/tmp" # Builds write large temporary files: keep them off a small /tmp. export TMPDIR="$root/target/tmp" export TSAN_OPTIONS="suppressions=$root/scripts/tsan.supp ${TSAN_OPTIONS:-}" tsan_rust() { RUSTFLAGS="-Zsanitizer=thread --cfg louise_rt_tsan" CARGO_TARGET_DIR="$target" \ cargo "+$toolchain" "$@" -Zbuild-std --target x86_64-unknown-linux-gnu \ -p louise_rt --no-default-features --features stats,crypto,tls } echo "tsan: runtime unit tests" tsan_rust test --quiet --lib tsan_rust build --quiet cargo build --quiet --release -p louise export LOUISE_LIB_DIR="$target/x86_64-unknown-linux-gnu/debug" export LOUISE_CC=clang export LOUISE_CFLAGS="-O1 -g1 -fsanitize=thread -mllvm -tsan-instrument-func-entry-exit=0" # generators.lse: `total` handles 20,000 yields with a non-tail # `x + resume(())`, about 80,000 instrumented runtime frames deep. SKIP=(generators) failed=0 check() { # program threads local program="$1" n="$2" name expected code name="$(basename "$program" .lse)" if [[ " ${SKIP[*]} " == *" $name "* ]]; then echo "tsan: $name skipped (recursion deeper than TSan's shadow stack)" return fi (cd "$(dirname "$program")" && "$root/target/release/louise" build "$program" -o "$out/$name") >/dev/null expected=0 if grep -q '^//! panic:' "$program"; then expected=101; fi code=0 LOUISE_LEAKS=1 LOUISE_THREADS="$n" timeout 600 "$out/$name" >/dev/null 2>"$out/$name.err" || code=$? if grep -q 'WARNING: ThreadSanitizer' "$out/$name.err" || [ "$code" != "$expected" ] \ || ! grep -q '^louise: live blocks: 0$' "$out/$name.err"; then echo "tsan: $name ($n threads): exit $code (expected $expected)" head -60 "$out/$name.err" failed=1 else echo "tsan: $name ok ($n threads)" fi } for program in tests/run/*.lse; do check "$root/$program" "$threads"; done for program in tests/stress/*.lse tests/bench/messages.lse tests/bench/fanin.lse tests/bench/timers.lse; do check "$root/$program" 8 done # Distribution end to end: two processes, the transport's threads and # TLS, built against the TSan runtime as the programs above (the leak # oracle and exit codes are the tests'; TSan's report exits 66). echo "tsan: distribution end to end" LOUISE_THREADS="$threads" cargo test --quiet -p louise --test dist || failed=1 if [ "$failed" != 0 ]; then echo "tsan: failures"; exit 1; fi echo "tsan: no reports" # The instrumented-std target dir is peak disk on a runner, not a cache. [ "${CI:-}" = true ] && rm -rf "$target" || true