Guest TLS load patcher has never patched anything: PatchTlsPatterns scans host memory, killing God of War / Minecraft / UFC 5
Emulator Version / Commit
418eb7e (current main). Established from 15 user-submitted logs in sharpemu/sharpemu-site plus the source.
Summary
The guest TLS load patcher has never patched a single instruction, in any title, in any log ever submitted. PatchTlsPatterns scans the wrong memory, so guest mov reg, fs:[0] instructions reach the CPU unrewritten. There is no fault-time fallback, so the first one a title executes is fatal.
This is the terminal crash for at least God of War Ragnarök, Minecraft, and EA UFC 5 — all three currently "Nothing" on the compatibility list — and the faulting instruction is byte-identical in all three.
Evidence 1 — every log says zero
Every log attached to a compatibility report, across every title and build:
Patched 0 TLS loads, 161 TLS stores, 0 stack-canary accesses, 0 SSE4a EXTRQ blends (Bugsnax)
Patched 0 TLS loads, 164 TLS stores, ... (God of War)
Patched 0 TLS loads, 164 TLS stores, ... (Minecraft)
Patched 0 TLS loads, 164 TLS stores, ... (UFC 5)
Patched 0 TLS loads, 160 TLS stores, ... (PAC-MAN)
Patched 0 TLS loads, 164 TLS stores, ... (DREDGE)
...15 of 15 logs, without exception
Two things stand out. Loads are always zero. And the store count is 160/161/164 — essentially constant regardless of which game is running. A game-independent count means the scan is finding the same fixed region every time, not the title's code.
Evidence 2 — the scan runs over host memory
PatchTlsPatterns (DirectExecutionBackend.cs:3143) anchors on _entryPoint:
const ulong MaxScanBytes = 134217728uL; // 128 MiB
ulong num = _entryPoint;
ulong num2 = num + MaxScanBytes;
and _entryPoint is whatever TryExecute was last called with (DirectExecutionBackend.cs:1137), which during module init is a host address. From God of War's log:
[LOADER][INFO] Final entry point: 0x0000000800000080 <- the GUEST entry
[LOADER][INFO] ExecuteEntry starting at 0x0000020109330010 <- what _entryPoint actually holds
[LOADER][INFO] RIP: 0x0000000800284106 <- where it dies, in guest code
0x0000020109330010 is a host allocation — the same 0x2xx_________ range as every other host pointer in that log (Exception handler installed: 0x00000201079FA850). So the scan covers [host, host+128 MiB) and never touches the guest image at 0x800000000. EA UFC 5 is identical (0x000002701CC00010).
That is why the store count is game-independent: it is patching the emulator's own runtime, every run.
Evidence 3 — even a guest anchor misses, because the scan only goes forward
Minecraft does get a guest entry point, and still dies:
[LOADER][INFO] ExecuteEntry starting at 0x0000000811950010
[LOADER][INFO] RIP: 0x000000080D104AA0
The fault is at 0x80D104AA0, below the scan start of 0x811950010. The loop only walks upward from the anchor, so everything at a lower address is never examined.
Evidence 4 — three titles, one instruction
Minecraft Code at RIP: 66 66 66 64 48 8B 04 25 00 00 00 00 48 8B 7B ...
EA UFC 5 Code at RIP: 66 66 66 64 48 8B 04 25 00 00 00 00 C5 F8 57 ...
God of War Code at RIP: 66 66 66 64 48 8B 04 25 00 00 00 00 48 8B 0D ...
Byte-identical: 66 66 66 (three operand-size prefixes, LLVM's TLS general-dynamic relaxation padding), 64 (FS segment override), 48 8B 04 25 00000000 = mov rax, fs:[0].
FS base is 0 in the host process, so this reads linear address 0 and the log reports exactly that:
[LOADER][INFO] AV access: read
[LOADER][INFO] AV target: 0x0000000000000000
I originally read those as null-pointer dereferences from an HLE call returning a bad pointer. They are not — they are unrewritten thread-pointer loads.
Evidence 5 — the matcher itself is fine
TryPatchTlsLoadInstruction (DirectExecutionBackend.cs:3317) handles this encoding correctly: it skips 0x66 prefixes, requires 0x64, accepts an optional REX, then 8B /r with mod=00 rm=100, SIB=0x25, disp32==0. Hand-tracing the bytes above gives destination rax, length 12, which clears MinTlsPatchInstructionBytes (9).
So the matcher is not the defect. It is simply never shown the guest's code.
Why this is fatal rather than degraded
The VEH recovery ladder (DirectExecutionBackend.Exceptions.cs) has cases for lazily-committed pages, the guest-allocator hole, unresolved-import sentinels, BMI/ABM and SSE4a — but nothing for an access violation on a segment-prefixed operand. So an instruction the ahead-of-time patcher misses has no second chance.
Worth noting: until #781 (the REX.B bug in the VEH spinlock) no fault reached the managed handler at all, so a fault-time fallback could not have run even if one existed. That is now fixed, which makes a fallback viable.
Suggested direction
Two independent changes, either of which helps:
- Scan the loaded guest modules rather than
[_entryPoint, _entryPoint + 128 MiB). The anchor is wrong (often a host address) and the direction is wrong (forward only). Using the module ranges the loader already knows would cover the actual code. - Add a fault-time fallback for unpatched
fs:/gs:accesses, in the same shape as the existing BMI and SSE4a recoveries: decode at the faulting RIP, and for the thread-pointer form set the destination register to the value the TLS handler would have returned —TlsGetValue(_guestTlsBaseTlsIndex)(DirectExecutionBackend.cs:2346) — then step RIP past the instruction.
(2) is the more robust of the two, since it is correct regardless of what the ahead-of-time pass misses, and it is testable without a title by driving the recovery over a fabricated CONTEXT.
I am happy to send a PR for (2). Flagging it here first because the scope touches the fault path and I would rather a maintainer weigh in on which of the two directions is preferred before I build it.
Log File
# God of War Ragnarök (PPSA08329), from compatibility report 5
[LOADER][INFO] Final entry point: 0x0000000800000080
[LOADER][INFO] Patched 0 TLS loads, 164 TLS stores, 0 stack-canary accesses, 0 SSE4a EXTRQ blends
[LOADER][INFO] ExecuteEntry starting at 0x0000020109330010
...
[LOADER][INFO] NATIVE EXCEPTION CAUGHT!
[LOADER][INFO] Code: 0xC0000005
[LOADER][INFO] Exception Address: 0x0000000800284106
[LOADER][INFO] RIP: 0x0000000800284106
[LOADER][INFO] RAX: 0x0000000000000000
[LOADER][INFO] AV access: read
[LOADER][INFO] AV target: 0x0000000000000000
[LOADER][INFO] Code at RIP: 66 66 66 64 48 8B 04 25 00 00 00 00 48 8B 0D
Correcting my own suggested fix, and adding the reason this went unnoticed.
My suggestion (1) is incomplete and would not work on its own
I wrote that scanning the loaded modules instead of [_entryPoint, +128 MiB) would fix this. It would not, and I would rather say so than have someone build it and find out.
The patch emits E8 rel32 — a relative call to the TLS handler — and the handler is allocated near _entryPoint via TryAllocateNearEntry. When _entryPoint is a host address, the handler lands in host memory while the code being patched is in the guest image. From the logs:
| title | TLS handler | guest image | distance | rel32 reachable? |
|---|---|---|---|---|
| EA UFC 5 | 0x0000027020C00000 |
0x800000000 |
~2.6 TB | no |
| God of War | 0x000002010C330000 |
0x800000000 |
~2.2 TB | no |
| Minecraft | 0x0000000812950000 |
0x800000000 |
~0.3 GB | yes |
So for God of War and UFC 5, fixing scan coverage alone converts the access violation into [LOADER][WARNING] TLS patch out of rel32 range at 0x... and the instruction still executes unpatched. Handler locality has to be fixed in the same change — one handler per scanned range, allocated near that range, rather than a single handler near _entryPoint.
Minecraft is the useful control: its handler is in range, so there the only defect is the scan direction (fault at 0x80D104AA0, scan starting at 0x811950010 and only walking upward).
Why this was never spotted: the diagnostic looks at the wrong byte
DirectExecutionBackend.Exceptions.cs:365 already has a hint for exactly this condition:
byte[] code = new byte[16];
if (TryReadHostBytes(rip, code))
{
Console.Error.WriteLine("[LOADER][INFO] Code at RIP: " + ...);
if (code[0] == 100) // 0x64, FS prefix
{
Console.Error.WriteLine("[LOADER][ERROR] Detected FS segment prefix - TLS access not patched!");
}
It tests only code[0]. LLVM's TLS general-dynamic relaxation emits three 0x66 padding prefixes first, so the real byte layout is 66 66 66 64 ... — code[0] is 0x66 and the FS prefix sits at code[3].
The result is that in all 15 logs the line Code at RIP: 66 66 66 64 48 8B 04 25 ... is printed with no accompanying "TLS access not patched" diagnosis. The emulator has been detecting this crash correctly and describing it as an ordinary unmapped-memory access for as long as the check has existed, which is why every report reads as a generic null dereference — including my own first reading of them.
I am sending a PR for the diagnostic on its own, since it is a few lines, carries no regression risk, and immediately makes the next round of user captures self-diagnosing. The patcher fix (scan coverage + handler locality together) is the larger change and I will hold it for maintainer direction as offered above.
One more correction
An earlier investigation of mine framed these access violations as "an HLE export returning OK without writing its out-parameter." For these three logs that is wrong — the faults are unrewritten TLS loads, not bad out-parameters. I have not found evidence for the out-parameter theory in any of the 15 logs.
Direction (2) is up as #791, with hardware verification.
Two things worth recording here that I could only establish by building it.
The fallback works, and I can now reproduce the miss deliberately. A synthetic bare ELF that puts the thread-pointer load in a function below the entry point reproduces Evidence 3 exactly — Patched 0 TLS loads, then the fault. With the recovery on, the guest observes a correct non-null thread pointer in the register the instruction names and runs to completion; with SHARPEMU_DISABLE_TLS_LOAD_FAULT_RECOVERY=1 on the same build it segfaults at the same RIP with AV target: 0x0, the same shape as the three title logs.
The fault path is inert on current main. Running that guest on main gives a 20-second stall with no exception report at all — no NATIVE EXCEPTION CAUGHT, nothing — with the recovery enabled or disabled. The managed handler is simply never reached, because of the REX.B bug in the VEH spinlock compare-exchange (#781). I had noted that dependency here as an inference; it is now measured. Concretely: #781 gates every fault-time recovery in the ladder, not just this one, and until it lands #791 is correct but cannot fire.
Also worth noting for anyone reading the existing logs: the Detected FS segment prefix hint that should have flagged all of this has never fired, because it tests code[0] and the real encoding puts the override at code[3]. That is #790, and it does fire on the reproduction above.
Direction (1) — scan coverage plus handler locality — I am still holding for your call. It is independent of #791 and the two compose.
Excellent, thorough investigation — wanted to add some real-world data points from unrelated titles I've been testing (Astro Bot PPSA21564, Stray PPSA02101, Paw Patrol: Grand Prix PPSA05521), since they confirm part of this and also narrow the remaining scope.
Confirms Evidence 2's "game-independent scan" claim, on two more titles. My own logs show the same shape:
Astro Bot: Patched 0 TLS loads, 164 TLS stores, ...
Paw Patrol: Patched 0 TLS loads, 164 TLS stores, ... <- identical count, unrelated engine
Stray: Patched 0 TLS loads, 0 TLS stores, ...
164 showing up identically for two completely unrelated titles (Astro Bot's in-house engine vs. Paw Patrol's) on the first scan pass is more confirmation that it's finding a fixed host region, not guest code.
But: #824 (landed 2026-08-17, after this issue's 418eb7e baseline) already added a second, unconditional scan pass over the guest's standard PS5/PS4 image base — PatchTlsPatterns() now always also scans [0x800000000, 0x800000000+128MiB) regardless of what _entryPoint resolves to, specifically citing "Astro Bot... has an FS:[0] TLS load near +0x70A0000" as the motivating case. That's real coverage for soufian3hm's Direction (1). And on all three of my titles, that second pass does find and successfully patch genuine guest loads:
Astro Bot: Patched 3630 TLS loads ... (lazy-commit rescan 0x0000000800000000-0x0000000808000000)
Paw Patrol: Patched 5 TLS loads ... (lazy-commit rescan 0x0000000800000000-0x0000000808000000)
Stray: Patched 12 TLS loads ... (lazy-commit rescan 0x0000000800000000-0x0000000808000000)
(The log line is labelled "lazy-commit rescan" but reading the code, that's just the generic non-announce message reused for this second unconditional pass — it isn't actually gated on a lazy page-commit event.)
Crucially, zero "TLS patch out of rel32 range" warnings in any of my three runs — so the handler-locality bug you found is not universal. Checking why: _tlsHandlerAddress is allocated via TryAllocateNearEntry, anchored on _entryPoint — and in all three of my titles' logs, _entryPoint/Final entry point is already a genuine guest address (0x800000000 range) by the time PatchTlsPatterns runs, for every module. That's different from your God of War/UFC5 logs, where _entryPoint held a host address (0x2xx_________) at scan time. So it looks like:
#824already fixes the scan-coverage half for any title where_entryPointis a guest address by the time the scan runs (my three titles, apparently the common case) — the handler ends up correctly local since it's anchored on that same guest address.- The handler-locality bug you found is specifically triggered when
_entryPointstill holds a host address at scan time (God of War, UFC5) — which might come down to when in those titles' specific boot sequencePatchTlsPatternsgets invoked relative to_entryPointbeing set to the real module entry, rather than being universal.
None of this changes your conclusion — (2), the fault-time fallback in #791, is still the right fix since it's correct regardless of scan coverage or handler locality (a recovered load doesn't need any patch to have landed at all). Just wanted to narrow down exactly which titles are still exposed post-#824, since it's not all of them anymore. Noticed #791 currently shows a merge conflict against current main — unrelated to any of this, just flagging in case it's not already on your radar.
Both halves of this report are now accounted for. Summary:
Direction (1) — scan the loaded guest modules — already fixed on main.
PatchTlsPatterns no longer anchors on _entryPoint. It now derives scan ranges from the loader's own view of the guest address space:
var ranges = BuildTlsPatchScanRanges(virtualMemory.SnapshotRegions());
via PatchTlsPatternsInRange(range.Start, range.End), and only scans executable sub-ranges within each. Landed in 37694df (Alpha Rebuild Progress, #887).
That covers both failure modes in the evidence here: the anchor being a host address (God of War / UFC 5, where _entryPoint was 0x0000020109330010) and the direction being forward-only (Minecraft, where the fault at 0x80D104AA0 sits below the old scan start of 0x811950010). The game-independent 160/161/164 store count should also change, since the scan is no longer walking the emulator's own runtime.
Coverage: tests/SharpEmu.Libs.Tests/Cpu/NativeTlsPatchRangeTests.cs pins that a region based at 0x8000_0000 produces a scan range extending past imageBase + 0x0800_0000 — i.e. the full executable region, not a forward window from some later anchor.
Direction (2) — fault-time fallback — proposed in #1048.
Exceptions.cs previously only logged Detected FS segment prefix - TLS access not patched!; the ladder had no case for it. #1048 adds the recovery in the same shape as the BMI/ABM and SSE4a ones, decoding at the faulting RIP and writing TlsGetValue(_guestTlsBaseTlsIndex) to the destination register — deliberately the same decode and the same value TryPatchTlsLoadInstruction would have produced, so it is the patcher's semantics applied late rather than a new interpretation of fs:[0]. Handles GS as well as FS. The decode is extracted and tested against the exact bytes from your logs, including near misses that must be refused.
One thing this report left open that the fix inherits: the decode covers the absolute fs:/gs:[0] form (REX.W or REX.W+R, mod=00, rm=100, SIB 0x25, disp32 == 0). A non-zero displacement is a different TLS access the ahead-of-time patcher does not model either, so it would still be refused rather than recovered.
I do not have permission to close issues on this repo, so this comment stands in place of one.