Skip to content

Trace failed XMem virtual allocations while preserving last error - #6733

Open
aidity wants to merge 2 commits into
ItsNotRexed:mainfrom
aidity:fix/xmem-allocation-trace
Open

aidity wants to merge 2 commits into
ItsNotRexed:mainfrom
aidity:fix/xmem-allocation-trace

Conversation

@aidity

@aidity aidity commented Nov 23, 2026 •

Copy link
Copy Markdown

Failed XMemVirtualAlloc calls currently return null without a structured startup-trace explanation. A title may then trigger its own breakpoint, obscuring which pool size, access attributes or protection were rejected.

Add an xmem_virtual_alloc_failed trace event with size, allocation_type, attributes, protect and the Win32 error. The wrapper records the existing allocator's result and restores GetLastError after tracing. Allocation policy is unchanged, and the original guest caller remains attached to the existing attribute event.

Claimed in #6603 at #6603 (comment). Updated against main 867d922 after the physical-page allocator in #6919 merged. Only runtime/shims/xmem.cpp and runtime/test/xmem_test.cpp differ from that main.

Validation: Windows x64, MSVC 14.44, Windows SDK 10.0.26100. A main worktree with only this diagnostic change built xmem_test and passed all 179 checks. The new synthetic test covers trace disabled, invalid size, invalid allocation type, request metadata, and preservation of the Win32 error while tracing. Existing allocation and physical-page tests still pass. The no-game-data scanner passes both changed files. No GPU or game files are required.

AI-assisted with Codex. No game data or game-derived reports are included.

@thehollowedone

Copy link
Copy Markdown

Independent native Windows x64 check of ffac546d, MSVC 19.38 and Windows SDK 10.0.26100. All 14 focused checks passed.

A zero-size request preserved Win32 error 87, and the request with unsupported attributes preserved error 50. With tracing enabled, the JSONL contained exactly two allocation failure records with the expected request fields and errors. Valid reservations stayed uncommitted and released successfully. Successful calls and calls made with tracing disabled produced zero failure records. All inputs were synthetic.

AI-assisted.

@SirKingg SirKingg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking: runtime/shims/xmem.cpp:218-224 formats the trace with allocating std::string operations after the allocation has already failed. If the CRT heap cannot allocate, std::bad_alloc escapes XMemVirtualAlloc before SetLastError, instead of returning null with the original error. This is especially relevant to a diagnostic for allocation failures. Please make trace formatting/emission best-effort without letting exceptions escape and restore the saved error even when logging fails; add a focused injected-failure regression test.

Independent Windows 11 x64 build 26300, MSVC 19.44.35230, SDK 10.0.26100: CMake/Ninja built xmem_test; ctest -R ^xmem$ --output-on-failure passed (1/1; 108 checks). A separate synthetic MSVC harness tested 800 concurrent failures across eight threads with tracing on: all returned error 87 and produced 800 valid JSON failure events; successful direct and wrapped allocations preserved the same last error. With global operator new forced to throw after opening the trace, a zero-size call with tracing off returned null/error 87, while tracing on let std::bad_alloc escape (reproduced). No game files, guest execution, GPU, Xbox hardware or cross-PR integration were tested. #6709 and #4531 change the same allocation path and need their behavior and tracing retained during integration; this review does not validate those combinations.

AI-assisted review.

@devnull34 devnull34 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed and tested at head fac546d5bb416d02951ffa24d844d948c54bfbd on Windows 11 with MSVC 19.44:

  • Behavior verified:
    • XMemVirtualAlloc wraps the internal virtual alloc call and, upon null return with startup trace enabled, logs xmem_virtual_alloc_failed recording size, allocation_type, attributes, protect, and error code.
    • Crucially, SetLastError(error) is called after logging to preserve the caller's last error value across trace emission.
  • Test execution:
    • xmem_test.exe: Built cleanly and passed 108/108 checks (OK).
    • Verified failed reservation preserves last error with trace off and on, trace retains invalid-size (ERROR_INVALID_PARAMETER) and unsupported-attributes (ERROR_NOT_SUPPORTED) errors, and trace correctly captures request payload and host error 50.
  • Diff & formatting: git diff --check 1cc6e3c..HEAD clean.

No game data used.

AI-assisted verification.

@Stalkerino

Copy link
Copy Markdown

Wine 11.22 / CachyOS x86-64, clang-cl/xwin. I integrated your tracing change at ffac546d5 with the local 2 MiB alignment and reserve-only fixes. Both xmem and its VirtualAlloc2-fallback variant passed all 172 checks in that integration.

The failure events distinguished two successive calls with the same allocation type (0x20003000) but different access/protection pairs (0x183/0x22004 and 0x283/0x2404). Each reported Win32 error 50. In a separate CPU-only diagnostic run that let those earlier allocations proceed, the trace then isolated the committed 64 KiB CPU request addressed by #6814. Keeping the numeric fields separate made it possible to test that case independently.

@IAmNotSiob

Copy link
Copy Markdown
Contributor

heads up: #6919 (xmem physical pages, the #6824 boot stop) also covers this XMemVirtualAlloc change, and all of these touch xmem.cpp. might be worth checking whether anything here is still missing on top of it, otherwise one of them will need a rebase :) AI-assisted (Claude Code).

@shayderrr shayderrr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

runtime/shims/xmem.cpp splits XMemVirtualAlloc into an inner xmem_virtual_alloc() and a tracing wrapper that emits an xmem_virtual_alloc_failed event (size, allocation type, attributes, protection, host error) when the call fails and tracing is on, restoring GetLastError() afterward. xmem_test.cpp verifies error preservation and event contents.

Why merge

Failed guest allocations during startup were invisible in the trace, making boot diagnostics guesswork. Capturing the request and host error while preserving the last error keeps the shim's contract intact and gives real failure data.

Verdict: Approve

@shayderrr

Copy link
Copy Markdown
Collaborator

Summary

runtime/shims/xmem.cpp splits XMemVirtualAlloc into an inner xmem_virtual_alloc() and a tracing wrapper that emits an xmem_virtual_alloc_failed event (size, allocation type, attributes, protection, host error) when the call fails and tracing is on, restoring GetLastError() afterward. xmem_test.cpp verifies error preservation and event contents.

Why merge

Failed guest allocations during startup were invisible in the trace, making boot diagnostics guesswork. Capturing the request and host error while preserving the last error keeps the shim's contract intact and gives real failure data.

Verdict: Approve

@SirKingg

Copy link
Copy Markdown
Contributor

Independent integration observation: I adapted the failure-event wrapper from ffac546d5 to the newer XMem implementation in a temporary checkout with #8379 (a9085b661) and #6582 (4224c53b1). This is not a test of the PR applied verbatim: the original diff conflicts with the newer XMem code. The adaptation preserves the existing attribute event's caller and restores GetLastError; the focused XMem regression and four loader/runtime tests passed.

In two Windows Sandbox runs with networking disabled, this instrumentation recorded zero xmem_virtual_alloc_failed events and five XMem attribute events before the same guest breakpoint as the uninstrumented comparison. This rules out a reported XMemVirtualAlloc failure in those runs, not other allocation or startup failures. Traces remain local. AI-assisted.

@SirKingg

Copy link
Copy Markdown
Contributor

Follow-up control on the same adapted diagnostic: xmem_test also passed 176 checks inside the network-disabled Windows Sandbox, including the synthetic failed-allocation event and GetLastError check. Two further retail traces recorded no XMem virtual-allocation failures before the same breakpoint as the preceding comparison. The first run terminated with 0x80000003; the second had recorded the breakpoint but had not terminated when the Sandbox was disposed, so no exit outcome is claimed for it. Traces remain local. AI-assisted.

@milas6669

Copy link
Copy Markdown

Checked head 416a98227 merged onto main 867d922f0, clean (2 files, +51/-2). Native Windows 11 26200, MSVC 19.44 RelWithDebInfo, RTX 3070 (Ampere). SirKingg's notes above are on an adapted diff, so this is a check of the head as it is.

  • ctest -R "xmem|startup_trace|run_tiny": 18/18 pass. xmem_test prints OK (179 checks), including invalid allocation preserves last error with tracing disabled, XMemTransferMemory leaves the thread's last error alone and the new failure-event checks.
  • Negative control, only runtime/shims/xmem.cpp put back to main's with the PR's test kept: xmem_test exits 1 with FAIL both allocation failures produce diagnostic events and FAIL failure event preserves the requested size, type, attributes, protection and Win32 error, so the new checks do depend on the change.

No defect. No game data. AI-assisted (Claude Code).

@DaviDreamerz

Copy link
Copy Markdown

Tested 416a98227 on Windows 11 (build 26300), MSVC 14.44 RelWithDebInfo: pass.

  • ctest -R "^xmem$": 179 checks, 0 failures
  • Negative control: with only runtime/shims/xmem.cpp from 867d922f0 in the same tree, the same test gives 179 checks, 2 failures, and both are the new checks:
    • both allocation failures produce diagnostic events
    • failure event preserves the requested size, type, attributes, protection and Win32 error
  • No GPU and no game files

AI-assisted: yes.

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.

9 participants