Skip to content

Runtime / Loader / Platform: static TLS alignment & attach races, eh::dump_threads deadlock & VEH bounds, C-unwind FFI boundaries, AVX2 check, and hybrid CPU affinity / PLS quota #106

Open

Description

@raylitebull

Summary

Verified several PE loader, static TLS, exception-handling, and host-platform edge cases during an audit of crates/xcl/src/ (on main 60599a9) that warrant focused follow-up PRs:

1. Static TLS edge cases (crates/xcl/src/tls.rs, crates/xcl/src/main.rs, crates/xcl/src/loader.rs)

  • on_thread_attach() IMAGES.try_read() contention: If a game DLL with static TLS is being installed (IMAGES.write()), concurrently attaching OS/game threads hit WouldBlock and silently skip copying all mapped TLS templates, leaving thread_local variables zeroed.
  • .CRT$XLY ordering after take_slot_zero: XCL_TLS_CALLBACK is placed in .CRT$XLY, which runs after Rust std's .CRT$XLB callback. Once take_slot_zero rewrites xcl.exe's _tls_index to k, any .CRT$XLB...CRT$XLX callback on a new thread reads slot k (the title's carrier block) before exchange_for_new_thread swaps slots 0 and k.
  • take_slot_zero ThreadLocalStoragePointer UAF window & MOVED stale TEBs: Raw arr = *(teb + 0x58) pointers are held across Sleep(20) without suspending threads (vulnerable if a thread loads a TLS DLL and reallocates its TLS pointer array), and TerminateThread leaves stale TEBs in MOVED.
  • Ignored IMAGE_TLS_DIRECTORY64.Characteristics alignment & unchecked AddressOfIndex == 0: tls.rs hardcodes Characteristics = 0 in the carrier DLL (dropping 32B/64B alignment required by AVX2/cache-line thread_locals), and callback-only TLS directories with address_of_index == 0 dereference adj(0).

2. Exception-handling & diagnostic safety (crates/xcl/src/eh.rs)

  • dump_threads() deadlock: Allocates (Vec/format!) and calls RtlLookupFunctionEntry, GetModuleHandleExW (Loader Lock), and GetThreadDescription/LocalFree (Process Heap lock) while holding another thread suspended via SuspendThread. Capturing CONTEXT on the stack and calling ResumeThread immediately before symbolizing/formatting avoids deadlocking if the target held the heap or loader lock.
  • cpp_exception_logger VEH crash risk: Performs unbounded CStr::from_ptr after checking only readable(s, 1) (and assumes [object + 8] is std::exception::what for every thrown C++ object), which can fault inside the VEH near a guard/unmapped page and turn a caught C++ exception fatal; also lacks null checks on info/ExceptionRecord/ContextRecord.
  • watch_writes / start_probes: Missing snap != INVALID_HANDLE_VALUE and SuspendThread(h) != u32::MAX guards.

3. PE loader bounds & failure cleanup (crates/xcl/src/pe.rs, crates/xcl/src/loader.rs)

  • pe.rs::relocations (and thunks/imports/delay_imports/exports) use unchecked u32 addition without verifying block <= d.size - pos (panic in debug / potential infinite loop in release on malformed reloc blocks).
  • loader.rs registers mapped modules before pe.imports() / tls_install() without unregistering (or freeing on TLS error) on failure, and caches DLLs whose DllMain(PROCESS_ATTACH) returned FALSE in MAPPED so a second load_game_dll returns Ok(m).

4. FFI -unwind boundaries, startup CPUID check, and PC-sized platform values

  • Guest callbacks across non--unwind extern "system" / extern "C" boundaries: XMemAlloc/XMemFree guest hooks (hle/kernelx.rs), console_wndproc (hle/user.rs), DllMain/TlsCallback (loader.rs), and blocking GameInput::RegisterDeviceCallback (hle/gameinput.rs) cross non--unwind Rust frames; C++/SEH exceptions unwinding across them abort or skip Drop guards.
  • Startup AVX2 / Zen 2 CPUID check (host.rs / launcher.rs): Retail Xbox Series X|S binaries assume Zen 2 (AVX2/FMA/F16C/BMI1/BMI2/POPCNT/SSE4.2/LZCNT); detecting missing ISA features at startup and printing a clear error avoids opaque 0xC000001D crashes on unsupported CPUs.
  • Hybrid CPU affinity masks & core counts (hle/kernelx.rs): SetThreadAffinityMask / SetThreadGroupAffinity / GetSystemInfo / GlobalMemoryStatusEx currently fall through unshimmed to Windows, which can fail on <7-core CPUs, pin console 7-core masks onto E-cores on hybrid Intel CPUs, or report PC-sized logical CPU counts / RAM.
  • S:\ PLS disk space quota (vfs_hooks.rs): GetDiskFreeSpaceExW/A reports the full host NVMe volume instead of clamping S:\ to config.pls_mb.
  • Minor: Config::load_or_create (config.rs) overwrites xcl.toml on any read error (not just ErrorKind::NotFound), and Config::resolve matches "auto"/"advanced" case-sensitively; report::write_final (report.rs) can drop crash if WRITE_LOCK.try_lock() fails concurrently; RegisterClassExA (hle/user.rs) omits cbSize validation and console_window passes SWP_NOSIZE after subclassing WM_GETMINMAXINFO.

Activity

  1. added a commit that references this issue on Nov 23, 2026
  2. ivall commented on Nov 23, 2026

    @ivall
    Collaborator

    Thanks. Triage against current main:

    • Config::load_or_create overwriting xcl.toml on any read error – confirmed, fixed in ea4bd62 (only a missing file gets the defaults; an unreadable one is an error).
    • S:\ free space – true that it reports the host volume; titles compare it against what they need, so a larger answer is harmless. Low priority.
    • AVX2 / Zen 2 check – a clear startup error would be nice; small PR welcome.
    • TLS / EH / loader edge cases – plausible; each needs a focused PR with a test (the TLS ones are delicate – every change there should run the console DLL e2e tests and a game).
    • Hybrid CPU affinity – the title sees the PC's processors by design (user decision in HANDOFF); masks for > available cores are worth clamping if a title is seen failing on them.
  3. ivall commented on Nov 23, 2026

    @ivall
    Collaborator

    Progress: #141 (a DLL that failed to load is not left registered), #148 (title_memory in any case), #183 (TLS without an index variable) and #147 (crash report below a module's base) are merged; #196 (TLS Characteristics alignment / attach contention) and #197 (system-unwind across guest re-entry) are open.

  4. yous0serious commented on Nov 23, 2026

    @yous0serious
    Contributor

    The hybrid CPU affinity item: #221 clamps SetThreadAffinityMask / SetThreadGroupAffinity masks that name processors outside the process's affinity (the case triaged above as worth clamping). Masks made from the PC's processors pass unchanged, so the title still sees the PC's processors.

    Measured on an i7-13700KF: Windows fails such a mask with ERROR_INVALID_PARAMETER when none of its processors exists (0xFF000000 on 24 processors), or when any is outside a limited process mask (0x7F / 0x3F / 0x40 under start /affinity F). The E-core concern above does not hold there: Windows numbers the P-core threads first (0–15), so console masks (bits 0–13) land on P-cores.

    The other items of this issue are not touched by #221.

  5. mariuslozo-boop commented on Nov 23, 2026

    @mariuslozo-boop

    A crash in eh::dump_threads (XCL_THREAD_DUMP_AT), adding to the dump_threads item here.

    On main d4f17ca with XCL_THREAD_DUMP_AT=90,180,270 and a retail title running (RTX 5070 Ti, Windows 11 26300), the dumps at 90 s and 180 s were fine. At 270.052 s, the dump thread itself crashed and took the process down:

    unhandled exception 0xc0000005 in ntdll.dll+0x1ff2a (read at address 0xf8) rax=0x0 rbx=0x3 rcx=0x0 … r8=0xf8 … ; on the stack: xcl.exe+0x5b1e00, xcl.exe+0x268e1e, xcl.exe+0x630308, xcl.exe+0xdac7a
    
    • ntdll+0x1ff2a is inside RtlVirtualUnwind2: mov rcx, qword ptr [r8] with r8 = 0xf8. While walking a suspended thread's stack, the unwinder dereferences a near-null context pointer.
    • The likely cause is a frame that eh::unwind starts from a context it should not trust, or a frame without a valid function entry. The read happens inside RtlVirtualUnwind, so no check in unwind after the call can catch it.
    • So far it has happened once out of three dumps in that run. I have not reproduced it yet.

    AI-assisted (Claude Code). No game files attached.

  6. notquiteog commented on Nov 24, 2026

    @notquiteog

    A field occurrence that fits the dump_threads() deadlock in section 2, on main 9e3831e (release, cargo build --release --workspace), Windows 11 26100, RTX 5060 Ti, XCL_NETWORK=0, retail title 1.0.2.1.

    The title takes its usual watchdog exit (+0x23cf683, #846) at 104.8 s. xcl.log ends on that line:

    [  104.828][13436][ERROR] unhandled exception 0xc0000005 in game_scarlett_master.exe+0x23cf683 (write at address 0x0) rax=0x0 rbx=0x0 rcx=0x70024c348 rdx=0x1 ...
    

    Nothing follows it: no on the stack: list, no process ends with status … line, no thread dump, and no crash-reporter lines. The process stayed alive for another 8+ minutes until my harness killed it at its 10-minute cap.

    For comparison, on the same machine and title the same exit was handled normally three times: twice on 077ce40 and once on the theaustinayers fork at 76d3b9f. Each time the log has the stack, a full per-thread dump and process ends with status 0xc0000005 within about 1 s, and the process exits. So it's intermittent, as a lock held by a suspended thread would be. eh.rs did not change between 077ce40 and 9e3831e.

    I don't have a dump of the hung process, so this is consistent with the suspended-thread heap/loader-lock deadlock described above rather than proof of it. One practical effect: a run that should end in ~1 s after the title's fatal exit instead hangs indefinitely, so unattended test runs need their own timeout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions