Skip to content

formal: a TLA+ model and Lean examples of a task that takes a local of a loop pass - #4374

Merged
hiraditya merged 1 commit into
vx-lang:mainfrom
minzyfox:formal-tasks-copy-captures-in-loops
Dec 2, 2026
Merged

hiraditya merged 1 commit into
vx-lang:mainfrom
minzyfox:formal-tasks-copy-captures-in-loops

Conversation

@minzyfox

Copy link
Copy Markdown
Contributor

Issue: #4345

This PR adds the TLA+ and Lean parts of #4345. The compiler fix is in #4356. This PR does not
need #4356: the models describe the rule for the lowering, and they do not run the compiler.

The rule

A loop in a tasks block has one stack slot for its local. When each pass starts a task that
moves the local, the spawn must copy the slot into the task. With the address of the slot, a
task can run after the next pass wrote the slot, and it then reads the value of that pass.

TLA+: formal/runtime/SpawnCapture.tla

A loop of N passes (N = 3 in SpawnCapture.cfg). Each pass writes its value into the one slot
and starts a task. A task reads its value at any time after its spawn, and then drops it. The
end of the block joins each task. With JoinEachPass, the task borrows the local and writes it,
and each pass joins its task.

The claims:

  • EachTaskSeesItsPass: each task reads the value of its own pass.
  • EachValueIsDroppedOnce: the program drops each value once.
  • TheJoinSeesTheWrite: after the join of a task that borrows the local, the pass sees the
    write of the task.
  • TheBlockEnds.

run_tlc.py runs the module with each check and each mistake. The results with TLC 1.7.4:

Check Result
as in SpawnCapture.cfg no error
with a join in each pass no error
mistake address_of_a_reused_slot (the old lowering) TLC reports EachTaskSeesItsPass
mistake address_of_a_reused_slot TLC reports EachValueIsDroppedOnce
mistake copy_a_borrowed_local TLC reports TheJoinSeesTheWrite
mistake a_join_waits_for_a_later_task TLC reports TheBlockEnds

I ran only the new module with the code of run_tlc.py. This PR does not change the other
modules.

Lean: formal/memory_model/lean/VxMemoryModel/SpawnCaptureExample.lean

Two programs of the memory model, each with one execution in the order of the bug: pass 0
writes 10 and starts task 0, pass 1 writes 11, and then task 0 reads.

  • byAddress, the old lowering: task 0 reads the slot. The model allows the execution in which
    task 0 reads 11, and check finds a data race between the write of pass 1 and that read.
  • byCopy, the copy at the spawn: task 0 reads its own place. It reads 10, and check finds no
    race.

A Run ties each execution to its program, as in MutexExample.lean. To check that the guards
run, I flipped each expected answer of check, and Lean then failed at both #guard lines.

A limit of the Lean part

The examples cover one execution each. #4345 asks for a theorem that covers each execution of a
loop of spawns. The footprint rule (Footprint.lean) cannot prove that yet, also for the
copy. It compares all the code of a tasks block with each task of the block. So a copy that
the code of the block makes before the spawn counts as a conflict with the task. A general
theorem needs a rule that uses the order of the fork. That is a larger change to the proofs, and
I suggest a separate issue for it.

How I checked it

  • TLC 1.7.4 (tla2tools.jar with the SHA-256 of formal.yml): the table above.
  • Lean 4.35.0: lake env lean VxMemoryModel/SpawnCaptureExample.lean gives no error and no
    warning. A full lake build --wfail on my machine did not finish in time:
    BroadcastExample.lean, which this PR does not change, ran for more than 35 minutes. The
    formal workflow builds the whole library.

🤖 Generated with Claude Code

…f a loop pass

A loop in a `tasks` block has one stack slot for its local. When each
pass starts a task that moves the local, the `spawn` must copy the slot.
With the address of the slot, a task can read the value of a later
pass. The compiler had this bug (vx-lang#4345). These models state the rule
that the lowering must follow.

SpawnCapture.tla models such a loop. Each task reads its value at any
time after its `spawn`. TLC checks that each task sees the value of its
own pass, that each value is dropped once, and that a task that borrows
the local and is joined in the same pass writes the local itself. Three
mistakes break these claims:

- address_of_a_reused_slot: the old lowering, which gave the task the
  address of the slot.
- copy_a_borrowed_local: the `spawn` copies a local that the task
  borrows, so the write of the task is lost.
- a_join_waits_for_a_later_task: the block never ends.

SpawnCaptureExample.lean gives two executions of the memory model in
the order of the bug: pass 1 writes the slot before task 0 reads it.
With the old lowering, task 0 reads the value of pass 1, and `check`
finds a data race. With the copy at the `spawn`, task 0 reads its own
value, and `check` finds no race. A `Run` ties each execution to its
program.

The Lean examples cover one execution each. The footprint rule cannot
yet prove that every execution of such a loop has no race, because it
compares all the code of a block with each task of the block. A copy
that the block makes before the `spawn` then counts as a conflict.

Part of: vx-lang#4345

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@hiraditya hiraditya left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. The model fits the rule, and CI ran TLC and Lean on it. Three small points, none of which blocks the merge.

1. The Lean guard checks less than its comment. The comment says that the race is between event 2 and event 5. But #guard byAddressData.check == some (true, true) checks only that a race exists. These guards check the comments, and they pass with Lean 4.35.0:

#guard byAddressData.raceOn 2 5 == some true
#guard byAddressData.raceOn 0 5 == some false
#guard byCopyData.raceOn 4 7 == some false

The last one checks the comment of byCopy, that the write of pass 1 (event 4) and the read (event 7) do not race. When I changed the first guard to some false, Lean failed.

2. EachValueIsDroppedOnce cannot fail with JoinEachPass.

In that mode, Drop leaves dropped as it is, and End adds 1 to each count. So the claim holds for every behavior. It is a real check only without JoinEachPass, where address_of_a_reused_slot breaks it. A sentence in the claim's comment would say so.

3. The comment of End does not match the action.

The comment says that the block drops a borrowed local "at the end of its pass". But End drops each value at the end of the block.

Generated With Claude Code

@hiraditya
hiraditya merged commit 23fefe5 into vx-lang:main Dec 2, 2026
9 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.

2 participants