Files
GVFG_Standalone/GVFG_SDK/sdk/gvfg/formal/CaptureFrameHold.tla
T
iverson3yaandClaude Opus 5.5 208fb6c811 Add a frame-hold verification skill and TLA+ model shared by Claude Code and Codex
- Add GVFG_SDK/sdk/gvfg/formal: a TLA+ model of one channel's frame-hold protocol (read, release, stop, start and signal-change recovery) and six TLC configs: the shipped code with the reader joined before stop/start and with it running across them, and four mutations that must fail.
- Add .agents/skills/verify-gvfg-frame-hold: check.ps1 runs every config against its expected result, prints counterexamples with -Trace, and reports when the covered code moves past the sync commit; f1-hardware-repro.ps1 stages a stop and start during a read on a real card.
- Add a Claude Code entry in .claude/skills that points to the shared copy, and notes on which steps must run outside the Codex Windows sandbox.

Validation: git diff --cached --check passed. check.ps1 met all six expectations and reported the model in step with 1d89392. f1-hardware-repro.ps1 reproduced the stuck channel against b47f27b and passed against 1d89392 on a GIGABYTE SDI VFG100. TLC needs Java 17+ and tla2tools 1.7.4; check.ps1 -Install fetches the jar into the git-ignored formal/tools. Not yet run from Codex.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 16:46:15 +08:00

348 lines
17 KiB
Plaintext

--------------------------- MODULE CaptureFrameHold ---------------------------
(***************************************************************************)
(* The frame-hold protocol of one GVFG capture channel. *)
(* *)
(* Promise checked: while the application holds a frame whose data is the *)
(* backend capture buffer, (1) that buffer still belongs to the running *)
(* driver stream -- no recovery restart and no stop/start has replaced it *)
(* -- and (2) the one gvfg_release_channel_frame() the application must *)
(* make will succeed. *)
(* *)
(* Harm if broken: the application reads a zero-copy buffer the driver is *)
(* refilling (torn or foreign video), and/or its release returns *)
(* GVFG_ESTATE and the channel stays "frame held", so every later read is *)
(* rejected until stop. *)
(* *)
(* Threads: R = application video worker (read / release), *)
(* U = application lifecycle thread (stop / start, serialized), *)
(* E = session event thread (signal change -> recovery restart). *)
(* Each action is one indivisible step in the code: a section under one *)
(* mutex, or one atomic access. Code references name the function in *)
(* C = GVFG_SDK/sdk/gvfg/src/gvfg_capture.cpp (gvfg_channel_session_t) *)
(* S = GVFG_SDK/sdk/gvfg/src/gigabyte/gigabyte_capture_session.cpp *)
(***************************************************************************)
EXTENDS Naturals
CONSTANTS
AppQuiescesReader, \* TRUE: app joins/pauses its reader before stop/start (srcroot GvfgSource::stop does)
Guards, \* code checks present; the shipped code has all of them (see MC .cfg files)
MaxLifecycleOps, \* bound on stop/start calls
MaxSignalEvents, \* bound on format-change / plug-in events
MaxReads \* bound on read attempts
AllGuards == {"RestartWaitsForRelease", "CaptureRejectsDuringRecovery",
"DeliverRechecksRunning", "DeliverChecksRunGeneration"}
ASSUME Guards \subseteq AllGuards
VARIABLES
\* --- channel, C: gvfg_channel_session_t -------------------------------
running, \* C running (std::atomic<bool>)
readInProgress, \* C readInProgress (frameMutex)
frameHeld, \* C frameHeld (frameMutex)
heldNeedsBackend, \* C heldFrameNeedsBackendRelease (frameMutex)
heldEpoch, \* ghost: driver stream epoch of the buffer in heldSessionFrame
runGen, \* C runGeneration (frameMutex): +1 on every successful start
\* --- session, S: GigabyteCaptureSession -------------------------------
sRunning, \* S running_
sFrameHeld, \* S frame_held_ (state_mutex_)
recoveryPending, \* S recovery_pending_
generation, \* S signal_generation_
epoch, \* ghost: +1 on every GvfgStartCapture (driver buffers handed out anew)
monitoring, \* S monitoring_ (event thread alive)
\* --- threads ---------------------------------------------------------
rpc, rGen, rRunGen, rEpoch, rNeeds, reads,
upc, uNeeds, ops, readerPaused,
epc, signals,
releaseFailed \* ghost: app's release of a held frame returned GVFG_ESTATE
chanVars == <<running, readInProgress, frameHeld, heldNeedsBackend, heldEpoch, runGen>>
sessVars == <<sRunning, sFrameHeld, recoveryPending, generation, epoch, monitoring>>
rVars == <<rpc, rGen, rRunGen, rEpoch, rNeeds, reads>>
uVars == <<upc, uNeeds, ops, readerPaused>>
eVars == <<epc, signals>>
vars == <<chanVars, sessVars, rVars, uVars, eVars, releaseFailed>>
RStates == {"idle", "begin_lock", "wait_check", "waiting", "capture", "osd", "deliver",
"stop_release", "holding", "fail"}
UStates == {"idle", "quiesce", "choose", "stop_frames", "stop_backend",
"stop_stream", "stop_join", "start_session", "start_channel"}
EStates == {"idle", "decide", "wait_hold", "restart", "finish"}
TypeOK ==
/\ running \in BOOLEAN /\ readInProgress \in BOOLEAN /\ frameHeld \in BOOLEAN
/\ heldNeedsBackend \in BOOLEAN /\ heldEpoch \in Nat /\ runGen \in Nat
/\ sRunning \in BOOLEAN /\ sFrameHeld \in BOOLEAN /\ recoveryPending \in BOOLEAN
/\ generation \in Nat /\ epoch \in Nat /\ monitoring \in BOOLEAN
/\ rpc \in RStates /\ rGen \in Nat /\ rRunGen \in Nat /\ rEpoch \in Nat
/\ rNeeds \in BOOLEAN /\ reads \in Nat
/\ upc \in UStates /\ uNeeds \in BOOLEAN /\ ops \in Nat /\ readerPaused \in BOOLEAN
/\ epc \in EStates /\ signals \in Nat
/\ releaseFailed \in BOOLEAN
\* Channel open and started once: C start() -> S start_stream() succeeded.
Init ==
/\ running = TRUE /\ readInProgress = FALSE /\ frameHeld = FALSE
/\ heldNeedsBackend = FALSE /\ heldEpoch = 0 /\ runGen = 1
/\ sRunning = TRUE /\ sFrameHeld = FALSE /\ recoveryPending = FALSE
/\ generation = 0 /\ epoch = 1 /\ monitoring = TRUE
/\ rpc = "idle" /\ rGen = 0 /\ rRunGen = 0 /\ rEpoch = 0 /\ rNeeds = FALSE /\ reads = 0
/\ upc = "idle" /\ uNeeds = FALSE /\ ops = 0 /\ readerPaused = FALSE
/\ epc = "idle" /\ signals = 0 /\ releaseFailed = FALSE
(***************************************************************************)
(* R: gvfg_read_channel_frame / gvfg_release_channel_frame *)
(***************************************************************************)
\* C readFrame: `if (!running)` before taking frameMutex.
RBegin ==
/\ rpc = "idle" /\ reads < MaxReads
/\ ~(AppQuiescesReader /\ readerPaused)
/\ running
/\ rpc' = "begin_lock" /\ reads' = reads + 1
/\ UNCHANGED <<chanVars, sessVars, rGen, rRunGen, rEpoch, rNeeds, uVars, eVars, releaseFailed>>
\* C readFrame, first frameMutex section: reject if held or another read; record the run.
RBeginLock ==
/\ rpc = "begin_lock"
/\ IF frameHeld \/ readInProgress
THEN rpc' = "idle" /\ UNCHANGED <<readInProgress, rRunGen>>
ELSE readInProgress' = TRUE /\ rRunGen' = runGen /\ rpc' = "wait_check"
/\ UNCHANGED <<running, frameHeld, heldNeedsBackend, heldEpoch, runGen, sessVars,
rGen, rEpoch, rNeeds, reads, uVars, eVars, releaseFailed>>
\* S wait_frame entry: running_ check; state_mutex_: snapshot generation, reject if held.
RWaitCheck ==
/\ rpc = "wait_check"
/\ IF ~sRunning \/ sFrameHeld
THEN rpc' = "fail" /\ UNCHANGED rGen
ELSE rGen' = generation /\ rpc' = "waiting"
/\ UNCHANGED <<chanVars, sessVars, rRunGen, rEpoch, rNeeds, reads, uVars, eVars, releaseFailed>>
\* S wait_frame: WaitForMultipleObjects(stop, recovery, frame events).
\* stop_event set (stop_stream) -> ESTATE; recovery_event -> wait_for_recovery -> non-OK.
RWakeFail ==
/\ rpc = "waiting" /\ (~sRunning \/ recoveryPending)
/\ rpc' = "fail"
/\ UNCHANGED <<chanVars, sessVars, rGen, rRunGen, rEpoch, rNeeds, reads, uVars, eVars, releaseFailed>>
\* A frame event fires while the driver stream runs.
RWakeFrame ==
/\ rpc = "waiting" /\ sRunning
/\ rpc' = "capture"
/\ UNCHANGED <<chanVars, sessVars, rGen, rRunGen, rEpoch, rNeeds, reads, uVars, eVars, releaseFailed>>
\* S wait_frame capture section, one step under capture_mutex_: re-check, get the buffer, frame_held_ = true.
RCapture ==
/\ rpc = "capture"
/\ IF ~sRunning
\/ ("CaptureRejectsDuringRecovery" \in Guards /\ (recoveryPending \/ rGen # generation))
THEN rpc' = "fail" /\ UNCHANGED <<sFrameHeld, rEpoch>>
ELSE sFrameHeld' = TRUE /\ rEpoch' = epoch /\ rpc' = "osd"
/\ UNCHANGED <<chanVars, sRunning, recoveryPending, generation, epoch, monitoring,
rGen, rRunGen, rNeeds, reads, uVars, eVars, releaseFailed>>
\* C readFrame frame processor: no OSD, or in-place OSD, keeps the backend hold ...
ROsdKeep ==
/\ rpc = "osd"
/\ rNeeds' = TRUE /\ rpc' = "deliver"
/\ UNCHANGED <<chanVars, sessVars, rGen, rRunGen, rEpoch, reads, uVars, eVars, releaseFailed>>
\* ... OSD into a separate SDK buffer releases the backend frame now (S release_frame).
ROsdEarlyRelease ==
/\ rpc = "osd"
/\ IF sFrameHeld
THEN sFrameHeld' = FALSE /\ rNeeds' = FALSE /\ rpc' = "deliver"
ELSE rpc' = "fail" /\ UNCHANGED <<sFrameHeld, rNeeds>>
/\ UNCHANGED <<chanVars, sRunning, recoveryPending, generation, epoch, monitoring,
rGen, rRunGen, rEpoch, reads, uVars, eVars, releaseFailed>>
\* C readFrame delivery, frameMutex: deliver unless capture stopped, or stopped and
\* restarted, since the read began.
Stale ==
\/ ("DeliverRechecksRunning" \in Guards /\ ~running)
\/ ("DeliverChecksRunGeneration" \in Guards /\ rRunGen # runGen)
RDeliver ==
/\ rpc = "deliver"
/\ IF Stale
THEN /\ readInProgress' = FALSE
/\ rpc' = IF rNeeds THEN "stop_release" ELSE "idle"
/\ UNCHANGED <<frameHeld, heldNeedsBackend, heldEpoch>>
ELSE /\ frameHeld' = TRUE /\ heldNeedsBackend' = rNeeds /\ heldEpoch' = rEpoch
/\ readInProgress' = FALSE /\ rpc' = "holding"
/\ UNCHANGED <<running, runGen, sessVars, rGen, rRunGen, rEpoch, rNeeds, reads,
uVars, eVars, releaseFailed>>
\* C readFrame stoppedWhileWaiting: session->release_frame() (clears the hold if still held).
RStopRelease ==
/\ rpc = "stop_release"
/\ sFrameHeld' = FALSE /\ rpc' = "idle"
/\ UNCHANGED <<chanVars, sRunning, recoveryPending, generation, epoch, monitoring,
rGen, rRunGen, rEpoch, rNeeds, reads, uVars, eVars, releaseFailed>>
\* C releaseFrame, one step under frameMutex (calls S release_frame inside).
RRelease ==
/\ rpc = "holding"
/\ rpc' = "idle"
/\ CASE ~frameHeld -> \* invalidated by stop: ESTATE, documented
UNCHANGED <<frameHeld, heldNeedsBackend, sFrameHeld, releaseFailed>>
[] frameHeld /\ heldNeedsBackend /\ ~sFrameHeld -> \* S rejects; C returns before clearing
/\ releaseFailed' = TRUE
/\ UNCHANGED <<frameHeld, heldNeedsBackend, sFrameHeld>>
[] OTHER ->
/\ frameHeld' = FALSE /\ heldNeedsBackend' = FALSE
/\ sFrameHeld' = IF heldNeedsBackend THEN FALSE ELSE sFrameHeld
/\ UNCHANGED releaseFailed
/\ UNCHANGED <<running, readInProgress, heldEpoch, runGen, sRunning, recoveryPending,
generation, epoch, monitoring, rGen, rRunGen, rEpoch, rNeeds, reads,
uVars, eVars>>
RFail ==
/\ rpc = "fail"
/\ readInProgress' = FALSE /\ rpc' = "idle"
/\ UNCHANGED <<running, frameHeld, heldNeedsBackend, heldEpoch, runGen, sessVars,
rGen, rRunGen, rEpoch, rNeeds, reads, uVars, eVars, releaseFailed>>
(***************************************************************************)
(* U: gvfg_stop_channel / gvfg_start_channel, serialized by the app *)
(***************************************************************************)
UBegin ==
/\ upc = "idle" /\ ops < MaxLifecycleOps
/\ IF AppQuiescesReader
THEN readerPaused' = TRUE /\ upc' = "quiesce"
ELSE upc' = "choose" /\ UNCHANGED readerPaused
/\ UNCHANGED <<chanVars, sessVars, rVars, uNeeds, ops, eVars, releaseFailed>>
\* App protocol (docs/GVFG_FRAME_FLOW_AND_STATE.md section 5): reader released its frame and is joined.
UQuiesced ==
/\ upc = "quiesce" /\ rpc = "idle"
/\ upc' = "choose"
/\ UNCHANGED <<chanVars, sessVars, rVars, uNeeds, ops, readerPaused, eVars, releaseFailed>>
\* C stop: running = false.
UStop ==
/\ upc = "choose" /\ running
/\ running' = FALSE /\ ops' = ops + 1 /\ upc' = "stop_frames"
/\ UNCHANGED <<readInProgress, frameHeld, heldNeedsBackend, heldEpoch, runGen,
sessVars, rVars, uNeeds, readerPaused, eVars, releaseFailed>>
\* C releaseHeldFrameForStop, frameMutex: invalidate the app's frame.
UStopFrames ==
/\ upc = "stop_frames"
/\ uNeeds' = (frameHeld /\ heldNeedsBackend)
/\ frameHeld' = FALSE /\ heldNeedsBackend' = FALSE
/\ upc' = "stop_backend"
/\ UNCHANGED <<running, readInProgress, heldEpoch, runGen, sessVars, rVars, ops,
readerPaused, eVars, releaseFailed>>
\* C releaseHeldFrameForStop: session->release_frame().
UStopBackend ==
/\ upc = "stop_backend"
/\ sFrameHeld' = IF uNeeds THEN FALSE ELSE sFrameHeld
/\ upc' = "stop_stream"
/\ UNCHANGED <<chanVars, sRunning, recoveryPending, generation, epoch, monitoring,
rVars, uNeeds, ops, readerPaused, eVars, releaseFailed>>
\* S stop_stream: running_ = false, events set, frame_held_ = false.
UStopStream ==
/\ upc = "stop_stream"
/\ sRunning' = FALSE /\ recoveryPending' = FALSE /\ sFrameHeld' = FALSE
/\ monitoring' = FALSE /\ upc' = "stop_join"
/\ UNCHANGED <<chanVars, generation, epoch, rVars, uNeeds, ops, readerPaused,
eVars, releaseFailed>>
\* S stop_stream: join the event thread, GvfgStopCapture.
UStopJoin ==
/\ upc = "stop_join" /\ epc = "idle"
/\ upc' = "idle" /\ readerPaused' = FALSE
/\ UNCHANGED <<chanVars, sessVars, rVars, uNeeds, ops, eVars, releaseFailed>>
\* C start: no-op while running. S start_stream: GvfgStartCapture,
\* frame_held_ = false, running_ = true, event thread started.
UStartSession ==
/\ upc = "choose" /\ ~running
/\ ops' = ops + 1
/\ IF sRunning
THEN UNCHANGED <<sRunning, sFrameHeld, recoveryPending, epoch, monitoring>>
ELSE /\ sRunning' = TRUE /\ sFrameHeld' = FALSE /\ recoveryPending' = FALSE
/\ epoch' = epoch + 1 /\ monitoring' = TRUE
/\ upc' = "start_channel"
/\ UNCHANGED <<chanVars, generation, rVars, uNeeds, readerPaused, eVars, releaseFailed>>
\* C start, frameMutex: ++runGeneration; running = true.
UStartChannel ==
/\ upc = "start_channel"
/\ runGen' = runGen + 1 /\ running' = TRUE
/\ upc' = "idle" /\ readerPaused' = FALSE
/\ UNCHANGED <<readInProgress, frameHeld, heldNeedsBackend, heldEpoch, sessVars,
rVars, uNeeds, ops, eVars, releaseFailed>>
(***************************************************************************)
(* E: S event_thread_proc -> handle_signal_event (format change / plug-in) *)
(***************************************************************************)
\* S handle_signal_event first block, under capture_mutex_ + state_mutex_.
ESignal ==
/\ epc = "idle" /\ monitoring /\ sRunning /\ signals < MaxSignalEvents
/\ generation' = generation + 1 /\ recoveryPending' = TRUE
/\ signals' = signals + 1 /\ epc' = "decide"
/\ UNCHANGED <<chanVars, sRunning, sFrameHeld, epoch, monitoring, rVars, uVars, releaseFailed>>
\* S handle_signal_event: restart only if the input format really changed.
EDecide ==
/\ epc = "decide"
/\ epc' \in {"wait_hold", "finish"}
/\ UNCHANGED <<chanVars, sessVars, rVars, uVars, signals, releaseFailed>>
\* S restart_stream_after_signal_change: wait for !frame_held_ || !running_.
EWaitHold ==
/\ epc = "wait_hold"
/\ ("RestartWaitsForRelease" \notin Guards \/ ~sFrameHeld \/ ~sRunning)
/\ epc' = "restart"
/\ UNCHANGED <<chanVars, sessVars, rVars, uVars, signals, releaseFailed>>
\* S restart_stream_after_signal_change under capture_mutex_: GvfgStopCapture + GvfgStartCapture.
ERestart ==
/\ epc = "restart"
/\ epoch' = IF sRunning THEN epoch + 1 ELSE epoch
/\ epc' = "finish"
/\ UNCHANGED <<chanVars, sRunning, sFrameHeld, recoveryPending, generation, monitoring,
rVars, uVars, signals, releaseFailed>>
\* S handle_signal_event end: recovery done.
EFinish ==
/\ epc = "finish"
/\ recoveryPending' = FALSE /\ epc' = "idle"
/\ UNCHANGED <<chanVars, sRunning, sFrameHeld, generation, epoch, monitoring,
rVars, uVars, signals, releaseFailed>>
(***************************************************************************)
Terminated ==
/\ rpc = "idle" /\ epc = "idle" /\ upc = "idle" /\ ops = MaxLifecycleOps
Next ==
\/ RBegin \/ RBeginLock \/ RWaitCheck \/ RWakeFail \/ RWakeFrame \/ RCapture
\/ ROsdKeep \/ ROsdEarlyRelease \/ RDeliver \/ RStopRelease \/ RRelease \/ RFail
\/ UBegin \/ UQuiesced \/ UStop \/ UStopFrames \/ UStopBackend \/ UStopStream
\/ UStopJoin \/ UStartSession \/ UStartChannel
\/ ESignal \/ EDecide \/ EWaitHold \/ ERestart \/ EFinish
\/ (Terminated /\ UNCHANGED vars)
Spec == Init /\ [][Next]_vars
(***************************************************************************)
(* Properties *)
(***************************************************************************)
\* (1) The buffer the app holds belongs to the driver stream that is running now.
HeldFrameIsCurrent == (frameHeld /\ heldNeedsBackend) => heldEpoch = epoch
\* (2) The app's single release of that frame will succeed (S still records the hold).
ReleaseWillSucceed == (frameHeld /\ heldNeedsBackend) => sFrameHeld
\* (2') Consequence observed by the app.
NoFailedRelease == ~releaseFailed
=============================================================================