[3] drawIndexedIndirect clamp verdict survives an index-buffer rewrite
WebGPU proved a draw safe once, then kept trusting the proof after the indices changed.
High, and deterministic. A cached "safe to skip the clamp" verdict ignored the one buffer its result was computed from, so script-chosen indices drive vertex fetches past the bound buffer on the GPU. The ceiling is a read-only GPU-memory disclosure, gated on driver robustness behaviour and what neighbours the vertex buffer.
Indexed draws on Metal fetch per-vertex data at index * stride without bounds checking, so a WebGPU implementation has to validate index values itself before the GPU runs. For drawIndexedIndirect — a draw whose count and offsets live in a GPU-side args buffer instead of the CPU call — WebKit runs a clamp pass that scans the index buffer and zeroes the draw count if any index is out of range. Because that pass costs GPU time, its verdict is cached, and the expectation is that any change to an input the verdict read invalidates the cache.
The angle: a page can validate a draw once with in-range indices, rewrite only the index buffer, and have the next identical draw skip the clamp, so script-chosen indices drive GPU vertex fetches out of bounds.
The commit message (rdar://182324564): a stale cache leads to drawIndexedIndirect performing an OOB read on the GPU cores.
LayoutTests/fast/webgpu/regression/draw-indexed-indirect-stale-clamp-after-index-write.html
Patch Details
The production hunks are not part of this write-up; the regression test above shows the defect's shape. The test's description names the functions involved, indirectIndexedBufferRequiresRecomputation() and indirectIndexedBufferRecomputed(). The test sets up a 3-vertex buffer (minVertexCount 3), an index buffer holding 0,1,2,0, and an args buffer written once with indexCount 3. Frame 0 draws and caches a passing verdict on the args buffer. The test then rewrites only the index buffer with 3,4,5,0 and draws again with every other parameter unchanged, expecting frame 1 to show the red clear (clamp re-ran and zeroed indexCount) instead of green (draw executed). The fix makes the cached verdict sensitive to index-buffer content changes. Buffer.h declares a per-buffer m_indexContentsGeneration bumped by indexBufferContentsModified(...), which is a plausible new invalidation key, though the exact mechanism is not established here.
Memoized validation verdict cached on one object but derived from another object's mutable contents, which are absent from the invalidation key.
Background
Indexed draws.
drawIndexed/drawIndexedIndirect read a list of uint16 or uint32 indices from an index buffer. Each index selects which vertex's attributes to fetch, at index * arrayStride within each vertex-step-mode vertex buffer.
Indirect draws.
drawIndexedIndirect(argsBuffer, offset) reads indexCount, instanceCount, firstIndex, baseVertex and firstInstance from a GPU buffer in the MTLDrawIndexedPrimitivesIndirectArguments layout, so the CPU does not know them at encode time.
minVertexCount and robust access.
minVertexCount is the smallest number of vertices any bound vertex-step-mode buffer can supply (size divided by arrayStride); indices at or above it name vertices that don't exist. WebGPU requires that out-of-range accesses never read outside the bound resource. Metal's indexed fetch doesn't provide that, so WebKit's backend validates itself — for indirect indexed draws, with the clamp pass.
Validation caching.
Buffer keeps caches recording that a draw configuration has already been proven safe, so identical later draws skip validation. The indirect-indexed verdict is managed by indirectIndexedBufferRecomputed / indirectIndexedBufferRequiresRecomputation. Buffer.h also tracks a per-write index content generation (m_indexContentsGeneration) and monotone upper bounds on index values ever written (m_indexValueUpperBoundUint / m_indexValueUpperBoundUshort).
Analysis
This is a stale validation cache — an incomplete cache key — with an out-of-bounds GPU read as the consequence.
Before: After:
frame 0: clamp pass, 0,1,2 < 3 frame 0: clamp pass, verdict cached
verdict cached on argsBuffer
writeBuffer(indexBuffer, 3,4,5) writeBuffer(indexBuffer, 3,4,5)
argsBuffer untouched index-content change observed
frame 1: predicate compares frame 1: verdict invalid
offset, minVertexCount, ... == └─► clamp pass re-runs
└─► clamp skipped └─► indexCount = 0
└─► fetch vertex[3..5] OOB └─► red clear, no draw
The verdict is a function of two buffers, but the left column's predicate compared only args-buffer-side terms: indirectOffset, minVertexCount, minInstanceCount, indexType, indexBufferOffsetInBytes and drawType, per the test description. The writeBuffer arrow bumps the index buffer's generation and leaves the args buffer untouched, so frame 1's comparison still matches and new index values are never bounds-checked. The test description stresses that "no high-water-mark trick is needed": the args-buffer verdict didn't observe index-buffer writes at all, independently of the monotone upper-bound tracking.
The test makes the result observable without depending on fetched attribute values (it uses @builtin(vertex_index)), reads back one pixel per frame, and shows that with a stale verdict the draw would "skip the clamp, draw green, and fetch vertex attributes past the end of the 3-vertex buffer". Reachability is two ordinary drawIndexedIndirect calls sharing an unchanged args buffer with a writeBuffer on the index buffer between them, and the skip is deterministic.
The primitive is a GPU-side out-of-bounds read relative to the vertex buffer's base, at an offset of script-chosen index times arrayStride — up to 65535 * arrayStride for uint16 and 2^32 * arrayStride for uint32. It is read-only: the skipped pass guards only index-driven vertex fetches. Whether it produces a real out-of-bounds memory read assumes the Metal driver or hardware applies no robustness of its own that would zero or clamp the fetch. If fetched values were routed into a readable render target — which the WebGPU API makes straightforward — and neighbouring memory held another context's data, this would amount to a GPU-memory disclosure bounded by attribute format and draw count. Encoding happens in the GPU process, but the access runs on shader cores, so this bug alone gives no code execution or sandbox escape.
This vulnerability weakens the WebGPU robustness boundary. The security model assumes no draw issued by web content can make shader cores read outside the buffers bound to it, and WebKit enforces that with the clamp pass. Before the fix, script-chosen indices drove vertex fetches past the end of the vertex buffer; if those reads landed in other GPU allocations and flowed into output readable via copyTextureToBuffer or mapAsync, this would be a GPU-memory information disclosure reachable from web content, with cross-origin reach depending on GPU memory layout.
Insight: Buffer.h already carries two kinds of index-content tracking — a per-write generation and monotone high-water marks. The generation changes on every write, so it's the sound invalidation key for any cached proof that depends on exact contents; the upper bound is only sound for proofs that depend solely on the max. Any cache in this file that keys on the monotone bounds, or is stored on a different buffer than the one whose contents it read, deserves the same scrutiny — and this week's appendix carries three siblings (cb21dfb, fb81a00, 45270f2) in the same cache family.
Audit directions
- Memoized validation verdict whose cache key omits one of its inputs. A cached proof must be invalidated by every mutation of every object it read, not only the object it's stored on. Narrow: enumerate the other skip-validation caches in
Source/WebGPU/WebGPU/Buffer.mm(canSkipDrawIndexedValidation,skippedDrawIndexedValidation,skippedBundleIndirectDrawValidation,didReadOOB) and list, for each, which buffers' contents the verdict depended on versus which generations it compares; a hit is a verdict that reads buffer B but compares state stored only on buffer A. Wider: RenderBundle and indirect command buffer reuse, where a pre-validated draw is replayed after a dependent vertex or index buffer changed, and bind-group size validation cached across rebinding. Widest: any validator memoizing a safety verdict — Dawn and wgpu indirect-draw validation, ANGLE's element-array range cache, JIT inline-cache validity keyed on a subset of guard inputs. Tell: a cache key that is a tuple of parameters missing one input the proof actually dereferenced. - Invalidation driven by monotone summaries instead of per-write generations. Running maxima, high-water marks and "min validated" values only fire when the summary moves, so writes that stay below it but change contents are invisible. Narrow: audit every reader of
m_indexValueUpperBoundUint,m_indexValueUpperBoundUshort,allIndexValuesBelow,needsIndexValidationandm_minValidatedVertexCountinBuffer.mmandRenderPassEncoder.mm; a hit is a skip decision taken from the upper bound alone while a cached per-draw verdict also exists. Wider: any "dirty only if the bound grew" optimization, such as vertex-buffer size caches or texture-usage summaries. Widest: the general summary-statistic-as-invalidation class, also seen in GL driver index-range caches and database query-plan caches keyed on statistics. In review, astd::max-style update feeding an equality or less-than check that gates skipping work is the shape to stop on. - Index-buffer write paths that bypass the generation counter. Trace whether every path that can change index-buffer contents calls
indexBufferContentsModifiedor otherwise bumpsm_indexContentsGeneration. Narrow: compareQueue::writeBufferagainstBuffer::copyFrom, unmap after a mapped write,copyBufferToBuffer/copyTextureToBufferinto an INDEX buffer,clearBuffer, and compute-shader writes to a buffer created withINDEX | STORAGE— a GPU-side write is a hit unless it forcesm_mustTakeSlowIndexValidationPath, since the CPU cannot learn the new max. Wider: vertex buffers feedingminVertexCount, and args buffers written by compute (INDIRECT | STORAGE), wherecontentsGeneration()must also advance. Widest: any CPU-side shadow of GPU-writable memory, as in Dawn or ANGLE robustness layers, is sound only if every GPU write either updates the shadow or disables the shortcut. Tell: a write entry point to a buffer with INDEX or INDIRECT usage that never touches a generation counter.