Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@ jobs:
grep -q 'sum: 499500' post.txt

# Deliberate misuse: two cases the compiler must reject, four it must
# still accept (and get wrong in the documented way).
# still accept (and get wrong in the documented way) — and then each of
# those four, plus the corrected listing, rebuilt under
# `--sanitize thread` and checked against the `# expect-tsan:` line in
# the file.
#
# The sanitizer leg runs on macOS and skips itself on Linux: with Mojo
# 1.0.0 a `--sanitize thread` binary links on linux-64 and then aborts
# before `main`, because the runtime's bundled TCMalloc cannot get a
# 1 GiB-aligned mapping inside the address space TSan has reserved. A
# canary inside check.sh decides, so this stays one command on both.
- name: Caught and uncaught
run: pixi run check

Expand Down
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ which wrote the `threads-mojo = { git = …, rev = … }` line in
## What the compiler catches, and what it does not

`pixi run check` builds six deliberately wrong programs under
[`tests/`](tests/) and holds the compiler to a claim about each one.
[`tests/`](tests/) and holds the compiler to a claim about each one — and then
asks a second question of the four that compile: what does a race detector
make of them?

[`tests/caught/`](tests/caught/) must fail to compile, with the diagnostic the
file names:
Expand All @@ -82,6 +84,41 @@ compiler starts rejecting one, `check` fails on it — move the file to
those tests poisons the cell rather than freeing it, so the misuse stays a
number that can be asserted instead of undefined behaviour.

### And what a sanitizer catches

`check` rebuilds each uncaught case with `mojo build --sanitize thread` and
holds ThreadSanitizer to the verdict the file declares in a
`# expect-tsan: race` or `# expect-tsan: clean` line, next to its `# expect:`
lines. [`src/origins.mojo`](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.

| file | ThreadSanitizer |
| --- | --- |
| `origins` (the control) | clean |
| `untracked_ctx_drops_early` | clean |
| `opaque_escapes_origin` | clean |
| `field_deref_after_last_use` | clean |
| `plain_store_races` | **data race**, both accesses named |

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 two cases, after the join in the third — so every access to it is ordered
and there is no race to see. A use-after-destroy is not a data race, and a
race detector is the wrong instrument for one; `L001` and `L002` stay the
things that catch them.

`plain_store_races` is the one that is genuinely concurrent, and it is now
caught twice over: statically by `L003`, and at runtime by a tool that names
the load and the store in `_parallel_worker` and the two threads they ran on.

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 — so a canary decides and `check` prints a skip line rather than
failing. `CHECK_TSAN=1` forces it (to find out whether a newer toolchain has
fixed it); `CHECK_TSAN=0` skips it.

Three of the four are lifetime bugs, and each has a fix that exists today:
pass the state as a `ref` argument (the typed `parallel_for`), keep the
erasure inside the library, and own heap memory through a type whose deref
Expand Down
6 changes: 6 additions & 0 deletions src/origins.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ itself, which is why the program below is the post's listing minus the
pointer plumbing.
"""

# The control for the sanitizer leg of `tests/check.sh`: this program is
# correct, so ThreadSanitizer must have nothing to say about it. A "clean"
# verdict on the uncaught cases is only worth reading if a correct program
# earns one too — that is what rules out a sanitizer that is silently off.
# expect-tsan: clean

from threads import AtomicCounter, num_cpus, parallel_for


Expand Down
101 changes: 101 additions & 0 deletions tests/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
# stops compiling, the compiler has learned to see something it could not —
# move the file to caught/, record the diagnostic, and shorten the list of
# things a reviewer has to check by hand.
#
# Then a third claim, 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 the file's `# expect-tsan: race` or
# `# expect-tsan: clean` line. `src/origins.mojo` — the corrected listing —
# carries the same marker and is the control: a "clean" verdict only means
# something if a correct program earns one.
set -u
cd "$(dirname "$0")/.."
command -v mojo >/dev/null || { echo "mojo not on PATH — run as: pixi run check"; exit 2; }
Expand Down Expand Up @@ -76,5 +83,99 @@ for f in tests/uncaught/*.mojo; do
fi
done

# ── the sanitizer's verdict ─────────────────────────────────────────────────
# What a tool can see at runtime, as opposed to what the compiler sees at
# build time. Only `plain_store_races` is a data race; the three lifetime bugs
# come out clean, and that is the finding rather than a hole in the harness.
# Each of them 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 nothing for TSan to report. A race detector is the wrong instrument
# for a use-after-destroy; mojolint's L001/L002 remain the ones that catch
# those.
#
# The leg is skipped where ThreadSanitizer cannot run at all. On linux-64 with
# Mojo 1.0.0 the binary links, but the runtime's bundled TCMalloc aborts during
# startup under TSan's address-space reservation ("MmapAligned() failed ...
# TCMalloc assumes a 48-bit virtual address space"). A canary decides;
# CHECK_TSAN=1 forces the leg to run anyway (to find out whether a newer
# toolchain has fixed it), CHECK_TSAN=0 skips it.

# tsan_canary — can a trivial `--sanitize thread` binary build and run here?
tsan_canary() {
cat >build/tests/tsan_canary.mojo <<'CANARY'
def main():
print("tsan canary ok")
CANARY
mojo build --sanitize thread build/tests/tsan_canary.mojo \
-o build/tests/tsan_canary >build/tests/tsan_canary.log 2>&1 || return 1
build/tests/tsan_canary >>build/tests/tsan_canary.log 2>&1
grep -q 'tsan canary ok' build/tests/tsan_canary.log
}

run_tsan=1
skip_reason=
case "${CHECK_TSAN:-auto}" in
0) run_tsan=0 skip_reason="CHECK_TSAN=0" ;;
1) ;;
*) tsan_canary || {
run_tsan=0
skip_reason="the canary did not run — see build/tests/tsan_canary.log"
} ;;
esac

if [ "$run_tsan" = 0 ]; then
echo "skip ThreadSanitizer leg: $skip_reason"
else
# Without this TSan prints "Stack dump without symbol names" and a report
# names an address instead of a Mojo frame. The symbolizer ships in the
# same conda package as mojo.
if [ -z "${LLVM_SYMBOLIZER_PATH:-}" ]; then
symbolizer=$(command -v llvm-symbolizer || true)
[ -n "$symbolizer" ] && export LLVM_SYMBOLIZER_PATH="$symbolizer"
fi
for f in tests/uncaught/*.mojo src/origins.mojo; do
name=$(basename "$f" .mojo)
want=$(expect_lines "$f" expect-tsan)
if [ "$want" != race ] && [ "$want" != clean ]; then
echo "FAIL tsan/$name: no '# expect-tsan: race|clean' line in $f"
fail=$((fail + 1))
continue
fi
if ! out=$(mojo build --sanitize thread "$f" -I src \
-o "build/tests/$name-tsan" 2>&1); then
echo "FAIL tsan/$name: --sanitize thread build failed:"
echo "$out" | grep -i 'error' | head -3
fail=$((fail + 1))
continue
fi
log="build/tests/$name.tsan.log"
# A reported race aborts the process, and the shell announces that
# ("Abort trap: 6") on its own stderr, in the middle of the results.
# Run it one shell down so that announcement goes to /dev/null while
# the program's own output still lands in the log.
bash -c '"$0" >"$1" 2>&1' "build/tests/$name-tsan" "$log" 2>/dev/null
status=$?
# TSan's exit code is not the verdict: a reported race aborts the
# process here, and a clean run under a different version might not
# exit 0 either. The verdict is what it printed.
races=$(grep -c 'WARNING: ThreadSanitizer' "$log")
if [ "$want" = race ] && [ "$races" -eq 0 ]; then
echo "FAIL tsan/$name: expected a race; ThreadSanitizer reported none"
fail=$((fail + 1))
elif [ "$want" = clean ] && [ "$races" -ne 0 ]; then
echo "FAIL tsan/$name: expected a clean run; ThreadSanitizer reported $races:"
grep -A3 'WARNING: ThreadSanitizer' "$log" | head -16 | sed 's/^/ /'
fail=$((fail + 1))
elif [ "$want" = clean ] && [ "$status" -ne 0 ]; then
echo "FAIL tsan/$name: clean, but the run exited $status:"
tail -5 "$log" | sed 's/^/ /'
fail=$((fail + 1))
else
echo "ok tsan/$name ($want)"
pass=$((pass + 1))
fi
done
fi

echo "$pass passed, $fail failed"
[ "$fail" = 0 ]
3 changes: 3 additions & 0 deletions tests/uncaught/field_deref_after_last_use.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
# expect: via field deref: -1
# expect: via OwnedPointer field deref: 499500
# expect: Owned dropped
# Clean under ThreadSanitizer: this bug is not even concurrent — the drop and
# the deref both happen on the main thread, after `parallel_for` has joined.
# expect-tsan: clean
from std.memory import OwnedPointer
from std.memory.alloc import unsafe_alloc
from threads import AtomicCounter, parallel_for
Expand Down
4 changes: 4 additions & 0 deletions tests/uncaught/opaque_escapes_origin.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
# expect: Totals dropped
# expect: before parallel_for
# expect: after parallel_for: 499499
# Clean under ThreadSanitizer, for the same reason as
# `untracked_ctx_drops_early`: `totals` is destroyed on the main thread before
# the first task starts, so every access to the poisoned cell is ordered.
# expect-tsan: clean
from std.memory.alloc import unsafe_alloc
from threads import AtomicCounter, OpaquePtr, opaque_ptr, parallel_for

Expand Down
5 changes: 5 additions & 0 deletions tests/uncaught/plain_store_races.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
# updates are a certainty rather than a likelihood, and the worker count is
# fixed so a single-core runner cannot serialise the tasks by accident.
# expect: plain store, lost updates: True
# Two threads, a plain load and a plain store, no ordering between them: this
# is the one case here a race detector can see, and `--sanitize thread` names
# both accesses inside `_parallel_worker`. The lint (L003) and the sanitizer
# agree, from opposite ends.
# expect-tsan: race
from std.time import perf_counter_ns
from threads import parallel_for

Expand Down
6 changes: 6 additions & 0 deletions tests/uncaught/untracked_ctx_drops_early.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
# expect: Totals dropped
# expect: before parallel_for
# expect: after parallel_for: 499499
# Clean under ThreadSanitizer, and that is the point rather than a gap: the
# poisoning write happens on the main thread *before* `pthread_create`, so it
# is ordered against every task's read of the cell and there is no race to
# report. A use-after-destroy is not a race; mojolint's L001 is what catches
# this one.
# expect-tsan: clean
from std.memory.alloc import unsafe_alloc
from threads import AtomicCounter, OpaquePtr, parallel_for

Expand Down
Loading