omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / native / test_freestanding_pathlen.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Freestanding borrowed-byte pathlen differential (A64 host and x86 QEMU). |
| 3 | |
| 4 | The reference is the hosted pathlen scan: a bounded memchr, never past n. |
| 5 | WolframLanguageEvaluator: Sum[n + 1, {n, 0, 256}] + 2 -> 33155 |
| 6 | plus three guard-page lengths (1, 16, 256) = 33158 checks. |
| 7 | |
| 8 | Empty NULL is status 0 and result 0. Nonempty NULL is status 32 and the |
| 9 | result slot is left unchanged. A guard page immediately after the bound must |
| 10 | not be read. |
| 11 | |
| 12 | Six source mutants and one generated-binding mutant must each die on both |
| 13 | targets. A crash counts as a kill. This is host and emulated evidence, not |
| 14 | chip qualification. |
| 15 | """ |
| 16 | import os |
| 17 | import shutil |
| 18 | import subprocess |
| 19 | import sys |
| 20 | import tempfile |
| 21 | from pathlib import Path |
| 22 | |
| 23 | ROOT = Path(__file__).resolve().parents[2] |
| 24 | UNIT = ROOT / "tests/native/freestanding/pathlen.om" |
| 25 | OMC = os.environ.get( |
| 26 | "OMEGA_NATIVE_OMC", |
| 27 | "/tmp/sweep/L1/wt/978/build/T624/seed/seed/out/omc", |
| 28 | ) |
| 29 | OMC2 = os.environ.get( |
| 30 | "OMEGA_NATIVE_OMC2", |
| 31 | str(Path.home() / "work/cudastack/omega-mv/seed/out/omc"), |
| 32 | ) |
| 33 | X86_OMC = os.environ.get( |
| 34 | "OMEGA_NATIVE_X86_OMC", |
| 35 | "/tmp/sweep/L1/wt/924/seed/out/omc", |
| 36 | ) |
| 37 | QEMU = os.environ.get( |
| 38 | "QEMU_X86_64", |
| 39 | str(Path.home() / ".local/opt/qemu-oracle-10.0.14/build/qemu-x86_64"), |
| 40 | ) |
| 41 | CHECKS = 33158 |
| 42 | |
| 43 | HARNESS = r""" |
| 44 | #include "omega_native.h" |
| 45 | #include <stdint.h> |
| 46 | #include <stdio.h> |
| 47 | #include <string.h> |
| 48 | #include <sys/mman.h> |
| 49 | #include <unistd.h> |
| 50 | static int want_of(const uint8_t *p, uint64_t n, uint64_t *out) { |
| 51 | if (n == 0) { *out = 0; return 0; } |
| 52 | if (!p) return 32; |
| 53 | const void *z = memchr(p, 0, (size_t)n); |
| 54 | *out = z ? (uint64_t)((const uint8_t *)z - p) : n; |
| 55 | return 0; |
| 56 | } |
| 57 | static int check(const uint8_t *p, uint64_t n) { |
| 58 | uint64_t got = 0xA5A5A5A5ULL, want = 0; |
| 59 | int ws = want_of(p, n, &want); |
| 60 | uint32_t st = omega2_pathlen(&got, p, n); |
| 61 | if (st != (uint32_t)ws) return 1; |
| 62 | if (st == 0 && got != want) return 2; |
| 63 | if (st != 0 && got != 0xA5A5A5A5ULL) return 3; |
| 64 | return 0; |
| 65 | } |
| 66 | int main(void) { |
| 67 | unsigned checks = 0; |
| 68 | if (check(NULL, 0)) return 10; |
| 69 | checks++; |
| 70 | if (check(NULL, 1)) return 11; |
| 71 | checks++; |
| 72 | unsigned char data[257]; |
| 73 | for (uint64_t n = 0; n <= 256; ++n) { |
| 74 | for (uint64_t split = 0; split <= n; ++split) { |
| 75 | memset(data, 'x', sizeof data); |
| 76 | data[split] = 0; |
| 77 | if (check(data, n)) return 12; |
| 78 | checks++; |
| 79 | } |
| 80 | } |
| 81 | long ps = sysconf(_SC_PAGESIZE); |
| 82 | if (ps < 256) return 13; |
| 83 | void *map = mmap(NULL, (size_t)ps * 2, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 84 | if (map == MAP_FAILED) return 14; |
| 85 | if (mprotect((char *)map + ps, (size_t)ps, PROT_NONE) != 0) return 15; |
| 86 | const uint64_t guards[] = {1, 16, 256}; |
| 87 | for (unsigned g = 0; g < 3; ++g) { |
| 88 | uint64_t n = guards[g]; |
| 89 | unsigned char *p = (unsigned char *)map + ps - (long)n; |
| 90 | memset(p, 0x41, (size_t)n); |
| 91 | if (check(p, n)) return 16; |
| 92 | checks++; |
| 93 | } |
| 94 | if (checks != 33158u) return 17; |
| 95 | printf("PASS pathlen freestanding differential checks=%u\n", checks); |
| 96 | return 0; |
| 97 | } |
| 98 | """ |
| 99 | |
| 100 | # Each source mutant is one unique site in pathlen.om. The binding mutant drops |
| 101 | # the generated nonempty-NULL refusal so the unit is entered on NULL. |
| 102 | SOURCE_MUTANTS = ( |
| 103 | ("off-by-one", "ret i\n", "ret add i 1\n"), |
| 104 | ("wrong-terminator", "if eq b 0\n", "if eq b 1\n"), |
| 105 | ("short-loop", "for i ulong n\n", "for i ulong sub n ulong 1\n"), |
| 106 | ("ignore-absent", "ret n\n", "ret ulong 0\n"), |
| 107 | ("past-index", "b = get a i\n", "b = get a add i 1\n"), |
| 108 | ("past-bound", "for i ulong n\n", "for i ulong add n ulong 1\n"), |
| 109 | ) |
| 110 | BIND_OLD = "if (ombr2_view(sp, &ns, o_a, o_a_len, 1u, 0))" |
| 111 | BIND_NEW = "if (0 && ombr2_view(sp, &ns, o_a, o_a_len, 1u, 0))" |
| 112 | |
| 113 | |
| 114 | def run(cmd, env=None, timeout=120): |
| 115 | return subprocess.run( |
| 116 | cmd, |
| 117 | env=env, |
| 118 | timeout=timeout, |
| 119 | stdout=subprocess.PIPE, |
| 120 | stderr=subprocess.STDOUT, |
| 121 | ) |
| 122 | |
| 123 | |
| 124 | def fail(msg): |
| 125 | print("FAIL " + msg, flush=True) |
| 126 | sys.exit(1) |
| 127 | |
| 128 | |
| 129 | def build(out, unit, target, cc): |
| 130 | env = dict(os.environ) |
| 131 | env["OMEGA_NATIVE_OMC"] = OMC |
| 132 | env["OMEGA_NATIVE_OMC2"] = OMC2 |
| 133 | env["CC"] = cc |
| 134 | cmd = [str(ROOT / "tools/native"), "--freestanding"] |
| 135 | if target == "x86linux": |
| 136 | env["OMEGA_NATIVE_X86_OMC"] = X86_OMC |
| 137 | cmd += ["--target", "x86linux"] |
| 138 | cmd += [str(out), str(unit)] |
| 139 | r = run(cmd, env) |
| 140 | if r.returncode != 0: |
| 141 | sys.stdout.buffer.write(r.stdout) |
| 142 | fail("tools/native %s rc %s" % (target, r.returncode)) |
| 143 | |
| 144 | |
| 145 | def link(out, cc, extra): |
| 146 | src = out / "harness.c" |
| 147 | src.write_text(HARNESS) |
| 148 | cmd = [cc, "-O2", "-Wall", "-Wextra", "-Werror", "-I", str(out), |
| 149 | "-o", str(out / "harness"), str(src), str(out / "omega_native.o")] + extra |
| 150 | r = run(cmd) |
| 151 | if r.returncode != 0: |
| 152 | sys.stdout.buffer.write(r.stdout) |
| 153 | fail("link rc %s" % r.returncode) |
| 154 | |
| 155 | |
| 156 | def execute(out, target): |
| 157 | exe = out / "harness" |
| 158 | if target == "aarch64linux": |
| 159 | cmd = [str(exe)] |
| 160 | else: |
| 161 | cmd = [QEMU, str(exe)] |
| 162 | return run(cmd, timeout=60) |
| 163 | |
| 164 | |
| 165 | def expect_pass(out, target, label): |
| 166 | r = execute(out, target) |
| 167 | text = r.stdout.decode("utf-8", "replace") |
| 168 | if r.returncode != 0 or "PASS pathlen freestanding differential checks=%u" % CHECKS not in text: |
| 169 | sys.stdout.buffer.write(r.stdout) |
| 170 | fail("%s %s positive rc %s" % (target, label, r.returncode)) |
| 171 | print("PASS %s %s" % (target, label), flush=True) |
| 172 | |
| 173 | |
| 174 | def expect_kill(out, target, name): |
| 175 | r = execute(out, target) |
| 176 | text = r.stdout.decode("utf-8", "replace") |
| 177 | if r.returncode == 0 and "PASS pathlen" in text: |
| 178 | fail("%s mutant %s not killed" % (target, name)) |
| 179 | print("PASS %s mutant %s killed rc %s" % (target, name, r.returncode), flush=True) |
| 180 | |
| 181 | |
| 182 | def recompile_object(out, cc): |
| 183 | cmd = [cc, "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror", "-Wmissing-prototypes", |
| 184 | "-ffreestanding", "-fno-builtin", "-fno-stack-protector", |
| 185 | "-include", str(out / "omega_native.h"), |
| 186 | "-c", "-o", str(out / "omega_native.o"), str(out / "omega_native.c")] |
| 187 | r = run(cmd) |
| 188 | if r.returncode != 0: |
| 189 | sys.stdout.buffer.write(r.stdout) |
| 190 | fail("recompile rc %s" % r.returncode) |
| 191 | |
| 192 | |
| 193 | def one_target(target, cc, link_extra, work): |
| 194 | out = work / target |
| 195 | out.mkdir() |
| 196 | build(out, UNIT, target, cc) |
| 197 | link(out, cc, link_extra) |
| 198 | expect_pass(out, target, "differential") |
| 199 | src = UNIT.read_text() |
| 200 | for name, old, new in SOURCE_MUTANTS: |
| 201 | if src.count(old) != 1: |
| 202 | fail("mutant %s site found %s times" % (name, src.count(old))) |
| 203 | mut = work / (name + ".om") |
| 204 | mut.write_text(src.replace(old, new, 1)) |
| 205 | mout = work / (target + "-" + name) |
| 206 | mout.mkdir() |
| 207 | build(mout, mut, target, cc) |
| 208 | link(mout, cc, link_extra) |
| 209 | expect_kill(mout, target, name) |
| 210 | native = (out / "omega_native.c").read_text() |
| 211 | if native.count(BIND_OLD) != 1: |
| 212 | fail("binding site found %s times" % native.count(BIND_OLD)) |
| 213 | (out / "omega_native.c").write_text(native.replace(BIND_OLD, BIND_NEW, 1)) |
| 214 | recompile_object(out, cc) |
| 215 | link(out, cc, link_extra) |
| 216 | expect_kill(out, target, "nonempty-null") |
| 217 | |
| 218 | |
| 219 | def main(): |
| 220 | for p in (OMC, OMC2, X86_OMC, QEMU, UNIT): |
| 221 | if not Path(p).is_file(): |
| 222 | fail("missing " + p) |
| 223 | work = Path(tempfile.mkdtemp(prefix="pathlen-diff-")) |
| 224 | try: |
| 225 | one_target("aarch64linux", "cc", [], work) |
| 226 | one_target("x86linux", "x86_64-linux-gnu-gcc", ["-static"], work) |
| 227 | finally: |
| 228 | shutil.rmtree(work, ignore_errors=True) |
| 229 | print("PASS freestanding pathlen differential A64+x86 mutants=7 checks=%u" % CHECKS) |
| 230 | |
| 231 | |
| 232 | if __name__ == "__main__": |
| 233 | main() |