Skip to content

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

Open
aidity wants to merge 1 commit into
ItsNotRexed:mainfrom
aidity:fix/xmem-allocation-trace
Open

aidity wants to merge 1 commit 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 result of the existing allocator and restores GetLastError after tracing. Allocation policy is unchanged.

Claimed in #6603 at #6603 (comment). This diagnostic is independent of the reservation implementation in #6709 and the large-page implementation in #4531.

Validation used Windows x64, MSVC 14.44 and Windows SDK 10.0.26100. A clean main worktree with only this commit built xmem_test and passed all 108 checks. Synthetic tests cover tracing disabled, invalid-size failure, unsupported-attribute failure, request metadata, and preservation of the caller's last error. 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

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.

7 participants