Shaders: keep native root parameters inside the SGPRs, clamp the thread-id count, and name each blob refusal #333

Closed
github-bot wants to merge 83 commits from gh-pr/333/head into gh-pr/333/base
Owner

@⁠SkippySteve on GitHub · 2026-11-24 02:21 UTC · original
shader-native-root-sgprs → main · +5036 −22 · 1 files · 83 commits · closed without merging

Thirteen changes to crates/victory-shader/src/native.rs: twelve fixes, and tests that name each refusal. This PR also replaces #330 and #339, which edited the same file.

Fix: root parameters must sit in the SGPRs

The FSSM root layout gives each root parameter a user-data byte and an SGPR count, both read straight from the blob. Resource tracking (resource.rs) turns them into registers with HwStage::user_sgpr. For merged and mesh stages that does index + 8 as a u8, and a root descriptor's high half adds one more.

A corrupt or unexpected record with a user-data byte of 248 or more on a merged stage overflows that add. Debug builds panic. In release the register wraps to a low SGPR, so inputs are attributed to the wrong root parameter with no error.

Such records are now rejected when they're read. A parameter whose SGPRs run past s105, the last SGPR before VCC, returns Malformed("root parameter outside the SGPRs"), the same way the parser already handles other out-of-range fields. Real layouts use low user SGPRs and are unaffected.

reads_root_tables checks the boundary with a synthetic fixture: s9 plus 97 dwords (ending at s105) is accepted, 98 dwords is rejected, and so is a user-data byte of 0xFF.

Fix: a thread-id count field of 3 still means three components

COMPUTE_PGM_RSRC2.TIDIG_COMP_CNT is a 2-bit field, and ComputeIo::thread_id_vgprs returned it plus one: 4 for a value of 3. io::raise then labelled v3 as Input::ThreadIdInGroup(3), and lowering asked DXIL's threadIdInGroup for component 3, which does not exist (the console-lanes path quietly treated it as z instead).

A thread id has only x, y and z, so the field is clamped to 2 before adding one: v0-v2 are the thread id and v3 stays an ordinary VGPR. A synthetic unit test covers the field at 3. (Previously #339.)

Fix: a vertex input must load into the VGPRs

When io::raise handles a native vertex shader, it loads input channel c from Reg::V(vgpr + c). vgpr is a byte read straight from the stage data, so it can hold any value. An input at v253 or above with a later channel enabled overflows that u8 add. Debug builds panic. In release the register wraps, so a low VGPR is labelled as the attribute and there is no error.

This is the same kind of bug as the SGPR fix above, and it is fixed the same way. When the stage data is read, an input whose last enabled channel falls past v255 returns Malformed("vertex input outside the VGPRs"). Only the VS path checks this, because mesh stages discard these inputs. names_each_stage_data_refusal covers the boundary:

  • accepted: v252 with all four channels (ending at v255), v255 with x only, v255 with no channels enabled
  • refused: v253 with all four channels, v255 with y

Fix: a compute group must be one numthreads allows

A compute record's thread-group size is three u32s read straight from the stage data. Lowering multiplies them (cs.threads.iter().product::<u32>() in emit/lower.rs, to decide whether the group fits one wave) and writes them as the DXIL numthreads. A corrupt record of 65536 × 65536 × 1 overflows that product: debug builds panic, and in release it wraps to 0, so the group is treated as fitting in one wave. A zero component instead produces a numthreads that DXIL rejects.

Such records are now refused when they're read, with Malformed("implausible thread-group size"), using D3D12's limits: each component at least 1, x and y up to 1024, z up to 64, and at most 1024 threads in total. Real HLSL can't exceed these. The bounds are checked per component before the product is taken, so that check can't overflow either. The synthetic fssm helper now writes numthreads(1, 1, 1) for compute records. names_each_stage_data_refusal covers each limit: [1024,1,1], [1,1024,1], [1,16,64], [32,32,1] are accepted; [0,1,1], [1,1,0], [1025,1,1], [1,1,65], [64,32,1], [65536,65536,1] and u32::MAX in all three are refused.

Fix: a mesh group must have a lane

A mesh record's thread-group size is a u16 read straight from the stage data. Lowering writes it as the DXIL numthreads x, and for containers whose PSV0 lacks the HLSL thread count (runtime info before version 2) it is also the stride the group id is multiplied by (mesh_stride in emit/mod.rs). mesh_prologue already refuses groups over 128 lanes, but a record of 0 passed: it emitted numthreads(0, 1, 1), which DXIL rejects, and on the fallback path gave every group id 0 with no error.

A zero mesh group is now refused when it's read, with the compute path's Malformed("implausible thread-group size"). The synthetic fssm helper writes a one-lane mesh group, and names_each_stage_data_refusal covers both: one lane is accepted, zero is refused.

Fix: a mesh group's LDS must fit a thread group

A mesh record's LDS size is a u32 read straight from the record (+0x4c). Lowering declares the LDS as lds_bytes.div_ceil(4) dwords, then writes it back to PSV0 as lds_dwords * 4 (emit/shader.rs), a u32 product. A corrupt record near u32::MAX overflows it: debug builds panic, and in release it wraps to a tiny size, so the container states far less LDS than the shader declares.

A GFX10 thread group addresses at most 64 KiB of LDS, so larger sizes are refused when they're read, with Malformed("implausible LDS size"). names_each_stage_data_refusal accepts exactly 64 KiB and refuses one byte more and u32::MAX; with the check disabled, the test fails.

Fix: a pixel program's export patches must not overlap

A pixel record holds an export patch per render target: a code offset (in dwords) and replacement code of a shared size. NativeProgram::patched_code writes them in MRT order. Each patch was already checked to lie inside the code, but not against the others. If a corrupt record gives two MRTs patches that share dwords, the later one overwrites part of the earlier export. The shader then exports the wrong values, or decodes a broken instruction, and there is no error.

Such records are now refused when they're read, with Malformed("export patches overlap"). names_each_stage_data_refusal uses two-dword patches for MRT0 and MRT1 in a four-dword program:

  • accepted: offsets 0 and 2, offsets 2 and 0, and a patch size of 0 (nothing to overlap)
  • refused: offsets 0 and 1, 1 and 0, and 1 and 1

With the check disabled, the test fails.

Fix: a root descriptor's high half must sit in the SGPRs too

Resource tracking (resource.rs) reads a root descriptor's high half from user_sgpr(user_data) + 1, whatever SGPR count the record states. The SGPR check above reserved max(sgprs, 1) registers, so a descriptor that states 0 or 1 SGPRs and sits at s105 passed it. Its high half was then s106, which is VCC_LO, and was used as the descriptor's address with no error.

For this check, a root descriptor now spans at least two SGPRs. reads_root_tables covers it for stated counts of 0, 1 and 2: a descriptor at s104 is accepted and one at s105 is refused with Malformed("root parameter outside the SGPRs"). A table at s105 still takes a single SGPR and is accepted. Its existing boundary cases now check the exact message instead of only is_err(). With the fix reverted, the test fails.

Fix: a compute group's LDS must fit a thread group

A compute record's LDS size comes from COMPUTE_PGM_RSRC2.LDS_SIZE, a 9-bit count of 512-byte granules (ComputeIo::lds_bytes). That lets a corrupt record state up to 255.5 KiB, and lowering declares that much as the group's shared memory (emit/lower.rs). The mesh path already refuses more than the 64 KiB a GFX10 thread group can address, but the compute path didn't.

Compute records now apply the same limit when they're read, with the same Malformed("implausible LDS size"). names_each_stage_data_refusal accepts 128 granules (exactly 64 KiB) and refuses 129 and the field's maximum of 511. With the check disabled, the test fails.

Fix: a compute root parameter must not cover a system SGPR

A compute program's group ids and group info arrive in the SGPRs right after its user SGPRs, as COMPUTE_PGM_RSRC2 enables them (ComputeIo::system_sgprs), and io::raise labels them there. The layout's SGPR check only made sure each parameter fits below s106, so a corrupt layout could state a root parameter over one of those registers. That register was then both a root parameter (to resource tracking) and a group id (to raising), with no error.

Compute records now refuse such a layout when they're read, with Malformed("root parameter over a system SGPR"). Only an actual overlap is refused: a parameter past the enabled system SGPRs, or one where no group id is enabled, is still read. A root descriptor counts its high half, as in the SGPR check. compute_root_params_stay_off_system_sgprs uses 4 user SGPRs with group id x, y and the group info in s4-s6:

  • accepted: constants in s0-s3, a table in s3, constants in s7, and constants in s4 with no system SGPRs enabled
  • refused: five dwords from s0, a one-SGPR descriptor in s3 (high half s4), constants in s5, a table in s6

With the check disabled, the test fails.

Fix: a vertex input must not load into a system VGPR

A native vertex program receives its vertex, primitive and instance id in v0, v2 and v3, and io::raise labels those registers first. It then labels each enabled input channel c at v[vgpr + c], and an input's value is matched to the first label its register has. A corrupt input that loads a channel into v0, v2 or v3 was therefore read as that id, with no error.

Such stage data is now refused when it's read, with Malformed("vertex input over a system VGPR"). Only an actual overlap is refused: v1 carries no id here, and inputs from v4 up are unaffected. names_each_stage_data_refusal accepts x in v1, x-w in v4 and an input in v0 with no channels enabled. It refuses x in v0, v2 or v3, and y or z of an input in v1. With the check disabled, the test fails.

Native outputs must not share a signature row

io::raise turns each exp param<n> into a write of its output entry's signature row, and lowering keys 16-bit outputs by that row (half_outputs). Two output entries naming one row therefore wrote the same signature element: the later export silently replaced the earlier, and a 16-bit entry's packing was overwritten by the other's.

Such stage data is now refused when it's read, with Malformed("vertex outputs share a row"). Per-vertex rows (OSG1) and per-primitive rows (PSG1, param bit 7) are separate signatures, so a vertex output and a per-primitive output in the same row number stay allowed. refuses_outputs_sharing_a_row covers VS and MS: distinct rows pass, a vertex and a primitive output both in row 0 pass, and two vertex outputs or two primitive outputs in one row are refused.

Fix: only one table's ranges may share a root parameter's SGPR

params_apart (the "root parameters share an SGPR" check) let any two layout entries with the same slot number cover the same SGPR, so the ranges of one table could share its address SGPR. But a matching slot byte was the only thing it compared. A table range and constants that state the same slot, the same constants listed twice, or one slot's constants overlapping themselves at a shifted user-data dword all got through. resource::param_of maps an SGPR to the first entry covering it, so the second entry's reads were attributed to the first with no error.

Entries may now share an SGPR only when both are table ranges with the same slot and the same table SGPR. Anything else is refused with Malformed("root parameters share an SGPR"). refuses_root_parameters_sharing_an_sgpr now also accepts three ranges of one table and refuses four more cases: constants in a table's SGPR stating its slot, one slot's constants overlapping themselves, constants listed twice, and a root descriptor listed twice.

Fix: a vertex output must not be marked per-primitive

Bit 7 of an output entry's parameter byte marks a mesh program's per-primitive output, a row of PSG1. io::raise applies it on any stage, so a plain vertex program's output with that bit became Target::PrimitiveParam. A vertex program has no primitive signature (prims is empty), so lowering found no element and dropped those exports with no error. Such an output was also skipped by the shared-row check (which keeps primitive rows separate) and by half_outputs.

Vertex stage data with such an output is now refused when it's read, with Malformed("vertex output marked per-primitive"). Mesh programs are unaffected. refuses_outputs_sharing_a_row still accepts a per-primitive output in row 0 next to a vertex output there for MS. For VS it now refuses that case, 0x81 in row 1 and 0xFF in row 7. With the check disabled, the test fails.

Fix: a table or heap root parameter takes one SGPR

A descriptor table, resource heap or sampler heap parameter is a single 32-bit address, but its SGPR count is read straight from the layout entry and any count was accepted. Resource tracking (resource::param_of) maps every stated SGPR to that parameter. The host fills each dword of a table with the table's heap offset (draw::user_value ignores the dword), and lowering reads a heap's as 0. So in a corrupt layout where a table states two or more SGPRs, the registers after the first silently read the table address, with no error.

Such layouts are now refused when they're read, with Malformed("root address wider than one SGPR"). A count of 0 is still accepted, as a further range of one table may state. Constants and root descriptors take what they state, as before. refuses_root_parameters_sharing_an_sgpr accepts 0 and 1 SGPRs for a table and for each heap kind, and refuses 2 and 8. The 97/98-dword boundary case in reads_root_tables now uses constants, and the reading fixture's table states one SGPR. With the check disabled, the test fails.

Used descriptors: one kind per descriptor, one heap per table

Lowering picks CBV, SRV or UAV access from a single used-resource entry (used_kind: a root descriptor's first, a table's last at or below the offset), while the host binds every entry by its own kind (draw::readable). Two entries naming the same table offset, or the same root descriptor, with different kinds would bind the view one way and read it another, so native_programs now refuses them ("used descriptors read one descriptor two ways"). The same entry listed twice as one kind still passes.

A table whose reads mix samplers with CBV/SRV/UAV views is refused too ("root table mixes samplers and views"). D3D12 keeps samplers in their own heap, and the host rebuilds a table holding a constant buffer in its view heap (draw::tables), where a sampler has no place.

Fix: a root parameter must be of a known kind

A layout entry whose root type is none of constants (0), root descriptor (1), table (3), fixed sampler (4), resource heap (5) or sampler heap (6) is now refused as root parameter of unknown kind, as a read of an unknown kind already is. Every stage decides what a parameter is by its kind (resource tracking, lowering's user data, the host's user_value), and the shader cache stores an unknown kind n as 6 + n, which overflows near u32::MAX and would reload as another kind.

  • accepted: each of the six known kinds
  • refused: kind 2 (with or without SGPRs, and next to a known parameter), kind 7, kind u32::MAX

Tests: each refusal names its reason

rejects_corrupt_blobs only asserted is_err(). If one check stopped firing, the test still passed whenever a later check happened to catch the same bytes. Each Malformed reason that native_programs can return now has a case that expects that exact message:

  • XDAT: implausible entry count, entry out of bounds. Entries of other kinds are never read, so their bounds don't matter; that is pinned too.
  • XBCH: magic, version, implausible unpacked size, stream out of bounds, a stated size one byte either side of the inflated length, truncated header.
  • FSSM header: magic, version, payload size, slot count, record out of bounds.
  • Record: code out of bounds, VGPR count 257 (256 still passes), root table out of bounds.
  • Stage data: a VS input past v255 or over a system VGPR, VS input/output and PS input semantic counts, export patch size, a patch at the code's last dword (passes) versus one past it (refused), overlapping export patches, outputs sharing a signature row, a VS output marked per-primitive, pixel inputs reading one row two ways (one fp16 and one 32-bit, or packed differently: lowering keys a row's 16-bit layout by its first fp16 input), the compute thread-group limits, an empty mesh group, and a compute or mesh LDS over 64 KiB.

The test blobs are built from the existing synthetic fssm/xbch/part helpers. No title data is used.

Tested on Linux (x86_64-unknown-linux-gnu), rebased on current main

  • cargo test -p victory-shader --lib: 72 passed (25 in native)
  • cargo clippy -p victory-shader -- -D warnings: clean
  • cargo fmt -p victory-shader --check: clean
  • cargo xtask policy: 151 files ok
**@⁠SkippySteve** on GitHub · 2026-11-24 02:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/333) `shader-native-root-sgprs` → `main` · +5036 −22 · 1 files · 83 commits · closed without merging Thirteen changes to `crates/victory-shader/src/native.rs`: twelve fixes, and tests that name each refusal. This PR also replaces #330 and #339, which edited the same file. ### Fix: root parameters must sit in the SGPRs The FSSM root layout gives each root parameter a user-data byte and an SGPR count, both read straight from the blob. Resource tracking (`resource.rs`) turns them into registers with `HwStage::user_sgpr`. For merged and mesh stages that does `index + 8` as a `u8`, and a root descriptor's high half adds one more. A corrupt or unexpected record with a user-data byte of 248 or more on a merged stage overflows that add. Debug builds panic. In release the register wraps to a low SGPR, so inputs are attributed to the wrong root parameter with no error. Such records are now rejected when they're read. A parameter whose SGPRs run past `s105`, the last SGPR before VCC, returns `Malformed("root parameter outside the SGPRs")`, the same way the parser already handles other out-of-range fields. Real layouts use low user SGPRs and are unaffected. `reads_root_tables` checks the boundary with a synthetic fixture: `s9` plus 97 dwords (ending at `s105`) is accepted, 98 dwords is rejected, and so is a user-data byte of 0xFF. ### Fix: a thread-id count field of 3 still means three components `COMPUTE_PGM_RSRC2.TIDIG_COMP_CNT` is a 2-bit field, and `ComputeIo::thread_id_vgprs` returned it plus one: 4 for a value of 3. `io::raise` then labelled `v3` as `Input::ThreadIdInGroup(3)`, and lowering asked DXIL's `threadIdInGroup` for component 3, which does not exist (the console-lanes path quietly treated it as z instead). A thread id has only x, y and z, so the field is clamped to 2 before adding one: `v0`-`v2` are the thread id and `v3` stays an ordinary VGPR. A synthetic unit test covers the field at 3. (Previously #339.) ### Fix: a vertex input must load into the VGPRs When `io::raise` handles a native vertex shader, it loads input channel `c` from `Reg::V(vgpr + c)`. `vgpr` is a byte read straight from the stage data, so it can hold any value. An input at `v253` or above with a later channel enabled overflows that `u8` add. Debug builds panic. In release the register wraps, so a low VGPR is labelled as the attribute and there is no error. This is the same kind of bug as the SGPR fix above, and it is fixed the same way. When the stage data is read, an input whose last enabled channel falls past `v255` returns `Malformed("vertex input outside the VGPRs")`. Only the VS path checks this, because mesh stages discard these inputs. `names_each_stage_data_refusal` covers the boundary: - accepted: `v252` with all four channels (ending at `v255`), `v255` with x only, `v255` with no channels enabled - refused: `v253` with all four channels, `v255` with y ### Fix: a compute group must be one `numthreads` allows A compute record's thread-group size is three `u32`s read straight from the stage data. Lowering multiplies them (`cs.threads.iter().product::<u32>()` in `emit/lower.rs`, to decide whether the group fits one wave) and writes them as the DXIL `numthreads`. A corrupt record of 65536 × 65536 × 1 overflows that product: debug builds panic, and in release it wraps to 0, so the group is treated as fitting in one wave. A zero component instead produces a `numthreads` that DXIL rejects. Such records are now refused when they're read, with `Malformed("implausible thread-group size")`, using D3D12's limits: each component at least 1, x and y up to 1024, z up to 64, and at most 1024 threads in total. Real HLSL can't exceed these. The bounds are checked per component before the product is taken, so that check can't overflow either. The synthetic `fssm` helper now writes `numthreads(1, 1, 1)` for compute records. `names_each_stage_data_refusal` covers each limit: `[1024,1,1]`, `[1,1024,1]`, `[1,16,64]`, `[32,32,1]` are accepted; `[0,1,1]`, `[1,1,0]`, `[1025,1,1]`, `[1,1,65]`, `[64,32,1]`, `[65536,65536,1]` and `u32::MAX` in all three are refused. ### Fix: a mesh group must have a lane A mesh record's thread-group size is a `u16` read straight from the stage data. Lowering writes it as the DXIL `numthreads` x, and for containers whose `PSV0` lacks the HLSL thread count (runtime info before version 2) it is also the stride the group id is multiplied by (`mesh_stride` in `emit/mod.rs`). `mesh_prologue` already refuses groups over 128 lanes, but a record of 0 passed: it emitted `numthreads(0, 1, 1)`, which DXIL rejects, and on the fallback path gave every group id 0 with no error. A zero mesh group is now refused when it's read, with the compute path's `Malformed("implausible thread-group size")`. The synthetic `fssm` helper writes a one-lane mesh group, and `names_each_stage_data_refusal` covers both: one lane is accepted, zero is refused. ### Fix: a mesh group's LDS must fit a thread group A mesh record's LDS size is a `u32` read straight from the record (`+0x4c`). Lowering declares the LDS as `lds_bytes.div_ceil(4)` dwords, then writes it back to `PSV0` as `lds_dwords * 4` (`emit/shader.rs`), a `u32` product. A corrupt record near `u32::MAX` overflows it: debug builds panic, and in release it wraps to a tiny size, so the container states far less LDS than the shader declares. A GFX10 thread group addresses at most 64 KiB of LDS, so larger sizes are refused when they're read, with `Malformed("implausible LDS size")`. `names_each_stage_data_refusal` accepts exactly 64 KiB and refuses one byte more and `u32::MAX`; with the check disabled, the test fails. ### Fix: a pixel program's export patches must not overlap A pixel record holds an export patch per render target: a code offset (in dwords) and replacement code of a shared size. `NativeProgram::patched_code` writes them in MRT order. Each patch was already checked to lie inside the code, but not against the others. If a corrupt record gives two MRTs patches that share dwords, the later one overwrites part of the earlier export. The shader then exports the wrong values, or decodes a broken instruction, and there is no error. Such records are now refused when they're read, with `Malformed("export patches overlap")`. `names_each_stage_data_refusal` uses two-dword patches for MRT0 and MRT1 in a four-dword program: - accepted: offsets 0 and 2, offsets 2 and 0, and a patch size of 0 (nothing to overlap) - refused: offsets 0 and 1, 1 and 0, and 1 and 1 With the check disabled, the test fails. ### Fix: a root descriptor's high half must sit in the SGPRs too Resource tracking (`resource.rs`) reads a root descriptor's high half from `user_sgpr(user_data) + 1`, whatever SGPR count the record states. The SGPR check above reserved `max(sgprs, 1)` registers, so a descriptor that states 0 or 1 SGPRs and sits at `s105` passed it. Its high half was then `s106`, which is VCC_LO, and was used as the descriptor's address with no error. For this check, a root descriptor now spans at least two SGPRs. `reads_root_tables` covers it for stated counts of 0, 1 and 2: a descriptor at `s104` is accepted and one at `s105` is refused with `Malformed("root parameter outside the SGPRs")`. A table at `s105` still takes a single SGPR and is accepted. Its existing boundary cases now check the exact message instead of only `is_err()`. With the fix reverted, the test fails. ### Fix: a compute group's LDS must fit a thread group A compute record's LDS size comes from `COMPUTE_PGM_RSRC2.LDS_SIZE`, a 9-bit count of 512-byte granules (`ComputeIo::lds_bytes`). That lets a corrupt record state up to 255.5 KiB, and lowering declares that much as the group's shared memory (`emit/lower.rs`). The mesh path already refuses more than the 64 KiB a GFX10 thread group can address, but the compute path didn't. Compute records now apply the same limit when they're read, with the same `Malformed("implausible LDS size")`. `names_each_stage_data_refusal` accepts 128 granules (exactly 64 KiB) and refuses 129 and the field's maximum of 511. With the check disabled, the test fails. ### Fix: a compute root parameter must not cover a system SGPR A compute program's group ids and group info arrive in the SGPRs right after its user SGPRs, as `COMPUTE_PGM_RSRC2` enables them (`ComputeIo::system_sgprs`), and `io::raise` labels them there. The layout's SGPR check only made sure each parameter fits below `s106`, so a corrupt layout could state a root parameter over one of those registers. That register was then both a root parameter (to resource tracking) and a group id (to raising), with no error. Compute records now refuse such a layout when they're read, with `Malformed("root parameter over a system SGPR")`. Only an actual overlap is refused: a parameter past the enabled system SGPRs, or one where no group id is enabled, is still read. A root descriptor counts its high half, as in the SGPR check. `compute_root_params_stay_off_system_sgprs` uses 4 user SGPRs with group id x, y and the group info in `s4`-`s6`: - accepted: constants in `s0`-`s3`, a table in `s3`, constants in `s7`, and constants in `s4` with no system SGPRs enabled - refused: five dwords from `s0`, a one-SGPR descriptor in `s3` (high half `s4`), constants in `s5`, a table in `s6` With the check disabled, the test fails. ### Fix: a vertex input must not load into a system VGPR A native vertex program receives its vertex, primitive and instance id in `v0`, `v2` and `v3`, and `io::raise` labels those registers first. It then labels each enabled input channel `c` at `v[vgpr + c]`, and an input's value is matched to the first label its register has. A corrupt input that loads a channel into `v0`, `v2` or `v3` was therefore read as that id, with no error. Such stage data is now refused when it's read, with `Malformed("vertex input over a system VGPR")`. Only an actual overlap is refused: `v1` carries no id here, and inputs from `v4` up are unaffected. `names_each_stage_data_refusal` accepts x in `v1`, x-w in `v4` and an input in `v0` with no channels enabled. It refuses x in `v0`, `v2` or `v3`, and y or z of an input in `v1`. With the check disabled, the test fails. ### Native outputs must not share a signature row `io::raise` turns each `exp param<n>` into a write of its output entry's signature row, and lowering keys 16-bit outputs by that row (`half_outputs`). Two output entries naming one row therefore wrote the same signature element: the later export silently replaced the earlier, and a 16-bit entry's packing was overwritten by the other's. Such stage data is now refused when it's read, with `Malformed("vertex outputs share a row")`. Per-vertex rows (`OSG1`) and per-primitive rows (`PSG1`, param bit 7) are separate signatures, so a vertex output and a per-primitive output in the same row number stay allowed. `refuses_outputs_sharing_a_row` covers VS and MS: distinct rows pass, a vertex and a primitive output both in row 0 pass, and two vertex outputs or two primitive outputs in one row are refused. ### Fix: only one table's ranges may share a root parameter's SGPR `params_apart` (the "root parameters share an SGPR" check) let any two layout entries with the same slot number cover the same SGPR, so the ranges of one table could share its address SGPR. But a matching slot byte was the only thing it compared. A table range and constants that state the same slot, the same constants listed twice, or one slot's constants overlapping themselves at a shifted user-data dword all got through. `resource::param_of` maps an SGPR to the first entry covering it, so the second entry's reads were attributed to the first with no error. Entries may now share an SGPR only when both are table ranges with the same slot and the same table SGPR. Anything else is refused with `Malformed("root parameters share an SGPR")`. `refuses_root_parameters_sharing_an_sgpr` now also accepts three ranges of one table and refuses four more cases: constants in a table's SGPR stating its slot, one slot's constants overlapping themselves, constants listed twice, and a root descriptor listed twice. ### Fix: a vertex output must not be marked per-primitive Bit 7 of an output entry's parameter byte marks a mesh program's per-primitive output, a row of `PSG1`. `io::raise` applies it on any stage, so a plain vertex program's output with that bit became `Target::PrimitiveParam`. A vertex program has no primitive signature (`prims` is empty), so lowering found no element and dropped those exports with no error. Such an output was also skipped by the shared-row check (which keeps primitive rows separate) and by `half_outputs`. Vertex stage data with such an output is now refused when it's read, with `Malformed("vertex output marked per-primitive")`. Mesh programs are unaffected. `refuses_outputs_sharing_a_row` still accepts a per-primitive output in row 0 next to a vertex output there for MS. For VS it now refuses that case, `0x81` in row 1 and `0xFF` in row 7. With the check disabled, the test fails. ### Fix: a table or heap root parameter takes one SGPR A descriptor table, resource heap or sampler heap parameter is a single 32-bit address, but its SGPR count is read straight from the layout entry and any count was accepted. Resource tracking (`resource::param_of`) maps every stated SGPR to that parameter. The host fills each dword of a table with the table's heap offset (`draw::user_value` ignores the dword), and lowering reads a heap's as 0. So in a corrupt layout where a table states two or more SGPRs, the registers after the first silently read the table address, with no error. Such layouts are now refused when they're read, with `Malformed("root address wider than one SGPR")`. A count of 0 is still accepted, as a further range of one table may state. Constants and root descriptors take what they state, as before. `refuses_root_parameters_sharing_an_sgpr` accepts 0 and 1 SGPRs for a table and for each heap kind, and refuses 2 and 8. The 97/98-dword boundary case in `reads_root_tables` now uses constants, and the `reading` fixture's table states one SGPR. With the check disabled, the test fails. ### Used descriptors: one kind per descriptor, one heap per table Lowering picks CBV, SRV or UAV access from a single used-resource entry (`used_kind`: a root descriptor's first, a table's last at or below the offset), while the host binds every entry by its own kind (`draw::readable`). Two entries naming the same table offset, or the same root descriptor, with different kinds would bind the view one way and read it another, so `native_programs` now refuses them ("used descriptors read one descriptor two ways"). The same entry listed twice as one kind still passes. A table whose reads mix samplers with CBV/SRV/UAV views is refused too ("root table mixes samplers and views"). D3D12 keeps samplers in their own heap, and the host rebuilds a table holding a constant buffer in its view heap (`draw::tables`), where a sampler has no place. ### Fix: a root parameter must be of a known kind A layout entry whose root type is none of constants (0), root descriptor (1), table (3), fixed sampler (4), resource heap (5) or sampler heap (6) is now refused as `root parameter of unknown kind`, as a read of an unknown kind already is. Every stage decides what a parameter is by its kind (resource tracking, lowering's user data, the host's `user_value`), and the shader cache stores an unknown kind `n` as `6 + n`, which overflows near `u32::MAX` and would reload as another kind. - accepted: each of the six known kinds - refused: kind 2 (with or without SGPRs, and next to a known parameter), kind 7, kind `u32::MAX` ### Tests: each refusal names its reason `rejects_corrupt_blobs` only asserted `is_err()`. If one check stopped firing, the test still passed whenever a later check happened to catch the same bytes. Each `Malformed` reason that `native_programs` can return now has a case that expects that exact message: - **XDAT**: implausible entry count, entry out of bounds. Entries of other kinds are never read, so their bounds don't matter; that is pinned too. - **XBCH**: magic, version, implausible unpacked size, stream out of bounds, a stated size one byte either side of the inflated length, truncated header. - **FSSM header**: magic, version, payload size, slot count, record out of bounds. - **Record**: code out of bounds, VGPR count 257 (256 still passes), root table out of bounds. - **Stage data**: a VS input past `v255` or over a system VGPR, VS input/output and PS input semantic counts, export patch size, a patch at the code's last dword (passes) versus one past it (refused), overlapping export patches, outputs sharing a signature row, a VS output marked per-primitive, pixel inputs reading one row two ways (one fp16 and one 32-bit, or packed differently: lowering keys a row's 16-bit layout by its first fp16 input), the compute thread-group limits, an empty mesh group, and a compute or mesh LDS over 64 KiB. The test blobs are built from the existing synthetic `fssm`/`xbch`/`part` helpers. No title data is used. ### Tested on Linux (`x86_64-unknown-linux-gnu`), rebased on current `main` - `cargo test -p victory-shader --lib`: 72 passed (25 in `native`) - `cargo clippy -p victory-shader -- -D warnings`: clean - `cargo fmt -p victory-shader --check`: clean - `cargo xtask policy`: 151 files ok
The FSSM root layout gives each parameter a user-data byte and an SGPR
count, and resource tracking turns them into registers with
HwStage::user_sgpr (index + 8 as u8 for merged and mesh stages) and, for
a root descriptor, that plus one. A byte of 248 or more in a merged-stage
record overflowed that add: a panic in debug builds, a wrapped register
that misattributes inputs in release. Reject, when the record is read, a
parameter whose SGPRs run past s105 (the last before VCC).
The corrupt-blob test only checked that native_programs fails, so a
check could stop firing while an earlier one caught the same bytes.
Each XDAT, XBCH, FSSM, record and stage-data refusal now has a case that
expects its own message, with the boundaries that still pass (256
VGPRs, a patch ending at the code's last dword, out-of-range entries
of other kinds).
COMPUTE_PGM_RSRC2.TIDIG_COMP_CNT is a 2-bit field, and
ComputeIo::thread_id_vgprs returned it plus one: 4 for a value of 3.
io::raise then labelled v3 as Input::ThreadIdInGroup(3), and lowering
asks DXIL's threadIdInGroup for component 3, which does not exist (the
console-lanes path quietly treats it as z instead).

A thread id has only x, y and z, so clamp the field to 2 before adding
one: v0-v2 are the thread id and v3 stays an ordinary VGPR. A unit test
covers the field at 3.
Raising a native vertex shader loads input channel c from
Reg::V(vgpr + c), with vgpr a byte read straight from the stage data.
An input at v253 or above with a later channel enabled overflowed that
u8 add: a panic in debug builds, and in release a wrapped register that
labels a low VGPR as the attribute. Reject, when the stage data is read,
an input whose last enabled channel lies past v255.
A compute record's thread-group size is three u32s read straight from the
stage data. Lowering multiplies them (a debug panic on overflow) and writes
them as the DXIL numthreads. Refuse a zero component, one past D3D12's
limits (x, y 1024; z 64), or a group over 1024 threads as
Malformed("implausible thread-group size").
A mesh record's thread-group size is a u16 read straight from the stage
data. It becomes the DXIL numthreads x and, for containers without the
HLSL thread count, the stride lowering multiplies the group id by. A
record of 0 emitted numthreads(0, 1, 1) and gave every group id 0.
Such records are now refused with the compute path's
Malformed("implausible thread-group size"); the synthetic fssm helper
writes a one-lane mesh group.
A mesh record's LDS size is a u32 read straight from the record. Lowering
declares it as dwords and writes it back to PSV0 as lds_dwords * 4, a u32
product that overflows for sizes near u32::MAX (a debug panic; in release
it wraps to a small size). A GFX10 thread group addresses at most 64 KiB of
LDS, so larger sizes are refused when read with Malformed("implausible LDS
size"). names_each_stage_data_refusal covers 64 KiB (accepted), one byte
more and u32::MAX (refused).
patched_code writes each MRT's export patch in order, so a corrupt
record whose patches share dwords clobbered an earlier export with no
error. Refuse it when the stage data is read, by name.
Resource tracking reads a root descriptor's high half from the SGPR after
its first, whatever count the record states. The SGPR check reserved
max(sgprs, 1), so a descriptor stating 0 or 1 SGPRs at s105 passed and
took VCC_LO as its high half. A descriptor now spans at least two SGPRs
in that check. The root-table test also names the refusal reason.
COMPUTE_PGM_RSRC2.LDS_SIZE is a 9-bit count of 512-byte granules, so a
corrupt record can state up to 255.5 KiB, which lowering declares as the
group's shared memory. Refuse more than 64 KiB when the record is read,
with the mesh path's "implausible LDS size".
A compute program's group ids and group info arrive in the SGPRs after its
user SGPRs (COMPUTE_PGM_RSRC2), and raising labels them there. A corrupt
layout whose root parameter spans one of those SGPRs makes the same
register both a root parameter and a group id, with no error. Refuse it
when the record is read, with "root parameter over a system SGPR".
Raising resolves `exp param<n>` and `v_interp` of attribute n to the
first matching stage-data entry, so a second entry with the same index
was silently dropped. Refuse such blobs as Malformed.
Resource tracking maps an SGPR to the first layout entry covering it, so
a second root parameter over the same SGPR (or over a root descriptor's
high half) had its reads attributed to the first. Refuse such layouts as
Malformed; ranges of one table share its SGPR and stay allowed, as do
parameters with no SGPRs.
Raising turns each `exp param<n>` into a write of its output's signature
row, and lowering keys 16-bit outputs by that row, so two outputs naming
one row wrote the same element, the later export silently replacing the
earlier. Refuse such blobs as Malformed; per-vertex and per-primitive
rows are separate signatures and stay independent.
Lowering finds a row's 16-bit layout as the first fp16 input naming it,
so a second input on that row read as 32-bit, or packed differently,
would be decoded with the first's layout. Inputs packed into one row the
same way stay accepted.
A packed 16-bit pixel input reads the halves past its own row from its
second row's signature element. Another input naming that row, or a
packed input of another row claiming it too, would read one element as
two inputs; refuse such stage data by name.
A shared slot number alone let a table range and constants, or one
parameter listed twice, cover the same SGPR; resource tracking would
attribute that SGPR's reads to whichever came first.
Lowering writes the mesh program's lane count, vertex and primitive
limits and topology into the DXIL mesh state and PSV info as they are.
A group past 128 lanes, more than 256 vertices or primitives, or a
topology other than lines or triangles would emit a shader D3D12
refuses to create, so the blob is refused by name instead.
A table or heap root parameter is one 32-bit address, but the layout
states its SGPR count and the parser took any count. Resource tracking
maps every stated SGPR to the parameter, and the host fills each dword
of a table with its heap offset (lowering reads a heap's as 0), so an
SGPR past the first silently read the address. Such a layout is now
refused as "root address wider than one SGPR".
Lowering picks a read's access from its kind and the host makes only SRV
and UAV views ready, so a read of an unknown kind would be lowered as a
view the host never prepares. Refuse it by name.
GFX10 fills a stage's user SGPRs from 32 user-data registers (16 for
compute). Lowering declares the user data up to the last dword a
parameter states and the host makes it root constants, so a parameter
far past them asked for a root signature larger than D3D12's 64 dwords.
A layout entry whose root type is none of constants, root descriptor,
table, fixed sampler or the two heaps is now refused, as a read of an
unknown kind already is. Every stage of the translation decides what a
parameter is by its kind, and the shader cache stores an unknown kind n
as 6 + n, which overflows near u32::MAX and would reload as another
kind.
The host makes a table holding a constant buffer afresh with a slot for
every descriptor up to the furthest read, reading each from title memory
before asking the heap for them; an offset near u32::MAX would read
gigabytes past the table. Refuse table offsets past D3D12's tier 2 heap
(a million descriptors of 8 dwords).
A vertex input was refused only past v255, and a compute program's thread
ids not at all, though the record states how many VGPRs the wave has and
relative VGPR reads already treat those past it as absent. An input or
thread id past that count would be labeled in a register the program never
has.
A static sampler stating no SGPRs was given a one-SGPR span, so a compute
program with no user SGPRs and group id x in s0 was refused whenever its
sampler's user-data byte was 0. Its byte is still bounded so user_sgpr
can't overflow. Tests pin system SGPR packing with disabled components and
the 31-SGPR user field.
# Conflicts:
#	crates/victory-shader/src/native.rs
Nothing enabled initializes no SGPR, scratch alone puts its wave offset in
s0, a lone group id z or group info follows the user SGPRs with no gap, and
the thread-id count and LDS size leave the count alone. A compute program
parsed from its record keeps its root parameter below that count.
A table, heap or constants entry stating zero SGPRs was skipped by the
user-data and shared-SGPR checks, while the compute system-SGPR check
already counted its first SGPR (sgpr_span). Lowering still reads a
table's address from that SGPR and declares user data up to it, so one
past dword 31 asked for an oversized root signature and one over another
parameter's SGPR was attributed to that parameter. All three checks now
take their span from sgpr_span.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:59:37 +00:00
github-bot closed this pull request 2026-11-26 03:59:37 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 03:45 UTC · original

Verified 3e3f42d on Arch Linux x86_64 (rustc 1.98.1, cargo test -p victory-shader): 64 passed, 0 failed, 0 ignored.

Not covered: real title FSSM blobs (no game data here) and the new refusals when a title blob actually trips them.

**@⁠DaviTostes** on GitHub · 2026-11-24 03:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/333#issuecomment-7121386989) Verified `3e3f42d` on **Arch Linux x86_64** (rustc 1.98.1, `cargo test -p victory-shader`): **64 passed, 0 failed, 0 ignored**. Not covered: real title FSSM blobs (no game data here) and the new refusals when a title blob actually trips them.
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 04:21 UTC · original

Tested PR #333 at head 1ff9c061e7ea8d7d2132536d7f98b9d19d0a8a21 (state at test time: OPEN). Pre-comment refresh: state OPEN, head unchanged at 1ff9c061e7ea8d7d2132536d7f98b9d19d0a8a21.

  • cargo test --locked -p victory-shader native::tests — passed (ok. 21 passed; 0 failed; 0 ignored; 0 measured; 60 filtered out; finished in 0.01s ); cases: native::tests::compute_dispatch_settings, native::tests::pc_layout_has_none, native::tests::thread_id_count_three_is_still_three_components, native::tests::reads_mesh_outputs_and_limits, and 17 more.

Behavior covered: Checks native root parameters stay outside system SGPRs, thread-id component counts remain exact, and each native blob refusal reports its reason.
Code: crates/victory-shader/src/native.rs

**@⁠jeremygold02** on GitHub · 2026-11-24 04:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/333#issuecomment-7121942461) Tested PR #333 at head `1ff9c061e7ea8d7d2132536d7f98b9d19d0a8a21` (state at test time: OPEN). Pre-comment refresh: state OPEN, head unchanged at `1ff9c061e7ea8d7d2132536d7f98b9d19d0a8a21`. - `cargo test --locked -p victory-shader native::tests` — passed (ok. 21 passed; 0 failed; 0 ignored; 0 measured; 60 filtered out; finished in 0.01s ); cases: native::tests::compute_dispatch_settings, native::tests::pc_layout_has_none, native::tests::thread_id_count_three_is_still_three_components, native::tests::reads_mesh_outputs_and_limits, and 17 more. Behavior covered: Checks native root parameters stay outside system SGPRs, thread-id component counts remain exact, and each native blob refusal reports its reason. Code: [crates/victory-shader/src/native.rs](https://github.com/Marvisak/VIctory/blob/1ff9c061e7ea8d7d2132536d7f98b9d19d0a8a21/crates/victory-shader/src/native.rs)
Author
Owner

@⁠slint6614-dev on GitHub · 2026-11-24 09:24 UTC · original

Measured on the full retail shader pack (26,503 containers) with victory translate --validate --pipelines (pipelines created on an AMD RX 9070, Windows 11, dxil.dll from SDK 10.0.26100). Counts are emitted / validated / pipelines. This PR was merged locally with current main c7a5f49 first (main: VS/PS 12224 / 12148, CS 1440 / 1417, MS 8163 / 8101; it includes #612).

  • VS/PS validated unchanged (12148), but 4 fewer programs emit (12220 instead of 12224): 6 are now refused as "malformed shader: vertex outputs share a row".
  • "a descriptor neither loaded nor built" 298 → 296.
  • CS and MS unchanged.
**@⁠slint6614-dev** on GitHub · 2026-11-24 09:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/333#issuecomment-7127373707) Measured on the full retail shader pack (26,503 containers) with `victory translate --validate --pipelines` (pipelines created on an AMD RX 9070, Windows 11, dxil.dll from SDK 10.0.26100). Counts are emitted / validated / pipelines. This PR was merged locally with current main c7a5f49 first (main: VS/PS 12224 / 12148, CS 1440 / 1417, MS 8163 / 8101; it includes #612). - VS/PS validated unchanged (12148), but 4 fewer programs emit (12220 instead of 12224): 6 are now refused as "malformed shader: vertex outputs share a row". - "a descriptor neither loaded nor built" 298 → 296. - CS and MS unchanged.
Author
Owner

@⁠DuarttBR on GitHub · 2026-11-24 15:30 UTC · original

An interaction with #302 / #489 worth knowing before both land.

This PR's user-data check (params_in_user_data, refusing as root parameter past the user data) runs when the layout is parsed. A compute program whose root arguments the console spilled to the table s[0:1] points to has every parameter at user data 16 or above, so it is refused there, before #489's NativeProgram::spills_root_args can say it reads them from memory. With both PRs merged, the 34 spilled-argument compute programs Grand Theft Auto VI's world dispatches all stop at this check again (they were the largest drops per frame in my runs).

What I did on my integration branch (5a993d5): a layout past the user data whose parameters all sit at 16 and up (the spill shape) is not refused at parse time. Once the stage data is read, it is refused with the same words if spills_root_args() says no (something of the hardware's own from s16 on) or if the stage data does not read. Any other layout past the user data is refused as before. Four tests that fixed the old rule now state the new one. cargo test --workspace passes, and the 34 programs then reach resource tracking (13 tracked; 7 emit once root UAVs are supported, #1077).

AI use: Claude Code (Claude Fable 5.2, Claude Opus 5.5) found the interaction, wrote the change and drafted this comment; I reviewed and posted it.

**@⁠DuarttBR** on GitHub · 2026-11-24 15:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/333#issuecomment-7136155648) An interaction with #302 / #489 worth knowing before both land. This PR's user-data check (`params_in_user_data`, refusing as `root parameter past the user data`) runs when the layout is parsed. A compute program whose root arguments the console spilled to the table `s[0:1]` points to has *every* parameter at user data 16 or above, so it is refused there, before #489's `NativeProgram::spills_root_args` can say it reads them from memory. With both PRs merged, the 34 spilled-argument compute programs Grand Theft Auto VI's world dispatches all stop at this check again (they were the largest drops per frame in my runs). What I did on my integration branch ([5a993d5](https://github.com/DuarttBR/VIctory/commit/5a993d5)): a layout past the user data whose parameters *all* sit at 16 and up (the spill shape) is not refused at parse time. Once the stage data is read, it is refused with the same words if `spills_root_args()` says no (something of the hardware's own from s16 on) or if the stage data does not read. Any other layout past the user data is refused as before. Four tests that fixed the old rule now state the new one. `cargo test --workspace` passes, and the 34 programs then reach resource tracking (13 tracked; 7 emit once root UAVs are supported, #1077). AI use: Claude Code (Claude Fable 5.2, Claude Opus 5.5) found the interaction, wrote the change and drafted this comment; I reviewed and posted it.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:02 UTC · original

Automated triage for the maintainers. Closing because by our reading the change is incorrect or would break something that works today.

5,036 lines in native.rs adding a dozen 'Malformed' refusals; one of them ('root parameter past the user data': compute parameters must sit below user-data dword 16) would refuse the compute programs whose root signature is spilled to memory, which main now translates (resource::USER_DATA).

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 00:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/333#issuecomment-7148066260) Automated triage for the maintainers. Closing because by our reading the change is incorrect or would break something that works today. 5,036 lines in native.rs adding a dozen 'Malformed' refusals; one of them ('root parameter past the user data': compute parameters must sit below user-data dword 16) would refuse the compute programs whose root signature is spilled to memory, which main now translates (`resource::USER_DATA`). If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!333
No description provided.