hle/kernelx: harden null-arg, VirtualFreeEx, GetThreadName, and overflow paths #86

Merged
certified-retart merged 1 commit from upstream-pr/87 into main 2026-11-23 10:47:31 +00:00
certified-retart commented 2026-11-23 02:26:53 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by certified-retart on 2026-11-23T02:26:53Z.


Summary

Fixes several Win32/Xbox ABI edge cases and overflow hazards in hle::kernelx and its downstream gpu::uma allocation rounding:

  • GetProcAddress / LoadLibraryEx{W,A} null-name handling:
    • GetProcAddress(h, NULL) previously entered the (name as usize) >> 16 == 0 ordinal branch as ordinal 0. It now rejects NULL with ERROR_INVALID_PARAMETER, matching real Win32 GetProcAddress while leaving valid non-zero ordinal lookups intact.
    • LoadLibraryExW / LoadLibraryExA with LOAD_AS_DATA flags previously passed "" into load_as_data when given a null filename; they now fail with ERROR_INVALID_PARAMETER.
  • SetThreadName / GetThreadName error & buffer-sizing semantics:
    • Propagates Win32-facility HRESULT failures to SetLastError via hresult_to_win32.
    • GetThreadName now rejects buf == NULL && len > 0 with ERROR_INVALID_PARAMETER and fails with FALSE + ERROR_INSUFFICIENT_BUFFER (while still populating *ret_len) whenever buf == NULL or len <= n (no room for the NUL terminator), rather than silently truncating and returning TRUE.
  • VirtualFree / VirtualFreeEx sparse & graphics bookkeeping:
    • VirtualFree removes an entry from GRAPHICS_RESERVATIONS only after mem::VirtualFree succeeds, preventing failed MEM_RELEASE calls (e.g. non-zero size) from untracking live reservations.
    • VirtualFree validates sparse MEM_RELEASE / MEM_DECOMMIT flag exclusivity, rejects non-zero MEM_RELEASE size and out-of-range decommits before mutating sparse state, resolves zero-size MEM_DECOMMIT at the reservation base to the full reservation, and propagates host VirtualFree failure.
    • VirtualFreeEx routes current-process (GetCurrentProcess()) frees for sparse, UMA, and graphics reservations through VirtualFree so bookkeeping stays in sync without bypassing handle access checks on restricted process handles.
  • XMemVirtualQuery protection classification:
    • Excludes PAGE_GUARD pages from the CPU-readable bitmask and includes PAGE_EXECUTE_WRITECOPY.
  • Checked arithmetic in allocation paths (kernelx + gpu::uma):
    • Replaces unchecked rounding/padding additions and multiplications in aligned_heap_alloc, xmem_graphics_layout, AllocateTitlePhysicalPages, Uma::sparse_alloc, and Uma::alloc_where with checked arithmetic so extreme sizes (including usize::MAX and usize::MAX & !(GRAIN - 1)) fail cleanly with ERROR_NOT_ENOUGH_MEMORY instead of overflowing or panicking.

Verification

  • cargo test -p xcl --lib hle::kernelx -- --test-threads=2: 11 passed; 0 failed
    • Added tests: get_proc_address_and_load_library_ex_reject_null_name (including Win32 GetLastError parity and non-zero ordinal lookup), get_thread_name_sizing_truncation_and_errors, xmem_virtual_query_excludes_guard_pages, xmem_and_physical_page_overflows_fail_cleanly, and virtual_free_ex_and_sparse_validation.
  • cargo test -p xcl --lib gpu::uma -- --test-threads=2: 7 passed; 0 failed
<!-- greyfinch-sync:ivall/xcl:issue:6551000094 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/87). Originally posted by [certified-retart](https://github.com/certified-retart) on 2026-11-23T02:26:53Z. --- ## Summary Fixes several Win32/Xbox ABI edge cases and overflow hazards in `hle::kernelx` and its downstream `gpu::uma` allocation rounding: - **`GetProcAddress` / `LoadLibraryEx{W,A}` null-name handling:** - `GetProcAddress(h, NULL)` previously entered the `(name as usize) >> 16 == 0` ordinal branch as ordinal `0`. It now rejects `NULL` with `ERROR_INVALID_PARAMETER`, matching real Win32 `GetProcAddress` while leaving valid non-zero ordinal lookups intact. - `LoadLibraryExW` / `LoadLibraryExA` with `LOAD_AS_DATA` flags previously passed `""` into `load_as_data` when given a null filename; they now fail with `ERROR_INVALID_PARAMETER`. - **`SetThreadName` / `GetThreadName` error & buffer-sizing semantics:** - Propagates Win32-facility `HRESULT` failures to `SetLastError` via `hresult_to_win32`. - `GetThreadName` now rejects `buf == NULL && len > 0` with `ERROR_INVALID_PARAMETER` and fails with `FALSE` + `ERROR_INSUFFICIENT_BUFFER` (while still populating `*ret_len`) whenever `buf == NULL` or `len <= n` (no room for the NUL terminator), rather than silently truncating and returning `TRUE`. - **`VirtualFree` / `VirtualFreeEx` sparse & graphics bookkeeping:** - `VirtualFree` removes an entry from `GRAPHICS_RESERVATIONS` only after `mem::VirtualFree` succeeds, preventing failed `MEM_RELEASE` calls (e.g. non-zero size) from untracking live reservations. - `VirtualFree` validates sparse `MEM_RELEASE` / `MEM_DECOMMIT` flag exclusivity, rejects non-zero `MEM_RELEASE` size and out-of-range decommits before mutating sparse state, resolves zero-size `MEM_DECOMMIT` at the reservation base to the full reservation, and propagates host `VirtualFree` failure. - `VirtualFreeEx` routes current-process (`GetCurrentProcess()`) frees for sparse, UMA, and graphics reservations through `VirtualFree` so bookkeeping stays in sync without bypassing handle access checks on restricted process handles. - **`XMemVirtualQuery` protection classification:** - Excludes `PAGE_GUARD` pages from the CPU-readable bitmask and includes `PAGE_EXECUTE_WRITECOPY`. - **Checked arithmetic in allocation paths (`kernelx` + `gpu::uma`):** - Replaces unchecked rounding/padding additions and multiplications in `aligned_heap_alloc`, `xmem_graphics_layout`, `AllocateTitlePhysicalPages`, `Uma::sparse_alloc`, and `Uma::alloc_where` with checked arithmetic so extreme sizes (including `usize::MAX` and `usize::MAX & !(GRAIN - 1)`) fail cleanly with `ERROR_NOT_ENOUGH_MEMORY` instead of overflowing or panicking. ## Verification - `cargo test -p xcl --lib hle::kernelx -- --test-threads=2`: **11 passed; 0 failed** - Added tests: `get_proc_address_and_load_library_ex_reject_null_name` (including Win32 `GetLastError` parity and non-zero ordinal lookup), `get_thread_name_sizing_truncation_and_errors`, `xmem_virtual_query_excludes_guard_pages`, `xmem_and_physical_page_overflows_fail_cleanly`, and `virtual_free_ex_and_sparse_validation`. - `cargo test -p xcl --lib gpu::uma -- --test-threads=2`: **7 passed; 0 failed**
upstream-sync closed this pull request 2026-11-24 06:57:06 +00:00
ivall commented 2026-11-24 07:00:27 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ivall on 2026-11-23T10:48:03Z.


Merged. XDefiant (sparse reservations, VirtualFree-heavy) runs as before. Tested on Windows 11 with an RX 9070 (RDNA4): cargo test --workspace green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with XCL_SPLIT_WAVE64=1; Unicorn Overlord and XDefiant run with this round merged. Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7098508406 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/87#issuecomment-7098508406). Originally posted by [ivall](https://github.com/ivall) on 2026-11-23T10:48:03Z. --- Merged. XDefiant (sparse reservations, VirtualFree-heavy) runs as before. Tested on Windows 11 with an RX 9070 (RDNA4): `cargo test --workspace` green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with `XCL_SPLIT_WAVE64=1`; Unicorn Overlord and XDefiant run with this round merged. Thanks!
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!86
No description provided.