Skip to content

shader: fixed buffer loads through built CODE descriptors can read below the code table - #197

Merged
pablogonz12 merged 7 commits into
mainfrom
fix/issue-135
Nov 26, 2026
Merged

pablogonz12 merged 7 commits into
mainfrom
fix/issue-135

Conversation

@pablogonz12

@pablogonz12 pablogonz12 commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Fixes #135.

What

code_table_start (crates/victory-shader/src/emit/lower.rs) picks the first byte of the constant table from the tracked CODE addresses: every scalar s_load_* through s_getpc_b64, and every built descriptor's base.

A buffer load through a built CODE descriptor (s_buffer_load_dword on a descriptor the code builds around s_getpc_b64) reads its base plus the load's own byte offset, and that offset can be negative, so the byte the load reads can lie below the base. Tracking recorded only the base, the table started there, and the load's index then rebased below the table and clamped to its last dword — a wrong constant where the code before the table-start change read the right one.

  • crates/victory-shader/src/resource.rs: Resources::code_reads records the effective byte (address.offset.wrapping_add(load offset)) of a load through a built CODE descriptor whose own offset is fixed (soffset NULL); the descriptor closure in track now returns the source it classified so the s_buffer_* arm can see it.
  • crates/victory-shader/src/emit/lower.rs: code_dword chains code_reads into the min that sets the table's first byte. Dynamic terms are unchanged (the entry carries address.dynamic, as before, and the lowest clamp stays).

Test

crates/victory-shader/src/emit/lower/tests/code_table.rs, hand-built words: s_getpc_b64 + s_add_u32 0x7c builds a descriptor whose base is 0x80, and s_buffer_load_dword s12, s[8:11], -64 reads byte 0x40, below the base. The test asserts the table starts at 0x40 and holds the code from there.

Gates

  • cargo test -p victory-shader: 188 passed, 0 failed, 2 ignored (unchanged).
  • cargo xwin clippy --target x86_64-pc-windows-msvc --workspace --all-targets -- -D warnings: clean.
  • offline-check.sh on xcl-bin/shaders-dump: 751/751 programs ok through all four stages (translation, dxil-spirv, spirv-val, RADV pipeline), so 751 pipelines. DXIL changed: 0/751.
  • offline-check.sh on xcl-bin/victory-run: 2912/2912 translated; 2909/2912 ok through all four stages (pipelines ≥ 2909). DXIL changed: 0/2912. The 3 (compute-559d9f0184e15fe3, compute-a1b20bc47e3c9597, compute-e8c962358d862e3f) segfault inside dxil-spirv on DXIL byte-identical to main's (checked program by program), so they are the converter's pre-existing crash, not this change: they fail on main's DXIL too, and older reports show the same three.
  • cargo xtask policy: 245 files ok; git grep -iE "gta ?(vi|6)|grand theft|rockstar": empty.

Off switch

VICTORY_CODE_READS=0 — the table starts at the descriptor's base again, as before. No program of either dump changes, so the switch has nothing to change on those; it is there if a title program's code table comes out wrong in a run.

Not tested in a game run.

Summary by CodeRabbit

  • Bug Fixes
    • Shader translation now accounts for code reads located before a descriptor’s base address, including the required earlier code in the translated output.
    • Code reads are included in code-table placement by default, with an option to exclude them. Changes to this setting are reflected in shader-cache keys.
  • Tests
    • Added regression coverage for code-table placement when shaders read from below a descriptor base.

…table low

A load through a descriptor the code builds around s_getpc_b64 reads the
descriptor's base plus the load's own byte offset, which can lie below
the base (a sub against s_getpc_b64). Tracking recorded only the base,
so the constant table started after it and the load's index rebased
below the table and clamped to its last dword: the wrong value.

Track the effective byte of fixed loads through a CODE descriptor
(Resources::code_reads) and let code_table_start see it.

Fixes #135
No program of the 751- and 2,912-shader dumps changes, so the switch is
the only way to get the pre-fix table start back.
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

Changes: Built CODE descriptor reads

Layer / File(s) Summary
Track effective CODE reads
crates/victory-shader/src/resource.rs
Resources records effective addresses for qualifying buffer loads through built CODE descriptors. Each address combines the descriptor offset and instruction offset with wrapping arithmetic, and retains the dynamic term.
Select and verify the table start
crates/victory-shader/src/emit/lower.rs, crates/victory-shader/src/emit/mod.rs, crates/victory-shader/src/emit/lower/tests/code_table.rs
Lowering includes tracked CODE reads among table-start candidates unless VICTORY_CODE_READS is set to 0. The setting is included in shader-cache keys. A regression test checks that the table starts at 0x40 and contains code from that byte.

Possibly related PRs

Suggested reviewers: fxceless

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to 6dd7f

This change makes the constant table start low enough to cover fixed-offset reads below a CODE descriptor's base, fixing incorrect clamped values. A regression test covers the case, and the cache key accounts for the new setting. No actionable merge-blocking risk remains.

Security architecture risk: Minimal · up to 6dd7f

The fix lets a shader read the intended constant from its own code without granting access to other memory. Restarting with the fix disabled selects a separate set of cached translations.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The newly reachable bytes are confined to an earlier portion of the input shader's own code image. The inspected path does not widen access to host memory or other GPU resources.

Trust Boundaries and Controls

  • observed — Shader-controlled descriptor arithmetic can influence the table start, but checked slicing and empty-table rejection constrain its contents to the shader image. Generated loads clamp indexes to the last valid dword before accessing the array.
Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 2
  • Waived: $0.50 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2dccd9de-59ae-4e75-b924-06dc9d29a6ef
Commits

Reviewing files that changed from the base of the PR and between d61f82e and 6dd7f13.

Files selected for processing: 2

  • crates/victory-shader/src/emit/lower.rs
  • crates/victory-shader/src/emit/mod.rs

Limit details: You’ve used all 10 included reviews currently available.

A translation cached with the switch on was served to a run with it
unset and the other way round (CodeRabbit on #197).
@pablogonz12

Copy link
Copy Markdown
Owner Author

Fixed in 554d89b: VICTORY_CODE_READS is now one of the switches hashed into shader_cache's key (crates/victory-gpu/src/shader_cache.rs), next to VICTORY_WAVE_WIDE and the clean-up pass set, so a translation cached in one mode is not served in the other.

The cache key moved to victory_shader::emit::settings_bytes, so the switch
goes in its SWITCHES list (the key_tests test checks it) and the edit to
shader_cache.rs is main's.
@pablogonz12

Copy link
Copy Markdown
Owner Author

Re-measured after merging hub/main (HEAD 3b54914, base 6e0b5da): retranslate over both dumps on each side gives 0/751 and 0/2912 programs with a changed DXIL (byte comparison per program), so the earlier 751/751 and 2909/2912 (3 pre-existing dxil-spirv segfaults on DXIL identical to main's) still describe the merged tree. The cache-key point from the review now lives in victory_shader::emit::SWITCHES (main moved the key to settings_bytes; key_tests covers VICTORY_CODE_READS).

@pablogonz12

Copy link
Copy Markdown
Owner Author

merged main in d61f82e: kept the CODE-descriptor buffer-load fix; descriptors are traced at the instruction's site. cargo xwin clippy --workspace --all-targets -D warnings green.

@pablogonz12

Copy link
Copy Markdown
Owner Author

gates: victory-gpu tests under Wine (VICTORY_TEST_ADAPTER=hw, RADV, native dxgi, --test-threads=1) or victory-shader on Linux — victory-shader 201 passed, 0 failed, no 'skipped: no device'.

@pablogonz12

Copy link
Copy Markdown
Owner Author

merged main again in 6dd7f13 (main had moved): conflicts were the SWITCHES list (union) and, in #147, main's compute-group counter, now carried by the Budget struct too. clippy -D warnings green.

@pablogonz12
pablogonz12 merged commit f0b2aeb into main Nov 26, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

shader: fixed buffer loads through built CODE descriptors can read below the code table start

1 participant