Skip to content

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

Merged
ivall merged 1 commit into
xcl-dev:mainfrom
certified-retart:fix/kernelx-edge-cases
Nov 23, 2026
Merged

ivall merged 1 commit into
xcl-dev:mainfrom
certified-retart:fix/kernelx-edge-cases

Conversation

@certified-retart

Copy link
Copy Markdown
Contributor

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

@ivall
ivall merged commit 25f943e into xcl-dev:main Nov 23, 2026
ivall added a commit that referenced this pull request Nov 23, 2026
@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants