mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-24 15:26:04 +00:00
fix(fex): free host signal state when a guest leaves its handler (#3052)
* fix(fex): free host signal state when a guest leaves its handler FEX keeps each signal's host context on the host stack and frees it only on rt_sigreturn. A guest that leaves its handler another way (siglongjmp, or CoreCLR resuming a catch after a NullReferenceException) left about 2 KB of host stack and one SignalHandlerRefCounter count behind. Some 3500 such exceptions on one thread overran the guard-less 8 MB host stack, and the CurseForge agent died with "Internal CLR error. (0x80131506)" and "Stack overflow occurred on the same thread again!" at the start of its second addon scan. 0009 chains each thread's signal frames with the guest frame range and alternate stack they used. A signal that arrives in JIT code at the host level of the newest frame, after the guest stack pointer has left that frame, drops the left frames and stores its context where the newest live frame (or the dispatcher entry) left the host stack. rt_sigreturn pops its frame and any newer ones it skips; thread stop clears the chain. Signals inside system calls or thunk callbacks are left alone. This follows the approach suggested in FEX-Emu/FEX#2487. 0008 now restores InSyscallInfo only for signals that interrupted JIT code, the only case that saves it. Restoring 0 for a signal that interrupted a system call cleared the mark while the call was still running; an itimer loop around time() crashed in 15 of 15 runs. Pocket FIT Elite: siglongjmp out of a SIGSEGV handler died after about 3500 iterations under FEX-2611.1 with and without 0008 and finishes 200000 with 0009; the new probe (350000 escapes on and off the alternate stack, mixed with returning handlers and nested handlers) finishes under 0009 and crashes before 2000 iterations without it. The 0008 probe still reports no stale registers. * test(fex): check every delivery hunk of the 0009 patch * fix(fex): reset the host stack when a system call ends a block * fix(fex): thunk calls mark the guest state as spilled * fix(fex): signal returns apply the handler's register changes * docs(fex): describe patches 0010 to 0012 and credit the upstream fix * test(fex): require AVX2 for the ymm probe and keep vdso canaries live
This commit is contained in:
1 parent
6400a9ef09
commit
2478c44948
16 files changed
+1234
-12
No files matched your search
@@ -0,0 +1,67 @@
|
||||
import platform
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
FEX = Path(__file__).resolve().parents[1] / "fex"
|
||||
PATCH = FEX / "patches/0010-syscall-exit-resets-host-stack.patch"
|
||||
PROBE = FEX / "probes/syscall-host-stack.c"
|
||||
BRANCH_OPS = "FEXCore/Source/Interface/Core/JIT/BranchOps.cpp"
|
||||
|
||||
|
||||
def hunks():
|
||||
text = PATCH.read_text()
|
||||
files = re.findall(r"^diff --git a/(\S+) ", text, re.M)
|
||||
return files, re.findall(r"^@@ [^@]+ @@ ?(.*)\n((?:[ +\-].*\n?)*)", text, re.M)
|
||||
|
||||
|
||||
class SyscallHostStackTest(unittest.TestCase):
|
||||
def test_only_the_branch_ops_change(self):
|
||||
files, found = hunks()
|
||||
self.assertEqual(files, [BRANCH_OPS])
|
||||
self.assertEqual(len(found), 2)
|
||||
|
||||
def test_both_ops_that_leave_the_block_reset_the_stack_after_spilling(self):
|
||||
_, found = hunks()
|
||||
for (function, body), op in zip(found, ("DEF_OP(Syscall)", "DEF_OP(ThreadRemoveCodeEntry)")):
|
||||
lines = body.splitlines()
|
||||
added = [line[1:].strip() for line in lines if line.startswith("+")]
|
||||
self.assertEqual([line for line in added if line], ["ResetStack();"], op)
|
||||
self.assertEqual([line for line in lines if line.startswith("-")], [], op)
|
||||
context = [line[1:].strip() for line in lines]
|
||||
self.assertIn(op, function + " " + " ".join(context))
|
||||
self.assertLess(context.index("SpillStaticRegs(TMP1);"), context.index("ResetStack();"), op)
|
||||
|
||||
def test_patch_is_skipped_once_upstream_ships_it(self):
|
||||
self.assertEqual(PATCH.read_text().splitlines()[0], "Fixed-upstream: FEX-2612")
|
||||
self.assertIn("Fixed-upstream: FEX-", (FEX / "build-in-arch.sh").read_text())
|
||||
|
||||
|
||||
@unittest.skipUnless(platform.machine() == "x86_64", "the probe is x86-64 code")
|
||||
class SyscallHostStackProbeTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
compiler = shutil.which("cc") or shutil.which("gcc") or shutil.which("clang")
|
||||
if not compiler:
|
||||
raise unittest.SkipTest("no C compiler")
|
||||
cls.tmp = Path(tempfile.mkdtemp(prefix="fex-syscall-stack-"))
|
||||
cls.binary = cls.tmp / "probe"
|
||||
subprocess.run([compiler, "-O2", "-o", str(cls.binary), str(PROBE)], check=True, capture_output=True, text=True)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
shutil.rmtree(cls.tmp, ignore_errors=True)
|
||||
|
||||
def test_native_kernel_runs_every_syscall(self):
|
||||
result = subprocess.run([str(self.binary), "200000"], capture_output=True, text=True, timeout=120)
|
||||
self.assertEqual(result.returncode, 0, result.stdout)
|
||||
counts = dict(part.split("=") for part in result.stdout.split())
|
||||
self.assertIn(counts["mode"], ("avx", "mmx"))
|
||||
self.assertEqual(counts["syscalls"], "200000")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user