From f83cf58279c34355e10fbdc150e256b2b8a45fd1 Mon Sep 17 00:00:00 2001 From: Utkarsh Dalal Date: Thu, 29 Oct 2026 17:56:01 +0530 Subject: [PATCH] series: keep guest loads whose value is unused (0007) HotSpot C1 null checks are cmp rax,[reg] with dead flags; the dead-flag pass removed the load with the compare and Necesse aborted on the next field load. Patch 0007 declares the guest load IR ops as side-effecting. Series version 5. --- gamenative/README.md | 1 + gamenative/SERIES_VERSION | 2 +- ...ep-guest-loads-whose-value-is-unused.patch | 96 +++++++++++++++++++ gamenative/series | 1 + 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 gamenative/patches/0007-fexcore-keep-guest-loads-whose-value-is-unused.patch diff --git a/gamenative/README.md b/gamenative/README.md index 00af4051ce..b1d2a0ec38 100644 --- a/gamenative/README.md +++ b/gamenative/README.md @@ -27,6 +27,7 @@ behaviour that is not a plain correctness fix is behind an env var. | 0004 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` | | 0005 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 | | 0006 fexcore-fence-plain-loads-before-volatile-metadata-tso-loads | GameNative | With PE volatile metadata, a listed (TSO) load gets a `dmb ishld` in front when a plain load may precede it in the same metadata block, restoring x86 load order. Counter-Strike 2's CTSQueue pop let node->next and the tail sequence drift past its sequence re-check and crashed reading NULL+8. Outside metadata blocks nothing changes | GameNative only for now | +| 0007 fexcore-keep-guest-loads-whose-value-is-unused | GameNative | The dead-flag pass dropped a guest load whose only consumer was a flag write that a later instruction overwrote. HotSpot C1 null checks are `cmp rax,[reg]` with dead flags and rely on the fault, so a null receiver reached the next field load and the VM aborted (Necesse, 13 s after start). Guest load ops now declare side effects, as LoadMemPair already did | GameNative only for now | Left out of 0003 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 diff --git a/gamenative/SERIES_VERSION b/gamenative/SERIES_VERSION index b8626c4cff..7ed6ff82de 100644 --- a/gamenative/SERIES_VERSION +++ b/gamenative/SERIES_VERSION @@ -1 +1 @@ -4 +5 diff --git a/gamenative/patches/0007-fexcore-keep-guest-loads-whose-value-is-unused.patch b/gamenative/patches/0007-fexcore-keep-guest-loads-whose-value-is-unused.patch new file mode 100644 index 0000000000..16e58bfc79 --- /dev/null +++ b/gamenative/patches/0007-fexcore-keep-guest-loads-whose-value-is-unused.patch @@ -0,0 +1,96 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Utkarsh Dalal +Date: Thu, 29 Oct 2026 17:55:38 +0530 +Subject: [PATCH] FEXCore: keep guest loads whose value is unused + +The dead-flag pass removes any node with no uses and no declared side +effects. A plain LoadMem has none declared, so a guest load that only +fed a flag calculation goes away with the flags: once `add [rsi+0x128],1` +overwrites every flag `cmp rax,[rdx]` produced, the compare is removed, +and then the load behind it. + +HotSpot relies on that load faulting. C1's explicit null check is +`cmp rax,[reg]` with the flags dead; the access violation at that PC is +what the JVM turns into a NullPointerException. With the load gone the +null receiver reaches the next field load, which faults at a PC that is +not in the nmethod's null-check table, and the VM aborts with +EXCEPTION_ACCESS_VIOLATION reading 0xc. Necesse (Temurin 17 x86-64) +dies this way 13 s after start in +necesse.engine.save.LoadData.getInt/getBoolean, an 8-byte method whose +inlined invokevirtual is guarded by exactly that probe; every hs_err +has the same registers (rdx = 0 from the interpreted caller, +rsi = 0x46e, the live profile counter) and the event log records +"Implicit null exception at to 0x0", the lookup that found no +continuation. C2 safepoint polls (`test rax,[poll]`) are the same shape. + +Declare the guest load ops as having side effects so the pass keeps +them; LoadMemPair already is. Loads that are truly dead stay in the +block, which compiled x86 rarely produces except as a probe. + +A stand-alone probe (static method calling a virtual on its argument, +null every 5000th call, -XX:TieredStopAtLevel=3) reproduces the abort +on the stock build and passes with the loads kept. +--- + FEXCore/Source/Interface/IR/IR.json | 7 +++++++ + 1 file changed, 7 insertions(+) + +diff --git a/FEXCore/Source/Interface/IR/IR.json b/FEXCore/Source/Interface/IR/IR.json +index 880520077..99cd57273 100644 +--- a/FEXCore/Source/Interface/IR/IR.json ++++ b/FEXCore/Source/Interface/IR/IR.json +@@ -530,6 +530,7 @@ + }, + + "SSA = LoadMem RegisterClass:$Class, OpSize:#Size, GPR:$Addr, GPR:$Offset, OpSize:$Align, MemOffsetType:$OffsetType, u8:$OffsetScale": { ++ "HasSideEffects": true, + "Inline": ["", "Mem"], + "DestSize": "Size" + }, +@@ -575,6 +576,7 @@ + }, + + "SSA = LoadMemTSO RegisterClass:$Class, OpSize:#Size, GPR:$Addr, GPR:$Offset, OpSize:$Align, MemOffsetType:$OffsetType, u8:$OffsetScale": { ++ "HasSideEffects": true, + "Desc": ["Does a x86 TSO compatible load from memory. Offset must be Invalid()." + ], + "Inline": ["", "Memtso"], +@@ -590,6 +592,7 @@ + }, + + "FPR = VLoadVectorMasked OpSize:#RegisterSize, OpSize:#ElementSize, FPR:$Mask, GPR:$Addr, GPR:$Offset, MemOffsetType:$OffsetType, u8:$OffsetScale": { ++ "HasSideEffects": true, + "Desc": ["Does a masked load similar to VPMASKMOV/VMASKMOV where the upper bit of each element", + "determines whether or not that element will be loaded from memory"], + "ImplicitFlagClobber": true, +@@ -605,6 +608,7 @@ + "ElementSize": "ElementSize" + }, + "FPR = VLoadVectorGatherMasked OpSize:#RegisterSize, OpSize:#ElementSize, FPR:$Incoming, FPR:$Mask, GPR:$AddrBase, FPR:$VectorIndexLow, FPR:$VectorIndexHigh, OpSize:$VectorIndexElementSize, u8:$OffsetScale, u8:$DataElementOffsetStart, u8:$IndexElementOffsetStart, OpSize:$AddrSize, i1:$MaskAllOnes{false}": { ++ "HasSideEffects": true, + "Desc": [ + "Does a masked load similar to VPGATHERD* where the upper bit of each element", + "determines whether or not that element will be loaded from memory.", +@@ -620,6 +624,7 @@ + ] + }, + "FPR = VLoadVectorGatherMaskedQPS OpSize:#RegisterSize, OpSize:#ElementSize, FPR:$Incoming, FPR:$MaskReg, GPR:$AddrBase, FPR:$VectorIndexLow, FPR:$VectorIndexHigh, u8:$OffsetScale, OpSize:$AddrSize, i1:$MaskAllOnes{false}": { ++ "HasSideEffects": true, + "Desc": [ + "Does a masked load similar to VPGATHERQPS where the upper bit of each element", + "determines whether or not that element will be loaded from memory.", +@@ -637,6 +642,7 @@ + ] + }, + "FPR = VLoadVectorElement OpSize:#RegisterSize, OpSize:#ElementSize, FPR:$DstSrc, u8:$Index, GPR:$Addr": { ++ "HasSideEffects": true, + "Desc": ["Does a memory load to a single element of a vector.", + "Leaves the rest of the vector's data intact.", + "Matches arm64 ld1 semantics"], +@@ -776,6 +782,7 @@ + ] + }, + "FPR = VLoadNonTemporal OpSize:#RegisterSize, GPR:$Addr, i8:$Offset": { ++ "HasSideEffects": true, + "Desc": ["Does a non-temporal memory load of a vector.", + "Matches arm64 SVE ldnt1b semantics.", + "Specifically weak-memory model ordered to match x86 non-temporal stores." diff --git a/gamenative/series b/gamenative/series index 5f645822d4..f6b75f7051 100644 --- a/gamenative/series +++ b/gamenative/series @@ -4,3 +4,4 @@ 0004-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch 0005-windows-make-volatile-code-regions-opt-in.patch 0006-fexcore-fence-plain-loads-before-volatile-metadata-t.patch +0007-fexcore-keep-guest-loads-whose-value-is-unused.patch