xmem: a refused XMemVirtualAlloc leaves the host's reason in the last error #2855

Open
github-bot wants to merge 1 commit from github-bot/gh-2855 into main AGit
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-25 22:27 UTC · original
0xkn0wh3r3:hle/xmem-virtual-alloc-keeps-last-error → main · +53 −3 · 1 files · 1 commits · open

What

When VirtualAlloc2 refuses an XMemVirtualAlloc, the refusal is logged before the function returns:

  • a refused commit goes through commit_refused, which appends a crash record;
  • anything else goes through tracing::warn!.

The crash record is written with CreateFileW(OPEN_ALWAYS). When crashes.log already exists, that call succeeds and sets the thread's last error to ERROR_ALREADY_EXISTS (183). A file-backed tracing writer can do the same. So the title's GetLastError() after a null return read the logger's code instead of the host's reason (ERROR_COMMITMENT_LIMIT, ERROR_INVALID_ADDRESS, …).

The fix takes the code right after the allocation (and after the low-guard retry), uses it for the log, and puts it back with SetLastError before returning.

Where it came from

From DexNom's fork, DexNom/VIctory@570a941 (crates/victory-hle/src/modules/xmem.rs). It saves and restores the error the same way and is credited with Co-authored-by: DexNom.

Tested

  • New test xmem::tests::a_refused_reservation_keeps_the_hosts_last_error. It installs a subscriber whose events set ERROR_ALREADY_EXISTS, the way a file-backed log does, then reserves over an existing reservation. The result must be null with ERROR_INVALID_ADDRESS. On current main the test fails (left: 183, right: 487); with the change it passes. (DexNom's own version of the test passes on main too, because unit tests have no logger.)
  • cargo test -p victory-hle: all pass (153 unit tests and the integration tests). Windows 11, RTX 5070 Ti.
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.

Not tested

  • Not run with the title. I haven't checked whether the title actually acts on the last error after a refused allocation.
**@⁠0xkn0wh3r3** on GitHub · 2026-11-25 22:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/2855) `0xkn0wh3r3:hle/xmem-virtual-alloc-keeps-last-error` → `main` · +53 −3 · 1 files · 1 commits · open ## What When `VirtualAlloc2` refuses an `XMemVirtualAlloc`, the refusal is logged before the function returns: - a refused commit goes through `commit_refused`, which appends a crash record; - anything else goes through `tracing::warn!`. The crash record is written with `CreateFileW(OPEN_ALWAYS)`. When `crashes.log` already exists, that call succeeds and sets the thread's last error to `ERROR_ALREADY_EXISTS` (183). A file-backed tracing writer can do the same. So the title's `GetLastError()` after a null return read the logger's code instead of the host's reason (`ERROR_COMMITMENT_LIMIT`, `ERROR_INVALID_ADDRESS`, …). The fix takes the code right after the allocation (and after the low-guard retry), uses it for the log, and puts it back with `SetLastError` before returning. ## Where it came from From DexNom's fork, [DexNom/VIctory@570a941](https://github.com/DexNom/VIctory/commit/570a941e9b4e945121c26eb30dfc10c126845207) (`crates/victory-hle/src/modules/xmem.rs`). It saves and restores the error the same way and is credited with `Co-authored-by: DexNom`. ## Tested - New test `xmem::tests::a_refused_reservation_keeps_the_hosts_last_error`. It installs a subscriber whose events set `ERROR_ALREADY_EXISTS`, the way a file-backed log does, then reserves over an existing reservation. The result must be null with `ERROR_INVALID_ADDRESS`. On current main the test fails (`left: 183, right: 487`); with the change it passes. (DexNom's own version of the test passes on main too, because unit tests have no logger.) - `cargo test -p victory-hle`: all pass (153 unit tests and the integration tests). Windows 11, RTX 5070 Ti. - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. ## Not tested - Not run with the title. I haven't checked whether the title actually acts on the last error after a refused allocation.
When VirtualAlloc2 refused an XMemVirtualAlloc, the refusal was logged
before returning: a refused commit through commit_refused, which appends
a crash record (CreateFileW with OPEN_ALWAYS on an existing crashes.log
sets ERROR_ALREADY_EXISTS), anything else through tracing, whose writer
can do the same. The title then read the logger's last error, not the
host's reason (ERROR_COMMITMENT_LIMIT, ERROR_INVALID_ADDRESS, ...). The
code is now taken right after the allocation and put back before
returning.

Test: with a subscriber whose events set ERROR_ALREADY_EXISTS, a
reservation over an existing one returns null with ERROR_INVALID_ADDRESS
(183 before the change).

From DexNom/VIctory@570a941e9b, which
saves and restores the error the same way.

Co-authored-by: DexNom <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:16:00 +00:00
Author
Owner

@⁠Kajanos on GitHub · 2026-11-26 00:07 UTC · original

Windows 11 / NVIDIA test run, head 0630d7c0ce merged onto main 63bcca43c6.

Setup: Windows 11 Home build 26300, RTX 5080, NVIDIA 621.40, rustc 1.98.1 MSVC, VS 2022 Build Tools, Windows SDK 10.0.26100 (dxil.dll from the SDK).

  • cargo test --locked -p victory-hle: 152 passed, 1 failed.
  • The one failure is lifecycle::tests::showing_a_title_window_shows_its_stand_in, and it also fails on main in this run. The tests ran over SSH, i.e. in session 0 with no interactive desktop, so the window can't be shown. On the desktop session the same test passes on main (12/12 lifecycle). This PR doesn't touch lifecycle.

Not run: the retail title, clippy, and other crates.

AI-assisted (Claude Code)

**@⁠Kajanos** on GitHub · 2026-11-26 00:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/2855#issuecomment-7178420424) **Windows 11 / NVIDIA test run**, head `0630d7c0ce` merged onto `main` `63bcca43c6`. Setup: Windows 11 Home build 26300, RTX 5080, NVIDIA 621.40, rustc 1.98.1 MSVC, VS 2022 Build Tools, Windows SDK 10.0.26100 (`dxil.dll` from the SDK). - `cargo test --locked -p victory-hle`: 152 passed, 1 failed. - The one failure is `lifecycle::tests::showing_a_title_window_shows_its_stand_in`, and it also fails on `main` in this run. The tests ran over SSH, i.e. in session 0 with no interactive desktop, so the window can't be shown. On the desktop session the same test passes on `main` (12/12 `lifecycle`). This PR doesn't touch lifecycle. Not run: the retail title, clippy, and other crates. AI-assisted (Claude Code)
Author
Owner

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

Automated triage for the maintainers.

Verdict: merge candidate. In XMemVirtualAlloc (crates/victory-hle/src/modules/xmem.rs), a refusal now reads GetLastError right after the allocation (and after low_guards::retry, which already preserves the allocation's error). That code goes into the log, and SetLastError puts it back before returning. On main, commit_refused's crash-record append or a file-backed tracing writer can overwrite the last error (for example with ERROR_ALREADY_EXISTS) before the title reads it. A null return with the host's reason in the last error is plain Win32 behaviour, so no further evidence is needed.

The new test a_refused_reservation_keeps_the_hosts_last_error uses a subscriber that clobbers the last error, so it fails on main and checks the real path. A second contributor's run on an RTX 5080 passed. The change is small and touches nothing else.

CI: passing (check). Merges cleanly.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 03:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/2855#issuecomment-7181703118) <!-- victory-triage v1 head=0630d7c0cee1de2d686e5347a81fd9a98bb8341a verdict=merge-candidate at=2026-11-26T03:25:14Z --> Automated triage for the maintainers. **Verdict: merge candidate.** In `XMemVirtualAlloc` (`crates/victory-hle/src/modules/xmem.rs`), a refusal now reads `GetLastError` right after the allocation (and after `low_guards::retry`, which already preserves the allocation's error). That code goes into the log, and `SetLastError` puts it back before returning. On `main`, `commit_refused`'s crash-record append or a file-backed tracing writer can overwrite the last error (for example with `ERROR_ALREADY_EXISTS`) before the title reads it. A null return with the host's reason in the last error is plain Win32 behaviour, so no further evidence is needed. The new test `a_refused_reservation_keeps_the_hosts_last_error` uses a subscriber that clobbers the last error, so it fails on `main` and checks the real path. A second contributor's run on an RTX 5080 passed. The change is small and touches nothing else. CI: passing (`check`). Merges cleanly. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 22:50 UTC · original · reviewed

Classic last-error clobber, correctly fixed: capture GetLastError before any logging (a tracing append can set ERROR_ALREADY_EXISTS itself), restore via SetLastError before returning so the title sees VirtualAlloc2's real reason. The clobbering-subscriber test reproduces the actual hazard. LGTM.

**@⁠kotekompotek** on GitHub · 2026-11-25 22:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/2855#pullrequestreview-6066064210) · **reviewed** Classic last-error clobber, correctly fixed: capture GetLastError before any logging (a tracing append can set ERROR_ALREADY_EXISTS itself), restore via SetLastError before returning so the title sees VirtualAlloc2's real reason. The clobbering-subscriber test reproduces the actual hazard. LGTM.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 00:17 UTC · original · reviewed

Verified head 0630d7c0 cleanly merged onto origin/main (63bcca43) on Windows 11 x64.

Code Review

  • Capturing GetLastError() immediately when at.is_null() (covering both allocate and low_guards::retry, which preserves its own VirtualAlloc2 error) and restoring it with SetLastError(code) right before returning cleanly prevents commit_refused and tracing logging from clobbering the host error code.
  • The new a_refused_reservation_keeps_the_hosts_last_error test directly verifies preservation against a ClobbersLastError tracing subscriber.

Local Verification

  • cargo test --locked -p victory-hle: 153/153 unit tests (+ child-process test 1/1), fidelityfx (1/1), no_dll_planting (1/1) passed.
  • cargo clippy --locked -p victory-hle --tests -- -D warnings (default and --all-features) & cargo xtask policy (191 files ok): clean.

Looks clean and ready to merge.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 00:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/2855#pullrequestreview-6066835061) · **reviewed** Verified head `0630d7c0` cleanly merged onto `origin/main` (`63bcca43`) on Windows 11 x64. ### Code Review - Capturing `GetLastError()` immediately when `at.is_null()` (covering both `allocate` and `low_guards::retry`, which preserves its own `VirtualAlloc2` error) and restoring it with `SetLastError(code)` right before returning cleanly prevents `commit_refused` and `tracing` logging from clobbering the host error code. - The new `a_refused_reservation_keeps_the_hosts_last_error` test directly verifies preservation against a `ClobbersLastError` tracing subscriber. ### Local Verification - `cargo test --locked -p victory-hle`: **153/153** unit tests (+ child-process test `1/1`), `fidelityfx` (1/1), `no_dll_planting` (1/1) passed. - `cargo clippy --locked -p victory-hle --tests -- -D warnings` (default and `--all-features`) & `cargo xtask policy` (`191 files ok`): clean. Looks clean and ready to merge.
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 10:30 UTC · original · approved these changes

Verified against origin/main (63bcca4):

  • cargo test -p victory-hle --lib modules::xmem:: -- --test-threads=1: 15/15 passed (including a_refused_reservation_keeps_the_hosts_last_error).
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly preserves the host's last error when XMemVirtualAlloc fails.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 10:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/2855#pullrequestreview-6072468753) · **approved these changes** Verified against `origin/main` (`63bcca4`): - `cargo test -p victory-hle --lib modules::xmem:: -- --test-threads=1`: 15/15 passed (including `a_refused_reservation_keeps_the_hosts_last_error`). - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly preserves the host's last error when `XMemVirtualAlloc` fails. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2855/head:github-bot/gh-2855'
git switch 'github-bot/gh-2855'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2855'
git switch 'github-bot/gh-2855'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2855'
git switch 'github-bot/gh-2855'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2855'
git switch 'main'
git merge --squash 'github-bot/gh-2855'
git switch 'main'
git merge --ff-only 'github-bot/gh-2855'
git switch 'main'
git merge 'github-bot/gh-2855'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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!2855
No description provided.