Tests: preserve GetLastError() in util::under_wine() #1135

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

@⁠bigiello on GitHub · 2026-11-24 12:19 UTC · original
fix/util-under-wine-last-error → main · +42 −5 · 1 files · 1 commits · closed without merging

The first call to xcl::util::under_wine() calls GetProcAddress(ntdll, "wine_get_version"). On native Windows that export does not exist, so GetProcAddress fails and sets the calling thread's last error to 127 (ERROR_PROC_NOT_FOUND), clobbering any Win32 error the caller was about to inspect.

Save and restore GetLastError() around the GetModuleHandleA / GetProcAddress probe so under_wine() never mutates the calling thread's last error, even on its first invocation.

Fixes #1125

What is in it

  • crates/xcl/src/util.rs:
    • Extract a private probe_under_wine() helper called by WINE.get_or_init(...) that saves GetLastError() before probing ntdll.dll for wine_get_version and restores it with SetLastError(saved).
    • Add util::tests::under_wine_preserves_last_error exercising both a cold under_wine() call and direct probe_under_wine() calls across 0, ERROR_ACCESS_DENIED, and a custom sentinel (0x1234_5678) so the test fails without the fix regardless of test order.

Tested
Hardware: Windows 11 x64, AMD Ryzen 5 5600, 32 GB RAM, NVIDIA GeForce RTX 4070 SUPER (12 GB VRAM, D3D12 SM 6.6), rustc 1.100.0 (stable-x86_64-pc-windows-msvc), Windows SDK DXC 1.8.2502.11, debug build. No XCL_SAMPLE_* variables set.

  • cargo test -p xcl --lib util::tests::under_wine_preserves_last_error -- --exact: 1 passed, 0 failed, 0 ignored (cold OnceLock initialization + direct probe).
  • cargo test -p xcl --lib util::tests: 6 passed, 0 failed, 0 ignored.
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 742 passed, 0 failed, 41 ignored, 2 GPU skipped (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware: no wave64) — xcl --lib: 699 passed, 0 failed, 21 ignored; 15 integration test binaries: 43 passed, 0 failed, 20 ignored; bins and doc-tests: 0 passed, 0 failed.
  • cargo test -p xcl --lib -- --test-threads=1: 699 passed, 0 failed, 21 ignored, 2 GPU skipped (no wave64).
  • cargo test -p xcl --lib (default 12 threads, without --test-threads): process exited abnormally with 0xc0000005 (STATUS_ACCESS_VIOLATION) during parallel D3D12 tests (list_x::tests / readback_routing_tests, unrelated to util.rs).
  • cargo clippy --workspace --all-targets: 0 errors, 0 new warnings on the touched lines of crates/xcl/src/util.rs.
  • rustfmt --edition 2021 --check crates/xcl/src/util.rs: 0 diffs on the modified lines (pre-existing diffs on untouched lines left alone per AGENTS.md).

Not tested

  • Retail titles and scripts/smoke.ps1 regression runs (no game files on this machine; Draft PR until a contributor with a title runs smoke or maintainers waive it).
  • Linux / Wine and AMD GPUs (wave64 tests skipped on NVIDIA RTX 4070 SUPER).
  • Release build and XCL_SAMPLE_* tests.

AI use
Antigravity, Neon investigated #1125 and commit d78597d, implemented the GetLastError() save/restore and unit test in crates/xcl/src/util.rs, ran the test/clippy/rustfmt checks above, and drafted this PR description.

**@⁠bigiello** on GitHub · 2026-11-24 12:19 UTC · [original](https://github.com/xcl-dev/xcl/pull/1135) `fix/util-under-wine-last-error` → `main` · +42 −5 · 1 files · 1 commits · closed without merging The first call to `xcl::util::under_wine()` calls `GetProcAddress(ntdll, "wine_get_version")`. On native Windows that export does not exist, so `GetProcAddress` fails and sets the calling thread's last error to `127` (`ERROR_PROC_NOT_FOUND`), clobbering any Win32 error the caller was about to inspect. Save and restore `GetLastError()` around the `GetModuleHandleA` / `GetProcAddress` probe so `under_wine()` never mutates the calling thread's last error, even on its first invocation. Fixes #1125 **What is in it** - `crates/xcl/src/util.rs`: - Extract a private `probe_under_wine()` helper called by `WINE.get_or_init(...)` that saves `GetLastError()` before probing `ntdll.dll` for `wine_get_version` and restores it with `SetLastError(saved)`. - Add `util::tests::under_wine_preserves_last_error` exercising both a cold `under_wine()` call and direct `probe_under_wine()` calls across `0`, `ERROR_ACCESS_DENIED`, and a custom sentinel (`0x1234_5678`) so the test fails without the fix regardless of test order. **Tested** Hardware: Windows 11 x64, AMD Ryzen 5 5600, 32 GB RAM, NVIDIA GeForce RTX 4070 SUPER (12 GB VRAM, D3D12 SM 6.6), `rustc 1.100.0` (`stable-x86_64-pc-windows-msvc`), Windows SDK DXC `1.8.2502.11`, debug build. No `XCL_SAMPLE_*` variables set. - `cargo test -p xcl --lib util::tests::under_wine_preserves_last_error -- --exact`: 1 passed, 0 failed, 0 ignored (cold `OnceLock` initialization + direct probe). - `cargo test -p xcl --lib util::tests`: 6 passed, 0 failed, 0 ignored. - `.\scripts\test.ps1` (`cargo build --workspace` + `cargo test --workspace -- --test-threads=4`): 742 passed, 0 failed, 41 ignored, 2 GPU skipped (`gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`: `no wave64`) — `xcl --lib`: 699 passed, 0 failed, 21 ignored; 15 integration test binaries: 43 passed, 0 failed, 20 ignored; bins and doc-tests: 0 passed, 0 failed. - `cargo test -p xcl --lib -- --test-threads=1`: 699 passed, 0 failed, 21 ignored, 2 GPU skipped (`no wave64`). - `cargo test -p xcl --lib` (default 12 threads, without `--test-threads`): process exited abnormally with `0xc0000005` (`STATUS_ACCESS_VIOLATION`) during parallel D3D12 tests (`list_x::tests` / `readback_routing_tests`, unrelated to `util.rs`). - `cargo clippy --workspace --all-targets`: 0 errors, 0 new warnings on the touched lines of `crates/xcl/src/util.rs`. - `rustfmt --edition 2021 --check crates/xcl/src/util.rs`: 0 diffs on the modified lines (pre-existing diffs on untouched lines left alone per `AGENTS.md`). **Not tested** - Retail titles and `scripts/smoke.ps1` regression runs (no game files on this machine; Draft PR until a contributor with a title runs smoke or maintainers waive it). - Linux / Wine and AMD GPUs (wave64 tests skipped on NVIDIA RTX 4070 SUPER). - Release build and `XCL_SAMPLE_*` tests. **AI use** Antigravity, Neon investigated #1125 and commit `d78597d`, implemented the `GetLastError()` save/restore and unit test in `crates/xcl/src/util.rs`, ran the test/clippy/rustfmt checks above, and drafted this PR description.
The first call to util::under_wine() looks up wine_get_version in
ntdll.dll via GetProcAddress. On native Windows that export does not
exist, so GetProcAddress fails and overwrites the calling thread's last
error with ERROR_PROC_NOT_FOUND (127), breaking callers that read
GetLastError() after under_wine() when it happens to run cold.

Save and restore the thread's last error inside a private
probe_under_wine() helper used by OnceLock::get_or_init, and add a unit
test that checks both under_wine() and the uncached probe_under_wine()
path across zero and non-zero sentinel error codes.

Fixes #1125

Assisted-by: Antigravity (Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:44:36 +00:00
github-bot closed this pull request 2026-11-26 04:44:36 +00:00
Author
Owner

@⁠JoaoPauluu on GitHub · 2026-11-24 13:45 UTC · original

The Wine side of this change.

Tested

  • RTX 3060 12 GB (NVIDIA 620.70.06, 32-lane waves), i5-10400F, 16 GB, CachyOS (kernel 7.3.3), Proton Experimental 11.0-20261112 with the patched vkd3d-proton 3.1 from #257 (DXIL → SPIR-V through dxil-spirv), DXC 1.9.2609.5. Release, cross-built for x86_64-pc-windows-gnu. 8da86a5 as it is (based on 47efa2e), no uncommitted changes. The lib test binary runs under Wine.
  • util::tests::under_wine_preserves_last_error (--exact, one process): ok.
  • Under Wine, ntdll.dll exports wine_get_version, so the probe's GetProcAddress succeeds and does not set the last error. This run therefore checks that the save and restore does no harm on Wine. It does not exercise the Windows failure from #1125, so it would pass without the fix too.

Not tested

  • native Windows; the rest of the suite on this head.

AI use

  • Claude Code (Claude Opus 5.5) built the head, ran the tests and wrote this report. The account owner asked for test reports to be posted and has not reviewed this one.
**@⁠JoaoPauluu** on GitHub · 2026-11-24 13:45 UTC · [original](https://github.com/xcl-dev/xcl/pull/1135#issuecomment-7133290370) The Wine side of this change. **Tested** - RTX 3060 12 GB (NVIDIA 620.70.06, 32-lane waves), i5-10400F, 16 GB, CachyOS (kernel 7.3.3), Proton Experimental 11.0-20261112 with the patched vkd3d-proton 3.1 from #257 (DXIL → SPIR-V through dxil-spirv), DXC 1.9.2609.5. Release, cross-built for `x86_64-pc-windows-gnu`. `8da86a5` as it is (based on `47efa2e`), no uncommitted changes. The lib test binary runs under Wine. - `util::tests::under_wine_preserves_last_error` (`--exact`, one process): **ok**. - Under Wine, `ntdll.dll` exports `wine_get_version`, so the probe's `GetProcAddress` succeeds and does not set the last error. This run therefore checks that the save and restore does no harm on Wine. It does not exercise the Windows failure from #1125, so it would pass without the fix too. **Not tested** - native Windows; the rest of the suite on this head. **AI use** - Claude Code (Claude Opus 5.5) built the head, ran the tests and wrote this report. The account owner asked for test reports to be posted and has not reviewed this one.
Author
Owner

@⁠upipibi on GitHub · 2026-11-24 14:06 UTC · original

This and #1128 (opened 9 minutes earlier) make the same change for #1125. Both save GetLastError() around the wine_get_version lookup and restore it, and both run the probe through OnceLock::get_or_init. Their tests check the same two paths: the probe called directly, and under_wine(). They differ only in names and comments, and they conflict in util.rs, so CONTRIBUTING's one-of-several rule applies. Read from the diffs only; I have not run either.

AI-assisted (Claude Code).

**@⁠upipibi** on GitHub · 2026-11-24 14:06 UTC · [original](https://github.com/xcl-dev/xcl/pull/1135#issuecomment-7133846847) This and #1128 (opened 9 minutes earlier) make the same change for #1125. Both save `GetLastError()` around the `wine_get_version` lookup and restore it, and both run the probe through `OnceLock::get_or_init`. Their tests check the same two paths: the probe called directly, and `under_wine()`. They differ only in names and comments, and they conflict in `util.rs`, so CONTRIBUTING's one-of-several rule applies. Read from the diffs only; I have not run either. AI-assisted (Claude Code).
Author
Owner

@⁠Mischief8713 on GitHub · 2026-11-24 16:10 UTC · original

Native Windows run of 8da86a5 (NVIDIA GTX 1660 Ti): the new test passes with the fix and fails without it, with 127 (ERROR_PROC_NOT_FOUND)

Tested

  • Windows 11 (10.0.26300) native, Ryzen 5 2600X, rustc 1.100.0, debug build. The existing report is Wine only.
  • cargo test -p xcl --lib util::tests::under_wine_preserves_last_error on the head: 1 passed.
  • Same test with only the SetLastError(saved); line in probe_under_wine deleted: FAILED at util.rs:491, left 127, right 305419896 (0x12345678).
  • The head does not contain current main; it was run as it is.
  • #1128 changes the same function the same way and passes and fails the same way here, so one of the two can be closed as a duplicate.

Not tested

  • The whole suite, no retail title, AMD GPUs. This is a CPU-only test.

AI use

  • Claude Code with Claude Sonnet 5.5 ran the commands and wrote this report. I checked the output.
**@⁠Mischief8713** on GitHub · 2026-11-24 16:10 UTC · [original](https://github.com/xcl-dev/xcl/pull/1135#issuecomment-7137236893) **Native Windows run of `8da86a5` (NVIDIA GTX 1660 Ti): the new test passes with the fix and fails without it, with 127 (`ERROR_PROC_NOT_FOUND`)** **Tested** - Windows 11 (10.0.26300) native, Ryzen 5 2600X, rustc 1.100.0, debug build. The existing report is Wine only. - `cargo test -p xcl --lib util::tests::under_wine_preserves_last_error` on the head: 1 passed. - Same test with only the `SetLastError(saved);` line in `probe_under_wine` deleted: FAILED at `util.rs:491`, left 127, right 305419896 (`0x12345678`). - The head does not contain current `main`; it was run as it is. - #1128 changes the same function the same way and passes and fails the same way here, so one of the two can be closed as a duplicate. **Not tested** - The whole suite, no retail title, AMD GPUs. This is a CPU-only test. **AI use** - Claude Code with Claude Sonnet 5.5 ran the commands and wrote this report. I checked the output.

Pull request closed

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