GPU: GetResourceAllocationInfo(2) inflates sub-64 KiB texture alignments and overflows on large totals (fix on a fork) #782

Open
opened 2026-11-26 04:13:57 +00:00 by github-bot · 3 comments
Owner

@⁠AndyLatimer on GitHub · 2026-11-24 10:36 UTC · original

Problem

In crates/victory-gpu/src/resource.rs, place() (shared by ID3D12Device::GetResourceAllocationInfo at slot 0xc8 and ID3D12Device8::GetResourceAllocationInfo2 at slot 0x360) has three defects:

  1. Sub-64 KiB texture alignments are inflated to 64 KiB:
    footprint() intentionally returns the console layout's own alignment (4 KiB for SW_4KB_S textures, 256 bytes for linear / SW_256B_* textures), matching XGCreateTextureComputer and CreatePlacedResource. However, place() initializes total.alignment = KIB_64 and updates total.alignment = total.alignment.max(one.alignment), clamping every query up to 64 KiB.

    • For the 512×512 BC7 10-mip texture checked in sizes_cover_every_mip (footprint = { size: 0x56000, alignment: 0x1000 }), GetResourceAllocationInfo returns { size: 0x56000, alignment: 0x10000 }.
    • GetResourceAllocationInfo2 contradicts itself on the same call: pResourceAllocationInfos1[0] receives alignment: 0x1000, while the returned D3D12_RESOURCE_ALLOCATION_INFO reports alignment: 0x10000.
    • Linear / SW_256B_* textures (256-byte alignment) are similarly inflated to 64 KiB.
  2. Unchecked u64 arithmetic in place():
    total.size.next_multiple_of(one.alignment) and offset + one.size can panic in debug builds (or wrap in release builds) when placing multiple large resources whose aligned sum exceeds u64::MAX, instead of returning { size: u64::MAX, alignment: 0 } and writing the failure sentinel to each[i].

  3. Null descriptor pointer with non-zero count:
    When count > 0 and descs is null, both allocation_info and allocation_info2 silently substitute &[] and report { size: 0, alignment: 65536 } instead of { size: u64::MAX, alignment: 0 }. For count == 0, host Windows D3D12 (ID3D12Device / ID3D12Device8) returns { SizeInBytes: 0, Alignment: 1 }.

Fix on a fork

Branch: AndyLatimer/VIctory@fix/gpu-allocation-info-alignment (based on origin/main ffe310e, commit ccdfb2e)

Summary of changes (crates/victory-gpu/src/resource.rs only):

  • Initialize total.alignment to 1 in place() so empty queries return { size: 0, alignment: 1 } (matching host D3D12) and non-empty queries return the exact maximum alignment of the requested resources (preserving 4 KiB and 256-byte console alignments).
  • Use checked_next_multiple_of and checked_add before writing each[i], returning { size: u64::MAX, alignment: 0 } and writing { offset: u64::MAX, alignment: 0, size: u64::MAX } on overflow.
  • Return { size: u64::MAX, alignment: 0 } when count > 0 and descs is null, and guard out with as_mut().
  • Add a synthetic regression test (resource::tests::allocation_info_keeps_console_alignments_and_checks_bounds) exercising GetResourceAllocationInfo (0xc8) and GetResourceAllocationInfo2 (0x360) on 4 KiB BC7 textures, linear textures, mixed 4 KiB/64 KiB batches, empty/null inputs, and both addition and alignment-padding overflows (verified to fail on ffe310e before the fix and pass after).

Verification

  • cargo test -p victory-gpu: 51 passed, 0 failed, 1 ignored (benchmark)
  • cargo test -p victory-gpu --features diagnostics: 54 passed, 0 failed, 1 ignored (benchmark)
  • cargo clippy -p victory-gpu --all-targets -- -D warnings (default and --features diagnostics): 0 warnings
  • cargo check -p victory --features diagnostics: clean
  • cargo xtask policy: asset policy: 154 files ok
  • rustfmt --edition 2024 --check crates/victory-gpu/src/resource.rs: no diffs in any modified lines (only the pre-existing untouched line on origin/main)

Disclosure

  • Tested with synthetic unit tests and host Windows D3D12 queries only; no game run or game data was used.
  • Prepared with AI assistance (Google Antigravity).
**@⁠AndyLatimer** on GitHub · 2026-11-24 10:36 UTC · [original](https://github.com/Marvisak/VIctory/issues/782) ### Problem In `crates/victory-gpu/src/resource.rs`, `place()` (shared by `ID3D12Device::GetResourceAllocationInfo` at slot `0xc8` and `ID3D12Device8::GetResourceAllocationInfo2` at slot `0x360`) has three defects: 1. **Sub-64 KiB texture alignments are inflated to 64 KiB**: `footprint()` intentionally returns the console layout's own alignment (4 KiB for `SW_4KB_S` textures, 256 bytes for linear / `SW_256B_*` textures), matching `XGCreateTextureComputer` and `CreatePlacedResource`. However, `place()` initializes `total.alignment = KIB_64` and updates `total.alignment = total.alignment.max(one.alignment)`, clamping every query up to 64 KiB. - For the 512×512 BC7 10-mip texture checked in `sizes_cover_every_mip` (`footprint` = `{ size: 0x56000, alignment: 0x1000 }`), `GetResourceAllocationInfo` returns `{ size: 0x56000, alignment: 0x10000 }`. - `GetResourceAllocationInfo2` contradicts itself on the same call: `pResourceAllocationInfos1[0]` receives `alignment: 0x1000`, while the returned `D3D12_RESOURCE_ALLOCATION_INFO` reports `alignment: 0x10000`. - Linear / `SW_256B_*` textures (256-byte alignment) are similarly inflated to 64 KiB. 2. **Unchecked `u64` arithmetic in `place()`**: `total.size.next_multiple_of(one.alignment)` and `offset + one.size` can panic in debug builds (or wrap in release builds) when placing multiple large resources whose aligned sum exceeds `u64::MAX`, instead of returning `{ size: u64::MAX, alignment: 0 }` and writing the failure sentinel to `each[i]`. 3. **Null descriptor pointer with non-zero count**: When `count > 0` and `descs` is null, both `allocation_info` and `allocation_info2` silently substitute `&[]` and report `{ size: 0, alignment: 65536 }` instead of `{ size: u64::MAX, alignment: 0 }`. For `count == 0`, host Windows D3D12 (`ID3D12Device` / `ID3D12Device8`) returns `{ SizeInBytes: 0, Alignment: 1 }`. ### Fix on a fork Branch: [`AndyLatimer/VIctory@fix/gpu-allocation-info-alignment`](https://github.com/AndyLatimer/VIctory/tree/fix/gpu-allocation-info-alignment) (based on `origin/main` `ffe310e`, commit [`ccdfb2e`](https://github.com/AndyLatimer/VIctory/commit/ccdfb2efe8d87ce40b580ff159d41431dba74db1)) Summary of changes (`crates/victory-gpu/src/resource.rs` only): - Initialize `total.alignment` to `1` in `place()` so empty queries return `{ size: 0, alignment: 1 }` (matching host D3D12) and non-empty queries return the exact maximum alignment of the requested resources (preserving 4 KiB and 256-byte console alignments). - Use `checked_next_multiple_of` and `checked_add` before writing `each[i]`, returning `{ size: u64::MAX, alignment: 0 }` and writing `{ offset: u64::MAX, alignment: 0, size: u64::MAX }` on overflow. - Return `{ size: u64::MAX, alignment: 0 }` when `count > 0` and `descs` is null, and guard `out` with `as_mut()`. - Add a synthetic regression test (`resource::tests::allocation_info_keeps_console_alignments_and_checks_bounds`) exercising `GetResourceAllocationInfo` (`0xc8`) and `GetResourceAllocationInfo2` (`0x360`) on 4 KiB BC7 textures, linear textures, mixed 4 KiB/64 KiB batches, empty/null inputs, and both addition and alignment-padding overflows (verified to fail on `ffe310e` before the fix and pass after). ### Verification - `cargo test -p victory-gpu`: 51 passed, 0 failed, 1 ignored (benchmark) - `cargo test -p victory-gpu --features diagnostics`: 54 passed, 0 failed, 1 ignored (benchmark) - `cargo clippy -p victory-gpu --all-targets -- -D warnings` (default and `--features diagnostics`): 0 warnings - `cargo check -p victory --features diagnostics`: clean - `cargo xtask policy`: `asset policy: 154 files ok` - `rustfmt --edition 2024 --check crates/victory-gpu/src/resource.rs`: no diffs in any modified lines (only the pre-existing untouched line on `origin/main`) ### Disclosure - Tested with synthetic unit tests and host Windows D3D12 queries only; no game run or game data was used. - Prepared with AI assistance (Google Antigravity).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:13:58 +00:00
Author
Owner

@⁠LAKSHYA-cmd-tech on GitHub · 2026-11-24 11:13 UTC · original

Cherry-picked and merged downstream in https://github.com/LAKSHYA-cmd-tech/VIctory/pull/13.

Verified on Windows 11 D3D12 runtime: all 88 \ ictory-gpu\ tests pass cleanly including
esource::tests::allocation_info_keeps_console_alignments_and_checks_bounds.

**@⁠LAKSHYA-cmd-tech** on GitHub · 2026-11-24 11:13 UTC · [original](https://github.com/Marvisak/VIctory/issues/782#issuecomment-7129822802) Cherry-picked and merged downstream in https://github.com/LAKSHYA-cmd-tech/VIctory/pull/13. Verified on Windows 11 D3D12 runtime: all 88 \ ictory-gpu\ tests pass cleanly including \ esource::tests::allocation_info_keeps_console_alignments_and_checks_bounds\.
Author
Owner

@⁠BastienRodz on GitHub · 2026-11-24 16:35 UTC · original

Two more sizing rules for crates/victory-gpu/src/resource.rs. I'm adding them here rather than opening a PR that would compete with ccdfb2e:

  1. Tail padding in place(). Per the GetResourceAllocationInfo Remarks, several descriptions are laid out the way a C++ struct is. Two tiny 64 KB-aligned resources and a tiny 4 MB-aligned one total 12 MB when the 4 MB resource is in the middle, and 8 MB otherwise. place() aligns each member's offset, but it never pads total.size to total.alignment.

    • Repro (Wine, main ffe310e, a 64 KiB buffer b and a 4 MiB-aligned 64×64 RGBA8 texture m): [b, m, b] gives 0x81_0000 where 12 MiB (0xC0_0000) is expected, and [m, b, b] gives 0x42_0000 where 8 MiB is expected.
    • Fix: one checked round-up of total.size to total.alignment at the end of place(), in the style of #782's overflow checks. sizes_cover_every_mip asserts the unpadded 0x65000 and would change.
  2. footprint()'s fallback path. Line 224 returns bytes.checked_next_multiple_of(KIB_64)? even when alignment is 4 MiB (MSAA, or an explicit 4 MiB alignment on a layout or format the console layout path doesn't handle). The size can then be smaller than the alignment. The layout path above it (line 189) already rounds to alignment.

    • Repro: layout: 2, alignment: 4 MiB, 64×64 RGBA8 gives { size: 65536, alignment: 4194304 } instead of a 4 MiB size. Both rules are needed for the doc's 8 MB / 12 MB totals to add up.

@⁠AndyLatimer, I can send these two lines and their tests as a commit for your branch, or leave them to you.

**@⁠BastienRodz** on GitHub · 2026-11-24 16:35 UTC · [original](https://github.com/Marvisak/VIctory/issues/782#issuecomment-7137904321) Two more sizing rules for `crates/victory-gpu/src/resource.rs`. I'm adding them here rather than opening a PR that would compete with `ccdfb2e`: 1. **Tail padding in `place()`.** Per the [`GetResourceAllocationInfo` Remarks](https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12device-getresourceallocationinfo(uint_uint_constd3d12_resource_desc)), several descriptions are laid out the way a C++ struct is. Two tiny 64 KB-aligned resources and a tiny 4 MB-aligned one total 12 MB when the 4 MB resource is in the middle, and 8 MB otherwise. `place()` aligns each member's offset, but it never pads `total.size` to `total.alignment`. - Repro (Wine, `main` `ffe310e`, a 64 KiB buffer `b` and a 4 MiB-aligned 64×64 RGBA8 texture `m`): `[b, m, b]` gives `0x81_0000` where 12 MiB (`0xC0_0000`) is expected, and `[m, b, b]` gives `0x42_0000` where 8 MiB is expected. - Fix: one checked round-up of `total.size` to `total.alignment` at the end of `place()`, in the style of #782's overflow checks. `sizes_cover_every_mip` asserts the unpadded `0x65000` and would change. 2. **`footprint()`'s fallback path.** Line 224 returns `bytes.checked_next_multiple_of(KIB_64)?` even when `alignment` is 4 MiB (MSAA, or an explicit 4 MiB alignment on a layout or format the console layout path doesn't handle). The size can then be smaller than the alignment. The layout path above it (line 189) already rounds to `alignment`. - Repro: `layout: 2`, `alignment: 4 MiB`, 64×64 RGBA8 gives `{ size: 65536, alignment: 4194304 }` instead of a 4 MiB size. Both rules are needed for the doc's 8 MB / 12 MB totals to add up. @⁠AndyLatimer, I can send these two lines and their tests as a commit for your branch, or leave them to you.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 03:52 UTC · original

Automated triage for the maintainers. Confirmed on main in place() (crates/victory-gpu/src/resource.rs), which both allocation_info and allocation_info2 use. The total alignment starts at 64 KiB, so 4 KiB and 256-byte console alignments come back as 64 KiB, and allocation_info2 then reports a smaller alignment per resource than in the total. next_multiple_of and the offset addition are unchecked. A null descs with a non-zero count is treated as an empty list. The tail-padding point in the comment above also holds: place() never rounds total.size up to total.alignment.

Open PR #1396 (preserve sub-64 KiB alignments and check bounds in allocation_info) addresses this; the tail-padding rule could be reviewed together with it there.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 03:52 UTC · [original](https://github.com/Marvisak/VIctory/issues/782#issuecomment-7152318931) <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-24T16:35:28Z at=2026-11-25T03:52:23Z --> Automated triage for the maintainers. Confirmed on `main` in `place()` (`crates/victory-gpu/src/resource.rs`), which both `allocation_info` and `allocation_info2` use. The total alignment starts at 64 KiB, so 4 KiB and 256-byte console alignments come back as 64 KiB, and `allocation_info2` then reports a smaller alignment per resource than in the total. `next_multiple_of` and the offset addition are unchecked. A null `descs` with a non-zero count is treated as an empty list. The tail-padding point in the comment above also holds: `place()` never rounds `total.size` up to `total.alignment`. Open PR #1396 (preserve sub-64 KiB alignments and check bounds in `allocation_info`) addresses this; the tail-padding rule could be reviewed together with it there. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
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#782
No description provided.