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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
pixi run run-post | tee post.txt
grep -q 'sum: 499500' post.txt

# Deliberate misuse: four cases the compiler must reject, three it must
# Deliberate misuse: four cases the compiler must reject, four it must
# still accept (and get wrong in the documented way).
- name: Caught and uncaught
run: pixi run check
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ which wrote the `threads-mojo = { git = …, rev = … }` line in

## What the compiler catches, and what it does not

`pixi run check` builds seven deliberately wrong programs under
`pixi run check` builds eight deliberately wrong programs under
[`tests/`](tests/) and holds the compiler to a claim about each one.

[`tests/caught/`](tests/caught/) must fail to compile, with the diagnostic the
Expand All @@ -73,14 +73,25 @@ and then print the wrong answer the file predicts:
| --- | --- | --- |
| `untracked_ctx_drops_early` | the post's `Ctx` erases the origin; `totals` is destroyed at its last visible use, before any thread starts | `Totals dropped` printed first; sum `499499` (−1 + 499500, the tasks adding into the poisoned cell) |
| `opaque_escapes_origin` | `share(totals).opaque()` outside `run` — the boundary of what origins cover | same |
| `field_deref_after_last_use` | `totals.cell[]` copies the pointer field, which is the struct's last use, and the deref reads a destroyed object | `-1`; the method read `totals.sum()` gives `499500` |
| `field_deref_after_last_use` | `totals.cell[]` copies the pointer field, which is the struct's last use, and the deref reads a destroyed object | `-1`; the method read `totals.sum()` gives `499500`, and so does the same field deref once the cell is an `OwnedPointer`, whose deref borrows the struct |
| `plain_store_races` | a task writes `sum` with a plain load and store instead of the atomic — every task holds `mut` access to the same value and nothing says the type is safe to share | lost updates, every run; not a lifetime bug but the one an origin cannot express |

The uncaught set is the list a reviewer has to check by hand. If a newer
compiler starts rejecting one, `check` fails on it — move the file to
`caught/`, record the diagnostic, and the list gets shorter. The destructor in
those tests poisons the cell rather than freeing it, so the misuse stays a
number that can be asserted instead of undefined behaviour.

Three of the four are lifetime bugs, and each has a fix that exists today:
keep the origin in the type (`origins.mojo`), keep the erasure inside `run`,
and own heap memory through a type whose deref borrows the owner
(`OwnedPointer`, `List`) rather than a raw untracked `Pointer`. The fourth is
a different kind: `Ctx` gives a thousand tasks mutable access to one value,
and only an atomic in the task keeps that honest. Rust would refuse the
aliased `&mut` and let the atomic through on the strength of `Sync`; Mojo has
no such trait yet, so the race is invisible to the compiler and lands in the
uncaught set with the others.

## Comments

The post has no comment box; this repo is where the code can be discussed.
Expand Down
4 changes: 2 additions & 2 deletions src/origins.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ def share[

def run[
T: AnyType, origin: MutOrigin, //, work: WorkFn
](n_tasks: Int, ctx: Ctx[T, origin]) raises:
](n_tasks: Int, ctx: Ctx[T, origin], num_workers: Int = 0) raises:
"""`parallel_for`, taking the tracked context rather than the pointer.

`ctx` is alive for the whole call, so the state it points to is too. The
erasure to `void *` happens here and nowhere else.
"""
parallel_for[work](n_tasks, ctx.opaque())
parallel_for[work](n_tasks, ctx.opaque(), num_workers)


@fieldwise_init
Expand Down
32 changes: 32 additions & 0 deletions tests/uncaught/field_deref_after_last_use.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
# copy and the deref, and the deref reads the poisoned cell. A method that
# reads through `self` borrows the whole struct for the call and gets the
# right number.
#
# The fix is a type, not a compiler change: `OwnedPointer.__getitem__` returns
# a ref whose origin is the pointer's own, so `owned.cell[]` borrows `owned`
# and it stays alive through the deref. Same shape, right answer, dropped last.
# expect: via method: 499500
# expect: Totals dropped
# expect: via field deref: -1
# expect: via OwnedPointer field deref: 499500
# expect: Owned dropped
from std.memory import OwnedPointer
from std.memory.alloc import unsafe_alloc
from origins import Ctx, run, share
from threads import AtomicCounter, OpaquePtr
Expand Down Expand Up @@ -33,13 +40,38 @@ struct Totals(Movable):
self.cell[] = -1


struct Owned(Movable):
"""The same struct with the cell behind an `OwnedPointer`.

Nothing to poison: the deref borrows `self`, so the drop cannot come
first, and the pointer frees its cell after this destructor runs.
"""

var cell: OwnedPointer[Int64]

def __init__(out self):
self.cell = OwnedPointer(Int64(0))

def __deinit__(deinit self):
print("Owned dropped")


def task(i: Int, ptr: OpaquePtr) -> None:
var t = Ctx[Totals].of(ptr)
_ = AtomicCounter.at(Int(t[].cell)).fetch_add(Int64(i))


def owned_task(i: Int, ptr: OpaquePtr) -> None:
var t = Ctx[Owned].of(ptr)
_ = AtomicCounter.at(Int(Pointer(to=t[].cell[]))).fetch_add(Int64(i))


def main() raises:
var totals = Totals()
run[task](1000, share(totals))
print("via method:", totals.sum())
print("via field deref:", totals.cell[])

var owned = Owned()
run[owned_task](1000, share(owned))
print("via OwnedPointer field deref:", owned.cell[])
35 changes: 35 additions & 0 deletions tests/uncaught/plain_store_races.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Not a lifetime bug: a data race. `Ctx.__getitem__` hands every task a
# mutable ref to the same `Totals`, and nothing stops a task writing to
# `sum` with a plain load and store instead of the atomic. Rust rejects this
# before threads come into it — `&mut` aliased across closures is an
# exclusivity error, and the atomic works through `&AtomicI64` because that
# type is `Sync`. Mojo has no interior-mutability marker, so there is no way
# for `share` to demand one; the origin says only how long `totals` lives.
#
# The window between load and store is widened on purpose so that the lost
# 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
from std.time import perf_counter_ns
from origins import Ctx, run, share
from threads import OpaquePtr


@fieldwise_init
struct Totals(Copyable, Movable):
var sum: Int64


def task(i: Int, ptr: OpaquePtr) -> None:
var t = Ctx[Totals].of(ptr)
var seen = t[].sum
var t0 = perf_counter_ns()
while perf_counter_ns() - t0 < 20_000:
pass
t[].sum = seen + Int64(i)


def main() raises:
var totals = Totals(0)
run[task](1000, share(totals), num_workers=4)
print("plain store, lost updates:", totals.sum != 499500)
Loading