Repository navigation
formal: a TLA+ model and Lean examples of a task that takes a local of a loop pass - #4374
Conversation
…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
left a comment
There was a problem hiding this comment.
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 falseThe 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
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
tasksblock has one stack slot for its local. When each pass starts a task thatmoves the local, the
spawnmust copy the slot into the task. With the address of the slot, atask can run after the next pass wrote the slot, and it then reads the value of that pass.
TLA+:
formal/runtime/SpawnCapture.tlaA loop of N passes (N = 3 in
SpawnCapture.cfg). Each pass writes its value into the one slotand starts a task. A task reads its value at any time after its
spawn, and then drops it. Theend 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 thewrite of the task.
TheBlockEnds.run_tlc.pyruns the module with each check and each mistake. The results with TLC 1.7.4:SpawnCapture.cfgaddress_of_a_reused_slot(the old lowering)EachTaskSeesItsPassaddress_of_a_reused_slotEachValueIsDroppedOncecopy_a_borrowed_localTheJoinSeesTheWritea_join_waits_for_a_later_taskTheBlockEndsI ran only the new module with the code of
run_tlc.py. This PR does not change the othermodules.
Lean:
formal/memory_model/lean/VxMemoryModel/SpawnCaptureExample.leanTwo 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 whichtask 0 reads 11, and
checkfinds a data race between the write of pass 1 and that read.byCopy, the copy at thespawn: task 0 reads its own place. It reads 10, andcheckfinds norace.
A
Runties each execution to its program, as inMutexExample.lean. To check that the guardsrun, I flipped each expected answer of
check, and Lean then failed at both#guardlines.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 thecopy. It compares all the code of a
tasksblock with each task of the block. So a copy thatthe code of the block makes before the
spawncounts as a conflict with the task. A generaltheorem 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
tla2tools.jarwith the SHA-256 offormal.yml): the table above.lake env lean VxMemoryModel/SpawnCaptureExample.leangives no error and nowarning. A full
lake build --wfailon my machine did not finish in time:BroadcastExample.lean, which this PR does not change, ran for more than 35 minutes. Theformalworkflow builds the whole library.🤖 Generated with Claude Code