FEX: hand a traced program's ptrace state across exec

This commit is contained in:
maxjivi05 committed 2026-12-02 18:41:59 -05:00
1 parent f31f7d73f1
commit 8e652b8bed
1 file changed
+102
@@ -0,0 +1,102 @@
From 38ee3ef08d9bfd8312fccff688290572da82ef2a Mon Sep 17 00:00:00 2001
From: maxjivi05 <maxjividen@gmail.com>
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<SharedState*>(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