loader: keep the caller's flags in the TLS stubs #1571
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1571
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1571/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?
@Nawid3333 on GitHub · 2026-10-16 21:43 UTC · original
Nawid3333:loader/tls-stubs-keep-flags→main· +68 −9 · 2 files · 1 commits · merged 2026-10-16 22:05 UTCA patched TLS load (
mov reg, fs:[0]replaced by a call toTlsRegStub, which callsSafeCall) changed the guest's arithmetic flags. Both stubs moved RSP past the red zone withsub rsp,0x80andadd rsp,0x80, and SUB and ADD write the flags: the subs ran beforeSafeCall'spushfqsaved them, and the adds after itspopfqrestored them. Guest code that compares, loadsfs:[0]and then branches on the result could therefore branch on the stack-pointer arithmetic instead.Change (
jit.h):lea rsp,[rsp-0x80]plus a two-byte nop, so the patched call offset, the register byte andSafeCall's function pointer stay at the same offsets, andlea rsp,[rsp+0x80]. LEA doesn't touch the flags.Test:
TlsStubsPreserveFlags(VirtualMemoryAllocationTests) placesSafeCalland aTlsRegStubthe wayRuntimeLinkerdoes and calls the stub right afteradd al,1with two inputs: 0x7f (OF, SF and AF set) and 0xff (CF, PF, AF and ZF set). It checks the six status flags, the output register and RAX. With the old bytes it fails; with the change allVirtualMemoryAllocationTestspass (Windows 11, clang-cl), and fork CI runs it on Windows and Linux (macOS skips it like the file's other JIT tests).I don't know of a game where this shows; it affects any guest code that keeps flags live across a TLS load.
The same fix is in fxpw's fork (a980b942f, "Preserve arithmetic flags across guest TLS MOV replacements") and in ClGratton's (201eaf049); this one keeps the test in the existing loader tests.
If you'd like this done differently, tell me and I'll change it.
Written with Claude Code (AI): it wrote the change and the test and ran them on my PC (Windows 11). I reviewed the diff and this description before opening the PR.
🤖 Generated with Claude Code
@coderabbitai[bot] on GitHub · 2026-10-16 21:46 UTC · original
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
09a83ecd-977a-41c4-bc03-2f02fb451a79📥 Commits
Reviewing files that changed from the base of the PR and between
5dec523143and50f618f571.📒 Files selected for processing: 2
src/loader/jit.htests/VirtualMemoryAllocationTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
📝 Walkthrough
The JIT call stubs replace stack arithmetic with LEA instructions and add NOP instructions while retaining their declared buffer sizes. A Linux/Windows test invokes a TLS register stub through
SafeCalland checks the returned TLS address, RAX, and status flags for inputs0x7fand0xff.Suggested reviewers:
nmzikPriority: ⬇️ Low
Change: Bug fix
Merge Risk: ⚪ Minimal · up to
50f61This change keeps the caller's CPU flags intact across TLS stub calls and adds a test for it. No merge-blocking risk was found.
Pre-merge checks:
4 ·
1
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.