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
66 lines
2.2 KiB
C
66 lines
2.2 KiB
C
#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;
|
|
}
|