sovereign-intelligence
Front door to Drake Stapleton's sovereign stack.
Brought by drakestapleton
sovereign-intelligence / evidence / x86-native / qualify-x86-native.patch
| 1 | diff --git a/seed/omc/qualify-x86.py b/seed/omc/qualify-x86.py |
| 2 | index 9e5bf897..76e4a826 100755 |
| 3 | --- a/seed/omc/qualify-x86.py |
| 4 | +++ b/seed/omc/qualify-x86.py |
| 5 | @@ -4,6 +4,10 @@ |
| 6 | Run on Linux AArch64, from any directory: |
| 7 | python3 seed/omc/qualify-x86.py --compiler /absolute/path/to/omc |
| 8 | Requires Python 3.10+, qemu-x86_64 and GNU x86-64 objdump or llvm-objdump. |
| 9 | +Or run on Linux x86-64 with --host x86-native: x86-64 programs run directly and |
| 10 | +AArch64 programs run under qemu-aarch64, which needs an enabled binfmt_misc entry |
| 11 | +for AArch64 ELF executables with flag P (Ubuntu: qemu-user-static's |
| 12 | +/usr/lib/binfmt.d/qemu-aarch64.conf). The assertions are the same in both modes. |
| 13 | Use --inventory for corpus categorization without compiling/running anything. |
| 14 | |
| 15 | All outputs go to stdout and a new TemporaryDirectory, removed on exit. Existing |
| 16 | @@ -69,6 +73,29 @@ QEMU_IOCTL_EXCERPT = b""" if (ie->target_cmd == 0) { |
| 17 | return -TARGET_ENOTTY; |
| 18 | } |
| 19 | """ |
| 20 | +# --host x86-native: the reviewed qemu-aarch64, Ubuntu 24.04 qemu-user-static 1:8.2.2+ds-0ubuntu1.18. |
| 21 | +# Its linux-user/syscall.c (the orig tarball with the package's patch series applied; three |
| 22 | +# patches touch it, none near do_ioctl) holds QEMU_IOCTL_EXCERPT byte for byte at lines 5601-5605. |
| 23 | +QEMU_A64_ORACLE_SHA256 = "e4f8d99e9ff69c3cefffab71cee358ce2af1ecba1282d04c3eeb44ef76f5a71e" |
| 24 | +QEMU_ATTRIBUTION = { |
| 25 | + QEMU_ORACLE_SHA256: { |
| 26 | + "upstream_source": QEMU_IOCTL_URL, |
| 27 | + "upstream_source_sha256": QEMU_SYSCALL_SHA256, |
| 28 | + "upstream_checked_source_path": "/home/drakestapleton/work/bridge-x86/build/qemu-oracle/qemu-10.0.14/linux-user/syscall.c", |
| 29 | + "upstream_source_archive": "https://download.qemu.org/qemu-10.0.14.tar.xz", |
| 30 | + "upstream_source_archive_sha256": QEMU_SOURCE_ARCHIVE_SHA256}, |
| 31 | + QEMU_A64_ORACLE_SHA256: { |
| 32 | + "upstream_source": "qemu_8.2.2+ds-0ubuntu1.18 linux-user/syscall.c#L5601-L5605, patch series applied", |
| 33 | + "upstream_source_sha256": "f50db23a66cf8e9af73725c27b6dae09db2dbe3c1bbc3dcf5f50942d14bc6873", |
| 34 | + "upstream_checked_source_path": "qemu-8.2.2/linux-user/syscall.c", |
| 35 | + "upstream_source_archive": "http://archive.ubuntu.com/ubuntu/pool/main/q/qemu/qemu_8.2.2+ds.orig.tar.xz", |
| 36 | + "upstream_source_archive_sha256": "5e692dd20950a819ff6550cd3974e00319a93f47c0a15f808c58df1a9e4d2478", |
| 37 | + "upstream_patch_archive": "http://archive.ubuntu.com/ubuntu/pool/main/q/qemu/qemu_8.2.2+ds-0ubuntu1.18.debian.tar.xz", |
| 38 | + "upstream_patch_archive_sha256": "7f1993808bbe15fee04c13aeb8ab5bc4f011b0b3b3a15d511c19d8956b34dc48"}, |
| 39 | +} |
| 40 | +# --host x86-native: the x86 arm's launcher slot, where an AArch64 host names qemu-x86_64. |
| 41 | +X86_DIRECT = "x86-64-direct" |
| 42 | +HOST = {"mode": "aarch64"} # set once by main() |
| 43 | |
| 44 | # Inspected host fixtures at the source revision below. These are not authority |
| 45 | # to run a changed fixture: hashes pin exact reviewed effects. Drivers use local |
| 46 | @@ -809,12 +836,59 @@ def limits(cpu_seconds=None, nofile=256): |
| 47 | resource.setrlimit(resource.RLIMIT_CPU, (cpu_seconds, cpu_seconds + 2)) |
| 48 | |
| 49 | |
| 50 | +def launch(command): |
| 51 | + """Popen argv and executable. The x86 arm's [X86_DIRECT, "-0", argv0, program, *args] or |
| 52 | + [X86_DIRECT, program, *args] runs program directly with the argv qemu-x86_64 would give it. |
| 53 | + AArch64 programs, the corpus's own exec children included, reach qemu-aarch64 through the |
| 54 | + binfmt_misc entry a64_binfmt() verified. Every other command is unchanged.""" |
| 55 | + if not command or command[0] != X86_DIRECT: |
| 56 | + return command, None |
| 57 | + if command[1] == "-0": |
| 58 | + return [command[2], *command[4:]], command[3] |
| 59 | + return list(command[1:]), command[1] |
| 60 | + |
| 61 | + |
| 62 | +def a64_binfmt(qemu): |
| 63 | + """--host x86-native: the one enabled binfmt_misc entry that hands AArch64 ELF executables to |
| 64 | + qemu with argv[0] kept (flag P), and no entry that would take an x86-64 ELF away from the kernel.""" |
| 65 | + def header(machine): # ELF64, little-endian, version 1, System V, ET_EXEC, e_machine |
| 66 | + return b"\x7fELF\x02\x01\x01" + bytes(9) + b"\x02\x00" + machine |
| 67 | + found = [] |
| 68 | + for entry in sorted(Path("/proc/sys/fs/binfmt_misc").iterdir()): |
| 69 | + if entry.name in ("register", "status"): |
| 70 | + continue |
| 71 | + text = entry.read_text() |
| 72 | + lines = text.splitlines() |
| 73 | + fields = dict(line.split(" ", 1) for line in lines[1:] if " " in line) |
| 74 | + if lines[:1] != ["enabled"] or "magic" not in fields or fields.get("offset") != "0": |
| 75 | + continue |
| 76 | + magic = bytes.fromhex(fields["magic"]) |
| 77 | + mask = bytes.fromhex(fields.get("mask", "ff" * len(magic))) |
| 78 | + def matches(image): |
| 79 | + return all(a & m == b & m for a, b, m in zip(image, magic, mask)) |
| 80 | + if matches(header(b"\x3e\x00")): |
| 81 | + raise Failure(f"binfmt_misc entry {entry.name} would run x86-64 programs through {fields.get('interpreter')}") |
| 82 | + if matches(header(b"\xb7\x00")): |
| 83 | + found.append((entry, text, fields)) |
| 84 | + if len(found) != 1: |
| 85 | + raise Failure(f"--host x86-native needs exactly one enabled AArch64 binfmt_misc entry, found {len(found)}") |
| 86 | + entry, text, fields = found[0] |
| 87 | + if os.path.realpath(fields["interpreter"]) != os.path.realpath(qemu) or "P" not in fields.get("flags:", ""): |
| 88 | + raise Failure(f"binfmt_misc {entry.name} must name {qemu} with flag P: {text!r}") |
| 89 | + return {"binfmt_entry": str(entry), "binfmt": text} |
| 90 | + |
| 91 | + |
| 92 | def run(command, data, cwd, timeout, *, extra_env=None, cpu_seconds=None, nofile=256): |
| 93 | # Regular-file stdin matches seekable-input fixtures; no shell interpolation. |
| 94 | with tempfile.TemporaryFile() as inp, tempfile.TemporaryFile() as out, tempfile.TemporaryFile() as err: |
| 95 | inp.write(data) |
| 96 | inp.seek(0) |
| 97 | - process = subprocess.Popen(command, stdin=inp, stdout=out, stderr=err, cwd=cwd, |
| 98 | + command, executable = launch(command) |
| 99 | + hard = resource.getrlimit(resource.RLIMIT_NOFILE)[1] |
| 100 | + if hard != resource.RLIM_INFINITY and nofile > hard: |
| 101 | + # Without CAP_SYS_RESOURCE no child here can get more descriptors than this hard limit. |
| 102 | + raise Failure(f"BLOCKED_ON host RLIMIT_NOFILE: hard limit {hard}, the fixture needs {nofile}") |
| 103 | + process = subprocess.Popen(command, executable=executable, stdin=inp, stdout=out, stderr=err, cwd=cwd, |
| 104 | env={"LC_ALL": "C", "PATH": os.defpath, "HOME": ".", "OMTEST": "hello", **(extra_env or {})}, |
| 105 | start_new_session=True, preexec_fn=lambda: limits(cpu_seconds, nofile)) |
| 106 | try: |
| 107 | @@ -1136,7 +1210,10 @@ def ioctl_emulator_artifact(case, driver, arun, xrun, qemu, argv0, args, cwd, ti |
| 108 | # QEMU's own tracer, after the pinned native fixture has passed its EBADF |
| 109 | # assertions. Unknown ioctls are rejected before host ioctl(fd,cmd,arg). |
| 110 | request = {"devcall": 3223340587, "uvmctl": 805306369}.get(driver) |
| 111 | - if digest(Path(qemu).read_bytes()) != QEMU_ORACLE_SHA256: |
| 112 | + # --host x86-native passes the x86 run as arun and the qemu-aarch64 run as xrun. |
| 113 | + qemu_sha256 = digest(Path(qemu).read_bytes()) |
| 114 | + attribution = QEMU_ATTRIBUTION.get(qemu_sha256) |
| 115 | + if attribution is None: |
| 116 | return None # No source attribution for an unreviewed QEMU executable. |
| 117 | if request is None or arun[0] != 0 or xrun != (80, b"", b""): |
| 118 | return None |
| 119 | @@ -1155,12 +1232,8 @@ def ioctl_emulator_artifact(case, driver, arun, xrun, qemu, argv0, args, cwd, ti |
| 120 | "request": request, "native_required_errno": "EBADF", |
| 121 | "qemu_observed_errno": "ENOTTY", "qemu_trace_line": line.decode(), |
| 122 | "qemu_trace_sha256": digest(trace[2]), |
| 123 | - "upstream_source": QEMU_IOCTL_URL, |
| 124 | - "upstream_source_sha256": QEMU_SYSCALL_SHA256, |
| 125 | - "upstream_checked_source_path": "/home/drakestapleton/work/bridge-x86/build/qemu-oracle/qemu-10.0.14/linux-user/syscall.c", |
| 126 | - "upstream_source_archive": "https://download.qemu.org/qemu-10.0.14.tar.xz", |
| 127 | - "upstream_source_archive_sha256": QEMU_SOURCE_ARCHIVE_SHA256, |
| 128 | - "qemu_binary_sha256": QEMU_ORACLE_SHA256, |
| 129 | + **attribution, |
| 130 | + "qemu_binary_sha256": qemu_sha256, |
| 131 | "upstream_excerpt": QEMU_IOCTL_EXCERPT.decode(), |
| 132 | "upstream_excerpt_sha256": digest(QEMU_IOCTL_EXCERPT)} |
| 133 | return None |
| 134 | @@ -1378,7 +1451,8 @@ def _exercise_bridge(plan, compiler, qemu, options, scratch, state): |
| 135 | raise Failure("BLOCKED_ON BridgeX86: baseline compiler hash does not match its exact seeded generator pin") |
| 136 | if selected_sha != digest(Path(compiler).read_bytes()): |
| 137 | raise Failure("BLOCKED_ON BridgeX86: bridge compiler does not match selected qualification candidate") |
| 138 | - native_cc, x86_cc = shutil.which("cc"), shutil.which("x86_64-linux-gnu-gcc") |
| 139 | + native_cc = shutil.which("aarch64-linux-gnu-gcc" if HOST["mode"] == "x86-native" else "cc") |
| 140 | + x86_cc = shutil.which("x86_64-linux-gnu-gcc") |
| 141 | nm = shutil.which("nm") |
| 142 | if not native_cc or not x86_cc or not nm: |
| 143 | raise Failure("BLOCKED_ON BridgeX86: native cc, x86_64-linux-gnu-gcc and nm are required") |
| 144 | @@ -1492,6 +1566,10 @@ def main(): |
| 145 | parser.add_argument("--compiler", type=Path) |
| 146 | parser.add_argument("--x86-compiler", type=Path, help="optional x86-hosted paired compiler; otherwise built once from exact current seeded source") |
| 147 | parser.add_argument("--qemu", default="qemu-x86_64") |
| 148 | + parser.add_argument("--host", choices=("aarch64", "x86-native"), default="aarch64", |
| 149 | + help="x86-native: on Linux x86-64, run x86-64 programs directly and AArch64 programs under --qemu-aarch64") |
| 150 | + parser.add_argument("--qemu-aarch64", default="qemu-aarch64-static", |
| 151 | + help="--host x86-native: the qemu-aarch64 that the AArch64 binfmt_misc entry names") |
| 152 | parser.add_argument("--bridge", type=Path, help="reviewed x86-capable omega-bridge executable for external ABI drivers") |
| 153 | parser.add_argument("--bridge-baseline-compiler", type=Path, default=Path("/tmp/bridge-x86-baseline-omc"), |
| 154 | help="exact 734f0a39 seeded compiler required by the bridge generator") |
| 155 | @@ -1532,18 +1610,38 @@ def main(): |
| 156 | if not options.inventory: |
| 157 | if not options.compiler or not options.compiler.is_file(): |
| 158 | parser.error("--compiler must name an existing compiler") |
| 159 | - if platform.system() != "Linux" or platform.machine() not in ("aarch64", "arm64"): |
| 160 | + if options.host == "x86-native": |
| 161 | + if platform.system() != "Linux" or platform.machine() not in ("x86_64", "amd64"): |
| 162 | + parser.error("--host x86-native requires Linux x86-64") |
| 163 | + qemu_a64 = shutil.which(options.qemu_aarch64) |
| 164 | + if not qemu_a64: |
| 165 | + parser.error("--host x86-native requires qemu-aarch64 (--qemu-aarch64)") |
| 166 | + try: |
| 167 | + binfmt = a64_binfmt(qemu_a64) |
| 168 | + except (OSError, ValueError, Failure) as error: |
| 169 | + parser.error(str(error)) |
| 170 | + HOST.update(mode="x86-native", qemu_aarch64=qemu_a64) |
| 171 | + qemu = X86_DIRECT |
| 172 | + cpu = re.search(r"(?m)^model name\s*:\s*(.*)$", Path("/proc/cpuinfo").read_text()) |
| 173 | + emulation = {"host": "x86-native", "x86_64_execution": "NATIVE", "aarch64_execution": "EMULATED", |
| 174 | + "qemu_aarch64": qemu_a64, "qemu_aarch64_sha256": digest(Path(qemu_a64).read_bytes()), |
| 175 | + "qemu_aarch64_version": run([qemu_a64, "--version"], b"", ROOT, options.timeout)[1].decode().strip(), |
| 176 | + **binfmt, "cpu_model": cpu[1] if cpu else None, |
| 177 | + "kernel": " ".join(platform.uname()[2:4])} |
| 178 | + elif platform.system() != "Linux" or platform.machine() not in ("aarch64", "arm64"): |
| 179 | parser.error("native default baseline requires Linux AArch64") |
| 180 | + else: |
| 181 | + qemu = shutil.which(options.qemu) |
| 182 | + emulation = {"qemu": qemu, "qemu_sha256": digest(Path(qemu).read_bytes()) if qemu else None, |
| 183 | + "qemu_version": run([qemu, "--version"], b"", ROOT, options.timeout)[1].decode().strip() if qemu else None} |
| 184 | compiler = str(options.compiler.resolve()) |
| 185 | - qemu = shutil.which(options.qemu) |
| 186 | objdump = shutil.which(options.objdump) if options.objdump else next( |
| 187 | (p for name in ("x86_64-linux-gnu-objdump", "llvm-objdump", "objdump") if (p := shutil.which(name))), None) |
| 188 | if not qemu or not objdump: |
| 189 | parser.error("qemu-x86_64 and an x86-64-capable GNU/LLVM objdump are required") |
| 190 | llvm = "llvm" in Path(objdump).name |
| 191 | print(json.dumps({"compiler": compiler, "compiler_sha256": digest(Path(compiler).read_bytes()), |
| 192 | - "qemu": qemu, "qemu_sha256": digest(Path(qemu).read_bytes()), |
| 193 | - "qemu_version": run([qemu, "--version"], b"", ROOT, options.timeout)[1].decode().strip(), |
| 194 | + **emulation, |
| 195 | "objdump": objdump, "platform": platform.platform(), "timeout_seconds": options.timeout, |
| 196 | "bridge_big_timeout_seconds": options.bridge_big_timeout})) |
| 197 | cache = OrderedDict() |
| 198 | @@ -1965,8 +2063,12 @@ def main(): |
| 199 | (driver == "cpu-limit" and arun[0] == xrun[0] == case.expected_exit == -signal.SIGXCPU)): |
| 200 | raise Failure(f"runtime signal: default={arun[0]} x86={xrun[0]}") |
| 201 | if arun != xrun: |
| 202 | - artifact = ioctl_emulator_artifact(case, driver, arun, xrun, qemu, argv0, |
| 203 | - args, emulated, options.timeout) |
| 204 | + if HOST["mode"] == "x86-native": # here the AArch64 arm is the emulated one |
| 205 | + artifact = ioctl_emulator_artifact(case, driver, xrun, arun, HOST["qemu_aarch64"], argv0, |
| 206 | + args, native, options.timeout) |
| 207 | + else: |
| 208 | + artifact = ioctl_emulator_artifact(case, driver, arun, xrun, qemu, argv0, |
| 209 | + args, emulated, options.timeout) |
| 210 | if artifact: |
| 211 | record("NOT_RUN", case.name, "EMULATOR_ARTIFACT: exact invalid-fd ioctl returned QEMU ENOTTY instead of native EBADF", |
| 212 | source_sha256=key, evidence=case.evidence, emulator_artifact=artifact, |