diff --git a/docs/NATIVE-ABI.md b/docs/NATIVE-ABI.md index 892c042..7c41daf 100644 --- a/docs/NATIVE-ABI.md +++ b/docs/NATIVE-ABI.md @@ -95,6 +95,26 @@ unpatched QEMU 10.0.14. Do not alter callers or weaken signal/thread controls to accommodate the older emulator. The complete integrated suite and its failed/missing-toolchain controls subsequently passed under 10.0.14 (EMULATED). +The suite now checks the emulator before any caller runs. It prints +`INFO qemu executable=`, `INFO qemu sha256=` and `INFO qemu version=` for the +resolved `QEMU_X86_64`, then cross-builds `tests/native/x86/qemu_probe.c` and runs +it in two modes through the same `-L`/`-E` guest command as the callers: + +- `maps` reads `/proc/self/maps`, as glibc does during Rust and Go thread startup. + QEMU 8.2.2 crashes internally (`QEMU internal SIGSEGV {code=MAPERR, addr=0x20}`). +- `sigframe` takes SIGALRM while RSP is on a stack laid out like the owned Omega + stack and records RSP at handler entry in an assembly handler. The SysV ABI + requires RSP+8 to be 16-byte aligned there. QEMU 8.2.2 enters handlers 8 bytes + off, so the C caller's reentry handler faults on the `movaps` stack stores in + `omega2_vlen` (#GP, reported as signal 11). + +A mode that does not print exactly `QEMU-PROBE ok` and exit 0 prints +`NOT_RUN QEMU-ORACLE-UNUSABLE : …`, and the aggregate ends +`NATIVE-X86 NOT_RUN (not run: … qemu-oracle …)` with exit 1. The emulator is judged +by behavior, not by a version list. Set `OMEGA_NATIVE_X86_BAD_QEMU` to a known-bad +emulator to make `test_build_failures.py` require that refusal; unset, that +control prints NOT_RUN. + The replacement oracle comes from , archive SHA256 `c7230968906fdb5540d803e2af1f515e77590a414f2a4dbd3ec9ada3c47083d0`. diff --git a/tests/native/x86/qemu_probe.c b/tests/native/x86/qemu_probe.c new file mode 100644 index 0000000..231a8e8 --- /dev/null +++ b/tests/native/x86/qemu_probe.c @@ -0,0 +1,101 @@ +/* QEMU oracle preflight for tests/native/x86/suite.py. Each mode exercises one emulator + * service the ABI v2 callers depend on, without any Omega code: + * maps the guest reads /proc/self/maps (glibc pthread_getattr_np in Rust and Go + * startup does this); QEMU 8.2.2 crashed internally here (MAPERR addr 0x20). + * sigframe SIGALRM arrives while RSP is on an mmap'd stack laid out like the runtime's + * owned Omega stack (usable bytes above a PROT_NONE guard). The SysV ABI requires + * RSP+8 to be 16-byte aligned at handler entry; compiled handlers rely on it for + * aligned SSE stores. QEMU 8.2.2 entered handlers misaligned, so the C caller's + * reentry handler took #GP and died with SIGSEGV. + * A mode prints "QEMU-PROBE ok" and exits 0 only when the service worked; otherwise + * it exits nonzero, printing what it observed when the guest survives. */ +#include +#include +#include +#include +#include +#include +#include +#include + +#define STACK (8u << 20) +#define GUARD (8u << 20) + +/* Written only by on_alarm: RSP at handler entry, then 1. */ +volatile uint64_t probe_entry_rsp; +volatile int probe_fired; +static unsigned char *lo, *hi; + +/* Call fn with RSP at top (16-byte aligned), then restore the caller's stack. */ +void probe_on_stack(void *top, void (*fn)(void)); +__asm__(".text\n.globl probe_on_stack\n.type probe_on_stack,@function\nprobe_on_stack:\n" + "push %rbp\nmov %rsp,%rbp\nmov %rdi,%rsp\ncall *%rsi\nmov %rbp,%rsp\npop %rbp\nret\n" + ".size probe_on_stack,.-probe_on_stack\n"); + +/* Assembly, so no compiler prologue moves RSP before it is recorded. */ +void on_alarm(int s); +__asm__(".text\n.globl on_alarm\n.type on_alarm,@function\non_alarm:\n" + "mov %rsp,probe_entry_rsp(%rip)\nmovl $1,probe_fired(%rip)\nret\n" + ".size on_alarm,.-on_alarm\n"); + +static void spin(void) +{ + struct itimerval it = {{0, 0}, {0, 2000}}; + setitimer(ITIMER_REAL, &it, 0); + for (unsigned long k = 0; k < 4000000000ul && !probe_fired; k++) + __asm__ volatile("" ::: "memory"); +} + +static int maps(void) +{ + char buf[4096]; + int fd = open("/proc/self/maps", O_RDONLY | O_CLOEXEC); + ssize_t n = fd < 0 ? -1 : read(fd, buf, sizeof buf - 1); + if (fd >= 0) + close(fd); + if (n <= 0) + return 1; + buf[n] = 0; + return strchr(buf, '-') ? 0 : 1; +} + +static int sigframe(void) +{ + unsigned char *p = mmap(0, STACK + GUARD, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); + if (p == MAP_FAILED || mprotect(p, GUARD, PROT_NONE) != 0) + return 1; + lo = p + GUARD; + hi = p + GUARD + STACK; + struct sigaction sa; + memset(&sa, 0, sizeof sa); + sa.sa_handler = on_alarm; + if (sigaction(SIGALRM, &sa, 0) != 0) + return 1; + probe_on_stack(hi - 256, spin); + if (!probe_fired) { + printf("QEMU-PROBE sigframe: SIGALRM not delivered\n"); + return 1; + } + uint64_t rsp = probe_entry_rsp; + if (rsp <= (uintptr_t)lo || rsp >= (uintptr_t)hi) { + printf("QEMU-PROBE sigframe: handler ran off the owned stack (rsp %#llx)\n", (unsigned long long)rsp); + return 1; + } + if ((rsp + 8) % 16) { + printf("QEMU-PROBE sigframe: handler entry RSP+8 misaligned by %llu bytes\n", + (unsigned long long)((rsp + 8) % 16)); + return 1; + } + return 0; +} + +int main(int argc, char **argv) +{ + if (argc != 2) + return 2; + int rc = !strcmp(argv[1], "maps") ? maps() : !strcmp(argv[1], "sigframe") ? sigframe() : 2; + if (!rc) + printf("QEMU-PROBE %s ok\n", argv[1]); + return rc; +} diff --git a/tests/native/x86/suite.py b/tests/native/x86/suite.py index 6fb6e2c..f171d25 100755 --- a/tests/native/x86/suite.py +++ b/tests/native/x86/suite.py @@ -7,6 +7,9 @@ rustc with x86_64-unknown-linux-gnu std, Zig 0.13, Go with cgo, qemu-x86_64, and OMEGA_NATIVE_X86_PYTHON (an x86 Linux interpreter, NOT host python). OMEGA_NATIVE_X86_SYSROOT defaults to /usr/x86_64-linux-gnu; an extracted Python runtime may supply OMEGA_NATIVE_X86_LIBPATH. Missing tools refuse aggregate PASS. +The emulator's sha256 and version are recorded, and qemu_probe.c must show that it +provides the guest services the callers use; otherwise QEMU-ORACLE-UNUSABLE refuses +aggregate PASS before any caller runs. No CPU timing or native hardware qualification is inferred from emulation. """ import hashlib @@ -20,6 +23,7 @@ import subprocess import sys ROOT = Path(__file__).resolve().parents[3] +HERE = Path(__file__).resolve().parent T = ROOT / "tests/native/v2" N = ROOT / "tests/native" OUTPUTS = {"c": "c", "h": "h", "python": "py", "rust": "rs", "zig": "zig", "go": "go", @@ -93,6 +97,33 @@ class Suite: cmd += ["-E", "LD_LIBRARY_PATH=" + os.environ["OMEGA_NATIVE_X86_LIBPATH"]] return cmd + [str(exe), *map(str, args)] + def qemu_oracle(self): + # Identify the emulator, then probe the guest services the callers depend on. + # A probe, not a version string: a patched or rebuilt QEMU is judged by behavior. + exe = Path(shutil.which(self.qemu[0])).resolve() + print("INFO qemu executable=" + str(exe), flush=True) + print("INFO qemu sha256=" + digest(exe.read_bytes()), flush=True) + version = self.must(self.qemu + ["--version"]).decode(errors="replace").splitlines() + print("INFO qemu version=" + (version[0] if version else "(none)"), flush=True) + folder = self.out / "qemu-probe" + folder.mkdir() + probe = folder / "qemu_probe" + self.must(self.cc + ["-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror", + str(HERE / "qemu_probe.c"), "-o", str(probe)]) + usable = True + for mode in ("maps", "sigframe"): + result = self.call(self.guest(probe, mode)) + if result.returncode == 0 and result.stdout == f"QEMU-PROBE {mode} ok\n".encode(): + print(f"INFO qemu oracle {mode} ok", flush=True) + continue + usable = False + seen = (result.stdout + result.stderr).decode(errors="replace").strip().splitlines() + print(f"NOT_RUN QEMU-ORACLE-UNUSABLE {mode}: exit={result.returncode} " + + (seen[-1] if seen else "(no output)"), flush=True) + if not usable: + self.missing.append("qemu-oracle") + return usable + def preflight(self): tools = {"c": self.cc, "cpp": self.cxx, "rust": self.rust, "zig": self.zig, "go": self.go, "qemu": self.qemu, "nm": self.nm, "objdump": self.objdump} @@ -113,6 +144,8 @@ class Suite: and digest(Path(path).read_bytes()) == match.group(1))) if self.missing or self.failed: return False + if not self.qemu_oracle(): + return False rustlib = self.must(self.rust + ["--print", "target-libdir", "--target", "x86_64-unknown-linux-gnu"]).decode().strip() if not list(Path(rustlib).glob("libstd-*.rlib")): self.missing.append("rust-std-x86_64") diff --git a/tests/native/x86/test_build_failures.py b/tests/native/x86/test_build_failures.py index e90ca17..0c25be5 100755 --- a/tests/native/x86/test_build_failures.py +++ b/tests/native/x86/test_build_failures.py @@ -2,6 +2,9 @@ """Issue #34 controls: no missing/failed language can produce aggregate PASS. Runs the complete x86 boundary suite for each control and the positive case. Requires the same real toolchains/interpreter as run.sh; never emulates a build. +OMEGA_NATIVE_X86_BAD_QEMU, when set, names an emulator known to break the callers +(for example Ubuntu's QEMU 8.2.2); the suite must refuse it with +QEMU-ORACLE-UNUSABLE before any caller runs. Unset, that control is NOT_RUN. Optional EMPTY_OUTPUT retains every case's stdout, command/status and numbered build/run logs. By default a fresh timestamped directory is created under @@ -67,6 +70,16 @@ def main(): print("FAIL missing-toolchain control; logs: " + str(out)) return 1 print("PASS missing Zig and Go refuse aggregate PASS (#34)", flush=True) + bad = env.get("OMEGA_NATIVE_X86_BAD_QEMU") + if bad: + run = run_case("bad-qemu", dict(env, QEMU_X86_64=bad)) + if run.returncode == 0 or b"NOT_RUN QEMU-ORACLE-UNUSABLE" not in run.stdout or b"NATIVE-X86 NOT_RUN" not in run.stdout or b"caller EMULATED" in run.stdout or b"EMULATED PASS" in run.stdout: + sys.stdout.buffer.write(run.stdout) + print("FAIL known-bad QEMU control: emulator was not refused; logs: " + str(out)) + return 1 + print("PASS known-bad QEMU refused before any caller ran", flush=True) + else: + print("INFO known-bad QEMU control NOT_RUN: OMEGA_NATIVE_X86_BAD_QEMU unset", flush=True) run = run_case("positive", env) sys.stdout.buffer.write(run.stdout) if run.returncode != 0 or b"NATIVE-X86 EMULATED PASS (six languages built and run; native x86 NOT_RUN)" not in run.stdout: