Skip to content

Shaders: DXBC/DXIL parser and sampler/SP3 robustness fixes - #326

Closed
SkippySteve wants to merge 101 commits into
Marvisak:mainfrom
SkippySteve:SkippySteve/dxbc-part-overlap
Closed

SkippySteve wants to merge 101 commits into
Marvisak:mainfrom
SkippySteve:SkippySteve/dxbc-part-overlap

Conversation

@SkippySteve

@SkippySteve SkippySteve commented Nov 24, 2026 •

Copy link
Copy Markdown

Shader parser and translation robustness: small, independent fixes in victory-shader, each in its own file and commit. This combines #326, #300, #327 and #283.

1. DXBC: a part inside the container header is malformed (dxbc.rs)

Container::parse checked that each part lies inside the container, but not that it comes after the header. A part offset of 0 read "DXBC" as the part's FourCC and the first digest dword as its size. An offset inside the part offset table read the offsets themselves as a part. Shader containers come from title files, so the parser should reject these.

  • Part offsets below the end of the offset table (0x20 + 4 * count) now fail with Malformed("part overlaps the container header").
  • Each Malformed branch has a test built from synthetic containers: stated size too large or smaller than the header, an implausible part count, an offset table past the end, part header or data out of bounds, and parts inside the header or offset table. One test checks that bytes past the stated size are ignored.

Valid containers put parts after the table, so they parse as before.

2. DXIL: tests for each way a program header is refused (dxil.rs)

Tests only, no code change. DxilProgram::parse can refuse a program in five ways, and the tests only reached one of them. New synthetic-header tests cover a bad declared size (too large, below 24 bytes, u32::MAX dwords), a truncated header, a wrong magic, bitcode out of bounds, a bitcode offset inside the header, padding after the program that must not become a trailer, and an unknown program kind kept as ShaderKind::Unknown. Each rejection test checks the exact Malformed message.

3. Immediate sampler LOD bias stays in D3D12's range (emit/sampler.rs)

The sampler's LOD bias is signed 5.8 fixed point, so it decodes to anything from -32 to 31.996. D3D12 only accepts a static sampler MipLODBias between -16 and 15.99. A root signature with an out-of-range value is refused, and the pipeline is lost with it. The decoded bias is now clamped to D3D12's range; values inside it are unchanged. New test: lod_bias_stays_in_the_host_range.

4. SP3 text gives a message only the fields it has (gfx10/sp3.rs)

s_sendmsg operands were all written as GS messages, so the NGG allocation request showed as sendmsg(MSG_GS_ALLOC_REQ, GS_OP_NOP, 0). Messages are now spelled the way LLVM's AMDGPU assembler spells them: the operation appears only for MSG_GS, MSG_GS_DONE and MSG_SYSMSG, and the stream only for a GS emit or cut. Every GFX10 id has a name. Operands with bits no field accounts for stay as raw hex.

imm before after
0x0009 sendmsg(MSG_GS_ALLOC_REQ, GS_OP_NOP, 0) sendmsg(MSG_GS_ALLOC_REQ)
0x0008 sendmsg(8, GS_OP_NOP, 0) sendmsg(MSG_EARLY_PRIM_DEALLOC)
0x002F sendmsg(15, GS_OP_EMIT, 0) sendmsg(MSG_SYSMSG, SYSMSG_OP_REG_RD)
0x0122 sendmsg(MSG_GS, GS_OP_EMIT, 1) unchanged
0x0019 sendmsg(MSG_GS_ALLOC_REQ, GS_OP_CUT, 0) 0x0019

Only shader dumps use this text. Translation reads the raw immediate, so its behaviour does not change.

All tests use synthetic bytes, with no shader or game data.

5. GFX10 decode: a far jump to an unaligned target is refused (gfx10/mod.rs)

The far-jump resolver (s_getpc_b64 / s_add_u32 sN, sN, literal / s_addc_u32 / s_setpc_b64) checked that the target lies inside the code, but not that it is dword aligned. A literal that is not a multiple of 4 sent decoding into the middle of a dword, reading words that straddle two instructions. That target now fails with Isa { reason: "far jump target is not dword aligned" }. Both gfx10::decode and ir::build go through this resolver.

  • New test rejects_far_jumps_off_the_code_or_off_a_dword: a valid far jump, targets past the end and before the start, and literals 13/14/15/17.
  • New test each_refusal_names_its_reason_and_offset checks the exact offset and reason for each way decode refuses code: odd size, a branch or s_call_b64 out of range (forward and backward), a missing literal, running off the end without s_endpgm, a truncated VOP3 or MTBUF, a reserved FLAT segment, an unknown encoding, and a branch into an instruction. The old rejects_bad_code test only checked is_err().

6. Immediate sampler max LOD is never below its min LOD (emit/sampler.rs)

Word 1 holds the minimum and maximum LOD as independent 4.8 fields, so a sampler can encode a maximum below its minimum. That went straight into the static sampler's MinLOD/MaxLOD. A host sampler with MaxLOD < MinLOD is invalid: under Proton, vkd3d-proton creates a Vulkan sampler, and Vulkan requires maxLod >= minLod (VUID-VkSamplerCreateInfo-maxLod-01973). The maximum is now raised to the minimum. The hardware clamp gives the minimum LOD in that case, and an equal pair does the same. Ordered pairs and samplers without a mip filter are unchanged. New test: max_lod_is_never_below_min_lod.

Testing after this commit (Linux): cargo test -p victory-shader 72 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean; rustfmt --check --edition 2024 on sampler.rs clean; cargo xtask policy ok.

7. ir::build refuses more than 256 VGPRs (ir.rs)

A VGPR is named by a byte (Reg::V(u8)). M0-relative access (v_movrels_b32, v_movreld_b32 and the like) lists every register from its base up to the program's VGPR count, casting each index to u8. With a count above 256 those indices wrapped round to v0, so the instruction claimed to read and write registers it never touches, and SSA linked the wrong values. native.rs already refuses a count above 256 in the records it reads, but ir::build is public and took any u16. It now fails with Malformed("more than 256 VGPRs"); counts of 256 and below are unchanged.

New test refuses_more_vgprs_than_a_byte_names: a v_movrels_b32 with 256 VGPRs reaches v255, and 257 and u16::MAX are refused.

Testing after this commit (Linux): cargo test -p victory-shader 73 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean; rustfmt --check --edition 2024 on ir.rs clean; cargo xtask policy ok.

8. A vertex attribute past v255 is refused (io.rs)

A vertex shader's stage data names each attribute's first VGPR with a byte (VsInput::vgpr), and io::raise added the channel to it as a u8. A base of 253 or more with a high channel overflowed. In debug builds that panicked. In release it wrapped onto v0-v2, so an attribute channel claimed the register that holds the vertex id or primitive id. Such an input now fails with Malformed("vertex attribute past v255"). Bases whose channels end at v255 or below are unchanged. native.rs is not touched.

New test vertex_attributes_stay_below_v256: v252 with xyzw and v255 with x are kept; v253 xyzw, v255 y and v254 w are refused. Before the fix, the test panicked with an add overflow at io.rs.

Testing after this commit (Linux): cargo test -p victory-shader 74 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean; rustfmt --check --edition 2024 on io.rs clean; cargo xtask policy ok.

9. A far jump whose registers change before s_setpc is not resolved (gfx10/mod.rs)

The far-jump resolver walks back from s_setpc_b64 s[n:n+1] over s_getpc_b64, s_add_u32|s_sub_u32 sn, sn, literal and the carry into s(n+1). It skipped every other instruction in that run, even one that writes the pair. A second s_add_u32 s8, s8, 16, an s_mov_b32 s9, 0, an s_load_dword or a v_readfirstlane_b32 into s8/s9 sends the jump somewhere other than where the resolver says it goes, and decoding then followed the wrong target. Now any write to s[n:n+1] that is not the add or its carry leaves the jump unresolved: decode stops there and ir::build gives Exit::Indirect, as for any other s_setpc_b64. Writes to other registers, and the compiler's own four-instruction form, are unchanged.

New test a_far_jump_whose_registers_change_is_not_resolved: the plain form, s_mov_b32 s10 and v_readfirstlane_b32 s7 between are still followed. A second add, moves into s9, s[8:9] and s[7:8], v_readfirstlane_b32 s9, s_load_dword s8 and a carry into s8 are not. Without the fix the test fails because decoding follows the wrong target.

Testing after this commit (Linux): cargo test -p victory-shader 75 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean; rustfmt --check --edition 2024 on gfx10/mod.rs clean; cargo xtask policy ok.

10. A signature row count past its part is refused before allocating (emit/mod.rs)

signature() read the ISG1/OSG1 row count and called Vec::with_capacity(count) before reading any row. Signature parts come from title files. A count of 0xFFFF_FFFF asked for 206,158,430,160 bytes up front. The allocation failed and the process aborted (SIGABRT), so the error never came back as a ShaderError. Now the rows must fit in the part (base + 32 * count <= len, with checked arithmetic), or the call fails with Malformed("signature rows out of bounds"). Valid parts already put every row inside the part, so they parse as before.

New test a_signature_row_count_past_the_part_is_refused: a one-row part parses, and so does an empty one. Counts of u32::MAX, 0x0800_0000 and 2 are refused, as are rows that start past the end. Without the fix the test aborts with memory allocation of 206158430160 bytes failed.

11. A comparison sampler replaces the reduction instead of OR-ing into it (emit/sampler.rs, emit/mod.rs)

When an image_sample_c* used an immediate sampler, the code set comparison mode with filter |= 0x80. D3D12_FILTER_REDUCTION_TYPE is the two-bit field at bits 7-8: 0 standard, 1 comparison, 2 minimum, 3 maximum. OR-ing bit 7 into a minimum-reduction sampler (0x115) gave 0x195, which is a maximum filter. A maximum sampler stayed maximum with no comparison. Either way the static sampler stopped matching the shader's SamplerComparisonState declaration. The new StaticSampler::comparison() writes the reduction field as comparison and keeps the min/mag/mip and anisotropy bits.

New test comparison_replaces_the_reduction: point, linear and anisotropic filters, with minimum and maximum reductions, all come out as their comparison form. The other sampler fields are unchanged. Without the fix it fails with minimum: left 405 (0x195), right 149 (0x95).

Testing after these commits (Linux): cargo test -p victory-shader 77 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean; rustfmt --check --edition 2024 on both files clean; cargo xtask policy ok. Each commit builds and passes on its own. Separately, 200k random GFX10 programs went through gfx10::decode, the SP3 text, ir::build (random VGPR counts, wave32/64) and structure with no panics. That harness was not committed.

Testing (Linux, x86_64-unknown-linux-gnu)

  • cargo test -p victory-shader: 71 passed
  • cargo clippy -p victory-shader --lib -- -D warnings: clean
  • rustfmt --check --edition 2024 on the four touched files: clean
  • cargo xtask policy: ok

Also in this PR (folded from #262, now closed)

  • emit/mod.rs: constant_color() was only called by the two #[cfg(windows)] tests, so on Linux cargo clippy --all-targets -p victory-shader warned function constant_color is never used. This is the clippy warning still left over from Build: gate Windows-only paths so the workspace builds and tests on Linux #235. A new test, constant_color_is_a_well_formed_container, runs on every host and needs neither D3D12 nor dxil.dll. It checks that the container parses, that its size field matches its length, that the parts are SFI0 ISG1 OSG1 PSV0 HASH DXIL in that order, that the DXIL program header is there, and that the digest is correct.
  • The bitcode::Attr and shader::Shader imports move from the test module into the helper, which is the only code that uses them.
  • Tests only: no runtime code changes.

After the fold (Linux):

  • cargo test -p victory-shader: 69 passed
  • cargo clippy -p victory-shader --lib -- -D warnings: clean
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean (no constant_color warning)
  • rustfmt --check --edition 2024 on the touched files and cargo xtask policy: clean

Also: a root parameter's user data past s247 names no register (resource.rs)

RootParam::user_data is a raw byte of the title's root layout. On a merged stage (LS/HS/ES/GS/mesh) user data starts at s8, so HwStage::user_sgpr overflowed its u8 for values past 247. In a debug build that panicked. In a release build it wrapped onto s0-s7, and the tracer claimed registers the hardware fills there for a root parameter. resource.rs now counts the first SGPR in u32, so a parameter past the register file matches no register and the load fails with the usual "no root parameter" reason. native.rs is unchanged.

New test user_data_past_the_sgprs_names_no_register: user data 250 and 255 on each merged stage. Without the fix it fails with attempt to add with overflow.

After this commit (Linux): cargo test -p victory-shader 78 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean; rustfmt --check --edition 2024 clean; cargo xtask policy ok.

Also: a compute thread group past D3D12's limits is refused (emit/lower.rs)

A compute program's numthreads comes from the title's stage data as three raw u32s. Lowering multiplied them (the one-wave check and the console thread id) and divided by them without checking. A size of 65536 x 65536 panicked with attempt to multiply with overflow in a debug build and wrapped to 0 in a release build, which made the one-wave check pass wrongly. A 0 component became a division by zero in the host code. lower() now refuses any group D3D12 cannot run (1-1024 x 1-1024 x 1-64, at most 1024 threads) with Malformed("a thread group past D3D12's limits"). Mesh programs already check their limits. native.rs is unchanged.

New test a_thread_group_past_d3d12s_limits_is_refused: zero components, each axis one past its limit, totals over 1024, 65536 x 65536 and u32::MAX on both wave sizes are refused, and sizes at the limits still lower. Without the fix it fails, and 65536 x 65536 panics with the overflow.

After this commit (Linux): cargo test -p victory-shader 81 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean; rustfmt --check --edition 2024 clean; cargo xtask policy ok. Separately, about 3.2M random GFX10 programs (mutated from the crate's test words, with random stage data and root layouts) went through ir::build, resource::track, io::raise, structure and emit with no panics. That harness was not committed.

Also: a signature register past D3D12's 32 is refused (emit/mod.rs)

An ISG1/OSG1 row's register is a raw u32 of the title's container, and emit kept it as the row. The PSV0 writer multiplied it by four and wrote a dependency mask for every input scalar up to it. A register of 2^30 panicked with attempt to multiply with overflow (container.rs:241) in a debug build. Registers past 32 but below that sized the masks by the register instead of the rows. D3D12 links stages through 32 registers, so signature() now refuses anything past them with Malformed("signature register past the 32 D3D12 has"). u32::MAX still means no register. native.rs is unchanged.

New test a_signature_register_past_32_is_refused: registers 0, 31 and u32::MAX parse; 32, 255, 256, 2^30 and u32::MAX - 1 are refused.

After this commit (Linux): cargo test -p victory-shader 82 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean; rustfmt --check --edition 2024 clean; cargo xtask policy ok.

Also: a mesh group id strides by every axis of the HLSL group (emit/mod.rs)

A mesh program's group id arrives multiplied by the HLSL group's thread count, and emit reads that count from PSV0 (runtime info version 2 on) to divide it back out. PSV0 stores the group as NumThreadsX, Y and Z, but only X was read, so numthreads(8, 8, 1) strode by 8 instead of 64 and neighbouring groups got overlapping ids. Every mesh diff test is one-dimensional, which is why this never showed. The new mesh_group_threads multiplies all three. The sizes are raw u32s of the title's part, so a group D3D12 cannot run (an empty axis, or more than 128 threads) fails with Malformed("a mesh thread group past D3D12's limits") before they are multiplied. Containers without version 2 runtime info fall back to the stage data's lane count as before. native.rs is unchanged.

New test a_mesh_group_counts_every_axis: [32,1,1], [8,8,1], [4,4,8], [1,1,128] and [1,1,1] give their products. Zero axes, [129,1,1], [16,16,1], 65536 x 65536 and u32::MAX on all three are refused. Older runtime info and a missing part give no count, and a version-2 part cut off before Z is refused. Synthetic bytes only.

After this commit (Linux): cargo test -p victory-shader 86 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean; rustfmt --check --edition 2024 clean; cargo xtask policy ok.

Also: a signature with more elements than PSV0 counts is refused (emit/mod.rs)

PSV0 counts each signature's elements in a byte (container.rs writes inputs.len() as u8, and the same for outputs and per-primitive rows). The count comes from the title's ISG1/OSG1 part as a raw u32, and rows without a register are not bounded by the 32 registers. A part with 256 such rows wrapped the emitted count to 0, so the container disagreed with its own signature and D3D12 refused it. signature() now refuses more than 253 elements (a byte, less the vertex and instance id rows lowering can add) with Malformed("signature with more elements than PSV0 counts"), after the bounds check and before anything is allocated.

New test a_signature_past_psv0s_element_count_is_refused: 1 and 253 unallocated rows parse; 254, 255, 256 and 300 are refused. Synthetic bytes only.

After this commit (Linux): cargo test -p victory-shader --lib 87 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean on Linux and x86_64-pc-windows-msvc; cargo fmt --check clean; cargo xtask policy ok.

Also: a compute thread id past z is refused (io.rs)

COMPUTE_PGM_RSRC2's TIDIG_COMP_CNT is a two-bit field of the title's stage data: 0-2 enable the x, xy or xyz thread id VGPRs. ComputeIo::thread_id_vgprs returns the field plus one, so its fourth value made io::raise load v3 as ThreadIdInGroup(3), a component no thread id has. Lowering then passed component 3 to threadIdInGroup (or, with console lanes, treated it as z). io::raise now refuses that field with Malformed("a thread id component past z"). native.rs is unchanged.

New test compute_thread_ids_stop_at_z: counts 1 and 3 map v0 and v2 to their components, v2 with only x and y is no thread id, and the fourth value is refused. Without the fix it fails with left: Ok(Io { inputs: [(Value(1), ThreadIdInGroup(0))] .. }). Synthetic bytes only.

After this commit (Linux): cargo test -p victory-shader --lib 88 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean; rustfmt --check --edition 2024 clean; cargo xtask policy ok.

Also: two signature elements in one register component are refused (emit/mod.rs)

Lowering finds an input or output element by its register and component (find_input returns the first match). A title's ISG1/OSG1 part listing a second element over components another element already has left that second element never read or written, and D3D12 refuses a signature whose elements overlap. signature() now refuses it with Malformed("two signature elements in one register component"). Elements in a register's other components (x+y and z+w), in other registers, or without a register still parse.

New test signature_elements_sharing_a_component_are_refused: disjoint layouts parse; full overlap, partial overlap (xy/yz, x/xyzw, w/zw, yzw/y in r31) are refused. Without the fix the test fails. Synthetic bytes only.

After this commit (Linux): cargo test -p victory-shader --lib 91 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean; cargo fmt --check clean; cargo xtask policy ok.

Also: a semantic listed twice in a signature is refused (emit/mod.rs)

A semantic (its name, ignoring case, plus its index) names one signature element. The next stage links by it, and D3D12 refuses a signature that lists the same one twice. A title's ISG1/OSG1/PSG1 part could list a second element with the same semantic in another register, and signature() let it through. D3D12 then refused the pipeline. signature() now refuses it with Malformed("two signature elements with one semantic").

New test a_semantic_listed_twice_is_refused: the same name with another index, or another name with the same index, still parses. The same semantic is refused in any case, with or without a register. Without the fix the test fails. Synthetic bytes only.

After this commit (Linux): cargo test -p victory-shader --lib 92 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean; cargo fmt --check clean; cargo xtask policy ok.

Also: a vertex or instance id past r31 is refused (emit/lower.rs)

The console signature leaves out the vertex and instance ids, so lowering gives each one the row after the last row the signature uses. A system input without a register gets the same treatment. A title's ISG1 that already uses r31 put the id at r32. D3D12 links stages through 32 registers, so it refused the pipeline. The id is now missing with the reason "a system input past the 32 input registers", so a program that reads it fails translation with that reason. Signatures with a free row are unchanged. native.rs is unchanged.

New test a_vertex_id_after_r31_is_refused: a vertex shader exports its vertex id. After an input in r0 the id goes to r1, and after r30 it goes to r31. After r31 the shader is refused. Without the fix, the test fails because the id is emitted at r32. Synthetic bytes only.

After this commit (Linux): cargo test -p victory-shader --lib 93 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean on Linux and x86_64-pc-windows-msvc; cargo fmt --check clean; cargo xtask policy ok.

Also: a DXBC container listing a known part twice is refused (dxbc.rs)

Container::part returns the first part with a given FourCC. A title container with a second ISG1, PSV0 or DXIL had that part silently ignored by translation, while anything else reading the container could take the other copy. DXC's validator refuses such a container ("More than one ... part"). Container::parse now fails with Malformed("a part listed twice") for the part types DXC knows (DXIL STAT RTS0 PSV0 ISG1 OSG1 PSG1 SFI0 HASH ILDB ILDN). A FourCC the crate does not know, which may be the title's own, can still repeat. native.rs is unchanged.

New test a_known_part_listed_twice_is_refused: each known part listed twice (with another part between) is refused. All eleven listed once parse, and a title's own PRIV part listed twice parses, with part() returning the first. Without the fix the test fails. Synthetic bytes only.

After this commit (Linux): cargo test -p victory-shader --lib 94 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean; rustfmt --check --edition 2024 on dxbc.rs clean; cargo xtask policy ok.

Also: group shared memory past D3D12's limits is refused (emit/lower.rs)

A compute program's LDS size comes from COMPUTE_PGM_RSRC2 (up to 255.5 KB in the field, 64 KB on the hardware) and a mesh program's from its record as a raw u32. D3D12 caps group shared memory at 32 KB for compute and 28 KB for mesh programs, so a larger declaration gave a module the validator refuses. A mesh size near u32::MAX also overflowed rounding to dwords and back (attempt to multiply with overflow in shader.rs, debug builds).

  • A compute program that uses LDS past 32 KB fails at that instruction with "group shared memory past D3D12's 32 KB". One that allocates more but never uses it still translates.
  • A mesh program's size goes into PSV0 either way, so one past 28 KB is refused before lowering with "mesh group shared memory past D3D12's 28 KB".

native.rs is unchanged.

New test group_shared_memory_past_d3d12s_limits_is_refused: a compute ds_write_b32 with 1, 32 and 64 granules translates, and 65, 128 and 511 are refused (unused, they translate). Mesh sizes 0, 4 and 28 KB translate; 28 KB + 1, 32 KB, 64 KB, u32::MAX - 2 and u32::MAX are refused. Without the fix, the compute and 28 KB + 1 cases are accepted and u32::MAX panics. Synthetic bytes only.

After this commit (Linux): cargo test -p victory-shader --lib 97 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean on Linux and x86_64-pc-windows-msvc; cargo fmt --check clean; cargo xtask policy ok.

Also: a colour or position export stays inside its element (emit/lower.rs)

An export's channels are the components of the register it targets. Lowering stored channel n of an mrt or pos0 export as column n of the SV_Target or SV_Position element, whatever columns the element actually had. A target narrower than a row (a float2 or float3) got storeOutput calls for columns past it, which DXC's validator refuses, and the emitted OSG1 claimed those components were written. The channel now maps to the element's own column, the same way parameter exports already do, and a channel the element does not cover is dropped. Four-column elements are unchanged. native.rs is unchanged.

New test a_colour_export_stays_inside_its_element: an exp mrt0 of all four channels into SV_Target elements xyzw, xyz, xy, x, zw and w. The emitted OSG1 lists exactly the components each element lacks as never written. Without the fix it fails for xyz (left: (7, 0), right: (7, 8)). Synthetic bytes only.

After this commit (Linux): cargo test -p victory-shader --lib 98 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean on Linux and x86_64-pc-windows-msvc; cargo fmt --check clean; cargo xtask policy ok.

Also: a signature name outside printable ASCII is refused (emit/mod.rs)

HLSL semantics are identifiers, but a signature row's name is raw bytes of the title's part, and it went through String::from_utf8_lossy. Each invalid byte became U+FFFD, which is three bytes. A name could pass the 64-character check and still reach DXC's validator longer than that (Meta.SemanticLen): 64 bytes of 0xFF became 192. Two different names, such as 0xFE and 0xFF, also became the same semantic. A name with any byte outside printable ASCII now fails with Malformed("signature name outside printable ASCII"). ASCII names are unchanged.

New test a_signature_name_outside_printable_ascii_is_refused: TEXCOORD, SV_Position and _a1~! are kept byte for byte. These are refused: 64 × 0xFF, 0xFE, an embedded 0x80, UTF-8 é, a space, a tab and 0x7F. Without the fix, the test fails. Synthetic bytes only.

After this commit (Linux): cargo test -p victory-shader --lib 100 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean on Linux and x86_64-pc-windows-msvc; cargo fmt --check clean; cargo xtask policy ok.

Also: two DXBC parts that overlap are refused (dxbc.rs)

Container::parse checked each part against the container and the header, but not against the other parts. Two offsets whose ranges share bytes made one part read the other's header or data as its own. DXC lays parts end to end, so parse now fails with Malformed("two parts overlap"). Parts that only touch still parse.

Also: a render target's semantic index matches its register (emit/mod.rs)

An SV_Target row names its render target twice: its semantic index goes into the emitted signature metadata, and its register is what the pipeline description (RTVFormats) and lowering (exp mrtN) use. Both are raw fields of the title's OSG1 row. A row whose index disagreed with its register was refused by DXC's validator (SM.PSTargetIndexMatchesRow), so the pipeline was lost after translation. A target with no register went into format slot 0 and no export reached it. signature() now refuses both with Malformed("render target index disagrees with its register"). Other elements keep any index. native.rs is unchanged.

New test a_render_target_index_matches_its_register: targets 0, 3 and 7 with a matching index parse. Index/register pairs (1,0), (0,1), (7,6), (8,0), (u32::MAX,0) and a target with no register are refused. A non-target element keeps index 5 in register 2, and an unallocated one keeps its index. Without the fix the test fails (index 1, register 0 parses). Synthetic bytes only.

After this commit (Linux): cargo test -p victory-shader --lib 101 passed; cargo clippy -p victory-shader --all-targets -- -D warnings clean on Linux and x86_64-pc-windows-msvc; cargo fmt --check clean; cargo xtask policy ok.

A pixel shader's parameter export is refused (io.rs)

A pixel shader has no parameter outputs, so exp param0 matched the vertex shader's dummy-export arm, became Target::Unread, and was dropped in lowering. Any exp param<n> from a pixel shader now fails with Malformed("a pixel shader exporting a parameter"). New test export_targets_map_or_are_refused maps every export target number and checks that the reserved gaps (10, 11, 17-19, 21-31) are refused.

DXBC: overlap against every earlier part; a known part repeated at one offset (dxbc.rs)

Tests only. overlap_is_checked_against_every_earlier_part: a third part pointed back into the first part's header or data, with the second part between them in the table, is refused as "two parts overlap". a_known_part_listed_twice_at_one_offset_is_a_repeat: two table entries for the same DXIL part are reported as "a part listed twice" before the overlap check runs, while a title's own FourCC listed twice at the same offset is reported as an overlap.

Testing (Linux): cargo test -p victory-shader 129 passed; clippy -D warnings clean; rustfmt --check --edition 2024 clean; cargo xtask policy ok.

Immediate samplers: each field decodes on its own (emit/sampler.rs)

Tests only. each_field_decodes_on_its_own walks every address mode on each axis (U, V, W independently), log2 anisotropy 0-7 (5-7 clamp to D3D12's 16), every comparison function (1-8), every border colour (custom falls back to transparent black), the reserved reduction 3 (filters, as 0 does), and checks bits outside the decoded fields change nothing. either_anisotropic_filter_is_anisotropic covers all 48 mag/min/mip combinations: one anisotropic filter makes the sampler 0x55, otherwise the point/linear bits map one to one.

Also merges upstream main (conflict in emit/lower.rs tests: lowers now takes rsrc2 and routes through dxil_io). cargo test -p victory-shader 146 passed; clippy -D warnings clean; rustfmt clean; cargo xtask policy ok.

SkippySteve added 2 commits November 23, 2026 20:53
…ry host

constant_color() was used only by the two Windows-gated tests, so off Windows
it was dead code and victory-shader's tests built with a warning. A new test
parses the container it builds, checks the size field, the pixel-shader parts
in order, the DXIL program header and that the digest is container::hash of
the bytes after it. It needs neither D3D12 nor dxil.dll, so it runs on Linux.
Only constant_color uses them, so they live inside it (from #258). The
helper itself stays on every host for the hermetic container test.
SkippySteve added 4 commits November 23, 2026 21:28
Container::parse accepted part offsets that point back into the 0x20-byte
header or the part offset table, so offset 0 read "DXBC" as a part's
FourCC and the digest as its size. Parts start after the offset table;
reject anything earlier, and cover each Malformed branch of the parser
with synthetic containers.
DxilProgram::parse has five rejections (program size, DXIL magic, bitcode
bounds, LLVM magic, bitcode offset) and trims a part to the program's own
size before reading the trailer, but the tests only reached the LLVM-magic
rejection. Synthetic headers now hit each one by name, check that padding
after the program is not taken for an Xbox trailer, and that an unknown
program kind is kept rather than refused.

Tests only; runs on any host.
The sampler word's LOD bias is a 14-bit signed 5.8 field, so it decodes
to anything from -32 to 31.996. D3D12 takes a static sampler's MipLODBias
only from -16 to 15.99 (D3D12_MIP_LOD_BIAS_MIN/MAX) and refuses a root
signature holding one outside that, which would lose the whole pipeline.
Clamp the decoded bias to that range; values inside it are unchanged.

The test covers both clamped ends and in-range values on either side,
and runs on every host.
The disassembler wrote every s_sendmsg as if it were a GS message, with a
GS operation and a stream, so the mesh stage's allocation request read
"sendmsg(MSG_GS_ALLOC_REQ, GS_OP_NOP, 0)", and only four of the GFX10
message ids had names.

Now messages are spelled the way LLVM's AMDGPU assembler spells them: the
operation only for GS, GS_DONE and SYSMSG, the stream only for a GS emit or
cut, and every GFX10 message id named. An operand whose bits no field
accounts for (an unknown id, a stream on a NOP, an operation on a message
that has none, bits above the stream) stays as raw hex instead of losing
those bits. The text is only used for shader dumps; translation does not
read it.
@SkippySteve
SkippySteve force-pushed the SkippySteve/dxbc-part-overlap branch from 7a6bfde to aa28828 Compare November 24, 2026 02:29
@SkippySteve SkippySteve changed the title Shaders: a DXBC part inside the container header is malformed Shaders: DXBC/DXIL parser and sampler/SP3 robustness fixes Nov 24, 2026
The far-jump resolver took any literal: one that is not a multiple of 4
sent decoding into the middle of a dword, reading words that straddle
two instructions. Such a target is now an ISA error.

Tests also pin the exact offset and reason of each way decode refuses
code: odd size, branch or call out of range, a truncated instruction or
literal, running off the end, a reserved FLAT segment, an unknown
encoding, a branch into an instruction, and far jumps off the code.
SkippySteve added 2 commits November 23, 2026 21:49
The sampler word's minimum and maximum LOD fields are independent, so a
maximum below the minimum decodes as is. A host sampler needs MaxLOD >=
MinLOD (Vulkan VUID-VkSamplerCreateInfo-maxLod-01973 under vkd3d-proton).
The maximum is now raised to the minimum, which gives the same clamp
result; ordered pairs are unchanged.
A VGPR is named by a byte. With a count past 256, M0-relative access
(v_movrels/v_movreld) listed registers up to the count and wrapped
round to v0, aliasing the registers it reads and writes. native.rs
already caps the count it reads; ir::build is public, so it now
checks too.
muhammed-aksoy pushed a commit to muhammed-aksoy/VIctory that referenced this pull request Nov 24, 2026
SkippySteve added 8 commits November 23, 2026 22:03
A vertex shader's stage data names each attribute's first VGPR with a
byte, and io::raise added the channel to it as a u8. A base of 253 or
more with a high channel overflowed: a panic in debug builds, and in
release a wrap onto v0-v2, so an attribute channel claimed the vertex
or primitive id register. Such an input now fails with
Malformed("vertex attribute past v255").

Test vertex_attributes_stay_below_v256: bases that reach v255 exactly
are kept, ones that pass it are refused (it panicked before the fix).
…lved

The far-jump resolver looked back over s_getpc_b64 / s_add_u32 /
s_addc_u32 / s_setpc_b64 and ignored any other instruction in that run,
even one that writes the jump's register pair. A second s_add_u32 to the
low dword or a move into the high dword sent the jump somewhere else than
the resolver computed. Such a jump is now left unresolved (an indirect
exit) instead of being followed to the wrong target.
…ting

signature() reserved room for the part's stated row count before reading
any row. A count of 2^32 - 1 asked for ~206 GB and the failed allocation
aborted the process. Rows must now fit in the part (base + 32 * count),
else Malformed("signature rows out of bounds").
D3D12_FILTER_REDUCTION_TYPE (bits 7-8) is one of filtering, comparison,
minimum or maximum. OR-ing bit 7 into a minimum-reduction filter made it
maximum, and left maximum without comparison. StaticSampler::comparison()
now sets the field to comparison and keeps the other filter bits.
…es no register

RootParam::user_data is a byte of the title's root layout. On a merged
stage (Ls/Hs/Es/Gs/Ms) user data starts at s8, so HwStage::user_sgpr
overflowed its u8 for values past 247: a panic in a debug build, and in
a release build a wrap onto s0-s7, where the tracer claimed registers
the hardware fills for a root parameter. resource.rs now counts the
first SGPR in u32, so such a parameter matches no register.

New test: user_data_past_the_sgprs_names_no_register.
… for

v_writelane_b32 and v_readlane_b32 take the lane from the low 5 (wave32)
or 6 (wave64) bits of their select. The spill tracer compared selects
unmasked, so a write to lane 35 of a wave32 was not seen to replace the
one to lane 3, and a read of lane 3 traced the older SGPR's root
parameter.
v_readlane_b32 and v_writelane_b32 take their lane from the low 5
(wave32) or 6 (wave64) bits of the select. Lowering passed the select
through unmasked, so a write to lane 35 of a wave32 compared equal to no
lane and a read of it asked the host for a lane past its wave. Console
lane selects are now masked (constants folded); a computed host lane in
a pixel or vertex program stays as it is.
A compute program's numthreads comes from the title's stage data as three
raw u32s. Lowering multiplied them (the one-wave check and the console
thread id) and divided by them unchecked, so 65536 x 65536 panicked with
"attempt to multiply with overflow" in a debug build and wrapped to 0 in
a release one, and a 0 component became a division by zero in the host
code. lower() now refuses a group D3D12 cannot run (1-1024 x 1-1024 x
1-64, at most 1024 threads) with Malformed("a thread group past D3D12's
limits"); mesh programs already check their limits.
@DaviTostes

Copy link
Copy Markdown

Verified 461fa29 on Arch Linux x86_64 (rustc 1.98.1, cargo test -p victory-shader): 81 passed, 0 failed, 0 ignored.

Not covered: refusing a real compute thread group beyond D3D12's limits at device time (no game data) and Windows/Wine validation of the emitted modules.

An ISG1/OSG1 row's register is a raw u32 of the title's container, and
emit kept it as the row. The PSV0 writer multiplied it by four and wrote
a dependency mask for every input scalar up to it, so a register of 2^30
panicked with `attempt to multiply with overflow` in a debug build, and
smaller ones past 32 sized the masks by the register rather than the
rows. D3D12 links stages through 32 registers, so signature() now
refuses anything past them (u32::MAX still means no register).

New test a_signature_register_past_32_is_refused.
SkippySteve added 4 commits November 24, 2026 05:35
@sebastianspicker

Copy link
Copy Markdown

A libFuzzer target that would keep these fixes from regressing: it calls Container::parse and, when that succeeds, emit::translate. Against this branch at 8789c38 it ran 3.2 million inputs in 60 s without a panic, from an empty corpus. It lives in a separate fuzz/ crate outside the workspace, so cargo test doesn't see it. Happy to put it on a fork branch if you want it alongside this PR.

AI-assisted.

SkippySteve added 4 commits November 24, 2026 05:50
…DPP masks and flags, SDWA destination and source modifiers, permlane fi/bound_ctrl and packed half selects
…ins or splits DS offsets by access, and follows the segment for FLAT offsets and addresses
…nstruction, names only the set parts of a scalar load offset, and sizes image addresses by every dimension with dlc first
SkippySteve added 3 commits November 24, 2026 06:02
…er with the operands they reshape, and names interpolation parameters with the attribute last
…one space after the mnemonic, and test the invalidations' operandless text
@Zilumz1

Zilumz1 commented Nov 24, 2026

Copy link
Copy Markdown

Cross-check from someone new to this code: I initially suspected DxilProgram::is_standard_layout() misclassified PC DXIL with zero padding after a ragged BitcodeSize. Checking the DXIL headers and LLVM's bitcode rules, that input is invalid — LLVM bitcode streams must be a multiple of 4 bytes, and SizeInUint32 is dword-rounded. This PR's bitcode size not a multiple of 4 check is the right fix. I wrote a local synthetic 17-byte test, confirmed the reasoning, and reverted my change rather than contribute a wrong relaxation.

This PR also covers the DXBC part-inside-header case I had flagged. Verified locally on main at ffe310e: cargo test -p victory-shader dxil::tests → 4 passed. No hardware-dependent tests run (RX 580, no SM 6.6). PR creation is collaborator-only for me right now, hence a comment rather than a PR.

SkippySteve and others added 4 commits November 24, 2026 06:14
…mmediates as hex, and puts a SOPK register before its immediate with s_version alone
…and image dmask edges

Co-authored-by: Cursor <cursoragent@cursor.com>
Hermetic containers hit the two refusals after DXBC parse: one with no DXIL
part, and a PC-layout DXIL part with no XDAT native entry.

Co-authored-by: Cursor <cursoragent@cursor.com>
Xeltax pushed a commit to Xeltax/VIcky that referenced this pull request Nov 24, 2026
…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>

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-shader/src/emit/lower.rs, crates/victory-shader/src/ir.rs, crates/victory-shader/src/resource.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

@SkippySteve SkippySteve closed this by deleting the head repository Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants