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

Open
0xkn0wh3r3 wants to merge 1 commit from upstream-pr/2855 into main
0xkn0wh3r3 commented 2026-11-25 22:27:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T22:27:34Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6615037422 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2855). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-25T22:27:34Z. --- ## 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.
kotekompotek commented 2026-11-25 23:05:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T22:50:27Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066064210 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2855#pullrequestreview-6066064210). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T22:50:27Z. **Review:** COMMENTED --- 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.
fuzeofficlal commented 2026-11-26 01:03:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T00:17:38Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066835061 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2855#pullrequestreview-6066835061). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T00:17:38Z. **Review:** COMMENTED --- 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.
Kajanos commented 2026-11-26 01:03:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Kajanos on 2026-11-26T00:07:29Z.


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)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7178420424 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2855#issuecomment-7178420424). Originally posted by [Kajanos](https://github.com/Kajanos) on 2026-11-26T00:07:29Z. --- **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)
Marvisak commented 2026-11-26 04:06:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T03:25:15Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7181703118 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2855#issuecomment-7181703118). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T03:25:15Z. --- <!-- 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)_
Dukestar69 commented 2026-11-26 11:03:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T10:30:37Z.

Review: APPROVED


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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6072468753 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2855#pullrequestreview-6072468753). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T10:30:37Z. **Review:** APPROVED --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2855:upstream-pr/2855'
git switch 'upstream-pr/2855'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2855'
git switch 'upstream-pr/2855'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2855'
git switch 'upstream-pr/2855'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2855'
git switch 'main'
git merge --squash 'upstream-pr/2855'
git switch 'main'
git merge --ff-only 'upstream-pr/2855'
git switch 'main'
git merge 'upstream-pr/2855'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!2853
No description provided.