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 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
118 lines
5.5 KiB
Python
118 lines
5.5 KiB
Python
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/0009-signal-handler-escape-frees-host-stack.patch"
|
|
PROBE = FEX / "probes/signal-handler-escape.c"
|
|
DELEGATOR = "Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp"
|
|
CONTEXT = "Source/Tools/LinuxEmulation/ArchHelpers/MContext.h"
|
|
THREADS = "Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h"
|
|
FIELDS = ["PrevGuestFrame", "GuestFrameEnd", "GuestInterruptedSP", "GuestAltBase", "GuestAltEnd"]
|
|
|
|
|
|
def files():
|
|
sections = re.split(r"^diff --git a/(\S+) \S+\n", PATCH.read_text(), flags=re.M)
|
|
return {sections[i]: sections[i + 1] for i in range(1, len(sections), 2)}
|
|
|
|
|
|
def hunks(section):
|
|
return re.findall(r"^@@ [^@]+ @@ ?(.*)\n((?:[ +\-].*\n?)*)", section, re.M)
|
|
|
|
|
|
def added(body):
|
|
return [line[1:].strip() for line in body.splitlines() if line.startswith("+") and not line.startswith("+++")]
|
|
|
|
|
|
def removed(body):
|
|
return [line for line in body.splitlines() if line.startswith("-") and not line.startswith("---")]
|
|
|
|
|
|
class SignalHandlerEscapeTest(unittest.TestCase):
|
|
def test_only_signal_state_files_change(self):
|
|
self.assertEqual(sorted(files()), sorted([CONTEXT, DELEGATOR, THREADS]))
|
|
|
|
def test_nothing_is_removed(self):
|
|
for name, section in files().items():
|
|
self.assertEqual(removed(section), [], name)
|
|
|
|
def test_added_code_has_no_comments(self):
|
|
for name, section in files().items():
|
|
for line in added(section):
|
|
self.assertFalse("//" in line or "/*" in line, f"{name}: {line}")
|
|
|
|
def test_both_context_backups_record_the_guest_frame(self):
|
|
lines = added(files()[CONTEXT])
|
|
self.assertEqual(lines, [f"uint64_t {field};" for field in FIELDS] * 2)
|
|
|
|
def test_each_thread_keeps_its_frame_chain(self):
|
|
self.assertEqual([line for line in added(files()[THREADS]) if line], ["uint64_t GuestFrames {};", "uint32_t GuestFrameCount {};"])
|
|
|
|
def test_delivery_pushes_and_reclaims_only_left_frames_at_the_dispatch_level(self):
|
|
found = hunks(files()[DELEGATOR])
|
|
delivery = "".join(body for function, body in found if "HandleDispatcherGuestSignal" in function)
|
|
lines = added(delivery)
|
|
self.assertIn("#ifdef ARCHITECTURE_arm64", lines)
|
|
gate = " ".join(lines)
|
|
self.assertIn("WasInJIT && Newest && ContextBackup->PrevSP == SignalInfo.GuestFrames", gate)
|
|
self.assertIn("Frame->SignalHandlerRefCounter == SignalInfo.GuestFrameCount + 1", gate)
|
|
self.assertIn("GuestLeftFrame(Newest, OldGuestSP", gate)
|
|
self.assertIn("ContextBackup->PrevSP = HostTop;", lines)
|
|
self.assertIn("Frame->SignalHandlerRefCounter -= LeftFrames;", lines)
|
|
self.assertIn("SignalInfo.GuestFrames = reinterpret_cast<uint64_t>(ContextBackup);", lines)
|
|
self.assertIn("++SignalInfo.GuestFrameCount;", lines)
|
|
self.assertLess(lines.index("#endif"), lines.index("ContextBackup->PrevGuestFrame = SignalInfo.GuestFrames;"))
|
|
|
|
def test_signal_return_pops_its_frame_and_any_newer_ones(self):
|
|
found = hunks(files()[DELEGATOR])
|
|
restore = "".join(body for function, body in found if "RestoreThreadState" in function)
|
|
lines = added(restore)
|
|
self.assertIn("if (Entry == OldSP) {", lines)
|
|
self.assertIn("SignalInfo.GuestFrames = EntryBackup->PrevGuestFrame;", lines)
|
|
self.assertIn("Thread->CurrentFrame->SignalHandlerRefCounter -= Depth - 1;", lines)
|
|
self.assertNotIn("TYPE_PAUSE", " ".join(lines))
|
|
|
|
def test_thread_stop_clears_the_chain(self):
|
|
found = hunks(files()[DELEGATOR])
|
|
pause = "".join(body for function, body in found if "HandleSignalPause" in function)
|
|
self.assertEqual(added(pause), ["ThreadObject->SignalInfo.GuestFrames = 0;", "ThreadObject->SignalInfo.GuestFrameCount = 0;"])
|
|
|
|
def test_patch_follows_the_syscall_state_patch_on_every_release(self):
|
|
names = sorted(path.name for path in (FEX / "patches").glob("*.patch"))
|
|
self.assertEqual(names[names.index(PATCH.name) - 1], "0008-signal-return-restores-syscall-state.patch")
|
|
self.assertNotIn("Fixed-upstream", PATCH.read_text())
|
|
self.assertIn("tools/fex/patches/*.patch", (FEX / "build-in-arch.sh").read_text())
|
|
|
|
|
|
@unittest.skipUnless(platform.machine() == "x86_64", "the probe is x86-64 code")
|
|
class SignalHandlerEscapeProbeTest(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-signal-escape-"))
|
|
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_survives_every_escape(self):
|
|
result = subprocess.run([str(self.binary), "20000"], capture_output=True, text=True, timeout=120)
|
|
self.assertEqual(result.returncode, 0, result.stdout)
|
|
counts = dict(part.split("=") for part in result.stdout.split())
|
|
for phase in ("onstack", "offstack", "mixed", "returns", "outer"):
|
|
self.assertEqual(counts[phase], "20000", phase)
|
|
self.assertEqual(counts["nested"], "60000")
|
|
self.assertEqual(counts["wrong"], "0")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|