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

Closed
Kalmarv wants to merge 1 commit from upstream-pr/226 into main
Kalmarv commented 2026-11-23 21:22:20 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-23T21:22:20Z.


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.

<!-- greyfinch-sync:ivall/xcl:issue:6569097004 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/226). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-23T21:22:20Z. --- `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.
upstream-sync closed this pull request 2026-11-24 06:57:44 +00:00
Kalmarv commented 2026-11-24 07:04:26 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-23T21:26:16Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7114459752 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/226#issuecomment-7114459752). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-23T21:26:16Z. --- 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
ivall commented 2026-11-24 07:04:27 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ivall on 2026-11-23T21:31:58Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7114585362 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/226#issuecomment-7114585362). Originally posted by [ivall](https://github.com/ivall) on 2026-11-23T21:31:58Z. --- 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

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
xcl-dev/xcl!223
No description provided.