| @@ -0,0 +1,64 @@ |
| 1 | +#!/usr/bin/env bash |
| 2 | +# ThreadSanitizer (ADR 0004, Revision 4): the runtime's unit tests, then |
| 3 | +# native programs on several threads, with the runtime and Rust's standard |
| 4 | +# library instrumented. Opt-in in CI (`TILC_CI_TSAN=1 scripts/ci.sh`): it |
| 5 | +# needs a nightly toolchain with rust-src, and clang. |
| 6 | +# |
| 7 | +# - The runtime is built with `-Zsanitizer=thread -Zbuild-std` and |
| 8 | +# `--cfg tilrt_tsan`, which announces fiber switches to TSan (fiber.rs), |
| 9 | +# and without mimalloc, whose C code is not instrumented. |
| 10 | +# - Generated C is built with `-fsanitize=thread`, but without TSan's |
| 11 | +# function entry/exit hooks: they do not pair up across `musttail` calls, |
| 12 | +# so an endless tail-recursive loop overflows TSan's shadow call stack. |
| 13 | +# Accesses are still checked; reports show fewer C frames. |
| 14 | +# - scripts/tsan.supp lists the suppressions, each with its reason. |
| 15 | +set -euo pipefail |
| 16 | +cd "$(dirname "$0")/.." |
| 17 | + |
| 18 | +toolchain="${TILC_TSAN_TOOLCHAIN:-nightly-2026-09-20}" |
| 19 | +threads="${TILC_THREADS:-4}" |
| 20 | +root="$PWD" |
| 21 | +target="$root/target/tsan" |
| 22 | +out="$root/target/tsan-programs" |
| 23 | +mkdir -p "$out" "$root/target/tmp" |
| 24 | +# Builds write large temporary files: keep them off a small /tmp. |
| 25 | +export TMPDIR="$root/target/tmp" |
| 26 | +export TSAN_OPTIONS="suppressions=$root/scripts/tsan.supp ${TSAN_OPTIONS:-}" |
| 27 | +tsan_rust() { |
| 28 | + RUSTFLAGS="-Zsanitizer=thread --cfg tilrt_tsan" CARGO_TARGET_DIR="$target" \ |
| 29 | + cargo "+$toolchain" "$@" -Zbuild-std --target x86_64-unknown-linux-gnu \ |
| 30 | + -p tilrt --no-default-features --features stats |
| 31 | +} |
| 32 | + |
| 33 | +echo "tsan: runtime unit tests" |
| 34 | +tsan_rust test --quiet --lib |
| 35 | +tsan_rust build --quiet |
| 36 | +cargo build --quiet --release -p tilc |
| 37 | + |
| 38 | +export TILRT_LIB_DIR="$target/x86_64-unknown-linux-gnu/debug" |
| 39 | +export TILC_CC=clang |
| 40 | +export TILC_CFLAGS="-O1 -g -fsanitize=thread -mllvm -tsan-instrument-func-entry-exit=0" |
| 41 | +failed=0 |
| 42 | +check() { # program threads |
| 43 | + local program="$1" n="$2" name expected code |
| 44 | + name="$(basename "$program" .til)" |
| 45 | + (cd "$(dirname "$program")" && "$root/target/release/tilc" build "$program" -o "$out/$name") >/dev/null |
| 46 | + expected=0 |
| 47 | + if grep -q '^//! panic:' "$program"; then expected=101; fi |
| 48 | + code=0 |
| 49 | + TILRT_LEAKS=1 TILC_THREADS="$n" timeout 600 "$out/$name" >/dev/null 2>"$out/$name.err" || code=$? |
| 50 | + if grep -q 'WARNING: ThreadSanitizer' "$out/$name.err" || [ "$code" != "$expected" ] \ |
| 51 | + || ! grep -q '^tilrt: live blocks: 0$' "$out/$name.err"; then |
| 52 | + echo "tsan: $name ($n threads): exit $code (expected $expected)" |
| 53 | + head -60 "$out/$name.err" |
| 54 | + failed=1 |
| 55 | + else |
| 56 | + echo "tsan: $name ok ($n threads)" |
| 57 | + fi |
| 58 | +} |
| 59 | +for program in tests/run/*.til; do check "$root/$program" "$threads"; done |
| 60 | +for program in tests/stress/*.til tests/bench/messages.til tests/bench/timers.til; do |
| 61 | + check "$root/$program" 8 |
| 62 | +done |
| 63 | +if [ "$failed" != 0 ]; then echo "tsan: failures"; exit 1; fi |
| 64 | +echo "tsan: no reports" |