From f18608a11bb69f99eb41ac21a2f537aa64dc4b5b Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 5 Aug 2026 06:05:04 +0300 Subject: [PATCH] :bug: Two decoded records fell through a guard into the no-op catch-all, and one of them loses a whole ICB batch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `runtime/exec.rs`, both pathways. ## The shape The render dispatch has seven guarded match arms. Five guard on a `has_*` flag — the decoder saying the record did not carry that field — so a record that fails one of those has nothing to apply and falling through to `_ => {}` costs nothing. Two guarded on a **decoded value**, which is a different thing entirely, and both landed in the same catch-all with no line and no counter: RenderKind::SetScissor if cmd.scissor_w > 0 && cmd.scissor_h > 0 RenderKind::ExecuteCommands if cmd.indirect_command_buffer_ref != 0 **The scissor one substitutes.** A guest replacing its scissor with an empty rect left `acc.scissor` holding the *previous* rect, so every draw after it clipped to a region the guest had just retired. That is wrong pixels rather than missing ones — the class nothing downstream can detect. **The ICB one is the largest loss a single render record can carry.** Unlike a `setTexture:atIndex:` with `ref == 0`, which is an unbind and therefore a meaning, `executeCommandsInBuffer:` has no argument to unbind: a zero ref is either a short decode or a stream naming a buffer it never defined, and the whole batch of commands that ICB holds is not executed. `AGENTS.md` names "an intentionally unbound `ref == 0`" as expected control flow; this is not one. ## What changed Both arms are now unguarded, with the false case handled explicitly by `note_empty_scissor` and `note_unnamed_icb_execute`. Each emits a `store_routes` count plus one deduped fail line — the count so a reader can see a rate, the line so the count is not the only record that guest work was lost. **Behaviour is unchanged in both.** An empty rect still does not become the scissor: this rail cannot express "clip everything", and adopting a degenerate rect would make the next draw's clip depend on how the backend handles one. A zero ICB ref still queues no execute. Only the substitution becomes visible. The ICB line carries the range and args-buffer fields because they are what tells the two causes apart: all-zero alongside the zero ref reads as a record never populated, while a plausible range with a zero ICB names a resource the stream lost. ## Verification - New test `a_decoded_record_that_no_arm_applies_names_what_happened_instead` drives both, asserts each counter moves by exactly one, asserts both fail lines, and asserts the surviving state is unchanged — so it cannot pass by altering behaviour. - **Mutation probe.** With both arms reverted to their guarded form the test FAILS; restored, it passes. - `cargo test -p reims-vgpu --no-default-features --features backend-vulkan,host-window -- --test-threads=1`: 1351 lib + 77 integration pass, 0 fail. - `cargo clippy --all-targets -- -D warnings` clean on Vulkan/host-window and on the `aarch64-apple-darwin` `backend-metal` arm. `cargo fmt` clean. ## The copying-rail boot the earlier commits on this branch owed Every `footprint` hook removed today sat on a guest-write rail, so `AGENTS.md` requires the boot a second time with `REIMS_VGPU_DMABUF=off`. Run, and all three prescribed confirmations hold: `vk_caps … dma_buf_import=disabled_by_env`, `guest_dmabuf_export off reason=backend_cannot_import` exactly once, and every `guest_dmabuf_*` counter absent from the log. Driven with the window-drag probe: 3 226 reposition events over 25.0 s at 128.9 Hz, `present_hz` median 18.85, `drain_duty` median 0.96, 2 555 MB/s written — the copy path genuinely carrying the frames. Fail-channel reasons were 23 `gpuwb_no_dmabuf` (this arm's expected decline), 11 `gva_zero_pfn`, and one each of `translation_moved`, `mapping_page_drift`, `llvm_vector_alloc_stride`, `backend_cannot_import`. Both cells of the memory matrix are therefore green on this branch. Not verified: neither new line fires on either boot, because the guest emits neither shape. They are alarms, and their invariant is zero. --- crates/reims-vgpu/src/runtime/exec.rs | 145 +++++++++++++++++++++++++- 1 file changed, 142 insertions(+), 3 deletions(-) diff --git a/crates/reims-vgpu/src/runtime/exec.rs b/crates/reims-vgpu/src/runtime/exec.rs index 78b997a9d..320be63f3 100644 --- a/crates/reims-vgpu/src/runtime/exec.rs +++ b/crates/reims-vgpu/src/runtime/exec.rs @@ -1458,9 +1458,13 @@ fn handle_render_record( note_extra_state_entries("viewport", cmd.count); acc.viewport = Some(cmd.viewport); } - RenderKind::SetScissor if cmd.scissor_w > 0 && cmd.scissor_h > 0 => { + RenderKind::SetScissor => { note_extra_state_entries("scissor", cmd.count); - acc.scissor = Some((cmd.scissor_x, cmd.scissor_y, cmd.scissor_w, cmd.scissor_h)); + if cmd.scissor_w > 0 && cmd.scissor_h > 0 { + acc.scissor = Some((cmd.scissor_x, cmd.scissor_y, cmd.scissor_w, cmd.scissor_h)); + } else { + note_empty_scissor(task_id, cmd.scissor_w, cmd.scissor_h); + } } RenderKind::SetBlendColor if cmd.has_blend_color => { acc.blend_color = Some(cmd.blend_color); @@ -1688,7 +1692,11 @@ fn handle_render_record( }); } } - RenderKind::ExecuteCommands if cmd.indirect_command_buffer_ref != 0 => { + RenderKind::ExecuteCommands => { + if cmd.indirect_command_buffer_ref == 0 { + note_unnamed_icb_execute(task_id, &cmd); + return; + } acc.execute_icb = Some(RenderIcbExecute { icb_ref: cmd.indirect_command_buffer_ref, is_range: cmd.icb_is_range, @@ -2078,6 +2086,64 @@ fn note_extra_state_entries(what: &'static str, count: u32) { ); } +/// A `setScissorRect:` naming a rect with no area. +/// +/// The five `has_*` guards beside this one in the render dispatch are the +/// decoder saying a record did not carry a field, so falling through them costs +/// nothing. This guard was not that: it tested a *decoded* value, and a record +/// that failed it reached the match's `_ => {}` and left `acc.scissor` holding +/// the **previous** rect. So every draw after it clipped to a region the guest +/// had just replaced, which is wrong pixels rather than missing ones — the +/// class that nothing downstream can detect. +/// +/// Behaviour is unchanged: an empty rect still does not become the scissor, +/// because this rail has no way to express "clip everything" and adopting a +/// zero rect would make the *next* draw's clip depend on how the backend +/// handles a degenerate one. What changes is that the substitution is now +/// visible. Deduped on the pair, because a guest emitting one emits it for a +/// reason that does not vary per record. +fn note_empty_scissor(task_id: u32, w: u32, h: u32) { + crate::runtime::drain::note_store_route("render_scissor_empty_kept_previous"); + if crate::observe::first_sight("render_scissor_empty", (u64::from(w) << 32) | u64::from(h)) { + crate::observe::fail(format!( + "render_scissor_empty reason=render_scissor_empty_kept_previous \ + task={task_id} w={w} h={h} \ + (the guest replaced its scissor with an empty rect; this rail kept \ + the previous one, so later draws clip to a region the guest \ + retired)" + )); + } +} + +/// An `executeCommandsInBuffer:` naming no buffer. +/// +/// Unlike a `setTexture:atIndex:` with `ref == 0` — an unbind, which is a +/// meaning — an ICB execute has no argument to unbind. A zero ref is either a +/// record this device decoded short or a stream that named a buffer it never +/// defined, and either way the entire batch of commands that ICB holds is not +/// executed. That is the largest single loss any one render record can carry, +/// and it used to fall through the arm's guard into `_ => {}`. +/// +/// The other fields are on the line because they are what says which of the two +/// it is: a record whose range and args buffer are also zero was probably never +/// populated, and one carrying a plausible range with a zero ICB names a +/// resource the stream lost. +fn note_unnamed_icb_execute(task_id: u32, cmd: &crate::runtime::decode::render::Command) { + crate::runtime::drain::note_store_route("render_icb_execute_unnamed"); + if crate::observe::first_sight("render_icb_execute_unnamed", u64::from(task_id)) { + crate::observe::fail(format!( + "render_icb_execute reason=render_icb_execute_unnamed task={task_id} \ + is_range={} range_loc={} range_len={} args={} args_off={:#x} \ + (executeCommandsInBuffer named no buffer; the whole batch is lost)", + cmd.icb_is_range as u8, + cmd.icb_range_location, + cmd.icb_range_length, + cmd.icb_args_buffer_ref, + cmd.icb_args_buffer_offset + )); + } +} + /// Which of the three argument tables a bind record names. /// /// [`apply_binds`] gates all three on the single [`MAX_BIND_SLOTS`], and that @@ -5668,6 +5734,79 @@ mod tests { ); } + /// Two guarded arms used to drop a decoded record into the `_ => {}` + /// catch-all, and both now name what they did instead. + /// + /// The five `has_*` guards beside them are the decoder saying a field was + /// absent, so falling through those costs nothing. These two tested decoded + /// *values*, which is a different thing: an empty scissor leaves the + /// previous rect clipping later draws, and an ICB execute naming no buffer + /// loses the whole batch that buffer holds. + #[test] + fn a_decoded_record_that_no_arm_applies_names_what_happened_instead() { + use crate::runtime::drain::store_route_count; + + let mut state = DeviceState::new(DeviceId(1), PAGE_SHIFT_ARM64E); + let host = FakeHost::new(); + let mut out = ExecResult::default(); + let mut acc = StreamAccum::default(); + + // `MTLScissorRect` is four NSUInteger. Set a real rect, then replace it + // with an empty one and check the real one is what survives. + let scissor = |w: u64, h: u64| { + let total = wire_render::SET_SCISSOR_TOTAL_LEN as usize; + let mut command = vec![0u8; total]; + let op = wire_render::OPCODE_SET_SCISSOR; + st32(&mut command[0..], op); + st32(&mut command[4..], total as u32); + let p = reims_vgpu_wire::OP_HEADER_LEN; + st64(&mut command[p..], 7); + st64(&mut command[p + 8..], 9); + st64(&mut command[p + 16..], w); + st64(&mut command[p + 24..], h); + (op, command) + }; + let (op, command) = scissor(64, 32); + handle_render_record(&mut state, &host, 1, op, &command, &mut out, &mut acc); + assert_eq!(acc.scissor, Some((7, 9, 64, 32))); + + let before = store_route_count("render_scissor_empty_kept_previous"); + let (op, command) = scissor(0, 32); + handle_render_record(&mut state, &host, 1, op, &command, &mut out, &mut acc); + assert_eq!( + store_route_count("render_scissor_empty_kept_previous") - before, + 1, + "an empty scissor must name itself" + ); + assert_eq!( + acc.scissor, + Some((7, 9, 64, 32)), + "and behaviour is unchanged: the previous rect is still what is kept" + ); + + // `executeCommandsInBuffer:` naming no buffer. + let before = store_route_count("render_icb_execute_unnamed"); + let total = wire_render::EXECUTE_COMMANDS_INDIRECT_TOTAL_LEN as usize; + let mut command = vec![0u8; total]; + let op = wire_render::OPCODE_EXECUTE_COMMANDS_INDIRECT; + st32(&mut command[0..], op); + st32(&mut command[4..], total as u32); + handle_render_record(&mut state, &host, 1, op, &command, &mut out, &mut acc); + assert_eq!( + store_route_count("render_icb_execute_unnamed") - before, + 1, + "an ICB execute naming no buffer loses the whole batch and must say so" + ); + assert!( + acc.execute_icb.is_none(), + "and still does not queue an execute against ref 0" + ); + + let log = std::fs::read_to_string(crate::observe::fail_log_path()).expect("fail log"); + assert!(log.contains("reason=render_scissor_empty_kept_previous")); + assert!(log.contains("reason=render_icb_execute_unnamed")); + } + /// A buffer-offset record that lands on nothing says so, both ways. /// /// `setVertexBufferOffset:atIndex:` is the second record the guest spends on