[1] WGSL `__ArrayLength` slots assigned in declaration order swap buffer bounds
Two WebGPU layouts looked identical. They disagreed on which buffer was which size.
High. The shader's bound for one storage buffer came from a sibling buffer's length word, so script picks both the index and the value of a write past a 16-byte allocation. The reach stays in GPU-visible memory; turning it into CPU-side compromise needs an isolation failure this change does not establish.
A shader compiler and the host-side resource layout code have to agree on an implicit binary layout, and a disagreement over which slot holds a buffer's size turns a bounds check into a check against the wrong buffer. WebKit's WGSL compiler, the translator from WebGPU's shading language to Metal, packs each bind group into an argument buffer (a struct of pointers and metadata at numeric indices) and adds a hidden __<name>_ArrayLength word, the per-buffer size that arrayLength() and runtime-array bounds handling rely on, for every runtime-sized array. The expectation is that two bind-group layouts that look identical to script also place those hidden words identically.
The angle: a page using WebGPU can get a small storage buffer bounded by a larger sibling's length and perform script-indexed, script-valued reads and writes past it in the GPU process's GPU-visible memory.
The commit message is terse: ensure buffer lengths are sorted by id to avoid clashes (rdar://185201177, webkit.org/b/323234). The regression test runs with Metal shader validation enabled.
Source/WebGPU/WGSL/GlobalVariableRewriter.cpp
LayoutTests/fast/webgpu/regression/repro_185201177.html
Patch Details
RewriteGlobalVariables::collectGlobals() makes three changes. The bufferLengths tuple widens from (variable, group) to (variable, group, binding) by also recording resource->binding. A new std::ranges::sort orders bufferLengths by std::pair { group, binding }, placed right after the existing per-group sort of m_groupBindingMap. The emission loop that synthesises one __<name>_ArrayLength variable per runtime-sized buffer updates its structured binding to [variable, group, webBinding]. The net effect: hidden length variables are now generated in (group, @binding) order rather than module-scope source order. The regression test declares group 1's bindings in reversed order and binds a group-0 bind group at index 1.
Hidden bounds metadata assigned slots in source declaration order, so layouts equal on their visible entries can disagree on which slot holds which buffer's length.
Background
WGSL resources.
WGSL resources are module-scope variables annotated @group(N) @binding(M). A runtime-sized array (array<T> with no length) has a size known only once a buffer is bound, and arrayLength(&x) returns it at run time. WebKit's compiler synthesises a hidden per-buffer word, __<var>_ArrayLength, which generated shader code reads whenever it needs that size.
Argument buffers on Metal.
Each WebGPU bind group becomes a Metal argument buffer: a struct of pointers, textures, samplers and the synthetic length words, each at a numeric index. The host-side BindGroup encodes values into those indices according to its BindGroupLayout; the BufferBindingType::ArrayLength entries in BindGroupLayout.mm are the host-side counterparts of the length words.
Auto layouts and layout reuse.
With layout: 'auto', a pipeline derives each group's BindGroupLayout from the shader via compiler reflection. WebGPU allows a bind group created from one layout to be used with another layout whose entries are equivalent, so the same layout description can serve multiple group slots.
collectGlobals.
RewriteGlobalVariables::collectGlobals is the pass entry point. It walks the module's declarations, records each resource's group and binding in m_groupBindingMap, packs resource types, and creates the synthetic length variables. It runs in the GPU process at pipeline creation.
Analysis
This is an OOB read/write on GPU device memory caused by a slot-assignment mismatch, not a missing check.
Group 0 source order Group 1 source order
@binding(0) a0 @binding(1) b1
@binding(1) a1 @binding(0) b0
len slots: [a0, a1] len slots: [b1, b0]
crossBindGroup (group 0 layout) set at index 1:
slot 0 = len(large, 1 MiB) ──► shader reads as len(b1)
slot 1 = len(small, 16 B) ──► shader reads as len(b0)
b1 is the 16-byte buffer, bounded by 1 MiB
└─► b1[a1[0] = 0x800] = a1[1] write lands past the allocation
Before the fix, collectGlobals appended to bufferLengths in the order declarations appeared and never sorted it, while user-visible entries in m_groupBindingMap were sorted by @binding. The order dependence follows from the fix reordering only this vector, and from the test's description of slots "chosen by @binding, not by declaration order"; the exact emission step that turns vector order into argument-buffer member indices is the one link that is inferred rather than demonstrated.
Each auto-generated layout should be internally consistent with its own group's declaration order, which would explain why the test's positive control (group 1 bound with getBindGroupLayout(1)) behaves. The broken invariant is narrower: two auto layouts with identical visible {binding, visibility, type} entries could place their length words at different indices. In the diagram, group 0 puts binding 0's length first and group 1 puts binding 1's first, so a group-0 bind group accepted at index 1 hands b1 the 1 MiB buffer's element count.
The trigger is fully web-reachable: a WGSL module with reversed declaration order in one group, a layout: 'auto' pipeline, and a bind group from one group's layout reused at the other index. The test places a 1 MiB buffer at binding 0 and a 16-byte buffer at binding 1; index 0x800 from a1[0] is then within the believed bound, and the store writes about 8192 bytes past the small allocation per the test comment. The same swap lets reads through, and arrayLength() itself reports the other binding's size. Whether the host-side compatibility check accepted the cross-group bind group depends on whether it compares hidden argument-buffer indices; the test treats rejection as a safe outcome, so acceptance is the conditional precondition for the write.
The primitive operates on GPU device memory relative to an attacker-chosen buffer's base, with attacker-supplied index and value, bounded by the swapped-in sibling's length. The open escalation question is what sits beyond a small storage buffer: buffers or textures from other WebGPU devices, or internal staging and indirect-argument buffers. If those could be placed reliably near an attacker-sized buffer, this would offer a data-corruption or disclosure primitive against them; reaching CPU-side GPU-process state would depend on Metal's GPU address-space isolation and the unified-memory mapping, which the supplied context does not establish. The shader runs on behalf of the GPU process, and this bug alone shows no path to CPU code execution or out of that sandbox.
This vulnerability weakens the WebGPU memory-safety boundary, which WebKit enforces in the shader compiler. WebGPU requires every shader access to a storage buffer to stay inside that buffer's bound range, and the per-binding __ArrayLength words are part of how that confinement is realised for runtime-sized arrays. Before the fix, web content could arrange for one buffer's accesses to be bounded by another buffer's length and perform attacker-indexed, attacker-valued reads and writes past a small storage buffer, which could corrupt or disclose other GPU-resident resources owned by the GPU process.
Insight: WebGPU safety on Metal depends on the compiler and the host-side layout code agreeing exactly on an implicit ABI — which index holds which pointer and which length. Each auto layout was self-consistent; the failure only appeared when one layout's encoding was used where a visibly identical one was expected. The fix removes the dependency by giving hidden slots a canonical (group, binding) order, and the general rule is that hidden per-binding metadata must either be a pure function of (group, binding) or be part of the layout-compatibility key.
Audit directions
- Compiler-synthesised hidden resources whose slot assignment depends on iteration order. The invariant is that every hidden slot index must be a pure function of the user-visible layout key. Narrow: grep
Source/WebGPU/WGSL/GlobalVariableRewriter.cppfor other vectors built inside them_shaderModule.declarations()walk and later turned into argument-buffer members —m_globalsUsingDynamicOffsetand the dynamic-offset buffer frominsertDynamicOffsetsBufferIfNeeded, packed-struct helper members, external-texture expansion members — and check that each is sorted by(group, binding)before slots are assigned. Wider: checkHashMap/ListHashSetiteration that feeds index assignment, such asm_lengthParameters, since hash or insertion order is not canonical either. Widest: the same class exists wherever a compiler emits a hidden ABI a separate runtime fills — SPIR-V descriptor remapping in Dawn and Tint, root-signature generation in naga and wgpu, Vulkan push-constant packing. In code review, aVectorappended during the declaration walk and consumed in a slot-assigning loop with no sort in between is the tell; at runtime, two programs with the same declared interface but different declaration order producing different hidden layouts. - Layout compatibility that compares only visible entries. Check whether WebGPU's bind-group-layout compatibility check, for
layout: 'auto'reuse across groups and pipelines, compares hidden entries such asBufferBindingType::ArrayLengthinBindGroupLayout.mmand their argument-buffer indices, or only the{binding, visibility, type}tuple. The invariant is that structural equality of visible entries must imply equality of the full encoded ABI. Narrow: trace howBindGroupLayoutequivalence is computed, and test pairs of auto layouts differing only in declaration order, texture/sampler pairing, or external-texture expansion. Wider: apply the same question toPipelineLayoutsharing across vertex, fragment and compute stages, where stages may see different subsets of length words throughbuffersForStage. Widest: any capability or type system that compares a public signature while acting on a richer private representation — ABI-compatible structs with different padding, FFI bindings with matching names but different layouts. Tell: an equality function that iterates only user-declared entries while the encoder also writes synthesised ones. - Decoupled-guard OOB — a bound fetched from a different object than the one indexed. Narrow: audit every place the WGSL Metal code generator emits bounds handling or
arrayLengthlowering, and verify that the length expression is tied to the same variable as the pointer throughm_bufferLengthMapandm_reverseBufferLengthMap, not through positional correspondence. Wider: inBindGroup.mm, check that the host writes each buffer's size into the length index associated with that buffer's binding, especially when dynamic offsets orminBindingSizechange the effective size. Widest: any runtime storing (pointer, length) as separately addressed fields — sanitizer fat pointers, Wasm memory64 bounds tables, JIT array-length caches — can lose bounds safety through a pairing error with no missing check. Tell: a pointer and its bound fetched through two independent index computations instead of one struct or tuple.