KernelX: memory tracing keeps the caller's last error #226

Closed
github-bot wants to merge 1 commit from gh-pr/226/head into gh-pr/226/base
Owner

@⁠Kalmarv on GitHub · 2026-11-23 21:22 UTC · original
Kalmarv:fix/kernelx-test-flake → main · +34 −7 · 1 files · 1 commits · closed without merging

hle::kernelx::tests::xmem_and_physical_page_overflows_fail_cleanly fails on main now and then (last error 203 where 8 is expected), now that CI runs the tests. The cause is in the code, not the test.

Cause

trace_mem reads XCL_TRACE_MEM into a OnceLock on its first call. With the variable unset, GetEnvironmentVariableW sets the thread's last error to 203 (ERROR_ENVVAR_NOT_FOUND). XMemAllocDefault and the VirtualAlloc hooks trace after the call has set its error. So the first traced call in a process replaced its own error with 203: XMemAllocDefault(usize::MAX, 0) reported 203 instead of ERROR_NOT_ENOUGH_MEMORY. A title would see the same.

The test fails whenever it is the process's first caller of trace_mem. That happens every time when it runs alone, and in some runs of the whole module, depending on which test thread gets there first.

Change

trace_mem runs its body under keep_last_error, a 6-line helper that saves the thread's last error and puts it back. That covers the first-call lookup and the logging.

Tested

Windows 11, MSVC, debug, CPU only; no GPU device is created by these tests.

  • The test alone, cargo test -p xcl --lib -- --exact hle::kernelx::tests::xmem_and_physical_page_overflows_fail_cleanly, 10 runs each: main af20b4b fails 10 of 10 (left: 203, right: 8); this branch (594f63a) passes 10 of 10.
  • The module, hle::kernelx with the library test binary built from each commit, 30 runs each: main af20b4b fails 9 of 30, every time in this test with 203; this branch fails 0 of 30 (16 tests per run, including the new one).
  • New test tracing_keeps_the_last_error. It sets ERROR_NOT_ENOUGH_MEMORY, then checks that the error survives an unset-variable lookup under keep_last_error, and a trace_mem call. It doesn't depend on test order. With the restore removed it fails with 203.

Not tested

  • The whole workspace suite on this branch.
  • Any title.
  • #214 adds a second XCL_TRACE_MEM lookup (trace_mem_on, reached from reserved_outside after a reservation), which can set 203 the same way. It merges with this branch without conflicts; wrapping that lookup in keep_last_error too would cover it.

AI use: Claude Code, Claude Opus 5.5.

**@⁠Kalmarv** on GitHub · 2026-11-23 21:22 UTC · [original](https://github.com/xcl-dev/xcl/pull/226) `Kalmarv:fix/kernelx-test-flake` → `main` · +34 −7 · 1 files · 1 commits · closed without merging `hle::kernelx::tests::xmem_and_physical_page_overflows_fail_cleanly` fails on main now and then (last error 203 where 8 is expected), now that CI runs the tests. The cause is in the code, not the test. ## Cause `trace_mem` reads `XCL_TRACE_MEM` into a `OnceLock` on its first call. With the variable unset, `GetEnvironmentVariableW` sets the thread's last error to 203 (`ERROR_ENVVAR_NOT_FOUND`). `XMemAllocDefault` and the `VirtualAlloc` hooks trace *after* the call has set its error. So the first traced call in a process replaced its own error with 203: `XMemAllocDefault(usize::MAX, 0)` reported 203 instead of `ERROR_NOT_ENOUGH_MEMORY`. A title would see the same. The test fails whenever it is the process's first caller of `trace_mem`. That happens every time when it runs alone, and in some runs of the whole module, depending on which test thread gets there first. ## Change `trace_mem` runs its body under `keep_last_error`, a 6-line helper that saves the thread's last error and puts it back. That covers the first-call lookup and the logging. ## Tested Windows 11, MSVC, debug, CPU only; no GPU device is created by these tests. - The test alone, `cargo test -p xcl --lib -- --exact hle::kernelx::tests::xmem_and_physical_page_overflows_fail_cleanly`, 10 runs each: main af20b4b fails 10 of 10 (`left: 203, right: 8`); this branch (594f63a) passes 10 of 10. - The module, `hle::kernelx` with the library test binary built from each commit, 30 runs each: main af20b4b fails 9 of 30, every time in this test with 203; this branch fails 0 of 30 (16 tests per run, including the new one). - New test `tracing_keeps_the_last_error`. It sets `ERROR_NOT_ENOUGH_MEMORY`, then checks that the error survives an unset-variable lookup under `keep_last_error`, and a `trace_mem` call. It doesn't depend on test order. With the restore removed it fails with 203. ## Not tested - The whole workspace suite on this branch. - Any title. - #214 adds a second `XCL_TRACE_MEM` lookup (`trace_mem_on`, reached from `reserved_outside` after a reservation), which can set 203 the same way. It merges with this branch without conflicts; wrapping that lookup in `keep_last_error` too would cover it. AI use: Claude Code, Claude Opus 5.5.
trace_mem reads XCL_TRACE_MEM on its first call. When the variable is unset
that read sets the thread's last error to 203 (ERROR_ENVVAR_NOT_FOUND), which
replaced the error the traced call had just set. XMemAllocDefault's
ERROR_NOT_ENOUGH_MEMORY, for one, reached the title as 203 when it was the
process's first trace. xmem_and_physical_page_overflows_fail_cleanly failed
for that reason whenever it ran before every other test that traces: always
alone, and in some runs of the whole module.

trace_mem now runs under keep_last_error, which puts the error back after
it.

Test: tracing_keeps_the_last_error. It fails without the restore, in any
test order.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:16:37 +00:00
github-bot closed this pull request 2026-11-26 04:16:37 +00:00
Author
Owner

@⁠Kalmarv on GitHub · 2026-11-23 21:26 UTC · original

Rebased on main d4f17ca (adeac80). The only conflict was the test module, next to the affinity tests #221 added; both are kept. cargo test --release -p xcl --lib hle::kernelx -- --test-threads=1: 18 passed, including tracing_keeps_the_last_error and xmem_and_physical_page_overflows_fail_cleanly.

🤖 Claude Opus 5.5

**@⁠Kalmarv** on GitHub · 2026-11-23 21:26 UTC · [original](https://github.com/xcl-dev/xcl/pull/226#issuecomment-7114459752) Rebased on main d4f17ca (adeac80). The only conflict was the test module, next to the affinity tests #221 added; both are kept. `cargo test --release -p xcl --lib hle::kernelx -- --test-threads=1`: 18 passed, including `tracing_keeps_the_last_error` and `xmem_and_physical_page_overflows_fail_cleanly`. 🤖 Claude Opus 5.5
Author
Owner

@⁠ivall on GitHub · 2026-11-23 21:31 UTC · original

Merged into main (0f34407) as 594f63a, the head before your rebase - the rebased one has the same content (main and your head agree on every file it touches), so GitHub does not show it as merged; closing by hand. Checked on an RX 9070 (RDNA4): cargo build --workspace && cargo test --workspace -- --test-threads=4 - 611 unit tests passed, 0 failed; the retail game still reaches its first menu at 30 fps.

**@⁠ivall** on GitHub · 2026-11-23 21:31 UTC · [original](https://github.com/xcl-dev/xcl/pull/226#issuecomment-7114585362) Merged into main (0f34407) as 594f63a, the head before your rebase - the rebased one has the same content (main and your head agree on every file it touches), so GitHub does not show it as merged; closing by hand. Checked on an RX 9070 (RDNA4): `cargo build --workspace && cargo test --workspace -- --test-threads=4` - 611 unit tests passed, 0 failed; the retail game still reaches its first menu at 30 fps.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.