# tinygrad: PSP ring write pointer never wraps — OOB write + permanent ring wedge **Found by hardware fuzzing of the PSP TA invoke interface. This is a tinygrad driver bug, NOT an AMD firmware vulnerability.** sOS behaves correctly; it refuses an out-of-range wptr. Fixed in `tinygrad/runtime/support/am/ip.py`. ## Symptom After exactly **1024 PSP ring submissions** since device init, every subsequent PSP command times out (`sOS ring not responding`). `is_sos_alive()` still returns True and the GPU keeps computing, but no PSP command ever completes again. Only a **PCIe link reset** clears it — a driver restart does not, because the wptr lives in a hardware register. ## Root cause `AM_PSP._ring_submit` advanced the write pointer without wrapping: ```python prev_wptr = self.adev.reg(f"{self.reg_pref}_67").read() self.adev.vram.view(self.ring_paddr + prev_wptr * 4, ctypes.sizeof(msg))[:] = ... self.adev.reg(f"{self.reg_pref}_67").write(prev_wptr + ctypes.sizeof(am.struct_psp_gfx_rb_frame) // 4) ``` `ring_size = 0x10000` and `sizeof(psp_gfx_rb_frame) = 64`, so the ring holds **1024 frames**. `prev_wptr` grows monotonically, so on submission 1024 the frame is written at `ring_paddr + 0x10000` — **64 bytes past the end of the ring allocation, into adjacent VRAM** — and the wptr register is set to `ring_size`, which sOS rejects. The kernel driver does this correctly in `psp_cmd_submit`: `psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw`. ## Evidence Measured directly, watching the wptr walk off the end: after 1004 invokes wptr=16320 byteoff=0xff00 (ring_size 0x10000) after 1005 invokes wptr=16336 byteoff=0xff40 after 1006 invokes wptr=16352 byteoff=0xff80 after 1007 invokes wptr=16368 byteoff=0xffc0 WEDGE after 1008 invokes FINAL wptr = 16384 -> byte offset 0x10000 OVERRUN=True Independently reproduced three times at the identical total count (1006 TA invocations + ~18 setup submits = 1024): * mixed-command fuzz, 15 hdcp commands * `cmd 0x03` alone, hang at iter 997 * `cmd 1`x400 -> `cmd 2`x400 -> `cmd 3`, hang at iter 197 (400+400+197 = 997) * **identical benign valid payloads**, wedge at invoke #1005 The last one matters: it is not a fuzz artifact. Any host that issues ~1024 PSP commands hits it, so long-running HDCP/DTM/RAS use would wedge the PSP. ## Fix 1. Wrap the wptr: `(prev_wptr + frame_size_dw) % ring_size_dw`, and mask the value read back from the register. 2. Make the fence value **monotonic and independent of the wptr**. It was `fence_value = prev_wptr + 1`; once the wptr wraps, that repeats, and a stale fence from the previous lap would satisfy `wait_cond` immediately — a silent "command completed" that never ran. Added `self.fence_value`, incremented per submit, and zeroed together with the fence buffer in `_ring_create`. ## Verification * 4000 consecutive PSP invokes = **3.9 laps** of the ring, no wedge; wptr wraps `16272 -> 48` and stays `< ring_size`. * 2500 PSP invokes interleaved with real GPU compute: matmul correct at every checkpoint, `sos_alive=True`, `MEMSIZE=0x5ff0`. * `DEV=AMD` normal operation unaffected: 512x512 matmul matches numpy, `Tensor.arange(1000).sum() == 499500`. ## Note on the security hunt This was found while hunting for a host-reachable *PSP firmware* vulnerability. It is not one. The OOB write is the driver corrupting 64 bytes of its own VRAM, self-inflicted and not attacker-controlled, and the wedge is sOS correctly rejecting a malformed wptr. Recorded honestly as a driver bug.