Runtime: fix ANSI CP_ACP path encoding, HLE alias stubs/handles, forwarders, and stub creation race #101

Manually merged
github merged 1 commit from gh-pr/101/head into gh-pr/101/base 2026-11-26 04:10:11 +00:00
Owner

@⁠raylitebull on GitHub · 2026-11-23 11:11 UTC · original
raylitebull:fix/resolver-aliases-and-ansi-paths → main · +568 −51 · 5 files · 1 commits · merged 2026-11-23 11:25 UTC

Summary

Fixes ANSI path codepage encoding, HLE alias stub return codes, GetModuleHandle parity for console-only aliases, ordinal/forwarder resolution, and a make_stub_info TOCTOU race in crates/xcl/src/{util.rs, resolver.rs, stubs.rs, modules.rs, hle/kernelx.rs}:

  1. write_ansi_out CP_ACP encoding (util.rs):

    • Confirmed CP_ACP matches util::from_ansi_ptr, vfs_hooks::w_to_a, and Windows GetModuleFileNameA.
    • Replaced raw UTF-8 copy with WideCharToMultiByte(CP_ACP, ...) and character-boundary-safe truncation (binary-searching complete Unicode scalar-value UTF-16 prefixes so DBCS/UTF-8 multi-byte sequences and surrogate pairs are never split, setting ERROR_INSUFFICIENT_BUFFER on truncation).
    • Tested on Windows (GetACP() == 1252):
      • C:\Jeux\Été\game.exe (CP1252) and C:\Hry\Český_Život\game.exe (CP1250) round-trip cleanly via from_ansi_ptr.
      • C:\Games\漢\game.exe (CJK outside CP1250/CP1252) substitutes '?' (C:\Games\?\game.exe, NUL-terminated, matching Windows GetModuleFileNameA) and round-trips on codepages representing '漢'.
  2. HLE alias stub return fallback (stubs.rs, resolver.rs):

    • Added StubDb::return_for_with_fallback(dll, fallback_dll, name) so routed HLE alias DLLs (xgameplatform.dll, xgameruntime.thunks.dll, d3d12x.dll, etc.) inherit their canonical module's stubdb.toml default (E_NOTIMPL) instead of silently returning 0 (S_OK), while BOOL/handle families (kernelx.dll) keep 0, per-alias overrides still win, and every stub call continues to be recorded in report.json.
  3. GetModuleHandleW/A/Ex parity for console-only aliases & loaded @stub DLLs (hle/kernelx.rs, modules.rs, resolver.rs):

    • For aliases_are_system() aliases, tries Windows GetModuleHandleW first (preserving real handles for kernel32.dll, etc.) and falls back to modules::hle_handle(m.name) when Windows returns 0 (advapi32legacy.dll, kernel32legacy.dll, minappmodelx.dll).
    • For Route::Stub, returns modules::find_hle_handle(&key).unwrap_or(0) so unloaded optional probes still return 0 while loaded @stub DLLs match LoadLibrary.
  4. Ordinal fallback & GetProcAddress forwarder contract (resolver.rs):

    • Unmapped ordinal imports in resolve_hle fall through to system_proc only when c == key (preventing cross-DLL ordinal mismatches on console-only aliases).
    • Threaded allow_stub through resolve_export_of_mapped so get_proc_address returns None instead of synthesizing stubs on unresolvable forwarders, and switched forwarder splitting to rsplit_once('.') to support dotted DLL names.
  5. stubs::make_stub_info TOCTOU race (stubs.rs):

    • Held BY_KEY across lookup, thunk allocation, ALL push, and map insert (strict lock order BY_KEY -> leaf ARENA/ALL), eliminating duplicate thunks and duplicate ALL entries under concurrency.

Testing

  • Added 7 targeted regression & stress tests:
    • ansi_out_encodes_codepage_and_truncates_on_char_boundaries (CP1252, CP1250 "Č"/"Ž", CJK '漢', CP932 DBCS, UTF-8 surrogate pair)
    • make_stub_info_is_race_free_under_concurrency (8 threads × 50 iterations stress test)
    • hle_alias_stubs_inherit_canonical_module_default
    • ordinal_imports_fall_through_only_to_matching_system_dll
    • mapped_forwarders_preserve_get_proc_address_contract_and_dotted_dll_names
    • get_module_handle_matches_load_library_for_console_aliases_and_stubs
    • builtin_db_parses_with_overrides
  • Verified after rebasing onto origin/main (60599a9): cargo build --workspace and cargo test --workspace -- --test-threads=4 pass with 0 failures and 0 new warnings.
**@⁠raylitebull** on GitHub · 2026-11-23 11:11 UTC · [original](https://github.com/xcl-dev/xcl/pull/101) `raylitebull:fix/resolver-aliases-and-ansi-paths` → `main` · +568 −51 · 5 files · 1 commits · merged 2026-11-23 11:25 UTC ## Summary Fixes ANSI path codepage encoding, HLE alias stub return codes, `GetModuleHandle` parity for console-only aliases, ordinal/forwarder resolution, and a `make_stub_info` TOCTOU race in `crates/xcl/src/{util.rs, resolver.rs, stubs.rs, modules.rs, hle/kernelx.rs}`: 1. **`write_ansi_out` `CP_ACP` encoding (`util.rs`):** - Confirmed `CP_ACP` matches `util::from_ansi_ptr`, `vfs_hooks::w_to_a`, and Windows `GetModuleFileNameA`. - Replaced raw UTF-8 copy with `WideCharToMultiByte(CP_ACP, ...)` and character-boundary-safe truncation (binary-searching complete Unicode scalar-value UTF-16 prefixes so DBCS/UTF-8 multi-byte sequences and surrogate pairs are never split, setting `ERROR_INSUFFICIENT_BUFFER` on truncation). - Tested on Windows (`GetACP() == 1252`): - `C:\Jeux\Été\game.exe` (CP1252) and `C:\Hry\Český_Život\game.exe` (CP1250) round-trip cleanly via `from_ansi_ptr`. - `C:\Games\漢\game.exe` (CJK outside CP1250/CP1252) substitutes `'?'` (`C:\Games\?\game.exe`, NUL-terminated, matching Windows `GetModuleFileNameA`) and round-trips on codepages representing `'漢'`. 2. **HLE alias stub return fallback (`stubs.rs`, `resolver.rs`):** - Added `StubDb::return_for_with_fallback(dll, fallback_dll, name)` so routed HLE alias DLLs (`xgameplatform.dll`, `xgameruntime.thunks.dll`, `d3d12x.dll`, etc.) inherit their canonical module's `stubdb.toml` default (`E_NOTIMPL`) instead of silently returning `0` (`S_OK`), while BOOL/handle families (`kernelx.dll`) keep `0`, per-alias overrides still win, and every stub call continues to be recorded in `report.json`. 3. **`GetModuleHandleW/A/Ex` parity for console-only aliases & loaded `@stub` DLLs (`hle/kernelx.rs`, `modules.rs`, `resolver.rs`):** - For `aliases_are_system()` aliases, tries Windows `GetModuleHandleW` first (preserving real handles for `kernel32.dll`, etc.) and falls back to `modules::hle_handle(m.name)` when Windows returns `0` (`advapi32legacy.dll`, `kernel32legacy.dll`, `minappmodelx.dll`). - For `Route::Stub`, returns `modules::find_hle_handle(&key).unwrap_or(0)` so unloaded optional probes still return `0` while loaded `@stub` DLLs match `LoadLibrary`. 4. **Ordinal fallback & `GetProcAddress` forwarder contract (`resolver.rs`):** - Unmapped ordinal imports in `resolve_hle` fall through to `system_proc` only when `c == key` (preventing cross-DLL ordinal mismatches on console-only aliases). - Threaded `allow_stub` through `resolve_export_of_mapped` so `get_proc_address` returns `None` instead of synthesizing stubs on unresolvable forwarders, and switched forwarder splitting to `rsplit_once('.')` to support dotted DLL names. 5. **`stubs::make_stub_info` TOCTOU race (`stubs.rs`):** - Held `BY_KEY` across lookup, thunk allocation, `ALL` push, and map insert (strict lock order `BY_KEY` -> leaf `ARENA`/`ALL`), eliminating duplicate thunks and duplicate `ALL` entries under concurrency. ## Testing - Added 7 targeted regression & stress tests: - `ansi_out_encodes_codepage_and_truncates_on_char_boundaries` (CP1252, CP1250 `"Č"/"Ž"`, CJK `'漢'`, CP932 DBCS, UTF-8 surrogate pair) - `make_stub_info_is_race_free_under_concurrency` (8 threads × 50 iterations stress test) - `hle_alias_stubs_inherit_canonical_module_default` - `ordinal_imports_fall_through_only_to_matching_system_dll` - `mapped_forwarders_preserve_get_proc_address_contract_and_dotted_dll_names` - `get_module_handle_matches_load_library_for_console_aliases_and_stubs` - `builtin_db_parses_with_overrides` - Verified after rebasing onto `origin/main` (`60599a9`): `cargo build --workspace` and `cargo test --workspace -- --test-threads=4` pass with 0 failures and 0 new warnings.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:10:10 +00:00
github manually merged commit eac85f952e into gh-pr/101/base 2026-11-26 04:10:11 +00:00
Author
Owner

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

Merged together with #95–#102 and checked on Windows 11 with an RX 9070: cargo test --workspace green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with XCL_SPLIT_WAVE64=1; XDefiant (title screen, a scripted A reaches its "Xbox network unavailable" dialog), Unicorn Overlord and Minecraft run. Thanks!

**@⁠ivall** on GitHub · 2026-11-23 11:25 UTC · [original](https://github.com/xcl-dev/xcl/pull/101#issuecomment-7099632010) Merged together with #95–#102 and checked on Windows 11 with an RX 9070: `cargo test --workspace` green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with `XCL_SPLIT_WAVE64=1`; XDefiant (title screen, a scripted A reaches its "Xbox network unavailable" dialog), Unicorn Overlord and Minecraft run. Thanks!
This discussion has been locked. Commenting is limited to contributors.
No description provided.