Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-bridge/blob/main/tests/native/x86/qemu_probe.c # omega-bridge Omega-first language interoperability: checked conversion backends and native language bindings Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-bridge / tests / native / x86 / qemu_probe.c` 101 lines ยท 3742 bytes Raw Blame History 1 /* QEMU oracle preflight for tests/native/x86/suite.py. Each mode exercises one emulator 2 * service the ABI v2 callers depend on, without any Omega code: 3 * maps the guest reads /proc/self/maps (glibc pthread_getattr_np in Rust and Go 4 * startup does this); QEMU 8.2.2 crashed internally here (MAPERR addr 0x20). 5 * sigframe SIGALRM arrives while RSP is on an mmap'd stack laid out like the runtime's 6 * owned Omega stack (usable bytes above a PROT_NONE guard). The SysV ABI requires 7 * RSP+8 to be 16-byte aligned at handler entry; compiled handlers rely on it for 8 * aligned SSE stores. QEMU 8.2.2 entered handlers misaligned, so the C caller's 9 * reentry handler took #GP and died with SIGSEGV. 10 * A mode prints "QEMU-PROBE ok" and exits 0 only when the service worked; otherwise 11 * it exits nonzero, printing what it observed when the guest survives. */ 12 #include 13 #include 14 #include 15 #include 16 #include 17 #include 18 #include 19 #include 20 21 #define STACK (8u << 20) 22 #define GUARD (8u << 20) 23 24 /* Written only by on_alarm: RSP at handler entry, then 1. */ 25 volatile uint64_t probe_entry_rsp; 26 volatile int probe_fired; 27 static unsigned char *lo, *hi; 28 29 /* Call fn with RSP at top (16-byte aligned), then restore the caller's stack. */ 30 void probe_on_stack(void *top, void (*fn)(void)); 31 __asm__(".text\n.globl probe_on_stack\n.type probe_on_stack,@function\nprobe_on_stack:\n" 32 "push %rbp\nmov %rsp,%rbp\nmov %rdi,%rsp\ncall *%rsi\nmov %rbp,%rsp\npop %rbp\nret\n" 33 ".size probe_on_stack,.-probe_on_stack\n"); 34 35 /* Assembly, so no compiler prologue moves RSP before it is recorded. */ 36 void on_alarm(int s); 37 __asm__(".text\n.globl on_alarm\n.type on_alarm,@function\non_alarm:\n" 38 "mov %rsp,probe_entry_rsp(%rip)\nmovl $1,probe_fired(%rip)\nret\n" 39 ".size on_alarm,.-on_alarm\n"); 40 41 static void spin(void) 42 { 43 struct itimerval it = {{0, 0}, {0, 2000}}; 44 setitimer(ITIMER_REAL, &it, 0); 45 for (unsigned long k = 0; k < 4000000000ul && !probe_fired; k++) 46 __asm__ volatile("" ::: "memory"); 47 } 48 49 static int maps(void) 50 { 51 char buf[4096]; 52 int fd = open("/proc/self/maps", O_RDONLY | O_CLOEXEC); 53 ssize_t n = fd < 0 ? -1 : read(fd, buf, sizeof buf - 1); 54 if (fd >= 0) 55 close(fd); 56 if (n <= 0) 57 return 1; 58 buf[n] = 0; 59 return strchr(buf, '-') ? 0 : 1; 60 } 61 62 static int sigframe(void) 63 { 64 unsigned char *p = mmap(0, STACK + GUARD, PROT_READ | PROT_WRITE, 65 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); 66 if (p == MAP_FAILED || mprotect(p, GUARD, PROT_NONE) != 0) 67 return 1; 68 lo = p + GUARD; 69 hi = p + GUARD + STACK; 70 struct sigaction sa; 71 memset(&sa, 0, sizeof sa); 72 sa.sa_handler = on_alarm; 73 if (sigaction(SIGALRM, &sa, 0) != 0) 74 return 1; 75 probe_on_stack(hi - 256, spin); 76 if (!probe_fired) { 77 printf("QEMU-PROBE sigframe: SIGALRM not delivered\n"); 78 return 1; 79 } 80 uint64_t rsp = probe_entry_rsp; 81 if (rsp <= (uintptr_t)lo || rsp >= (uintptr_t)hi) { 82 printf("QEMU-PROBE sigframe: handler ran off the owned stack (rsp %#llx)\n", (unsigned long long)rsp); 83 return 1; 84 } 85 if ((rsp + 8) % 16) { 86 printf("QEMU-PROBE sigframe: handler entry RSP+8 misaligned by %llu bytes\n", 87 (unsigned long long)((rsp + 8) % 16)); 88 return 1; 89 } 90 return 0; 91 } 92 93 int main(int argc, char **argv) 94 { 95 if (argc != 2) 96 return 2; 97 int rc = !strcmp(argv[1], "maps") ? maps() : !strcmp(argv[1], "sigframe") ? sigframe() : 2; 98 if (!rc) 99 printf("QEMU-PROBE %s ok\n", argv[1]); 100 return rc; 101 }