Skip to content

Deliberate misuse, sorted by what the compiler does with it - #2

Merged
winding-lines merged 1 commit into
originsfrom
probes
Sep 3, 2026
Merged

winding-lines merged 1 commit into
originsfrom
probes

Conversation

@winding-lines

Copy link
Copy Markdown
Contributor

Stacked on #1. Answers "how do we introduce bugs and find out whether the compiler catches them" with a harness that holds the compiler to a claim in both directions.

pixi run check builds seven deliberately wrong programs:

  • tests/caught/ — must fail to compile, with the diagnostic named in the file's # expect-error: line. The four refusals from Keep the origin in the context type #1: sharing a read argument, sharing a temporary, returning a Ctx to a local, a Ctx outliving its block.
  • tests/uncaught/ — must compile with no diagnostic, run, and print the # expect: lines in order — the wrong answer, reproduced on purpose:
    • untracked_ctx_drops_early — the post's Ctx: totals destroyed before any thread starts
    • opaque_escapes_origin — share(totals).opaque() outside run: the boundary of what origins cover
    • field_deref_after_last_use — totals.cell[] copies the pointer field, the struct dies, the deref reads a destroyed object; totals.sum() through a method gets 499500

If a caught case starts compiling, a guarantee the README states has gone. If an uncaught case stops compiling, the compiler learned to see something — move the file to caught/, record the diagnostic, and the by-hand review list gets shorter.

Two things found while writing it:

  1. A test of undefined behaviour cannot itself be undefined. With a real unsafe_free in the destructor, the thousand writes into the freed block corrupted the heap on some runs (pthread_join failed, rc=3). The destructor now poisons the cell to -1 and leaks it, so the misuse is a number to assert — the sum comes out 499499. Held over 15 consecutive runs.
  2. The harness was sabotaged in both directions before trusting it: a caught case rewritten to compile, and an uncaught case with its expectations reordered, are each reported as FAIL.

CI runs pixi run check after both programs.

🤖 Generated with Claude Code

https://claude.ai/code/session_015or6oTZmb7EQTXTbALbBXY

tests/check.sh builds seven wrong programs. tests/caught/ must fail to
compile with the diagnostic named in the file's `# expect-error:` line —
the four refusals PR #1 lists. tests/uncaught/ must compile with no
diagnostic, run, and print the `# expect:` lines in order — the wrong
answer, on purpose: the post's untracked Ctx dropping `totals` before
the threads start, `opaque()` outside `run` doing the same, and a
field-copy-then-deref reading a destroyed object.

Both directions are claims about the compiler. A caught case that starts
compiling means a stated guarantee is gone; an uncaught case that stops
compiling means the compiler learned to see it — move the file over.

The destructor in the uncaught cases poisons the cell to -1 and leaks it
rather than freeing it: with a real free, the thousand writes corrupted
the heap on some runs (pthread_join rc=3), and a test documenting
undefined behaviour cannot itself be undefined. Poisoned, the misuse is a
number — 499499 — and the harness held over 15 consecutive runs.

The harness was checked in both directions: a caught case rewritten to
compile and an uncaught case with its expectations reordered are each
reported as FAIL. Run it as `pixi run check`; it says so if `mojo` is
not on PATH.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015or6oTZmb7EQTXTbALbBXY
@winding-lines
winding-lines merged commit cfba420 into origins Sep 3, 2026
2 checks passed
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