Repository navigation
Shaders: keep native root parameters inside the SGPRs, clamp the thread-id count, and name each blob refusal - #333
SkippySteve wants to merge 83 commits into
Conversation
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).
2e2d9d3 to
fb7d023
Compare
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.
e51ee5a to
4f81a5f
Compare
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".
… clamp the thread-id count, and name each blob refusal Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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".
…attribute 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.
|
Verified Not covered: real title FSSM blobs (no game data here) and the new refusals when a title blob actually trips them. |
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.
…cross absent ones
… and only a mesh reads the record's LDS word
…-expand-333-or-246-lz
…ir header rules are named in order
… data, and the first bad slot is named
…isak#306 Marvisak#333 Marvisak#105 Marvisak#288 Marvisak#343 Marvisak#353 Marvisak#326 Marvisak#259 Marvisak#179 Marvisak#176 Marvisak#226 Marvisak#286) Conflicts: - emit/lower.rs: shader-opcodes and shader-tracking each keyed heap handles by the resource properties (a tuple vs two flattened words); the same key, shader-tracking's form and comment kept. Lowered's test-only fields, test imports and tests of both sides kept. - emit/wave.rs: tests of both sides kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
An interaction with #302 / #489 worth knowing before both land. This PR's user-data check ( 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 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. |
…GPRs, clamp the thread-id count, and name each blob refusal
…GPRs, clamp the thread-id count, and name each blob refusal
|
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 ( 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. |
…rs, vertex inputs and thread ids in real registers) into the integration Merged cleanly. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AZ9pSB4ZoXYUifuXg8gm66
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 withHwStage::user_sgpr. For merged and mesh stages that doesindex + 8as au8, 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, returnsMalformed("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_tableschecks the boundary with a synthetic fixture:s9plus 97 dwords (ending ats105) 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_CNTis a 2-bit field, andComputeIo::thread_id_vgprsreturned it plus one: 4 for a value of 3.io::raisethen labelledv3asInput::ThreadIdInGroup(3), and lowering asked DXIL'sthreadIdInGroupfor 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-v2are the thread id andv3stays 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::raisehandles a native vertex shader, it loads input channelcfromReg::V(vgpr + c).vgpris a byte read straight from the stage data, so it can hold any value. An input atv253or above with a later channel enabled overflows thatu8add. 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
v255returnsMalformed("vertex input outside the VGPRs"). Only the VS path checks this, because mesh stages discard these inputs.names_each_stage_data_refusalcovers the boundary:v252with all four channels (ending atv255),v255with x only,v255with no channels enabledv253with all four channels,v255with yFix: a compute group must be one
numthreadsallowsA compute record's thread-group size is three
u32s read straight from the stage data. Lowering multiplies them (cs.threads.iter().product::<u32>()inemit/lower.rs, to decide whether the group fits one wave) and writes them as the DXILnumthreads. 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 anumthreadsthat 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 syntheticfssmhelper now writesnumthreads(1, 1, 1)for compute records.names_each_stage_data_refusalcovers 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]andu32::MAXin all three are refused.Fix: a mesh group must have a lane
A mesh record's thread-group size is a
u16read straight from the stage data. Lowering writes it as the DXILnumthreadsx, and for containers whosePSV0lacks the HLSL thread count (runtime info before version 2) it is also the stride the group id is multiplied by (mesh_strideinemit/mod.rs).mesh_prologuealready refuses groups over 128 lanes, but a record of 0 passed: it emittednumthreads(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 syntheticfssmhelper writes a one-lane mesh group, andnames_each_stage_data_refusalcovers 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
u32read straight from the record (+0x4c). Lowering declares the LDS aslds_bytes.div_ceil(4)dwords, then writes it back toPSV0aslds_dwords * 4(emit/shader.rs), au32product. A corrupt record nearu32::MAXoverflows 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_refusalaccepts exactly 64 KiB and refuses one byte more andu32::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_codewrites 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_refusaluses two-dword patches for MRT0 and MRT1 in a four-dword program: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 fromuser_sgpr(user_data) + 1, whatever SGPR count the record states. The SGPR check above reservedmax(sgprs, 1)registers, so a descriptor that states 0 or 1 SGPRs and sits ats105passed it. Its high half was thens106, 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_tablescovers it for stated counts of 0, 1 and 2: a descriptor ats104is accepted and one ats105is refused withMalformed("root parameter outside the SGPRs"). A table ats105still takes a single SGPR and is accepted. Its existing boundary cases now check the exact message instead of onlyis_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_refusalaccepts 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_RSRC2enables them (ComputeIo::system_sgprs), andio::raiselabels them there. The layout's SGPR check only made sure each parameter fits belows106, 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_sgprsuses 4 user SGPRs with group id x, y and the group info ins4-s6:s0-s3, a table ins3, constants ins7, and constants ins4with no system SGPRs enableds0, a one-SGPR descriptor ins3(high halfs4), constants ins5, a table ins6With 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,v2andv3, andio::raiselabels those registers first. It then labels each enabled input channelcatv[vgpr + c], and an input's value is matched to the first label its register has. A corrupt input that loads a channel intov0,v2orv3was 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:v1carries no id here, and inputs fromv4up are unaffected.names_each_stage_data_refusalaccepts x inv1, x-w inv4and an input inv0with no channels enabled. It refuses x inv0,v2orv3, and y or z of an input inv1. With the check disabled, the test fails.Native outputs must not share a signature row
io::raiseturns eachexp 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_rowcovers 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_ofmaps 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_sgprnow 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::raiseapplies it on any stage, so a plain vertex program's output with that bit becameTarget::PrimitiveParam. A vertex program has no primitive signature (primsis 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 byhalf_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_rowstill accepts a per-primitive output in row 0 next to a vertex output there for MS. For VS it now refuses that case,0x81in row 1 and0xFFin 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_valueignores 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_sgpraccepts 0 and 1 SGPRs for a table and for each heap kind, and refuses 2 and 8. The 97/98-dword boundary case inreads_root_tablesnow uses constants, and thereadingfixture'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, sonative_programsnow 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'suser_value), and the shader cache stores an unknown kindnas6 + n, which overflows nearu32::MAXand would reload as another kind.u32::MAXTests: each refusal names its reason
rejects_corrupt_blobsonly assertedis_err(). If one check stopped firing, the test still passed whenever a later check happened to catch the same bytes. EachMalformedreason thatnative_programscan return now has a case that expects that exact message:v255or 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/parthelpers. No title data is used.Tested on Linux (
x86_64-unknown-linux-gnu), rebased on currentmaincargo test -p victory-shader --lib: 72 passed (25 innative)cargo clippy -p victory-shader -- -D warnings: cleancargo fmt -p victory-shader --check: cleancargo xtask policy: 151 files ok