Kernel: a title image's file name that fits clears the last error #186

Merged
Maraco91 merged 1 commit from upstream-pr/186 into main 2026-11-25 00:08:02 +00:00
Maraco91 commented 2026-11-24 01:15:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Maraco91 on 2026-11-24T01:15:23Z.


What

GetModuleFileNameA/W for a title image (the exe by NULL, or a title DLL) answers from our own record of its original path. copy_truncated sets ERROR_INSUFFICIENT_BUFFER when the name is cut, but leaves the thread's last error alone when the name fits. The host clears it on success, and callers rely on that, because the return value alone cannot tell "cut" from "filled the buffer exactly":

do { grow(buf); n = GetModuleFileNameW(NULL, buf, size); }
while (GetLastError() == ERROR_INSUFFICIENT_BUFFER);          // never ends

n = GetModuleFileNameA(NULL, buf, MAX_PATH);
if (n == 0 || GetLastError() == ERROR_INSUFFICIENT_BUFFER) fail();   // fails on a whole name

With our answer, the first loop spins forever once its first buffer was too small, and the second fails whenever some earlier call on the thread left 122 behind. Other modules' names go to the host and were already right; only title images took this path. xgame.rs notes that Grand Theft Auto VI reads its own path with GetModuleFileNameA; I do not know which of these shapes its code has (I have no game files).

A name that fits now sets the last error to 0. The truncated and zero-size cases are unchanged.

Tested

Windows 11 Pro 10.0.26200, rustc 1.98.1 (x86_64-pc-windows-msvc), no game data, no GPU involved.

New unit test a_module_name_that_fits_clears_the_last_error. It first pins down what the host does, on a host module (kernel32.dll, which our functions pass through): with the last error preset to 122, a successful GetModuleFileNameW and GetModuleFileNameA both leave it at 0 on this machine. Then the same for a title image: too small (returns the size, last error 122, as before), then with room, wide and narrow, and an exact fit (17 characters in 18); control: 17 characters in 17 is still cut and still reports 122.

  • main (d2a4bae) + the test only: cargo test -p victory-hle --lib a_module_name_that_fits fails at the first title-image check: a title image, wide: left: 122, right: 0. The two host checks before it pass.
  • with the fix: cargo test -p victory-hle --lib kernel::: 4 passed, 0 failed.
  • cargo clippy -p victory-hle --tests: nothing in kernel.rs. rustfmt --check kernel.rs: clean.
  • cargo xtask policy --staged: ok.

Not tested: a title run; Windows 10 (the reset on success dates from Vista as far as I know, but I only ran Windows 11).

AI-assisted (Claude Code); the tests above were run on the machine described.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6572361503 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/186). Originally posted by [Maraco91](https://github.com/Maraco91) on 2026-11-24T01:15:23Z. --- ## What `GetModuleFileNameA/W` for a title image (the exe by `NULL`, or a title DLL) answers from our own record of its original path. `copy_truncated` sets `ERROR_INSUFFICIENT_BUFFER` when the name is cut, but leaves the thread's last error alone when the name fits. The host clears it on success, and callers rely on that, because the return value alone cannot tell "cut" from "filled the buffer exactly": ```c do { grow(buf); n = GetModuleFileNameW(NULL, buf, size); } while (GetLastError() == ERROR_INSUFFICIENT_BUFFER); // never ends n = GetModuleFileNameA(NULL, buf, MAX_PATH); if (n == 0 || GetLastError() == ERROR_INSUFFICIENT_BUFFER) fail(); // fails on a whole name ``` With our answer, the first loop spins forever once its first buffer was too small, and the second fails whenever some earlier call on the thread left 122 behind. Other modules' names go to the host and were already right; only title images took this path. `xgame.rs` notes that Grand Theft Auto VI reads its own path with `GetModuleFileNameA`; I do not know which of these shapes its code has (I have no game files). A name that fits now sets the last error to 0. The truncated and zero-size cases are unchanged. ## Tested Windows 11 Pro 10.0.26200, rustc 1.98.1 (`x86_64-pc-windows-msvc`), no game data, no GPU involved. New unit test `a_module_name_that_fits_clears_the_last_error`. It first pins down what the host does, on a host module (`kernel32.dll`, which our functions pass through): with the last error preset to 122, a successful `GetModuleFileNameW` and `GetModuleFileNameA` both leave it at 0 on this machine. Then the same for a title image: too small (returns the size, last error 122, as before), then with room, wide and narrow, and an exact fit (17 characters in 18); control: 17 characters in 17 is still cut and still reports 122. - `main` (d2a4bae) + the test only: `cargo test -p victory-hle --lib a_module_name_that_fits` **fails** at the first title-image check: `a title image, wide: left: 122, right: 0`. The two host checks before it pass. - with the fix: `cargo test -p victory-hle --lib kernel::`: **4 passed, 0 failed**. - `cargo clippy -p victory-hle --tests`: nothing in `kernel.rs`. `rustfmt --check kernel.rs`: clean. - `cargo xtask policy --staged`: ok. Not tested: a title run; Windows 10 (the reset on success dates from Vista as far as I know, but I only ran Windows 11). AI-assisted (Claude Code); the tests above were run on the machine described. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
DaviTostes commented 2026-11-24 06:54:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T01:24:05Z.


Tested 3c7d845 under Wine 11.22 + vkd3d-proton 3.1 on Arch Linux x86_64 (AMD RX 9060 XT, RADV 26.3.1), Windows build with cargo xwin ... --target x86_64-pc-windows-msvc -p victory-hle and a native Microsoft d3dcompiler_47: pass.

  • victory_hle: 86 passed, 1 failed — the failure is modules::net::tests::only_loopback_is_reachable (Wine lacks WSAConnectByList, as in #118), not this PR. The test this PR adds passes.

No game data used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119063272 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/186#issuecomment-7119063272). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T01:24:05Z. --- Tested `3c7d845` under **Wine 11.22 + vkd3d-proton 3.1** on Arch Linux x86_64 (AMD RX 9060 XT, RADV 26.3.1), Windows build with `cargo xwin ... --target x86_64-pc-windows-msvc -p victory-hle` and a native Microsoft `d3dcompiler_47`: **pass**. - `victory_hle`: **86 passed, 1 failed** — the failure is `modules::net::tests::only_loopback_is_reachable` (Wine lacks `WSAConnectByList`, as in #118), not this PR. The test this PR adds passes. No game data used.
mercuryVM commented 2026-11-24 08:55:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T08:55:43Z.

Review: APPROVED


Tested 3c7d845 on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (265 passed, 0 failed), including a_module_name_that_fits_clears_the_last_error. Its first half shows that the host's own GetModuleFileNameW/A for kernel32.dll clears a stale ERROR_INSUFFICIENT_BUFFER on Windows 11, so matching that is right.

Setting ERROR_SUCCESS only on the "fits" path keeps the truncated case and the zero-size case at ERROR_INSUFFICIENT_BUFFER. The exact-fit boundary test (18 bytes fits, 17 doesn't) pins the off-by-one. It also composes with #373, which is in our fork: a null buffer goes through title_buffer to an empty slice, which still takes the insufficient-buffer branch.

No issues found.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6036249994 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/186#pullrequestreview-6036249994). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T08:55:43Z. **Review:** APPROVED --- Tested 3c7d845 on Windows 11, RTX 5070, MSVC: `cargo test --workspace --no-fail-fast` passes (265 passed, 0 failed), including `a_module_name_that_fits_clears_the_last_error`. Its first half shows that the host's own `GetModuleFileNameW`/`A` for `kernel32.dll` clears a stale `ERROR_INSUFFICIENT_BUFFER` on Windows 11, so matching that is right. Setting `ERROR_SUCCESS` only on the "fits" path keeps the truncated case and the zero-size case at `ERROR_INSUFFICIENT_BUFFER`. The exact-fit boundary test (18 bytes fits, 17 doesn't) pins the off-by-one. It also composes with #373, which is in our fork: a null buffer goes through `title_buffer` to an empty slice, which still takes the insufficient-buffer branch. No issues found. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-25 01:02:56 +00:00
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
Marvisak/VIctory!186
No description provided.