Skip to content

check.sh: hold ThreadSanitizer to a per-file verdict - #9

Merged
winding-lines merged 1 commit into
mainfrom
tsan
Sep 4, 2026
Merged

winding-lines merged 1 commit into
mainfrom
tsan

Conversation

@winding-lines

@winding-lines winding-lines commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

tests/check.sh asserts what the compiler does with each deliberately
wrong program: caught/* must fail to build with a named diagnostic,
uncaught/* must build silently and print the predicted wrong answer. This
adds a third assertion, about a tool rather than the compiler.

Each uncaught case is rebuilt with mojo build --sanitize thread and run, and
ThreadSanitizer's verdict is checked against a # expect-tsan: race /
# expect-tsan: clean line declared in the file, next to the # expect: lines
it already carries. src/origins.mojo — the corrected listing — carries the
same marker and is the control: a "clean" verdict is only worth reading if
a correct program earns one too, which is what rules out a sanitizer that is
silently doing nothing.

Verdicts

Confirmed by running each one ten times, not assumed:

file ThreadSanitizer
origins (control) clean, 0/10 runs reported anything
untracked_ctx_drops_early clean, 0/10
opaque_escapes_origin clean, 0/10
field_deref_after_last_use clean, 0/10
plain_store_races race, 2 reports in 10/10 runs

The three clean verdicts are the finding, not a gap in the harness. Each of
those bugs poisons its cell from the main thread — before pthread_create in
untracked_ctx_drops_early and opaque_escapes_origin, after the join in
field_deref_after_last_use — so every access to the poisoned cell is ordered
and there is nothing for a race detector to report. A use-after-destroy is not
a data race, and the harness poisoning rather than freeing is what keeps it
that way. L001 and L002 remain the things that catch those three; this
records that fact instead of leaving it to be rediscovered.

plain_store_races is the genuinely concurrent one, and it now has two
independent detectors: statically L003, and at runtime a tool that names the
load, the store, and the two threads they ran on, inside _parallel_worker.
The README table in this repo attributed it only to the lint — that row is
updated here. The blog post on magmalake.org is not touched, but it is
worth knowing that its "the compiler cannot see this" framing now has a
runtime counterpart on the one case that is a real race.

Mechanics

  • The verdict is a grep for WARNING: ThreadSanitizer, not the exit code:
    a reported race aborts the process (134) here, and the exit code has moved
    between sanitizer versions. A clean file must additionally exit 0.
  • A missing or malformed # expect-tsan: line is a failure, so a new
    uncaught/ case cannot silently skip the check.
  • LLVM_SYMBOLIZER_PATH is pointed at the llvm-symbolizer already in the
    pixi env; without it TSan prints "Stack dump without symbol names" and the
    report names an address rather than a Mojo frame.

Where it runs

On linux-64 with Mojo 1.0.0 a --sanitize thread binary links and then
aborts before main
: the Mojo runtime's bundled TCMalloc cannot find a
1 GiB-aligned mapping inside the address space TSan has reserved
(MmapAligned() failed … TCMalloc assumes a 48-bit virtual address space size). A --sanitize thread hello-world fails identically and setarch -R
does not help, so it is the toolchain rather than anything here.

So check.sh builds a canary first and prints

skip ThreadSanitizer leg: the canary did not run — see build/tests/tsan_canary.log

rather than failing. CHECK_TSAN=1 forces the leg anyway — useful for finding
out whether a newer toolchain has fixed it — and CHECK_TSAN=0 skips it. CI is
one command on both platforms and enforces the leg on macOS.

Local run, macOS arm64, Mojo 1.0.0: 11 passed, 0 failed, about 10 s. Both CI
jobs are green on this branch: macos-latest reports 11 passed, 0 failed with
all five sanitizer verdicts, ubuntu-latest reports 6 passed, 0 failed and
skip ThreadSanitizer leg: the canary did not run.

Co-Authored-By: Claude Fable 5 noreply@anthropic.com

tests/check.sh asserts what the compiler does with each deliberately wrong
program: caught/ must fail to build with a named diagnostic, uncaught/ must
build silently and print a predicted wrong answer. This adds a third
assertion, about a tool rather than the compiler: each uncaught case is
rebuilt with `mojo build --sanitize thread` and run, and what ThreadSanitizer
says is checked against a `# expect-tsan: race` / `# expect-tsan: clean` line
declared in the file next to its `# expect:` lines. src/origins.mojo — the
corrected listing — carries the same marker and is the control, because a
"clean" verdict is only worth reading if a correct program earns one.

The verdicts, confirmed by running each ten times rather than assumed:

  plain_store_races           race  (two reports, every run)
  untracked_ctx_drops_early   clean
  opaque_escapes_origin       clean
  field_deref_after_last_use  clean
  origins (control)           clean

The three clean verdicts are the finding, not a gap. Each of those bugs
poisons its cell from the main thread — before pthread_create in two cases,
after the join in the third — so every access is ordered and there is no race
to report. A use-after-destroy is not a data race; L001 and L002 stay the
things that catch them. plain_store_races is the genuinely concurrent one, and
it is now caught twice: statically by L003 and at runtime by a tool that names
the load, the store and the two threads.

The verdict is a grep for "WARNING: ThreadSanitizer" rather than the exit
code, which is 134 on a race here and has moved between sanitizer versions.
LLVM_SYMBOLIZER_PATH is pointed at the llvm-symbolizer already in the pixi
env, so a report names Mojo frames instead of an address.

The leg runs where the sanitizer does. On linux-64 with Mojo 1.0.0 a
--sanitize thread binary links and then aborts before main — the runtime's
bundled TCMalloc cannot get a 1 GiB-aligned mapping inside the address space
TSan reserved, and a hello-world fails identically — so a canary decides and
check.sh prints a skip line instead of failing. CHECK_TSAN=1 forces the leg
(to find out whether a newer toolchain has fixed it), CHECK_TSAN=0 skips it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@winding-lines
winding-lines merged commit 9338f44 into main Sep 4, 2026
2 checks passed
@winding-lines
winding-lines deleted the tsan branch September 4, 2026 04:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant