[pr-fix] hle: the narrow (A) text surface uses the ANSI code page #404

Manually merged
TrustNo1 merged 1 commit from qnjk/Darner:hle-ansi-narrow into main 2026-12-05 04:50:32 +00:00
Contributor

Contributor role (optional)

  • Fluxer ID:

What it changes

  • The problem on main: the narrow (A-suffix) text surface encodes UTF-8 where Windows uses the ANSI code page.

    // GetModuleFileNameA, kernel.rs:122-127
    copy_truncated(path.as_bytes(), title_buffer(out, size))
    // title_argv, kernel.rs:645-650
    .filter_map(|a| std::ffi::CString::new(a).ok())
    

    path is a Rust String, so as_bytes() is UTF-8; CString::new(&str) keeps the UTF-8 bytes too. The wide twin encodes properly (path.encode_utf16(), kernel.rs:117), and GetCommandLineA, six hundred lines below, already writes the code page (host::ansi(&wide), kernel.rs:736, helper at :1469) with a doc that says the narrow line "is the wide one in the ANSI code page, as Windows gives it". files::narrow decodes every path the title hands back as CP_ACP. So one non-ASCII character made our own APIs disagree with each other and with the host:

    • For C:\Spiele\Zoë\game.exe, GetModuleFileNameA wrote Zoë as C3 AB and returned 23 where GetModuleFileNameW returned 22. A title that took its directory from there and opened a file under it handed us those same bytes, which CP_ACP reads as ë: the file is not found.
    • __p___argv carried 5A 6F C3 AB while GetCommandLineA carried the code page's bytes for the same text — the very pair the doc at kernel.rs:740-744 says matches.
  • This change encodes both with host::ansi, through one new narrow_arg helper for argv, and decodes XLaunchNewGame's path and argument line with CP_ACP instead of UTF-8 (crates/darner-hle/src/modules/xgame.rs). That last part is required by the same change: launch_new_game gets the title's own path, which the doc it carries says comes from GetModuleFileNameA; leaving it on text() (UTF-8) would turn a correct ANSI path into a wrong one for a non-ASCII install folder.

How it was tested

  • Tested revision: 9acd1d4 (one commit on f3840d7).
  • Commands and results:
    • cargo test --locked -p darner-hle --lib — 495 passed, 0 failed, 15 ignored.
    • Two new tests, both bare-runner (no title, no GPU):
      • kernel::tests::the_narrow_module_name_is_in_the_ansi_code_page — records C:\Spiele\Zoë\game.exe with runtime::set_image_path, compares GetModuleFileNameA's bytes with host::ansi of the same path and its count with GetModuleFileNameW's, asserts the bytes are not UTF-8 when GetACP() != 65001, and checks an ASCII path is unchanged byte for byte.
      • kernel::tests::the_titles_arguments_are_in_the_ansi_code_page — reads title_argv("darner.exe", &["-name=Zoë", "-x=1"]) back as bytes and compares entry 1 with host::ansi, entry 0 and 2 with ASCII.
    • Fail-before, with only the two production lines put back (the tests kept):
      the_narrow_module_name_is_in_the_ansi_code_page ... FAILED
        assertion `left == right` failed: the ANSI code page
          left:  [67, 58, 92, 83, 112, 105, 101, 108, 101, 92, 90, 111, 195, 171, 92, ...]   (23 bytes)
          right: [67, 58, 92, 83, 112, 105, 101, 108, 101, 92, 90, 111, 101, 92, ...]        (22 bytes)
      the_titles_arguments_are_in_the_ansi_code_page ... FAILED
        left:  [45, 110, 97, 109, 101, 61, 90, 111, 195, 171]
        right: [45, 110, 97, 109, 101, 61, 90, 111, 101]
      
      Both pass with the change. The 195, 171 is zoë's UTF-8 ë; the right-hand bytes are what WideCharToMultiByte(CP_ACP, …) writes on this machine (its best-fit maps ë to e).
  • Changes a default: no. ASCII text is byte-identical; only characters outside the code page differ, and they now differ in the way Windows defines.
  • A/B result: not applicable — no default changes and no speed claim.
  • Gates run on that revision:
    • Gate 1, cargo clippy --workspace --all-targets --locked -- -D warnings: passes.
    • Gate 2, cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: passes.
    • Gate 3, cargo test --workspace --locked: not green on this machine, and not because of this change. Three pre-existing failures, each covered by an open pull request: darner-gpu stands still in parallel on the test deadlock #224 describes, and serially finishes 1772 passed and 1 failed (pipeline::make::tests::a_capped_draw_waits_once_then_drops, which passes on its own) beside the order-dependent watch::tests::ask_once_by_res_splits_reservations_without_refusals and texture::ahead::tests::drain_waits_until_then_callbacks_have_run flakes of #150; darner-shader fails emit::waterfall_tests::sampled_marks_restore_early_depth_only_when_safe_in_pixel_shaders, whose fixture #227 fixes. darner-hle's own suite is green.
  • Gameplay, when relevant: no gameplay run. Nothing on this machine's install path has a non-ASCII character, so a run could not show the difference; the two tests do, and they fail on main.
  • Shader wave sizes tested, when relevant: not applicable.
  • Hardware dependence: none, but the observable bytes depend on the machine's ANSI code page (GetACP()); the tests say so and skip the "not UTF-8" assertion on a UTF-8 code page.
  • Coverage gaps: only GetModuleFileNameA and __p___argv are converted. Other narrow surfaces keep their own encoding decisions and are not part of this change; the text() helper is still used for the protocol-activation URI, which is not a path the title produced from an A API. No non-ASCII run was done.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only).
    • Not run in gameplay: the install path on this machine is pure ASCII, so a run cannot distinguish the old bytes from the new ones. The evidence is the two tests, which fail on main and pass here.
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md. (cargo xtask policy: 555 files ok.)
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: none found. No open pull request changes crates/darner-hle/src/modules/kernel.rs or xgame.rs (checked against the file list of all 161 open pull requests), and no open or closed issue mentions GetModuleFileName, CP_ACP, the code page or argv.
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: ## What it changes - The problem on main: the narrow (A-suffix) text surface encodes UTF-8 where Windows uses the ANSI code page. ```rust // GetModuleFileNameA, kernel.rs:122-127 copy_truncated(path.as_bytes(), title_buffer(out, size)) // title_argv, kernel.rs:645-650 .filter_map(|a| std::ffi::CString::new(a).ok()) ``` `path` is a Rust `String`, so `as_bytes()` is UTF-8; `CString::new(&str)` keeps the UTF-8 bytes too. The wide twin encodes properly (`path.encode_utf16()`, `kernel.rs:117`), and `GetCommandLineA`, six hundred lines below, already writes the code page (`host::ansi(&wide)`, `kernel.rs:736`, helper at `:1469`) with a doc that says the narrow line "is the wide one in the ANSI code page, as Windows gives it". `files::narrow` decodes every path the title hands back as `CP_ACP`. So one non-ASCII character made our own APIs disagree with each other and with the host: - For `C:\Spiele\Zoë\game.exe`, `GetModuleFileNameA` wrote `Zoë` as `C3 AB` and returned 23 where `GetModuleFileNameW` returned 22. A title that took its directory from there and opened a file under it handed us those same bytes, which `CP_ACP` reads as `ë`: the file is not found. - `__p___argv` carried `5A 6F C3 AB` while `GetCommandLineA` carried the code page's bytes for the same text — the very pair the doc at `kernel.rs:740-744` says matches. - This change encodes both with `host::ansi`, through one new `narrow_arg` helper for argv, and decodes `XLaunchNewGame`'s path and argument line with `CP_ACP` instead of UTF-8 (`crates/darner-hle/src/modules/xgame.rs`). That last part is required by the same change: `launch_new_game` gets the title's own path, which the doc it carries says comes from `GetModuleFileNameA`; leaving it on `text()` (UTF-8) would turn a correct ANSI path into a wrong one for a non-ASCII install folder. ## How it was tested - Tested revision: `9acd1d4` (one commit on `f3840d7`). - Commands and results: - `cargo test --locked -p darner-hle --lib` — 495 passed, 0 failed, 15 ignored. - Two new tests, both bare-runner (no title, no GPU): - `kernel::tests::the_narrow_module_name_is_in_the_ansi_code_page` — records `C:\Spiele\Zoë\game.exe` with `runtime::set_image_path`, compares `GetModuleFileNameA`'s bytes with `host::ansi` of the same path and its count with `GetModuleFileNameW`'s, asserts the bytes are not UTF-8 when `GetACP() != 65001`, and checks an ASCII path is unchanged byte for byte. - `kernel::tests::the_titles_arguments_are_in_the_ansi_code_page` — reads `title_argv("darner.exe", &["-name=Zoë", "-x=1"])` back as bytes and compares entry 1 with `host::ansi`, entry 0 and 2 with ASCII. - Fail-before, with only the two production lines put back (the tests kept): ``` the_narrow_module_name_is_in_the_ansi_code_page ... FAILED assertion `left == right` failed: the ANSI code page left: [67, 58, 92, 83, 112, 105, 101, 108, 101, 92, 90, 111, 195, 171, 92, ...] (23 bytes) right: [67, 58, 92, 83, 112, 105, 101, 108, 101, 92, 90, 111, 101, 92, ...] (22 bytes) the_titles_arguments_are_in_the_ansi_code_page ... FAILED left: [45, 110, 97, 109, 101, 61, 90, 111, 195, 171] right: [45, 110, 97, 109, 101, 61, 90, 111, 101] ``` Both pass with the change. The `195, 171` is `zoë`'s UTF-8 `ë`; the right-hand bytes are what `WideCharToMultiByte(CP_ACP, …)` writes on this machine (its best-fit maps `ë` to `e`). - Changes a default: no. ASCII text is byte-identical; only characters outside the code page differ, and they now differ in the way Windows defines. - A/B result: not applicable — no default changes and no speed claim. - Gates run on that revision: - Gate 1, `cargo clippy --workspace --all-targets --locked -- -D warnings`: passes. - Gate 2, `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: passes. - Gate 3, `cargo test --workspace --locked`: **not green on this machine, and not because of this change.** Three pre-existing failures, each covered by an open pull request: `darner-gpu` stands still in parallel on the test deadlock #224 describes, and serially finishes 1772 passed and 1 failed (`pipeline::make::tests::a_capped_draw_waits_once_then_drops`, which passes on its own) beside the order-dependent `watch::tests::ask_once_by_res_splits_reservations_without_refusals` and `texture::ahead::tests::drain_waits_until_then_callbacks_have_run` flakes of #150; `darner-shader` fails `emit::waterfall_tests::sampled_marks_restore_early_depth_only_when_safe_in_pixel_shaders`, whose fixture #227 fixes. `darner-hle`'s own suite is green. - Gameplay, when relevant: no gameplay run. Nothing on this machine's install path has a non-ASCII character, so a run could not show the difference; the two tests do, and they fail on `main`. - Shader wave sizes tested, when relevant: not applicable. - Hardware dependence: none, but the *observable* bytes depend on the machine's ANSI code page (`GetACP()`); the tests say so and skip the "not UTF-8" assertion on a UTF-8 code page. - Coverage gaps: only `GetModuleFileNameA` and `__p___argv` are converted. Other narrow surfaces keep their own encoding decisions and are not part of this change; the `text()` helper is still used for the protocol-activation URI, which is not a path the title produced from an A API. No non-ASCII run was done. ## Checklist - [ ] I ran this change in the game, or say below why it can't be (docs, tests only). - Not run in gameplay: the install path on this machine is pure ASCII, so a run cannot distinguish the old bytes from the new ones. The evidence is the two tests, which fail on `main` and pass here. - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. (`cargo xtask policy`: 555 files ok.) - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: none found. No open pull request changes `crates/darner-hle/src/modules/kernel.rs` or `xgame.rs` (checked against the file list of all 161 open pull requests), and no open or closed issue mentions `GetModuleFileName`, `CP_ACP`, the code page or `argv`. - Replaces or overlaps: none found
`GetModuleFileNameA` wrote the UTF-8 bytes of a Rust `String` into the
title's buffer, and `__p___argv` built every element with
`CString::new(&str)`, which is UTF-8 too. A-suffix APIs exchange the ANSI
code page: `GetCommandLineA`, six hundred lines below, already encodes
with `host::ansi`, and `files::narrow` decodes every path the title hands
back as `CP_ACP`.

So one path with a non-ASCII character made our own APIs disagree: for
`C:\Spiele\Zoë\game.exe`, `GetModuleFileNameA` wrote `Zoë` as `C3 AB` and
reported 23 where `GetModuleFileNameW` reported 22, and a title that took
its directory from there and opened a file under it handed us bytes its
own code page never produced, so the file was not found. `__p___argv`
contradicted `GetCommandLineA` for the same argument, which the doc above
`GetCommandLineA` says must match.

Both now encode through `host::ansi`. `XLaunchNewGame`, whose path comes
from `GetModuleFileNameA` by its own documentation, decodes the same way
instead of reading UTF-8.
First-time contributor

Tested on Windows 11 (Ryzen 7 9800X3D, RTX 5080): the new tests pass, and the full darner-hle suite passes (495 passed, 0 failed, 15 ignored, plus 2 integration tests).

Review: this fixes a real path-handling bug: a title that took its directory from GetModuleFileNameA and opened a file under it was handed UTF-8 bytes its own code page never produced. The fix is consistent across all three surfaces (module name, narrow argv, and XLaunchNewGame), and decoding the relaunch arguments with MultiByteToWideChar(CP_ACP) matches how they were written. One note on coverage: the tests' "not UTF-8" assertions only run on a non-65001 ACP. This machine is 1252 so they did run, but on an ACP-65001 machine they would be skipped and the test would pass even with the old behavior.

Drafted with AI assistance (OpenCode).

Tested on Windows 11 (Ryzen 7 9800X3D, RTX 5080): the new tests pass, and the full `darner-hle` suite passes (495 passed, 0 failed, 15 ignored, plus 2 integration tests). Review: this fixes a real path-handling bug: a title that took its directory from `GetModuleFileNameA` and opened a file under it was handed UTF-8 bytes its own code page never produced. The fix is consistent across all three surfaces (module name, narrow `argv`, and `XLaunchNewGame`), and decoding the relaunch arguments with `MultiByteToWideChar(CP_ACP)` matches how they were written. One note on coverage: the tests' "not UTF-8" assertions only run on a non-65001 ACP. This machine is 1252 so they did run, but on an ACP-65001 machine they would be skipped and the test would pass even with the old behavior. Drafted with AI assistance (OpenCode).
TrustNo1 manually merged commit 375d7c1682 into main 2026-12-05 04:50:32 +00:00
Author
Contributor

Thanks for the run and the review. The note about the ACP is right, and it is worth saying why that skip cannot hide a regression: on an ACP-65001 machine the two behaviours are byte-identical. Today's code writes path.as_bytes(), which is UTF-8; the fix writes host::ansi(..), which is WideCharToMultiByte(CP_ACP, ..) and with CP_ACP = 65001 is UTF-8 as well. Same bytes, same count, so there is nothing on such a machine for a test to distinguish -- the fix is a no-op there, and the assert_ne! that is skipped is the only assertion that could differ.

What still runs on every machine is what pins the contract: the bytes equal host::ansi of the same path (which is what the host's own A APIs produce), the ASCII path is unchanged byte for byte, and the count equals GetModuleFileNameW's. The gate mirrors the neighbouring the_narrow_command_line_is_in_the_ansi_code_page, which skips its "not UTF-8" assertion the same way for the same reason.

For a UTF-8 ACP install the honest statement is that this change cannot be observed, not that the test is weak; I have not been able to construct a case where the old and the new code disagree there.

Thanks for the run and the review. The note about the ACP is right, and it is worth saying why that skip cannot hide a regression: on an ACP-65001 machine the two behaviours are byte-identical. Today's code writes `path.as_bytes()`, which is UTF-8; the fix writes `host::ansi(..)`, which is `WideCharToMultiByte(CP_ACP, ..)` and with `CP_ACP = 65001` is UTF-8 as well. Same bytes, same count, so there is nothing on such a machine for a test to distinguish -- the fix is a no-op there, and the `assert_ne!` that is skipped is the only assertion that could differ. What still runs on every machine is what pins the contract: the bytes equal `host::ansi` of the same path (which is what the host's own A APIs produce), the ASCII path is unchanged byte for byte, and the count equals `GetModuleFileNameW`'s. The gate mirrors the neighbouring `the_narrow_command_line_is_in_the_ansi_code_page`, which skips its "not UTF-8" assertion the same way for the same reason. For a UTF-8 ACP install the honest statement is that this change cannot be observed, not that the test is weak; I have not been able to construct a case where the old and the new code disagree there.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!404
No description provided.