FFI: use extern "system-unwind" across guest-reentry boundaries (#106) #197
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!197
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/197/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@JGonzalezRodriguez on GitHub · 2026-11-23 20:20 UTC · original
JGonzalezRodriguez:fix/ffi-unwind-boundaries→main· +1145 −186 · 14 files · 3 commits · closed without mergingPart of #106. Converts guest-callback function pointer types and re-entrant HLE entrypoints/trampolines/hooks to
extern "system-unwind"across 14 files and adds RAII cleanup guards so guest exceptions unwinding through Rust frames do not abort or leave locks/state wedged.What is in it
hle/kernelx.rs:XMemAllocFn/XMemFreeFn,XMemAlloc,XMemFree,LoadLibrary*,LoadPackagedLibrary,GetProcAddress. (hle/d3d12x/real.rsintentionally left untouched.)hle/user.rs:console_wndproc,CreateWindowExA/create_window_ex_w, andCallWindowProcW/SetWindowPos/CreateWindowExWimports.loader.rs&main.rs:TlsCallback,DllMain,ExeEntry,main_thread,xcl_tls_callback.eh.rs:unhandled_filterandTITLE_FILTERinvocation (VEH/terminate/gs_failure handlers stayextern "system"/"C").vfs_hooks.rs:MoveFileWithProgressA/W,CopyFileExW,CopyFile2hooks & orig types soGuard::dropresetsIN_HOOKon progress-callback unwind.hle/gameinput.rs:ReadingCallback,DeviceCallback,register_device_callback, plus RAIIReadingGuard/EntryCleanup.hle/xaudio2.rs:VoiceCallbackVtbl(all 7 methods) anddecoded_cb_*trampolines.hle/xgameruntime/:taskqueue.rs(callbacks/entrypoints +InflightGuard,CancelInflight,RunningGuard, dual-portRemaining;waiter_firedstays plainextern "system"forRegisterWaitForSingleObject),xasync.rs(callbacks/entrypoints +BeginCleanup,CompleteCleanup,ConsumeCleanup,NotifyGuard,State._owned_payload),api.rs,gamesave.rs,mod.rs,networking.rs.Tested
610.57.4,rustc 1.98.1x86_64-pc-windows-msvcviaclang-cl 23.1.1+lld-link+xwinSDK10.0.26100, Proton Experimental + local relaxedvkd3d-proton):cargo build --release --workspace --target x86_64-pc-windows-msvc: 0 errors, 0 warnings.cargo test --no-run --package xcl --lib --target x86_64-pc-windows-msvc: 0 errors, 0 warnings.unwind): 13 passed, 0 failed acrosseh,hle::gameinput,hle::kernelx,hle::user(throughuser32!CallWindowProcW),hle::xaudio2,hle::xgameruntime::{api,taskqueue,xasync}, andloader.hle::kernelx:: hle::gameinput:: hle::user:: loader:: hle::xgameruntime:: eh:: hle::xaudio2:: vfs_hooks::): 139 passed, 1 failed (pre-existing Wine/FAudio-onlyxma2_source_voice_decodes_and_manages_lifetime).xcl_testtitle.exe: 47/50 passed (all CPU/TLS/exceptions/XGameRuntime/D3D12.X checks pass; 3 known Wine/FAudio diffs).202bf56):@Kalmarvbuilt and passed all 10 initial unwind tests on Windows 11 Pro 26200, x64 MSVC,rustc 1.99.0(and flaggedXAsyncComplete/XAsyncGetResult/waiter_fired, all addressed ina82fbfb).Regression run
scripts/smoke.ps1run by a tester with a retail title.Not tested
scripts/smoke.ps1regression run; full native Windowsscripts/test.ps1on heada82fbfb.AI use
@Kalmarv on GitHub · 2026-11-23 20:38 UTC · original
One unwind path still leaves XAsync pending indefinitely. In
XAsyncComplete, the state becomesCompletingbefore the zero-result call toOP_CLEANUP. If that provider unwinds, control leaves before the terminal phase is published and beforeNotifyGuardexists. A waiter inXAsyncGetStatus(b, true)can remain asleep, and another completion returns early because the phase already changed.XAsyncGetResulthas a related transition toConsumingbefore invoking the provider. Please define the post-unwind status for those paths and guard the state transition as well as the notification. This finding is from code inspection.I built PR head
202bf56f1fdba9ab569fe2f6bbacdfdf308efa4edirectly on native Windows 11 Pro build 26200, x64 MSVC, rustc 1.99.0. All 10 new unwind tests passed, each in its own process. With the same ten tests carried onto base956a476bf738a098f39df4f4c8acef682e43030f, the test binary failed to build as expected. Rust rejected two GameInput callback assignments and seven XAudio2 vtable assignments because the base requires plainsystemcallbacks. That control has no runtime count.The head tests establish Rust panic propagation and cleanup through the exercised call paths. Foreign C++ exceptions and SEH from a title still need their own verification. In particular, casting
waiter_firedto the callback type accepted byRegisterWaitForSingleObjectpreserves the Win64 call layout but does not establish that an exception can leave a Windows thread-pool callback safely. Rust FFI unwinding rules@JGonzalezRodriguez on GitHub · 2026-11-23 20:58 UTC · original
Thanks @Kalmarv for testing on native Windows 11 and for the detailed code review! Both points are addressed in the latest commits (
4c6ffb4,1712e46):XAsyncComplete(Completing) andXAsyncGetResult(Consuming) unwind paths:CompleteCleanupinXAsyncCompletearound the zero-resultOP_CLEANUPcall: if the provider unwinds, it writesBlockHeader { status, flags: FLAG_DONE, id }, removes the block fromACTIVE, transitionsphasetoPhase::Consumed, and notifiess.cvso waiters inXAsyncGetStatus(b, true)wake up withstatusinstead of sleeping indefinitely.ConsumeCleanupinXAsyncGetResultaroundOP_GET_RESULT+OP_CLEANUP: if either unwinds, it writesBlockHeader { status, flags: FLAG_DONE | FLAG_CONSUMED, id }, removes the block fromACTIVE, and setsPhase::Consumedwithout re-entering guestOP_CLEANUPduring unwind (subsequentXAsyncGetResultreturnsE_ILLEGAL_METHOD_CALL).BeginCleanup(only marksE_ABORT/ removes if stillPhase::Pending, leaving synchronously completed non-empty ops intact), tiedbegin_with_fixupsPayloadlifetime toArc<State>on the XCL path (eliminating concurrent-worker unwind UAFs), and added regression tests covering these paths (13/13 unwind tests passing).waiter_fired/RegisterWaitForSingleObject:waiter_firedback to plainunsafe extern "system" fnand removed the callback cast, since it is a Windows thread-pool callback that only enqueues onto aTaskQueueport without invoking guest code.hle/d3d12x/real.rsto stay orthogonal to active D3D12 PRs, guardedXTaskQueueTerminateacross bothPORT_WORKandPORT_COMPLETION, and noted in the PR description that unit tests verify Rust panic unwinding + cleanup while foreign C++/SEH unwinding from a title still needs native verification.AI-assisted.
@ersinhf on GitHub · 2026-11-23 23:27 UTC · original
Native Windows
scripts/test.ps1on heada82fbfbef5e7363232040026237081907ea34a7a(the item under Not tested), clean tree. It merges onto currentmain4017f8fwithout conflicts (git merge-tree).Tested
stable-x86_64-pc-windows-msvc, debug; DXC 1.9.2602.17 (SDK 10.0.26100.0); XGameRuntime 10.0.26100.9656; noXCL_SAMPLE_*set.\scripts\test.ps1: 664 passed, 0 failed, 40 ignored (main4017f8fwithtest.ps1here: 652 / 0 / 40; this head is based on60565c4);SKIPPED2 (no wave64):gpu_geometry_wave32_prefix_count_over_a_mask,gpu_wave32_program_in_wave64_hardwareunhandled_filter_unwinds_cleanly_from_title_filter,xmem_allocation_hooks_unwind_cleanly,init_dll_and_tls_callback_unwind_cleanly, thexasync/taskqueue/gameinput/xaudio2/useronescargo test -p xcl --lib -- --test-threads=4three more times on this head: 624 passed, 0 failed, 20 ignored each timeA hang that is not this PR's
My first
test.ps1run of this head stopped in the lib tests: four GPU tests (gpu_typed_buffer_loads_and_stores,gpu_uma_import_check_at_init,gpu_valu_16_and_64_bit,gpu_valu_float_and_int) ran past 60 s with the process at 0 % CPU and every thread waiting, and it had to be killed. Running the lib tests alternately on this head and onmain4017f8f(three times each),mainhung the same way once (gpu_typed_buffer_loads_and_stores,gpu_uma_import_check_at_init,gpu_valu_16_and_64_bit,gpu_wave32_and_wave64_src_vccz_execz) and this head did not. So it is an intermittent hang already onmain, not caused by this PR.Not tested
scripts/smoke.ps1(no retail title here)AI use
@raylitebull on GitHub · 2026-11-24 10:43 UTC · original
Closing as a stale, conflicting ~1,300-line PR failing the
PR descriptioncheck. Feel free to reopen smaller single-purpose PRs rebased on currentmainperCONTRIBUTING.md.Pull request closed