From 8bdc6485c8e30940a9664d4aad8710f97a68f7ed Mon Sep 17 00:00:00 2001 From: maxjivi05 Date: Wed, 2 Dec 2026 18:41:59 -0500 Subject: [PATCH] FEX: hand a traced program's ptrace state across exec --- .../0024-ptrace-state-across-exec.patch | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 tools/fex/patches/0024-ptrace-state-across-exec.patch diff --git a/tools/fex/patches/0024-ptrace-state-across-exec.patch b/tools/fex/patches/0024-ptrace-state-across-exec.patch new file mode 100644 index 0000000000..c2056b8cb6 --- /dev/null +++ b/tools/fex/patches/0024-ptrace-state-across-exec.patch @@ -0,0 +1,102 @@ +From 38ee3ef08d9bfd8312fccff688290572da82ef2a Mon Sep 17 00:00:00 2001 +From: maxjivi05 +Date: Wed, 2 Dec 2026 18:41:52 -0500 +Subject: [PATCH] Hand the tracee's ptrace state across exec + +--- + .../LinuxSyscalls/PtraceEmu.cpp | 54 ++++++++++++++++++- + 1 file changed, 53 insertions(+), 1 deletion(-) + +diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp +index 49ece24a3..62acdb0da 100644 +--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp ++++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp +@@ -266,6 +266,38 @@ namespace { + ::close(fd); + } + ++ bool AdoptionTried = false; ++ ++ void AdoptSharedPage() { ++ if (AdoptionTried || Current != &StaticState || !IsMainThread()) { ++ return; ++ } ++ AdoptionTried = true; ++ const int fd = OpenSidecar("state", ::getpid(), O_RDWR); ++ if (fd < 0) { ++ return; ++ } ++ if (!HasHostTracer()) { ++ ::close(fd); ++ return; ++ } ++ void* Page = ::mmap(nullptr, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); ++ ::close(fd); ++ if (Page == MAP_FAILED) { ++ return; ++ } ++ auto* S = static_cast(Page); ++ if (S->Magic != SharedState::MagicValue) { ++ ::munmap(Page, 4096); ++ return; ++ } ++ S->Pending = 0; ++ S->DirtyMask = 0; ++ S->SignalNumber = 0; ++ S->FlushLen = 0; ++ Current = S; ++ } ++ + struct Remote { + uint8_t* Mapped = nullptr; + uintptr_t StaticAddr = 0; +@@ -332,6 +364,22 @@ namespace { + return &Remotes.emplace(Pid, std::move(R)).first->second; + } + ++ bool KeepAcrossExec(pid_t Pid) { ++ { ++ std::lock_guard lk(RemotesLock); ++ auto It = Remotes.find(Pid); ++ if (It != Remotes.end()) { ++ return It->second.Mapped != nullptr; ++ } ++ } ++ const int fd = OpenSidecar("state", Pid, O_RDONLY); ++ if (fd < 0) { ++ return false; ++ } ++ ::close(fd); ++ return true; ++ } ++ + void Forget(pid_t Pid, bool Exited) { + std::lock_guard lk(RemotesLock); + auto It = Remotes.find(Pid); +@@ -470,10 +518,12 @@ namespace { + + + bool IsAttached() { ++ AdoptSharedPage(); + return Current->Attached != 0; + } + + bool WantsSyscallStops() { ++ AdoptSharedPage(); + return Current->SyscallStops && IsMainThread(); + } + +@@ -610,7 +660,9 @@ bool FixupWaitStatus(pid_t Pid, int* Status) { + + if ((St >> 16) == X86_PTRACE_EVENT_EXEC) { + CaptureExecPath(Pid); +- Forget(Pid, false); ++ if (!KeepAcrossExec(Pid)) { ++ Forget(Pid, false); ++ } + } + + if (WSTOPSIG(St) == SIGSTOP) { +-- +2.43.0 +