GPU: allocate_upload accepts invalid sub-16 alignments and aborts on oversized/OOM requests (fix on a fork) #837

Open
opened 2026-11-26 04:14:02 +00:00 by github-bot · 4 comments
Owner

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

Pull requests are currently restricted to collaborators (#717), so I am sharing this bug report and verified fix from a fork branch off main (ffe310e).

Bug

In crates/victory-gpu/src/list.rs (extern "system" fn allocate_upload):

let align = usize::try_from(alignment).unwrap_or(0).max(16);
let Some(len) = usize::try_from(size).ok().and_then(|s| s.checked_add(align)) else {
    return std::ptr::null_mut();
};
if this.is_null() || !align.is_power_of_two() {
    return std::ptr::null_mut();
}
let mut block = vec![0u8; len];

Two bugs violate the "null on failure" contract across this FFI entry point:

  1. Invalid sub-16 alignments silently accepted: .max(16) runs before !align.is_power_of_two(), so non-power-of-two alignments 3, 5, 6, 7, 9..15 (and values failing usize::try_from) are silently rounded up to 16 and succeed instead of returning null.
  2. Panic / abort across extern "system" on oversized or unallocatable requests: size + align > isize::MAX (e.g., alignment = 1 << 63 with size = 16, or size = isize::MAX with alignment = 16) passes checked_add and is_power_of_two, then panics with capacity overflow inside vec![0u8; len] (aborting the process because extern "system" cannot unwind). Similarly, unallocatable sizes below isize::MAX (OOM) trigger handle_alloc_error and abort instead of returning null.

Fix

  • Check this.is_null() and validate alignment (allowing 0 as default 16-byte alignment and rejecting non-powers-of-two before .max(16)).
  • Allocate fallibly with Vec::try_reserve_exact(len) + block.resize(len, 0), returning null_mut() cleanly on both CapacityOverflow (len > isize::MAX) and AllocError (OOM).
  • Guard offset >= align before start.add(offset).
  • Added synthetic regression test list::tests::allocate_upload_rejects_bad_alignments_and_oversized_requests covering null list, sub-16 non-power-of-two alignments (3..15), oversized/wrapping/unallocatable sizes and alignments (1 << 63, isize::MAX, u64::MAX, 1 << 50), and valid alignments (0, 1, 2, 4, 8, 16, 64) + reset cleanup.

Verification

  • Before fix: allocate_upload_rejects_bad_alignments_and_oversized_requests failed immediately on alignment 3 (non-power-of-two alignment 3 must fail), and oversized power-of-two alignments/sizes aborted across FFI.
  • After fix (Windows 11, native D3D12):
    • cargo test -p victory-gpu: 51 passed, 0 failed, 1 ignored.
    • cargo test -p victory-gpu --features diagnostics: 54 passed, 0 failed, 1 ignored.
    • cargo clippy -p victory-gpu --all-targets -- -D warnings (default and --features diagnostics): clean.
    • cargo check -p victory --features diagnostics: clean.
    • rustfmt --edition 2024 --check on all modified lines of list.rs: clean.
    • cargo xtask policy: 154 files ok.

Disclosure

Synthetic tests only; no game data or console SDK assets were used. Assisted by Google Antigravity.

**@⁠AndyLatimer** on GitHub · 2026-11-24 11:56 UTC · [original](https://github.com/Marvisak/VIctory/issues/837) Pull requests are currently restricted to collaborators (#717), so I am sharing this bug report and verified fix from a fork branch off `main` (`ffe310e`). - **Branch:** https://github.com/AndyLatimer/VIctory/tree/fix/gpu-allocate-upload-validation - **Commit:** https://github.com/AndyLatimer/VIctory/commit/57c61f2d8218fbf67d0beb22845edb4787168cd8 ### Bug In `crates/victory-gpu/src/list.rs` (`extern "system" fn allocate_upload`): ```rust let align = usize::try_from(alignment).unwrap_or(0).max(16); let Some(len) = usize::try_from(size).ok().and_then(|s| s.checked_add(align)) else { return std::ptr::null_mut(); }; if this.is_null() || !align.is_power_of_two() { return std::ptr::null_mut(); } let mut block = vec![0u8; len]; ``` Two bugs violate the "null on failure" contract across this FFI entry point: 1. **Invalid sub-16 alignments silently accepted:** `.max(16)` runs *before* `!align.is_power_of_two()`, so non-power-of-two alignments `3, 5, 6, 7, 9..15` (and values failing `usize::try_from`) are silently rounded up to `16` and succeed instead of returning null. 2. **Panic / abort across `extern "system"` on oversized or unallocatable requests:** `size + align > isize::MAX` (e.g., `alignment = 1 << 63` with `size = 16`, or `size = isize::MAX` with `alignment = 16`) passes `checked_add` and `is_power_of_two`, then panics with `capacity overflow` inside `vec![0u8; len]` (aborting the process because `extern "system"` cannot unwind). Similarly, unallocatable sizes below `isize::MAX` (OOM) trigger `handle_alloc_error` and abort instead of returning null. ### Fix - Check `this.is_null()` and validate `alignment` (allowing `0` as default 16-byte alignment and rejecting non-powers-of-two before `.max(16)`). - Allocate fallibly with `Vec::try_reserve_exact(len)` + `block.resize(len, 0)`, returning `null_mut()` cleanly on both `CapacityOverflow` (`len > isize::MAX`) and `AllocError` (OOM). - Guard `offset >= align` before `start.add(offset)`. - Added synthetic regression test `list::tests::allocate_upload_rejects_bad_alignments_and_oversized_requests` covering null list, sub-16 non-power-of-two alignments (`3..15`), oversized/wrapping/unallocatable sizes and alignments (`1 << 63`, `isize::MAX`, `u64::MAX`, `1 << 50`), and valid alignments (`0, 1, 2, 4, 8, 16, 64`) + `reset` cleanup. ### Verification - **Before fix:** `allocate_upload_rejects_bad_alignments_and_oversized_requests` failed immediately on alignment `3` (`non-power-of-two alignment 3 must fail`), and oversized power-of-two alignments/sizes aborted across FFI. - **After fix (Windows 11, native D3D12):** - `cargo test -p victory-gpu`: 51 passed, 0 failed, 1 ignored. - `cargo test -p victory-gpu --features diagnostics`: 54 passed, 0 failed, 1 ignored. - `cargo clippy -p victory-gpu --all-targets -- -D warnings` (default and `--features diagnostics`): clean. - `cargo check -p victory --features diagnostics`: clean. - `rustfmt --edition 2024 --check` on all modified lines of `list.rs`: clean. - `cargo xtask policy`: 154 files ok. ### Disclosure Synthetic tests only; no game data or console SDK assets were used. Assisted by Google Antigravity.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:02 +00:00
Author
Owner

@⁠nopsled0x on GitHub · 2026-11-24 12:27 UTC · original

Independently verified 57c61f2 (fix/gpu-allocate-upload-validation) on native Windows, cargo 1.98.1. It fast-forwarded from main when I checked.

  • cargo test -p victory-gpu: 51 passed, 0 failed, 1 ignored, including list::tests::allocate_upload_rejects_bad_alignments_and_oversized_requests.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.

I read the diff and the logic matches the description: the power-of-two check runs before .max(16), try_reserve_exact replaces vec!, and the new offset < align guard precedes start.add.

Possible concern, not observed: the 1 << 50 case relies on the allocator refusing 1 PiB, which might succeed under overcommit (e.g. Linux) and make that assertion flaky.

Not tested: the new test failing on main without the fix, --features diagnostics, cargo xtask policy, rustfmt, or any title run. Synthetic tests only.

(AI-assisted: the checks were run by me; the diff review and this comment were prepared with Claude's help.)

**@⁠nopsled0x** on GitHub · 2026-11-24 12:27 UTC · [original](https://github.com/Marvisak/VIctory/issues/837#issuecomment-7131465347) Independently verified `57c61f2` (`fix/gpu-allocate-upload-validation`) on native Windows, cargo 1.98.1. It fast-forwarded from `main` when I checked. - `cargo test -p victory-gpu`: 51 passed, 0 failed, 1 ignored, including `list::tests::allocate_upload_rejects_bad_alignments_and_oversized_requests`. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. I read the diff and the logic matches the description: the power-of-two check runs before `.max(16)`, `try_reserve_exact` replaces `vec!`, and the new `offset < align` guard precedes `start.add`. Possible concern, not observed: the `1 << 50` case relies on the allocator refusing 1 PiB, which might succeed under overcommit (e.g. Linux) and make that assertion flaky. Not tested: the new test failing on `main` without the fix, `--features diagnostics`, `cargo xtask policy`, `rustfmt`, or any title run. Synthetic tests only. (AI-assisted: the checks were run by me; the diff review and this comment were prepared with Claude's help.)
Author
Owner

@⁠Louisreed on GitHub · 2026-11-24 13:25 UTC · original

This fills the gaps in the earlier verification of 57c61f2 (Windows 10 22H2 19045, Rust 1.98.1 MSVC; base ffe310e is still main):

  • Fails without the fix: with the branch's test module on main's code, allocate_upload_rejects_bad_alignments_and_oversized_requests fails, panicking inside the new test (list.rs:1466 in the combined file).
  • cargo test -p victory-gpu --locked: 51 passed, 0 failed, 1 ignored. Clippy -D warnings is clean.
  • cargo xtask policy: 154 files ok.
  • rustfmt --edition 2024 --check list.rs reports hunks (e.g. ClearDepthStencilView near line 754), but none fall inside this diff, and main's list.rs has the same 6 hunks.

On the 1 << 50 overcommit concern: Windows can't commit 1 PiB (x64 user address space is 128 TiB), so that assertion is deterministic here. It could still matter for a non-Windows host build.

AI-assisted (Claude). I ran the checks locally. Synthetic inputs only.

**@⁠Louisreed** on GitHub · 2026-11-24 13:25 UTC · [original](https://github.com/Marvisak/VIctory/issues/837#issuecomment-7132819761) This fills the gaps in the earlier verification of `57c61f2` (Windows 10 22H2 19045, Rust 1.98.1 MSVC; base `ffe310e` is still `main`): - **Fails without the fix:** with the branch's test module on `main`'s code, `allocate_upload_rejects_bad_alignments_and_oversized_requests` fails, panicking inside the new test (`list.rs:1466` in the combined file). - `cargo test -p victory-gpu --locked`: 51 passed, 0 failed, 1 ignored. Clippy `-D warnings` is clean. - `cargo xtask policy`: 154 files ok. - `rustfmt --edition 2024 --check list.rs` reports hunks (e.g. `ClearDepthStencilView` near line 754), but none fall inside this diff, and `main`'s `list.rs` has the same 6 hunks. On the `1 << 50` overcommit concern: Windows can't commit 1 PiB (x64 user address space is 128 TiB), so that assertion is deterministic here. It could still matter for a non-Windows host build. _AI-assisted (Claude). I ran the checks locally. Synthetic inputs only._
Author
Owner

@⁠wanchudao on GitHub · 2026-11-24 15:47 UTC · original

Tested AndyLatimer/VIctory's fix/gpu-allocate-upload-validation branch at 57c61f2 on
Windows 11 Pro build 26300, Ryzen 5 5500, rustc 1.98.1, MSVC 14.44.35207,
and Windows SDK 10.0.26100.0.

cargo test -p victory-gpu --lib allocate_upload -- --nocapture

Result: 1 passed, 0 failed, 0 ignored, 51 filtered out. This is an additional
Windows result for the CPU-side alignment and allocation-size assertions. The test
setup can create a D3D12 command list, and the log does not identify the adapter.

AI use — DeepSeek Harness (DeepSeek V4.1 Flash) ran the test and drafted this report.
OpenAI Codex (GPT-6.2 Sol) checked the archived result and revised it.

**@⁠wanchudao** on GitHub · 2026-11-24 15:47 UTC · [original](https://github.com/Marvisak/VIctory/issues/837#issuecomment-7136606464) Tested AndyLatimer/VIctory's fix/gpu-allocate-upload-validation branch at 57c61f2 on Windows 11 Pro build 26300, Ryzen 5 5500, rustc 1.98.1, MSVC 14.44.35207, and Windows SDK 10.0.26100.0. `cargo test -p victory-gpu --lib allocate_upload -- --nocapture` Result: 1 passed, 0 failed, 0 ignored, 51 filtered out. This is an additional Windows result for the CPU-side alignment and allocation-size assertions. The test setup can create a D3D12 command list, and the log does not identify the adapter. AI use — DeepSeek Harness (DeepSeek V4.1 Flash) ran the test and drafted this report. OpenAI Codex (GPT-6.2 Sol) checked the archived result and revised it.
Author
Owner

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

Automated triage for the maintainers.

Partly confirmed on current main, crates/victory-gpu/src/list.rs, allocate_upload:

  • Still present: .max(16) runs before the is_power_of_two() check, so a non-power-of-two alignment below 16 (3, 5, 6, 7, 9 to 15) is rounded up to 16 and succeeds instead of returning null.
  • Already fixed: the abort on oversized or unallocatable requests. Since 0f63757 the block comes from Upload::new, which uses VirtualAlloc2 on Windows and returns None, and therefore null, when the allocation fails. There is no vec! on that path any more.

Your branch is based on ffe310e and no longer applies to this code. Pull requests are open to everyone again (see #717), so please open a PR against current main with the alignment check and the test cases that still apply.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 03:54 UTC · [original](https://github.com/Marvisak/VIctory/issues/837#issuecomment-7152356067) <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-24T15:47:06Z at=2026-11-25T03:54:49Z --> Automated triage for the maintainers. Partly confirmed on current `main`, `crates/victory-gpu/src/list.rs`, `allocate_upload`: - **Still present:** `.max(16)` runs before the `is_power_of_two()` check, so a non-power-of-two alignment below 16 (3, 5, 6, 7, 9 to 15) is rounded up to 16 and succeeds instead of returning null. - **Already fixed:** the abort on oversized or unallocatable requests. Since `0f63757` the block comes from `Upload::new`, which uses `VirtualAlloc2` on Windows and returns `None`, and therefore null, when the allocation fails. There is no `vec!` on that path any more. Your branch is based on `ffe310e` and no longer applies to this code. Pull requests are open to everyone again (see #717), so please open a PR against current `main` with the alignment check and the test cases that still apply. --- _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#837
No description provided.