Repository navigation
check.sh: hold ThreadSanitizer to a per-file verdict - #9
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
tests/check.shasserts what the compiler does with each deliberatelywrong program:
caught/*must fail to build with a named diagnostic,uncaught/*must build silently and print the predicted wrong answer. Thisadds a third assertion, about a tool rather than the compiler.
Each uncaught case is rebuilt with
mojo build --sanitize threadand run, andThreadSanitizer's verdict is checked against a
# expect-tsan: race/# expect-tsan: cleanline declared in the file, next to the# expect:linesit already carries.
src/origins.mojo— the corrected listing — carries thesame 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:
origins(control)untracked_ctx_drops_earlyopaque_escapes_originfield_deref_after_last_useplain_store_racesThe 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_createinuntracked_ctx_drops_earlyandopaque_escapes_origin, after the join infield_deref_after_last_use— so every access to the poisoned cell is orderedand 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.
L001andL002remain the things that catch those three; thisrecords that fact instead of leaving it to be rediscovered.
plain_store_racesis the genuinely concurrent one, and it now has twoindependent detectors: statically
L003, and at runtime a tool that names theload, 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
WARNING: ThreadSanitizer, not the exit code:a reported race aborts the process (134) here, and the exit code has moved
between sanitizer versions. A
cleanfile must additionally exit 0.# expect-tsan:line is a failure, so a newuncaught/case cannot silently skip the check.LLVM_SYMBOLIZER_PATHis pointed at thellvm-symbolizeralready in thepixi 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 threadbinary links and thenaborts before
main: the Mojo runtime's bundled TCMalloc cannot find a1 GiB-aligned mapping inside the address space TSan has reserved
(
MmapAligned() failed … TCMalloc assumes a 48-bit virtual address space size). A--sanitize threadhello-world fails identically andsetarch -Rdoes not help, so it is the toolchain rather than anything here.
So
check.shbuilds a canary first and printsrather than failing.
CHECK_TSAN=1forces the leg anyway — useful for findingout whether a newer toolchain has fixed it — and
CHECK_TSAN=0skips it. CI isone 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 CIjobs are green on this branch: macos-latest reports
11 passed, 0 failedwithall five sanitizer verdicts, ubuntu-latest reports
6 passed, 0 failedandskip ThreadSanitizer leg: the canary did not run.Co-Authored-By: Claude Fable 5 noreply@anthropic.com