From d5fbf4a46db73112f54d9bc38f6a2420d6787de2 Mon Sep 17 00:00:00 2001 From: Utkarsh Dalal Date: Sun, 15 Nov 2026 13:00:21 +0530 Subject: [PATCH 1/3] series: stale NoExec retry deadlock and next-page fetch fault in 0001 (gn-7) Folds two later Dmage22/FEX-D2 d2r/clean fixes for Battle.net launchers into 0001. 2d8b53d393: SynchronousExceptionHandler now sets InSyscallCallback around the stale NoExec retry and clears it before re-entering the dispatcher or raising the guest exception. The fault-free fault stub reaches the handler with the flag clear, so the lookup cache decommit in the invalidation was reported to NotifyMemoryFree, which took the code invalidation mutex a second time on the same thread and hung the process. His CPUFeatures.cpp hunk is not needed: the series never turned the fault-free path off. ba05439d66: HandleGuestException gets its next-page probe back. A full NoExec fault carries the instruction start, so the decoder's address is used only when it is non-zero and differs from RIP; otherwise an instruction at the tail of an already unlocked page reported that page again and D2R_loader's handler faulted forever. Patches 0002-0012 only change in index hashes and line offsets. --- gamenative/README.md | 4 +- gamenative/SERIES_VERSION | 2 +- ...s-for-protected-launchers-that-decry.patch | 88 ++++++++++++++----- ...-tamper-stepping-and-fault-delivery-.patch | 6 +- ...rt-btcpu64notifynonimageviews-so-ntd.patch | 4 +- 5 files changed, 75 insertions(+), 29 deletions(-) diff --git a/gamenative/README.md b/gamenative/README.md index 3a40861ad8..ada2b777ad 100644 --- a/gamenative/README.md +++ b/gamenative/README.md @@ -21,7 +21,7 @@ behaviour that is not a plain correctness fix is behind an env var. | Patch | From | What | Upstream | |---|---|---|---| -| 0001 windows-fixes-for-protected-launchers | Dmage22/FEX-D2 `d2r/clean` (11 commits squashed) | Non-image section views, OS query for unknown executable pages, fault raise with `hlt` and the right exception code, stale NoExec retry (on the host exception path and in the FEX-2611 synchronous fault handler), PF/AF through context conversion (needs the ntdll export), disk-cache extents, compiler restart at inaccessible pages, `0x67` MOVS/STOS and FS/GS decode | analysis in FEX-Emu/FEX#5328; no PR yet | +| 0001 windows-fixes-for-protected-launchers | Dmage22/FEX-D2 `d2r/clean` (11 commits squashed, plus 2d8b53d393 and ba05439d66) | Non-image section views, OS query for unknown executable pages, fault raise with `hlt` and the right exception code, stale NoExec retry (on the host exception path and in the FEX-2611 synchronous fault handler, with `InSyscallCallback` set so the invalidation cannot re-enter the invalidation lock through `NotifyMemoryFree`), execute faults at the tail of an executable page reported on the next page unless the decoder has a partial-fetch address, PF/AF through context conversion (needs the ntdll export), disk-cache extents, compiler restart at inaccessible pages, `0x67` MOVS/STOS and FS/GS decode | analysis in FEX-Emu/FEX#5328; no PR yet | | 0002 windows-anti-tamper-stepping-and-fault-delivery | tarikbc 0004, 0006, 0009, 0012, 0013, 0014, 0017 (v1.6.0, f4a686a0) squashed | Guest-visible protection of trapped pages, raw syscall return registers, volatile code regions, 512 MB code buffer, translation reuse, guest exceptions delivered straight into `KiUserExceptionDispatcher` on the guest stack (fast raise). Env gates: `FEX_EXP_SKIP_CALLRET_RESET=1` opts in; `FEX_EXP_FASTRAISE=0` opts out. The trapless fault path of tarikbc 0017 (`FEX_EXP_FASTTRAP`) is upstream since FEX-2611 and was dropped here. Volatile code regions were on by default here; 0004 makes them opt-in | FEX-Emu/FEX#6022, #6023; Wine bug 60461 | | 0003 arm64ec-export-btcpu64notifynonimageviews | GameNative | Exports `BTCpu64NotifyNonImageViews` so ntdll only reports non-image views to a FEX that can take them | pairs with GameNative/proton-wine `dlls_arm64ec_emulator_notifications.patch` | | 0004 windows-make-volatile-code-regions-opt-in | GameNative | Volatile code region detection (from 0003) runs only with `FEX_EXP_VOLATILE=1`; any other value or unset is off. On by default it misfires on JIT runtimes: TMNT: Shredder's Revenge (FNA/.NET) recompiles on its main thread, runs about 10x slower and never creates a D3D device. Age of Empires IV needs it and sets it in its overlay. Env gate: `FEX_EXP_VOLATILE=1` opts in | GameNative only | @@ -37,7 +37,7 @@ behaviour that is not a plain correctness fix is behind an env var. Left out of 0002 on purpose: tarikbc 0002 (hides the emulator from the guest), 0007 (rewrites ntdll export bytes to pass one game's hook check), 0008 (debug tool), 0010 (obsolete on Proton 11.0-2), 0016 (hooks `powrprof` from the exit thunk; the fix belongs in Wine, bug 60462). -Left out of 0001: dmage22's log-level change (cosmetic). +Left out of 0001: dmage22's log-level change (cosmetic), the entry-block NoExec log cap (238fe0a6b9), and ba8346d046 (superseded by 2d8b53d393). Patch 0002 was rebased onto 0001 by hand: the volatile-code and translation-reuse hooks in `Core.cpp` sit after 0001's compile restart point, and the fast raise replaces the final diff --git a/gamenative/SERIES_VERSION b/gamenative/SERIES_VERSION index 1e8b314962..7f8f011eb7 100644 --- a/gamenative/SERIES_VERSION +++ b/gamenative/SERIES_VERSION @@ -1 +1 @@ -6 +7 diff --git a/gamenative/patches/0001-windows-fixes-for-protected-launchers-that-decry.patch b/gamenative/patches/0001-windows-fixes-for-protected-launchers-that-decry.patch index e8c2b9980d..8119a2fa3d 100644 --- a/gamenative/patches/0001-windows-fixes-for-protected-launchers-that-decry.patch +++ b/gamenative/patches/0001-windows-fixes-for-protected-launchers-that-decry.patch @@ -15,23 +15,33 @@ compiler at inaccessible pages instead of deadlocking on the invalidation lock, Upstream: analysis in FEX-Emu/FEX#5328; no PR yet. Rebased onto FEX-2611: the partial-fault address now comes from upstream (511f58356, the Break -op carries the first byte that could not be fetched), so the VirtualQuery heuristic in -HandleGuestException is gone. FEX-2611 also delivers JIT-raised guest faults on arm64ec straight -to SynchronousExceptionHandler (27cfd41bf, 07abec317), so the stale NoExec retry runs there too -(DropStaleNoExecFault + EnterDispatcherFillSRA), not only on the host exception path. +op carries the first byte that could not be fetched). FEX-2611 also delivers JIT-raised guest +faults on arm64ec straight to SynchronousExceptionHandler (27cfd41bf, 07abec317), so the stale +NoExec retry runs there too (DropStaleNoExecFault + EnterDispatcherFillSRA), not only on the +host exception path. + +Later d2r/clean fixes for Battle.net launchers: +- SynchronousExceptionHandler sets InSyscallCallback around the stale NoExec retry and clears it + before re-entering the dispatcher or raising. The fault stub reaches it with the flag clear, so + the lookup cache decommit in the invalidation went to NotifyMemoryFree, which took the code + invalidation mutex a second time on the same thread and hung the process (2d8b53d393). +- A full NoExec fault reports the next page again when the instruction's own page is executable; + the decoder's address is used only for a partial decode (non-zero and not the instruction + start). Reporting the instruction start made D2R_loader's handler fault forever on a page it had + already unlocked (ba05439d66). --- FEXCore/Source/Interface/Core/Core.cpp | 34 +++ FEXCore/Source/Interface/Core/DiskCache.cpp | 10 +- .../Interface/Core/Dispatcher/Dispatcher.cpp | 9 + FEXCore/Source/Interface/Core/Frontend.cpp | 5 + - .../Interface/Core/OpcodeDispatcher.cpp | 72 ++++--- + .../Interface/Core/OpcodeDispatcher.cpp | 72 +++--- .../Source/Interface/Core/OpcodeDispatcher.h | 2 + .../FEXCore/Debug/InternalThreadState.h | 13 ++ Source/Windows/ARM64EC/Module.S | 9 + - Source/Windows/ARM64EC/Module.cpp | 204 +++++++++++++++++- - Source/Windows/Common/Exception.h | 8 +- + Source/Windows/ARM64EC/Module.cpp | 211 +++++++++++++++++- + Source/Windows/Common/Exception.h | 31 ++- Source/Windows/Common/InvalidationTracker.cpp | 119 +++++++++- - 11 files changed, 439 insertions(+), 46 deletions(-) + 11 files changed, 468 insertions(+), 47 deletions(-) diff --git a/FEXCore/Source/Interface/Core/Core.cpp b/FEXCore/Source/Interface/Core/Core.cpp index 2ba56b69a..53d81a872 100644 @@ -369,7 +379,7 @@ index ce36c1d68..4d9fd8b7e 100644 + mov x11, #0 // ENTRY_FILL_SRA_SINGLE_INST_REG: normal block + br x1 diff --git a/Source/Windows/ARM64EC/Module.cpp b/Source/Windows/ARM64EC/Module.cpp -index 3be7e465a..4b8149bf3 100644 +index 3be7e465a..2af511082 100644 --- a/Source/Windows/ARM64EC/Module.cpp +++ b/Source/Windows/ARM64EC/Module.cpp @@ -50,6 +50,7 @@ $end_info$ @@ -455,7 +465,7 @@ index 3be7e465a..4b8149bf3 100644 ECContext.Fpcr = FPCR; ECContext.Fpsr = FPSR; -@@ -657,12 +688,125 @@ static void RethrowGuestException(FEXCore::Core::InternalThreadState* Thread, co +@@ -657,12 +688,132 @@ static void RethrowGuestException(FEXCore::Core::InternalThreadState* Thread, co RaiseException(Thread, Rec, GuestContext); } @@ -573,15 +583,22 @@ index 3be7e465a..4b8149bf3 100644 auto& Fault = Thread->CurrentFrame->SynchronousFaultData; + // Stale NoExec faults never reach the host exception handler on this path, so drop them here and resume at RIP. -+ if (DropStaleNoExecFault(Thread)) { -+ const auto CPUArea = GetCPUArea(); ++ // The fault stub branches here from JIT code with InSyscallCallback clear. Set it like the dispatcher's guarded ++ // calls and the host exception path do: the invalidation decommits lookup cache pages with the code invalidation ++ // mutex held, and without the flag ntdll reports that to NotifyMemoryFree, which takes the mutex again and hangs. ++ // Clear it again before either exit, since both run guest-visible code: the JIT, or the guest exception dispatch. ++ const auto CPUArea = GetCPUArea(); ++ CPUArea.Area->InSyscallCallback = true; ++ const bool StaleNoExec = DropStaleNoExecFault(Thread); ++ CPUArea.Area->InSyscallCallback = false; ++ if (StaleNoExec) { + EnterDispatcherFillSRA(CPUArea.Area, CPUArea.DispatcherLoopTopEnterECFillSRA(), CPUArea.EmulatorStackBase()); + } + uint64_t FPCR, FPSR; __asm volatile("mrs %[fpcr], fpcr" : [fpcr] "=r"(FPCR)); __asm volatile("mrs %[fpsr], fpsr" : [fpsr] "=r"(FPSR)); -@@ -730,7 +874,24 @@ public: +@@ -730,7 +881,24 @@ public: } FEXCore::HLE::ExecutableRangeInfo QueryGuestExecutableRange(FEXCore::Core::InternalThreadState* Thread, uint64_t Address) override { @@ -607,7 +624,7 @@ index 3be7e465a..4b8149bf3 100644 } void PreCompile() override { -@@ -744,9 +905,13 @@ extern "C" void SyncThreadContext(CONTEXT* Context) { +@@ -744,9 +912,13 @@ extern "C" void SyncThreadContext(CONTEXT* Context) { auto* Thread = GetCPUArea().ThreadState(); // All other EFlags bits are lost when converting to/from an ARM64EC context, so merge them in from the current JIT state. // This is advisable over dropping their values as thread suspend/resume uses this function, and that can happen at any point in guest code. @@ -624,7 +641,7 @@ index 3be7e465a..4b8149bf3 100644 uint32_t StateEFlags = CTX->ReconstructCompactedEFLAGS(Thread, false, nullptr, 0); Context->EFlags = (Context->EFlags & ECValidEFlagsMask) | (StateEFlags & ~ECValidEFlagsMask); -@@ -777,6 +942,7 @@ NTSTATUS ProcessInit() { +@@ -777,6 +949,7 @@ NTSTATUS ProcessInit() { const auto NtDll = GetModuleHandleW(L"ntdll.dll"); const bool IsWine = !!GetProcAddress(NtDll, "wine_get_version"); @@ -632,7 +649,7 @@ index 3be7e465a..4b8149bf3 100644 OvercommitTracker.emplace(IsWine); FEX::Windows::SetupEnvironmentVariableValues(NtDll); -@@ -876,6 +1042,13 @@ bool ResetToConsistentStateImpl(const ThreadCPUArea CPUArea, EXCEPTION_RECORD* E +@@ -876,6 +1049,13 @@ bool ResetToConsistentStateImpl(const ThreadCPUArea CPUArea, EXCEPTION_RECORD* E return true; } @@ -646,7 +663,7 @@ index 3be7e465a..4b8149bf3 100644 std::scoped_lock Lock(ThreadCreationMutex); if (InvalidationTracker && InvalidationTracker->HandleRWXAccessViolation(Thread, NativeContext->Pc, FaultAddress)) { FEXCORE_PROFILE_INSTANT_INCREMENT(Thread, AccumulatedSMCCount, 1); -@@ -919,6 +1092,10 @@ bool ResetToConsistentStateImpl(const ThreadCPUArea CPUArea, EXCEPTION_RECORD* E +@@ -919,6 +1099,10 @@ bool ResetToConsistentStateImpl(const ThreadCPUArea CPUArea, EXCEPTION_RECORD* E return true; } @@ -657,7 +674,7 @@ index 3be7e465a..4b8149bf3 100644 if (IsEmulatorStackAddress(CPUArea, reinterpret_cast(__builtin_frame_address(0)))) { Exception::RethrowGuestException(Thread, *Exception, *NativeContext); LogMan::Msg::DFmt("Rethrowing onto guest stack: {:X}", NativeContext->Sp); -@@ -1081,7 +1258,18 @@ NTSTATUS NotifyMapViewOfSection(void* Unk1, void* Address, void* Unk2, SIZE_T Si +@@ -1081,7 +1265,18 @@ NTSTATUS NotifyMapViewOfSection(void* Unk1, void* Address, void* Unk2, SIZE_T Si { std::scoped_lock Lock(ThreadCreationMutex); @@ -678,10 +695,18 @@ index 3be7e465a..4b8149bf3 100644 diff --git a/Source/Windows/Common/Exception.h b/Source/Windows/Common/Exception.h -index b41a5e64a..83814521d 100644 +index b41a5e64a..3ad78b7e2 100644 --- a/Source/Windows/Common/Exception.h +++ b/Source/Windows/Common/Exception.h -@@ -74,12 +74,16 @@ static inline EXCEPTION_RECORD HandleGuestException(FEXCore::Core::CpuStateFrame +@@ -6,6 +6,7 @@ + #include + + #include ++#include + #include + + namespace FEX::Windows { +@@ -74,12 +75,36 @@ static inline EXCEPTION_RECORD HandleGuestException(FEXCore::Core::CpuStateFrame } break; case FEXCore::X86State::X86_TRAPNO_OF: Dst.ExceptionCode = EXCEPTION_INT_OVERFLOW; return Dst; @@ -689,12 +714,33 @@ index b41a5e64a..83814521d 100644 - // A page-fault raised by an explicit break in JIT code is always an execute fault + case FEXCore::X86State::X86_TRAPNO_PF: { + // A page-fault raised by an explicit break in JIT code is always an execute fault. ++ // ++ // The reported address has to be the first byte that could not be fetched, which is not always ++ // the start of the instruction: an instruction sitting at the tail of an executable page and ++ // running into the next one faults on that next page. Reporting the instruction start instead ++ // makes decrypt-on-demand protectors spin forever - their handler looks at ++ // ExceptionInformation[1], sees a page it has already unlocked, decides there is nothing to do, ++ // resumes, and faults identically. An x86 instruction is at most 15 bytes, so it can only ever ++ // cross a single page boundary. ++ uint64_t FaultAddress = Rip; ++ const uint64_t RipPage = Rip & FEXCore::Utils::FEX_PAGE_MASK; ++ constexpr ULONG ExecProt = PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY; ++ MEMORY_BASIC_INFORMATION Info; ++ if (VirtualQuery(reinterpret_cast(RipPage), &Info, sizeof(Info)) && Info.State == MEM_COMMIT && ++ (Info.Protect & ExecProt)) { ++ FaultAddress = RipPage + FEXCore::Utils::FEX_PAGE_SIZE; ++ } ++ + // Set the code explicitly like every other case: the host trap that got us here is not necessarily an + // access violation (on Windows the SIGSEGV trampoline raises it with hlt). + Dst.ExceptionCode = EXCEPTION_ACCESS_VIOLATION; Dst.NumberParameters = 2; Dst.ExceptionInformation[0] = EXCEPTION_EXECUTE_FAULT; - Dst.ExceptionInformation[1] = Fault.FaultAddress ? Fault.FaultAddress : Rip; +- Dst.ExceptionInformation[1] = Fault.FaultAddress ? Fault.FaultAddress : Rip; ++ // A partial decode carries the exact first unfetchable byte, which is inside the instruction. A full NoExec ++ // carries the instruction start, which can sit on a page the protector has already unlocked while the bytes ++ // it is missing are on the next one; keep the page probe above for that case. ++ Dst.ExceptionInformation[1] = (Fault.FaultAddress && Fault.FaultAddress != Rip) ? Fault.FaultAddress : FaultAddress; return Dst; + } default: LogMan::Msg::EFmt("Unknown SIGSEGV trap: {}", Fault.TrapNo); break; diff --git a/gamenative/patches/0002-windows-anti-tamper-stepping-and-fault-delivery-.patch b/gamenative/patches/0002-windows-anti-tamper-stepping-and-fault-delivery-.patch index c2cfa7d6c3..c541984fcf 100644 --- a/gamenative/patches/0002-windows-anti-tamper-stepping-and-fault-delivery-.patch +++ b/gamenative/patches/0002-windows-anti-tamper-stepping-and-fault-delivery-.patch @@ -624,7 +624,7 @@ index 4d9fd8b7e..0ceed511b 100644 // A replacement for the standard ARM64EC call checker that ignores any FFS patches and always redirects to a function's // native implementation. As the only library FEX calls into is NTDLL, this is done using a LUT generated at init time. diff --git a/Source/Windows/ARM64EC/Module.cpp b/Source/Windows/ARM64EC/Module.cpp -index 4b8149bf3..8914f2c35 100644 +index 2af511082..b7b236750 100644 --- a/Source/Windows/ARM64EC/Module.cpp +++ b/Source/Windows/ARM64EC/Module.cpp @@ -91,8 +91,27 @@ uint64_t WineNtContinueSyscallId; @@ -719,7 +719,7 @@ index 4b8149bf3..8914f2c35 100644 } static void RethrowGuestException(FEXCore::Core::InternalThreadState* Thread, const EXCEPTION_RECORD& Rec, ARM64_NT_CONTEXT& HostContext) { -@@ -900,6 +953,197 @@ public: +@@ -907,6 +960,197 @@ public: }; } // namespace Exception @@ -917,7 +917,7 @@ index 4b8149bf3..8914f2c35 100644 extern "C" void SyncThreadContext(CONTEXT* Context) { ProcessPendingCrossProcessEmulatorWork(); auto* Thread = GetCPUArea().ThreadState(); -@@ -978,6 +1222,30 @@ NTSTATUS ProcessInit() { +@@ -985,6 +1229,30 @@ NTSTATUS ProcessInit() { PAGE_EXECUTE_READ); *reinterpret_cast(X64ReturnInstr) = 0xc3; diff --git a/gamenative/patches/0003-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch b/gamenative/patches/0003-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch index 59d2bc48df..e57ce9c8a5 100644 --- a/gamenative/patches/0003-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch +++ b/gamenative/patches/0003-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch @@ -26,10 +26,10 @@ index d57603079..67f351139 100644 void STDMETHODCALLTYPE FlushInstructionCacheHeavy(const void* Address, SIZE_T Size); void STDMETHODCALLTYPE BTCpu64FlushInstructionCache(const void* Address, SIZE_T Size); diff --git a/Source/Windows/ARM64EC/Module.cpp b/Source/Windows/ARM64EC/Module.cpp -index 8914f2c35..1fcea15d2 100644 +index b7b236750..f604db1a6 100644 --- a/Source/Windows/ARM64EC/Module.cpp +++ b/Source/Windows/ARM64EC/Module.cpp -@@ -1519,6 +1519,10 @@ void NotifyMemoryProtect(void* Address, SIZE_T Size, ULONG NewProt, BOOL After, +@@ -1526,6 +1526,10 @@ void NotifyMemoryProtect(void* Address, SIZE_T Size, ULONG NewProt, BOOL After, } } From 3921570624d19a6d1fbfe61e998bcf8e8921a93b Mon Sep 17 00:00:00 2001 From: Utkarsh Dalal Date: Sun, 15 Nov 2026 13:16:50 +0530 Subject: [PATCH 2/3] readme: the next-page report applies to any executable page --- gamenative/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gamenative/README.md b/gamenative/README.md index ada2b777ad..497ad810f3 100644 --- a/gamenative/README.md +++ b/gamenative/README.md @@ -21,7 +21,7 @@ behaviour that is not a plain correctness fix is behind an env var. | Patch | From | What | Upstream | |---|---|---|---| -| 0001 windows-fixes-for-protected-launchers | Dmage22/FEX-D2 `d2r/clean` (11 commits squashed, plus 2d8b53d393 and ba05439d66) | Non-image section views, OS query for unknown executable pages, fault raise with `hlt` and the right exception code, stale NoExec retry (on the host exception path and in the FEX-2611 synchronous fault handler, with `InSyscallCallback` set so the invalidation cannot re-enter the invalidation lock through `NotifyMemoryFree`), execute faults at the tail of an executable page reported on the next page unless the decoder has a partial-fetch address, PF/AF through context conversion (needs the ntdll export), disk-cache extents, compiler restart at inaccessible pages, `0x67` MOVS/STOS and FS/GS decode | analysis in FEX-Emu/FEX#5328; no PR yet | +| 0001 windows-fixes-for-protected-launchers | Dmage22/FEX-D2 `d2r/clean` (11 commits squashed, plus 2d8b53d393 and ba05439d66) | Non-image section views, OS query for unknown executable pages, fault raise with `hlt` and the right exception code, stale NoExec retry (on the host exception path and in the FEX-2611 synchronous fault handler, with `InSyscallCallback` set so the invalidation cannot re-enter the invalidation lock through `NotifyMemoryFree`), execute faults whose instruction starts on an executable page reported on the next page unless the decoder has a partial-fetch address, PF/AF through context conversion (needs the ntdll export), disk-cache extents, compiler restart at inaccessible pages, `0x67` MOVS/STOS and FS/GS decode | analysis in FEX-Emu/FEX#5328; no PR yet | | 0002 windows-anti-tamper-stepping-and-fault-delivery | tarikbc 0004, 0006, 0009, 0012, 0013, 0014, 0017 (v1.6.0, f4a686a0) squashed | Guest-visible protection of trapped pages, raw syscall return registers, volatile code regions, 512 MB code buffer, translation reuse, guest exceptions delivered straight into `KiUserExceptionDispatcher` on the guest stack (fast raise). Env gates: `FEX_EXP_SKIP_CALLRET_RESET=1` opts in; `FEX_EXP_FASTRAISE=0` opts out. The trapless fault path of tarikbc 0017 (`FEX_EXP_FASTTRAP`) is upstream since FEX-2611 and was dropped here. Volatile code regions were on by default here; 0004 makes them opt-in | FEX-Emu/FEX#6022, #6023; Wine bug 60461 | | 0003 arm64ec-export-btcpu64notifynonimageviews | GameNative | Exports `BTCpu64NotifyNonImageViews` so ntdll only reports non-image views to a FEX that can take them | pairs with GameNative/proton-wine `dlls_arm64ec_emulator_notifications.patch` | | 0004 windows-make-volatile-code-regions-opt-in | GameNative | Volatile code region detection (from 0003) runs only with `FEX_EXP_VOLATILE=1`; any other value or unset is off. On by default it misfires on JIT runtimes: TMNT: Shredder's Revenge (FNA/.NET) recompiles on its main thread, runs about 10x slower and never creates a D3D device. Age of Empires IV needs it and sets it in its overlay. Env gate: `FEX_EXP_VOLATILE=1` opts in | GameNative only | From 44256943afe4268b27664520b49bf422e0b3977d Mon Sep 17 00:00:00 2001 From: Utkarsh Dalal Date: Sun, 15 Nov 2026 15:40:42 +0530 Subject: [PATCH 3/3] series: keep 0001 to the stale NoExec deadlock fix, drop the next-page probe --- gamenative/README.md | 4 +- ...s-for-protected-launchers-that-decry.patch | 61 +++++-------------- 2 files changed, 16 insertions(+), 49 deletions(-) diff --git a/gamenative/README.md b/gamenative/README.md index 497ad810f3..52bd99c6c9 100644 --- a/gamenative/README.md +++ b/gamenative/README.md @@ -21,7 +21,7 @@ behaviour that is not a plain correctness fix is behind an env var. | Patch | From | What | Upstream | |---|---|---|---| -| 0001 windows-fixes-for-protected-launchers | Dmage22/FEX-D2 `d2r/clean` (11 commits squashed, plus 2d8b53d393 and ba05439d66) | Non-image section views, OS query for unknown executable pages, fault raise with `hlt` and the right exception code, stale NoExec retry (on the host exception path and in the FEX-2611 synchronous fault handler, with `InSyscallCallback` set so the invalidation cannot re-enter the invalidation lock through `NotifyMemoryFree`), execute faults whose instruction starts on an executable page reported on the next page unless the decoder has a partial-fetch address, PF/AF through context conversion (needs the ntdll export), disk-cache extents, compiler restart at inaccessible pages, `0x67` MOVS/STOS and FS/GS decode | analysis in FEX-Emu/FEX#5328; no PR yet | +| 0001 windows-fixes-for-protected-launchers | Dmage22/FEX-D2 `d2r/clean` (11 commits squashed, plus 2d8b53d393) | Non-image section views, OS query for unknown executable pages, fault raise with `hlt` and the right exception code, stale NoExec retry (on the host exception path and in the FEX-2611 synchronous fault handler, with `InSyscallCallback` set so the invalidation cannot re-enter the invalidation lock through `NotifyMemoryFree`), PF/AF through context conversion (needs the ntdll export), disk-cache extents, compiler restart at inaccessible pages, `0x67` MOVS/STOS and FS/GS decode | analysis in FEX-Emu/FEX#5328; no PR yet | | 0002 windows-anti-tamper-stepping-and-fault-delivery | tarikbc 0004, 0006, 0009, 0012, 0013, 0014, 0017 (v1.6.0, f4a686a0) squashed | Guest-visible protection of trapped pages, raw syscall return registers, volatile code regions, 512 MB code buffer, translation reuse, guest exceptions delivered straight into `KiUserExceptionDispatcher` on the guest stack (fast raise). Env gates: `FEX_EXP_SKIP_CALLRET_RESET=1` opts in; `FEX_EXP_FASTRAISE=0` opts out. The trapless fault path of tarikbc 0017 (`FEX_EXP_FASTTRAP`) is upstream since FEX-2611 and was dropped here. Volatile code regions were on by default here; 0004 makes them opt-in | FEX-Emu/FEX#6022, #6023; Wine bug 60461 | | 0003 arm64ec-export-btcpu64notifynonimageviews | GameNative | Exports `BTCpu64NotifyNonImageViews` so ntdll only reports non-image views to a FEX that can take them | pairs with GameNative/proton-wine `dlls_arm64ec_emulator_notifications.patch` | | 0004 windows-make-volatile-code-regions-opt-in | GameNative | Volatile code region detection (from 0003) runs only with `FEX_EXP_VOLATILE=1`; any other value or unset is off. On by default it misfires on JIT runtimes: TMNT: Shredder's Revenge (FNA/.NET) recompiles on its main thread, runs about 10x slower and never creates a D3D device. Age of Empires IV needs it and sets it in its overlay. Env gate: `FEX_EXP_VOLATILE=1` opts in | GameNative only | @@ -37,7 +37,7 @@ behaviour that is not a plain correctness fix is behind an env var. Left out of 0002 on purpose: tarikbc 0002 (hides the emulator from the guest), 0007 (rewrites ntdll export bytes to pass one game's hook check), 0008 (debug tool), 0010 (obsolete on Proton 11.0-2), 0016 (hooks `powrprof` from the exit thunk; the fix belongs in Wine, bug 60462). -Left out of 0001: dmage22's log-level change (cosmetic), the entry-block NoExec log cap (238fe0a6b9), and ba8346d046 (superseded by 2d8b53d393). +Left out of 0001: dmage22's log-level change (cosmetic), the entry-block NoExec log cap (238fe0a6b9), ba8346d046 (superseded by 2d8b53d393), and the next-page probe for full NoExec faults (ba05439d66: it changes the fault address the guest sees, on WoW64 too). Patch 0002 was rebased onto 0001 by hand: the volatile-code and translation-reuse hooks in `Core.cpp` sit after 0001's compile restart point, and the fast raise replaces the final diff --git a/gamenative/patches/0001-windows-fixes-for-protected-launchers-that-decry.patch b/gamenative/patches/0001-windows-fixes-for-protected-launchers-that-decry.patch index 8119a2fa3d..6de506a066 100644 --- a/gamenative/patches/0001-windows-fixes-for-protected-launchers-that-decry.patch +++ b/gamenative/patches/0001-windows-fixes-for-protected-launchers-that-decry.patch @@ -15,20 +15,16 @@ compiler at inaccessible pages instead of deadlocking on the invalidation lock, Upstream: analysis in FEX-Emu/FEX#5328; no PR yet. Rebased onto FEX-2611: the partial-fault address now comes from upstream (511f58356, the Break -op carries the first byte that could not be fetched). FEX-2611 also delivers JIT-raised guest -faults on arm64ec straight to SynchronousExceptionHandler (27cfd41bf, 07abec317), so the stale -NoExec retry runs there too (DropStaleNoExecFault + EnterDispatcherFillSRA), not only on the -host exception path. +op carries the first byte that could not be fetched), so the VirtualQuery heuristic in +HandleGuestException is gone. FEX-2611 also delivers JIT-raised guest faults on arm64ec straight +to SynchronousExceptionHandler (27cfd41bf, 07abec317), so the stale NoExec retry runs there too +(DropStaleNoExecFault + EnterDispatcherFillSRA), not only on the host exception path. -Later d2r/clean fixes for Battle.net launchers: -- SynchronousExceptionHandler sets InSyscallCallback around the stale NoExec retry and clears it - before re-entering the dispatcher or raising. The fault stub reaches it with the flag clear, so - the lookup cache decommit in the invalidation went to NotifyMemoryFree, which took the code - invalidation mutex a second time on the same thread and hung the process (2d8b53d393). -- A full NoExec fault reports the next page again when the instruction's own page is executable; - the decoder's address is used only for a partial decode (non-zero and not the instruction - start). Reporting the instruction start made D2R_loader's handler fault forever on a page it had - already unlocked (ba05439d66). +Later d2r/clean fix for Battle.net launchers (2d8b53d393): SynchronousExceptionHandler sets +InSyscallCallback around the stale NoExec retry and clears it before re-entering the dispatcher +or raising. The fault stub reaches it with the flag clear, so the lookup cache decommit in the +invalidation went to NotifyMemoryFree, which took the code invalidation mutex a second time on +the same thread and hung the process. --- FEXCore/Source/Interface/Core/Core.cpp | 34 +++ FEXCore/Source/Interface/Core/DiskCache.cpp | 10 +- @@ -39,9 +35,9 @@ Later d2r/clean fixes for Battle.net launchers: .../FEXCore/Debug/InternalThreadState.h | 13 ++ Source/Windows/ARM64EC/Module.S | 9 + Source/Windows/ARM64EC/Module.cpp | 211 +++++++++++++++++- - Source/Windows/Common/Exception.h | 31 ++- + Source/Windows/Common/Exception.h | 8 +- Source/Windows/Common/InvalidationTracker.cpp | 119 +++++++++- - 11 files changed, 468 insertions(+), 47 deletions(-) + 11 files changed, 446 insertions(+), 46 deletions(-) diff --git a/FEXCore/Source/Interface/Core/Core.cpp b/FEXCore/Source/Interface/Core/Core.cpp index 2ba56b69a..53d81a872 100644 @@ -695,18 +691,10 @@ index 3be7e465a..2af511082 100644 diff --git a/Source/Windows/Common/Exception.h b/Source/Windows/Common/Exception.h -index b41a5e64a..3ad78b7e2 100644 +index b41a5e64a..83814521d 100644 --- a/Source/Windows/Common/Exception.h +++ b/Source/Windows/Common/Exception.h -@@ -6,6 +6,7 @@ - #include - - #include -+#include - #include - - namespace FEX::Windows { -@@ -74,12 +75,36 @@ static inline EXCEPTION_RECORD HandleGuestException(FEXCore::Core::CpuStateFrame +@@ -74,12 +74,16 @@ static inline EXCEPTION_RECORD HandleGuestException(FEXCore::Core::CpuStateFrame } break; case FEXCore::X86State::X86_TRAPNO_OF: Dst.ExceptionCode = EXCEPTION_INT_OVERFLOW; return Dst; @@ -714,33 +702,12 @@ index b41a5e64a..3ad78b7e2 100644 - // A page-fault raised by an explicit break in JIT code is always an execute fault + case FEXCore::X86State::X86_TRAPNO_PF: { + // A page-fault raised by an explicit break in JIT code is always an execute fault. -+ // -+ // The reported address has to be the first byte that could not be fetched, which is not always -+ // the start of the instruction: an instruction sitting at the tail of an executable page and -+ // running into the next one faults on that next page. Reporting the instruction start instead -+ // makes decrypt-on-demand protectors spin forever - their handler looks at -+ // ExceptionInformation[1], sees a page it has already unlocked, decides there is nothing to do, -+ // resumes, and faults identically. An x86 instruction is at most 15 bytes, so it can only ever -+ // cross a single page boundary. -+ uint64_t FaultAddress = Rip; -+ const uint64_t RipPage = Rip & FEXCore::Utils::FEX_PAGE_MASK; -+ constexpr ULONG ExecProt = PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY; -+ MEMORY_BASIC_INFORMATION Info; -+ if (VirtualQuery(reinterpret_cast(RipPage), &Info, sizeof(Info)) && Info.State == MEM_COMMIT && -+ (Info.Protect & ExecProt)) { -+ FaultAddress = RipPage + FEXCore::Utils::FEX_PAGE_SIZE; -+ } -+ + // Set the code explicitly like every other case: the host trap that got us here is not necessarily an + // access violation (on Windows the SIGSEGV trampoline raises it with hlt). + Dst.ExceptionCode = EXCEPTION_ACCESS_VIOLATION; Dst.NumberParameters = 2; Dst.ExceptionInformation[0] = EXCEPTION_EXECUTE_FAULT; -- Dst.ExceptionInformation[1] = Fault.FaultAddress ? Fault.FaultAddress : Rip; -+ // A partial decode carries the exact first unfetchable byte, which is inside the instruction. A full NoExec -+ // carries the instruction start, which can sit on a page the protector has already unlocked while the bytes -+ // it is missing are on the next one; keep the page probe above for that case. -+ Dst.ExceptionInformation[1] = (Fault.FaultAddress && Fault.FaultAddress != Rip) ? Fault.FaultAddress : FaultAddress; + Dst.ExceptionInformation[1] = Fault.FaultAddress ? Fault.FaultAddress : Rip; return Dst; + } default: LogMan::Msg::EFmt("Unknown SIGSEGV trap: {}", Fault.TrapNo); break;