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:
MaxsTechReview authored and GitHub committed 2026-11-23 07:20:33 -05:00
1 parent 6400a9ef09
commit 2478c44948
16 files changed
+1234 -12

No files matched your search

@@ -1,12 +1,18 @@
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
index b4c7b24..e6777aa 100644
index b4c7b24..643185d 100644
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
@@ -262,6 +262,7 @@ void SignalDelegator::RestoreThreadState(FEXCore::Core::InternalThreadState* Thr
if (Context->UContextLocation) {
@@ -264,12 +264,13 @@ void SignalDelegator::RestoreThreadState(FEXCore::Core::InternalThreadState* Thr
auto Frame = Thread->CurrentFrame;
+ Frame->InSyscallInfo = Context->InSyscallInfo;
if (Context->Flags & ArchHelpers::Context::ContextFlags::CONTEXT_FLAG_INJIT) {
// XXX: Unsupported since it needs state reconstruction
// If we are in the JIT then SRA might need to be restored to values from the context
// We can't currently support this since it might result in tearing without real state reconstruction
+ Frame->InSyscallInfo = Context->InSyscallInfo;
}
if (Is64BitMode) {
RestoreFrame_x64(Thread, Context, Frame, ucontext);
} else {
if (Type == RestoreType::TYPE_NONREALTIME) {
@@ -0,0 +1,174 @@
diff --git a/Source/Tools/LinuxEmulation/ArchHelpers/MContext.h b/Source/Tools/LinuxEmulation/ArchHelpers/MContext.h
index 02bb6bf..2e72492 100644
--- a/Source/Tools/LinuxEmulation/ArchHelpers/MContext.h
+++ b/Source/Tools/LinuxEmulation/ArchHelpers/MContext.h
@@ -49,6 +49,11 @@ struct X86ContextBackup {
uint64_t FPStateLocation;
uint64_t UContextLocation;
uint64_t SigInfoLocation;
+ uint64_t PrevGuestFrame;
+ uint64_t GuestFrameEnd;
+ uint64_t GuestInterruptedSP;
+ uint64_t GuestAltBase;
+ uint64_t GuestAltEnd;
FEXCore::Core::CPUState GuestState;
static constexpr int RedZoneSize = 128;
};
@@ -76,6 +81,11 @@ struct ArmContextBackup {
uint64_t FPStateLocation;
uint64_t UContextLocation;
uint64_t SigInfoLocation;
+ uint64_t PrevGuestFrame;
+ uint64_t GuestFrameEnd;
+ uint64_t GuestInterruptedSP;
+ uint64_t GuestAltBase;
+ uint64_t GuestAltEnd;
FEXCore::Core::CPUState GuestState;
// Arm64 doesn't have a red zone
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
index 643185d..53179c4 100644
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
@@ -136,6 +136,31 @@ void SignalDelegator::SpillSRA(FEXCore::Core::InternalThreadState* Thread, void*
#endif
}
+namespace {
+ bool OnGuestAltStack(const ArchHelpers::Context::ContextBackup* Backup, uint64_t SP) {
+ return Backup->GuestAltEnd != 0 && SP >= Backup->GuestAltBase && SP <= Backup->GuestAltEnd;
+ }
+
+ bool GuestLeftFrame(const ArchHelpers::Context::ContextBackup* Backup, uint64_t GuestSP, uint32_t Limit) {
+ const bool SPOnAlt = OnGuestAltStack(Backup, GuestSP);
+ if (SPOnAlt == OnGuestAltStack(Backup, Backup->GuestFrameEnd)) {
+ return GuestSP >= Backup->GuestFrameEnd;
+ }
+ if (SPOnAlt) {
+ return false;
+ }
+ auto Entry = Backup;
+ while (OnGuestAltStack(Backup, Entry->GuestInterruptedSP)) {
+ if (--Limit == 0 || !Entry->PrevGuestFrame) {
+ return false;
+ }
+ Entry = reinterpret_cast<const ArchHelpers::Context::ContextBackup*>(Entry->PrevGuestFrame);
+ }
+ return OnGuestAltStack(Backup, Entry->GuestFrameEnd) && Entry->GuestAltBase == Backup->GuestAltBase &&
+ Entry->GuestAltEnd == Backup->GuestAltEnd && GuestSP >= Entry->GuestInterruptedSP;
+ }
+}
+
ArchHelpers::Context::ContextBackup* SignalDelegator::StoreThreadState(FEXCore::Core::InternalThreadState* Thread, int Signal, void* ucontext) {
// We can end up getting a signal at any point in our host state
// Jump to a handler that saves all state so we can safely return
@@ -249,6 +274,19 @@ void SignalDelegator::RestoreThreadState(FEXCore::Core::InternalThreadState* Thr
}
OldSP = *reinterpret_cast<uint64_t*>(GuestSP);
+
+ auto& SignalInfo = FEX::HLE::ThreadManager::GetStateObjectFromFEXCoreThread(Thread)->SignalInfo;
+ uint64_t Entry = SignalInfo.GuestFrames;
+ for (uint32_t Depth = 1; Entry && Depth <= SignalInfo.GuestFrameCount; ++Depth) {
+ auto EntryBackup = reinterpret_cast<ArchHelpers::Context::ContextBackup*>(Entry);
+ if (Entry == OldSP) {
+ SignalInfo.GuestFrames = EntryBackup->PrevGuestFrame;
+ SignalInfo.GuestFrameCount -= Depth;
+ Thread->CurrentFrame->SignalHandlerRefCounter -= Depth - 1;
+ break;
+ }
+ Entry = EntryBackup->PrevGuestFrame;
+ }
}
uintptr_t NewSP = OldSP;
@@ -287,6 +325,7 @@ bool SignalDelegator::HandleDispatcherGuestSignal(FEXCore::Core::InternalThreadS
auto ContextBackup = StoreThreadState(Thread, Signal, ucontext);
auto Frame = Thread->CurrentFrame;
+ auto& SignalInfo = FEX::HLE::ThreadManager::GetStateObjectFromFEXCoreThread(Thread)->SignalInfo;
// Ref count our faults
// We use this to track if it is safe to clear cache
@@ -336,6 +375,8 @@ bool SignalDelegator::HandleDispatcherGuestSignal(FEXCore::Core::InternalThreadS
uint64_t OldGuestSP = Frame->State.gregs[FEXCore::X86State::REG_RSP];
uint64_t NewGuestSP = OldGuestSP;
+ uint64_t GuestAltBase = 0;
+ uint64_t GuestAltEnd = 0;
// altstack is only used if the signal handler was setup with SA_ONSTACK
if (GuestAction->sa_flags & SA_ONSTACK) {
@@ -345,6 +386,8 @@ bool SignalDelegator::HandleDispatcherGuestSignal(FEXCore::Core::InternalThreadS
// Then that means we are hitting recursive signals and we need to walk back the stack correctly
uint64_t AltStackBase = reinterpret_cast<uint64_t>(GuestStack->ss_sp);
uint64_t AltStackEnd = AltStackBase + GuestStack->ss_size;
+ GuestAltBase = AltStackBase;
+ GuestAltEnd = AltStackEnd;
if (OldGuestSP >= AltStackBase && OldGuestSP <= AltStackEnd) {
// We are already in the alt stack, the rest of the code will handle adjusting this
} else {
@@ -353,6 +396,40 @@ bool SignalDelegator::HandleDispatcherGuestSignal(FEXCore::Core::InternalThreadS
}
}
+#ifdef ARCHITECTURE_arm64
+ auto Newest = reinterpret_cast<ArchHelpers::Context::ContextBackup*>(SignalInfo.GuestFrames);
+ if (WasInJIT && Newest && ContextBackup->PrevSP == SignalInfo.GuestFrames &&
+ Frame->SignalHandlerRefCounter == SignalInfo.GuestFrameCount + 1 && GuestLeftFrame(Newest, OldGuestSP, SignalInfo.GuestFrameCount)) {
+ uint32_t LeftFrames = 0;
+ while (Newest && LeftFrames < SignalInfo.GuestFrameCount &&
+ GuestLeftFrame(Newest, OldGuestSP, SignalInfo.GuestFrameCount - LeftFrames)) {
+ Newest = reinterpret_cast<ArchHelpers::Context::ContextBackup*>(Newest->PrevGuestFrame);
+ ++LeftFrames;
+ }
+
+ const uint64_t HostTop = Newest ? reinterpret_cast<uint64_t>(Newest) : Frame->ReturningStackLocation;
+ const uint64_t MovedBackup = FEXCore::AlignDown(
+ HostTop - ArchHelpers::Context::ContextBackup::RedZoneSize - sizeof(ArchHelpers::Context::ContextBackup), 16);
+ if (MovedBackup > reinterpret_cast<uint64_t>(ContextBackup)) {
+ memmove(reinterpret_cast<void*>(MovedBackup), ContextBackup, sizeof(ArchHelpers::Context::ContextBackup));
+ ContextBackup = reinterpret_cast<ArchHelpers::Context::ContextBackup*>(MovedBackup);
+ ContextBackup->PrevSP = HostTop;
+ ArchHelpers::Context::SetSp(ucontext, MovedBackup);
+ SignalInfo.GuestFrames = reinterpret_cast<uint64_t>(Newest);
+ SignalInfo.GuestFrameCount -= LeftFrames;
+ Frame->SignalHandlerRefCounter -= LeftFrames;
+ }
+ }
+#endif
+
+ ContextBackup->GuestFrameEnd = NewGuestSP;
+ ContextBackup->GuestInterruptedSP = OldGuestSP;
+ ContextBackup->GuestAltBase = GuestAltBase;
+ ContextBackup->GuestAltEnd = GuestAltEnd;
+ ContextBackup->PrevGuestFrame = SignalInfo.GuestFrames;
+ SignalInfo.GuestFrames = reinterpret_cast<uint64_t>(ContextBackup);
+ ++SignalInfo.GuestFrameCount;
+
// siginfo_t
siginfo_t* HostSigInfo = reinterpret_cast<siginfo_t*>(info);
@@ -491,6 +568,8 @@ bool SignalDelegator::HandleSignalPause(FEXCore::Core::InternalThreadState* Thre
// Our ref counting doesn't matter anymore
Thread->CurrentFrame->SignalHandlerRefCounter = 0;
+ ThreadObject->SignalInfo.GuestFrames = 0;
+ ThreadObject->SignalInfo.GuestFrameCount = 0;
// Set the new PC
if (CTX->IsAddressInCodeBuffer(Thread, ArchHelpers::Context::GetPc(ucontext))) {
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h b/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h
index dfcd4b0..e4a86a9 100644
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h
@@ -83,6 +83,9 @@ struct ThreadStateObject : public FEXCore::Allocator::FEXAllocOperators {
// Queue of thread local signal frames that have been deferred.
// Async signals aren't guaranteed to be delivered in any particular order, but FEX treats them as FILO.
fextl::vector<DeferredSignalState> DeferredSignalFrames;
+
+ uint64_t GuestFrames {};
+ uint32_t GuestFrameCount {};
} SignalInfo {};
// Seccomp thread specific data.
@@ -0,0 +1,23 @@
Fixed-upstream: FEX-2612
diff --git a/FEXCore/Source/Interface/Core/JIT/BranchOps.cpp b/FEXCore/Source/Interface/Core/JIT/BranchOps.cpp
index c2b1d8e287..fcdf261484 100644
--- a/FEXCore/Source/Interface/Core/JIT/BranchOps.cpp
+++ b/FEXCore/Source/Interface/Core/JIT/BranchOps.cpp
@@ -285,6 +285,8 @@ DEF_OP(CondJump) {
DEF_OP(Syscall) {
SpillStaticRegs(TMP1);
+ ResetStack();
+
// Jump to the syscall dispatch handler. We won't return after this.
ldr(TMP1, STATE, offsetof(FEXCore::Core::CpuStateFrame, Pointers.ThreadDispatchSyscallHandler));
br(TMP1);
@@ -376,6 +378,8 @@ DEF_OP(ThreadRemoveCodeEntry) {
SpillStaticRegs(TMP1);
+ ResetStack();
+
// Store the new RIP to go to.
str(GetReg(Op->NewRIP).X(), STATE, offsetof(FEXCore::Core::CpuStateFrame, State.rip));
@@ -0,0 +1,109 @@
diff --git a/FEXCore/Source/Interface/Core/Core.cpp b/FEXCore/Source/Interface/Core/Core.cpp
--- a/FEXCore/Source/Interface/Core/Core.cpp
+++ b/FEXCore/Source/Interface/Core/Core.cpp
@@ -383,7 +383,11 @@
}
void ContextImpl::HandleCallback(FEXCore::Core::InternalThreadState* Thread, uint64_t RIP) {
- static_cast<ContextImpl*>(Thread->CTX)->Dispatcher->ExecuteJITCallback(Thread->CurrentFrame, RIP);
+ auto Frame = Thread->CurrentFrame;
+ const auto InSyscallInfo = Frame->InSyscallInfo;
+ Frame->InSyscallInfo = 0;
+ static_cast<ContextImpl*>(Thread->CTX)->Dispatcher->ExecuteJITCallback(Frame, RIP);
+ Frame->InSyscallInfo = InSyscallInfo;
}
void ContextImpl::ExecuteThread(FEXCore::Core::InternalThreadState* Thread) {
diff --git a/FEXCore/Source/Interface/Core/JIT/BranchOps.cpp b/FEXCore/Source/Interface/Core/JIT/BranchOps.cpp
--- a/FEXCore/Source/Interface/Core/JIT/BranchOps.cpp
+++ b/FEXCore/Source/Interface/Core/JIT/BranchOps.cpp
@@ -305,6 +305,9 @@
PushDynamicRegs(TMP1);
+ LoadConstant(ARMEmitter::Size::i64Bit, TMP1, 0xFFFF);
+ str(TMP1, STATE, offsetof(FEXCore::Core::CpuStateFrame, InSyscallInfo));
+
mov(ARMEmitter::Size::i64Bit, ARMEmitter::Reg::r0, GetReg(Op->ArgPtr));
InsertNamedThunkRelocation(ARMEmitter::Reg::r2, Op->ThunkNameHash);
@@ -320,6 +323,8 @@
FillStaticRegs({
.NZCV = false,
});
+
+ str(ARMEmitter::XReg::zr, STATE, offsetof(FEXCore::Core::CpuStateFrame, InSyscallInfo));
}
DEF_OP(ValidateCode) {
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
@@ -107,6 +107,10 @@
#ifdef ARCHITECTURE_arm64
Thread->CurrentFrame->State.rip = CTX->RestoreRIPFromHostPC(Thread, ArchHelpers::Context::GetPc(ucontext));
+ if ((IgnoreMask & 0xFFFF) == 0xFFFF) {
+ return;
+ }
+
for (size_t i = 0; i < Config.SRAGPRCount; i++) {
const uint8_t SRAIdxMap = Config.SRAGPRMapping[i];
if (IgnoreMask & (1U << SRAIdxMap)) {
@@ -301,12 +305,10 @@
if (Context->UContextLocation) {
auto Frame = Thread->CurrentFrame;
- if (Context->Flags & ArchHelpers::Context::ContextFlags::CONTEXT_FLAG_INJIT) {
- // XXX: Unsupported since it needs state reconstruction
- // If we are in the JIT then SRA might need to be restored to values from the context
- // We can't currently support this since it might result in tearing without real state reconstruction
- Frame->InSyscallInfo = Context->InSyscallInfo;
- }
+ // XXX: Unsupported since it needs state reconstruction
+ // If we are in the JIT then SRA might need to be restored to values from the context
+ // We can't currently support this since it might result in tearing without real state reconstruction
+ Frame->InSyscallInfo = Context->InSyscallInfo;
if (Is64BitMode) {
RestoreFrame_x64(Thread, Context, Frame, ucontext);
@@ -361,6 +363,9 @@
ContextBackup->InSyscallInfo = Thread->CurrentFrame->InSyscallInfo;
Thread->CurrentFrame->InSyscallInfo = 0;
} else {
+ ContextBackup->InSyscallInfo = Frame->InSyscallInfo;
+ Frame->InSyscallInfo = 0;
+
if (!IsAddressInDispatcher(OldPC)) {
// This is likely to cause issues but in some cases it isn't fatal
// This can also happen if we have put a signal on hold, then we just reenabled the signal
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator/GuestFramesManagement.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator/GuestFramesManagement.cpp
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator/GuestFramesManagement.cpp
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator/GuestFramesManagement.cpp
@@ -131,7 +131,7 @@
if (Context->OriginalRIP != guest_uctx->uc_mcontext.gregs[FEXCore::x86_64::FEX_REG_RIP] || Context->FaultToTopAndGeneratedException) {
// Restore previous `InSyscallInfo` structure.
- Frame->InSyscallInfo = Context->InSyscallInfo;
+ Frame->InSyscallInfo = 0;
// Hack! Go back to the top of the dispatcher top
// This is only safe inside the JIT rather than anything outside of it
@@ -211,7 +211,7 @@
// If the guest modified the RIP then we need to take special precautions here
if (Context->OriginalRIP != guest_uctx->sc.ip || Context->FaultToTopAndGeneratedException) {
// Restore previous `InSyscallInfo` structure.
- Frame->InSyscallInfo = Context->InSyscallInfo;
+ Frame->InSyscallInfo = 0;
// Hack! Go back to the top of the dispatcher top
// This is only safe inside the JIT rather than anything outside of it
@@ -290,7 +290,7 @@
if (Context->OriginalRIP != guest_uctx->uc.uc_mcontext.gregs[FEXCore::x86::FEX_REG_EIP] || Context->FaultToTopAndGeneratedException) {
// Restore previous `InSyscallInfo` structure.
- Frame->InSyscallInfo = Context->InSyscallInfo;
+ Frame->InSyscallInfo = 0;
// Hack! Go back to the top of the dispatcher top
// This is only safe inside the JIT rather than anything outside of it
@@ -0,0 +1,112 @@
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.h b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.h
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.h
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.h
@@ -263,6 +263,8 @@
FEXCore::Core::CpuStateFrame* Frame, void* ucontext);
void RestoreRTFrame_ia32(FEXCore::Core::InternalThreadState* Thread, ArchHelpers::Context::ContextBackup* Context,
FEXCore::Core::CpuStateFrame* Frame, void* ucontext);
+ void ResumeWithGuestGPRs(ArchHelpers::Context::ContextBackup* Context, FEXCore::Core::CpuStateFrame* Frame, void* ucontext,
+ const uint64_t* GuestGPRs, size_t Count, uint64_t Mask);
///< Setup the signal frame for x64.
uint64_t SetupFrame_x64(FEXCore::Core::InternalThreadState* Thread, ArchHelpers::Context::ContextBackup* ContextBackup,
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator/GuestFramesManagement.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator/GuestFramesManagement.cpp
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator/GuestFramesManagement.cpp
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator/GuestFramesManagement.cpp
@@ -122,6 +122,32 @@
}
}
+void SignalDelegator::ResumeWithGuestGPRs(ArchHelpers::Context::ContextBackup* Context, FEXCore::Core::CpuStateFrame* Frame, void* ucontext,
+ const uint64_t* GuestGPRs, size_t Count, uint64_t Mask) {
+#ifdef ARCHITECTURE_arm64
+ const uint32_t Spilled = Context->InSyscallInfo & 0xFFFF;
+ const bool AllSpilled = Spilled == 0xFFFF;
+ if (!AllSpilled && !(Context->Flags & ArchHelpers::Context::ContextFlags::CONTEXT_FLAG_INJIT)) {
+ return;
+ }
+
+ for (size_t i = 0; i < Count && i < Config.SRAGPRCount; i++) {
+ const uint8_t HostReg = Config.SRAGPRMapping[i];
+ const bool InFrame = AllSpilled || (Spilled & (1U << HostReg));
+ const uint64_t Delivered = InFrame ? Frame->State.gregs[i] : ArchHelpers::Context::GetArmReg(ucontext, HostReg);
+ if (((Delivered ^ GuestGPRs[i]) & Mask) == 0) {
+ continue;
+ }
+
+ if (InFrame) {
+ Frame->State.gregs[i] = GuestGPRs[i];
+ } else {
+ ArchHelpers::Context::SetArmReg(ucontext, HostReg, GuestGPRs[i]);
+ }
+ }
+#endif
+}
+
void SignalDelegator::RestoreFrame_x64(FEXCore::Core::InternalThreadState* Thread, ArchHelpers::Context::ContextBackup* Context,
FEXCore::Core::CpuStateFrame* Frame, void* ucontext) {
auto* guest_uctx = reinterpret_cast<FEXCore::x86_64::ucontext_t*>(Context->UContextLocation);
@@ -202,6 +228,27 @@
const auto modulo_i = (i + CurrentOffset) % 8;
memcpy(&Frame->State.mm[modulo_i], &fpstate->_st[i], sizeof(Frame->State.mm[0]));
}
+ } else {
+ uint64_t GuestGPRs[FEXCore::Core::CPUState::NUM_GPRS];
+#define COPY_REG(x) GuestGPRs[FEXCore::X86State::REG_##x] = guest_uctx->uc_mcontext.gregs[FEXCore::x86_64::FEX_REG_##x];
+ COPY_REG(R8);
+ COPY_REG(R9);
+ COPY_REG(R10);
+ COPY_REG(R11);
+ COPY_REG(R12);
+ COPY_REG(R13);
+ COPY_REG(R14);
+ COPY_REG(R15);
+ COPY_REG(RDI);
+ COPY_REG(RSI);
+ COPY_REG(RBP);
+ COPY_REG(RBX);
+ COPY_REG(RDX);
+ COPY_REG(RAX);
+ COPY_REG(RCX);
+ COPY_REG(RSP);
+#undef COPY_REG
+ ResumeWithGuestGPRs(Context, Frame, ucontext, GuestGPRs, FEXCore::Core::CPUState::NUM_GPRS, ~0ULL);
}
}
@@ -280,6 +327,17 @@
const auto modulo_i = (i + CurrentOffset) % 8;
memcpy(&Frame->State.mm[modulo_i], &fpstate->_st[i], 10);
}
+ } else {
+ uint64_t GuestGPRs[8];
+ GuestGPRs[FEXCore::X86State::REG_RAX] = guest_uctx->sc.ax;
+ GuestGPRs[FEXCore::X86State::REG_RCX] = guest_uctx->sc.cx;
+ GuestGPRs[FEXCore::X86State::REG_RDX] = guest_uctx->sc.dx;
+ GuestGPRs[FEXCore::X86State::REG_RBX] = guest_uctx->sc.bx;
+ GuestGPRs[FEXCore::X86State::REG_RSP] = guest_uctx->sc.sp;
+ GuestGPRs[FEXCore::X86State::REG_RBP] = guest_uctx->sc.bp;
+ GuestGPRs[FEXCore::X86State::REG_RSI] = guest_uctx->sc.si;
+ GuestGPRs[FEXCore::X86State::REG_RDI] = guest_uctx->sc.di;
+ ResumeWithGuestGPRs(Context, Frame, ucontext, GuestGPRs, 8, 0xFFFF'FFFFULL);
}
}
@@ -359,6 +417,17 @@
const auto modulo_i = (i + CurrentOffset) % 8;
memcpy(&Frame->State.mm[modulo_i], &fpstate->_st[i], 10);
}
+ } else {
+ uint64_t GuestGPRs[8];
+ GuestGPRs[FEXCore::X86State::REG_RAX] = guest_uctx->uc.uc_mcontext.gregs[FEXCore::x86::FEX_REG_RAX];
+ GuestGPRs[FEXCore::X86State::REG_RCX] = guest_uctx->uc.uc_mcontext.gregs[FEXCore::x86::FEX_REG_RCX];
+ GuestGPRs[FEXCore::X86State::REG_RDX] = guest_uctx->uc.uc_mcontext.gregs[FEXCore::x86::FEX_REG_RDX];
+ GuestGPRs[FEXCore::X86State::REG_RBX] = guest_uctx->uc.uc_mcontext.gregs[FEXCore::x86::FEX_REG_RBX];
+ GuestGPRs[FEXCore::X86State::REG_RSP] = guest_uctx->uc.uc_mcontext.gregs[FEXCore::x86::FEX_REG_RSP];
+ GuestGPRs[FEXCore::X86State::REG_RBP] = guest_uctx->uc.uc_mcontext.gregs[FEXCore::x86::FEX_REG_RBP];
+ GuestGPRs[FEXCore::X86State::REG_RSI] = guest_uctx->uc.uc_mcontext.gregs[FEXCore::x86::FEX_REG_RSI];
+ GuestGPRs[FEXCore::X86State::REG_RDI] = guest_uctx->uc.uc_mcontext.gregs[FEXCore::x86::FEX_REG_RDI];
+ ResumeWithGuestGPRs(Context, Frame, ucontext, GuestGPRs, 8, 0xFFFF'FFFFULL);
}
}
+90
View File
@@ -0,0 +1,90 @@
#define _GNU_SOURCE
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static sigjmp_buf main_env, inner_env;
static volatile int depth, nested_mode;
static volatile long escapes, nested, returns, wrong;
static void fault(long i) {
*(volatile int *)(long)(8 + (i & 7)) = 1;
}
static void segv(int sig, siginfo_t *info, void *context) {
(void)sig;
(void)info;
(void)context;
if (!nested_mode) {
escapes++;
siglongjmp(main_env, 1);
}
if (depth == 1) {
nested++;
siglongjmp(inner_env, 1);
}
depth = 1;
for (volatile int k = 0; k < 3; k++)
if (sigsetjmp(inner_env, 0) == 0)
fault(k);
depth = 0;
escapes++;
siglongjmp(main_env, 1);
}
static void trap(int sig, siginfo_t *info, void *context) {
(void)sig;
(void)info;
(void)context;
returns++;
}
static void install(int onstack) {
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_sigaction = segv;
action.sa_flags = SA_SIGINFO | SA_NODEFER | (onstack ? SA_ONSTACK : 0);
sigaction(SIGSEGV, &action, NULL);
}
static long run(long count, int with_trap) {
long start = escapes;
for (volatile long i = 0; i < count; i++) {
if (with_trap) {
volatile long before = i * 3;
__asm__ volatile("int3" ::: "memory");
if (before != i * 3)
wrong++;
}
if (sigsetjmp(main_env, 1) == 0)
fault(i);
}
return escapes - start;
}
int main(int argc, char **argv) {
long want = argc > 1 ? atol(argv[1]) : 50000;
stack_t ss = {.ss_sp = malloc(262144), .ss_size = 262144};
struct sigaction action;
sigaltstack(&ss, NULL);
memset(&action, 0, sizeof(action));
action.sa_sigaction = trap;
action.sa_flags = SA_SIGINFO;
sigaction(SIGTRAP, &action, NULL);
install(1);
long onstack = run(want, 0);
install(0);
long offstack = run(want, 0);
install(1);
long mixed = run(want, 1);
nested_mode = 1;
long outer = run(want, 0);
printf("onstack=%ld offstack=%ld mixed=%ld returns=%ld outer=%ld nested=%ld wrong=%ld\n", onstack, offstack, mixed, returns,
outer, nested, wrong);
return onstack == want && offstack == want && mixed == want && returns == want && outer == want && nested == 3 * want && !wrong ? 0
: 1;
}
+88
View File
@@ -0,0 +1,88 @@
#define _GNU_SOURCE
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <ucontext.h>
#define MARK 0x5eed
static volatile sig_atomic_t alarms;
static volatile sig_atomic_t give_up;
static void count_alarm(void) {
if (++alarms > 100) {
give_up = 1;
}
}
static void on_alarm(int sig, siginfo_t *si, void *ctx) {
(void)sig;
(void)si;
ucontext_t *uc = ctx;
count_alarm();
#ifdef __x86_64__
uc->uc_mcontext.gregs[REG_R12] = MARK;
#else
uc->uc_mcontext.gregs[REG_ESI] = MARK;
#endif
}
#ifdef __i386__
static void on_alarm_legacy(int sig, struct sigcontext sc) {
(void)sig;
count_alarm();
((volatile struct sigcontext *)&sc)->esi = MARK;
}
#endif
int main(int argc, char **argv) {
int legacy = argc > 1 && strcmp(argv[1], "legacy") == 0;
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_RESTART;
#ifdef __i386__
if (legacy) {
sa.sa_handler = (void (*)(int))on_alarm_legacy;
} else
#endif
{
legacy = 0;
sa.sa_sigaction = on_alarm;
sa.sa_flags |= SA_SIGINFO;
}
sigaction(SIGALRM, &sa, NULL);
struct itimerval it = {{0, 20000}, {0, 20000}};
setitimer(ITIMER_REAL, &it, NULL);
unsigned long spins, seen;
#ifdef __x86_64__
__asm__ volatile("xor %%r12, %%r12\n"
"xor %0, %0\n"
"1: inc %0\n"
"test %%r12, %%r12\n"
"jnz 2f\n"
"cmpl $0, %2\n"
"je 1b\n"
"2: mov %%r12, %1\n"
: "=&r"(spins), "=&r"(seen)
: "m"(give_up)
: "r12", "cc");
#else
__asm__ volatile("xor %%esi, %%esi\n"
"xor %0, %0\n"
"1: inc %0\n"
"test %%esi, %%esi\n"
"jnz 2f\n"
"cmpl $0, %2\n"
"je 1b\n"
"2: mov %%esi, %1\n"
: "=&r"(spins), "=&r"(seen)
: "m"(give_up)
: "esi", "cc");
#endif
struct itimerval off = {{0, 0}, {0, 0}};
setitimer(ITIMER_REAL, &off, NULL);
printf("frame=%s seen=%d alarms=%d spins=%lu\n", legacy ? "legacy" : "rt", seen == MARK, (int)alarms, spins);
return seen == MARK ? 0 : 1;
}
+65
View File
@@ -0,0 +1,65 @@
#define _GNU_SOURCE
#include <cpuid.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#define YMM_STEP(a, b) "vpaddq %%ymm" #b ", %%ymm" #a ", %%ymm" #a "\n"
#define MMX_STEP(a, b) "paddq %%mm" #b ", %%mm" #a "\n"
static int has_avx2(void) {
unsigned a, b, c, d;
if (!__get_cpuid(1, &a, &b, &c, &d) || !(c & bit_AVX) || !(c & bit_OSXSAVE))
return 0;
unsigned lo, hi;
__asm__ volatile("xgetbv" : "=a"(lo), "=d"(hi) : "c"(0));
if ((lo & 6) != 6 || !__get_cpuid_count(7, 0, &a, &b, &c, &d))
return 0;
return (b & bit_AVX2) != 0;
}
static long ymm_round(long n) {
long pid = 0;
for (long i = 0; i < n; i++) {
__asm__ volatile(
YMM_STEP(0, 1) YMM_STEP(1, 2) YMM_STEP(2, 3) YMM_STEP(3, 4) YMM_STEP(4, 5) YMM_STEP(5, 6) YMM_STEP(6, 7) YMM_STEP(7, 8)
YMM_STEP(8, 9) YMM_STEP(9, 10) YMM_STEP(10, 11) YMM_STEP(11, 12) YMM_STEP(12, 13) YMM_STEP(13, 14) YMM_STEP(14, 15)
YMM_STEP(15, 0) YMM_STEP(0, 15) YMM_STEP(1, 14) YMM_STEP(2, 13) YMM_STEP(3, 12) YMM_STEP(4, 11) YMM_STEP(5, 10)
YMM_STEP(6, 9) YMM_STEP(7, 8)
"mov %1, %%eax\n"
"syscall\n"
: "=a"(pid)
: "i"(SYS_getpid)
: "rcx", "r11", "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7", "xmm8", "xmm9", "xmm10",
"xmm11", "xmm12", "xmm13", "xmm14", "xmm15");
}
__asm__ volatile("vzeroupper" ::: "memory");
return pid;
}
static long mmx_round(long n) {
long pid = 0;
for (long i = 0; i < n; i++) {
__asm__ volatile(
MMX_STEP(0, 1) MMX_STEP(1, 2) MMX_STEP(2, 3) MMX_STEP(3, 4) MMX_STEP(4, 5) MMX_STEP(5, 6) MMX_STEP(6, 7) MMX_STEP(7, 0)
"mov %1, %%eax\n"
"syscall\n"
: "=a"(pid)
: "i"(SYS_getpid)
: "rcx", "r11", "memory", "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7");
}
__asm__ volatile("emms" ::: "memory");
return pid;
}
int main(int argc, char **argv) {
long want = argc > 1 ? atol(argv[1]) : 2000000;
int avx = has_avx2();
long done = 0;
for (; done < want; done += 1000) {
if (avx ? ymm_round(1000) <= 0 : mmx_round(1000) <= 0)
return 2;
}
printf("mode=%s syscalls=%ld\n", avx ? "avx" : "mmx", done);
return 0;
}
+49
View File
@@ -0,0 +1,49 @@
#define _GNU_SOURCE
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
static volatile sig_atomic_t alarms;
static void on_alarm(int sig) {
(void)sig;
alarms++;
}
int main(int argc, char **argv) {
int seconds = argc > 1 ? atoi(argv[1]) : 3;
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = on_alarm;
sa.sa_flags = SA_RESTART;
sigaction(SIGALRM, &sa, NULL);
struct itimerval it = {{0, 1000}, {0, 1000}};
setitimer(ITIMER_REAL, &it, NULL);
struct timespec start, now, last;
clock_gettime(CLOCK_MONOTONIC, &start);
last = start;
unsigned long calls = 0, bad = 0;
unsigned long a = 1, b = 3, c = 1 ^ 0x5a5a5a5a5a5a5a5aUL;
do {
clock_gettime(CLOCK_MONOTONIC, &now);
calls++;
__asm__ volatile("" : "+r"(a), "+r"(b), "+r"(c));
if (b != a * 3 || c != (a ^ 0x5a5a5a5a5a5a5a5aUL))
bad++;
if (now.tv_sec < last.tv_sec || (now.tv_sec == last.tv_sec && now.tv_nsec < last.tv_nsec))
bad++;
last = now;
a = a * 6364136223846793005UL + 1442695040888963407UL;
b = a * 3;
c = a ^ 0x5a5a5a5a5a5a5a5aUL;
} while ((now.tv_sec - start.tv_sec) * 1000000000L + (now.tv_nsec - start.tv_nsec) < seconds * 1000000000L);
struct itimerval off = {{0, 0}, {0, 0}};
setitimer(ITIMER_REAL, &off, NULL);
printf("calls=%lu alarms=%d bad=%lu\n", calls, (int)alarms, bad);
return bad || alarms == 0;
}