omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / seed / qualification / om2 / oracle / leak / fuzz.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # seed/qualification/om2/oracle/leak/fuzz.py: decoder differential for leakscan.om (ORACLE, outside the |
| 4 | # delivered closure). It builds a word file from the 32-bit instruction constants written in omc.om |
| 5 | # (every number 2^24 .. 2^32-1 outside note lines that GNU objdump decodes as an instruction), each |
| 6 | # with random bits flipped in its register/immediate fields (or, for 1 in 6, in all 32 bits), drops |
| 7 | # the words that make objdump 2.42 abort (an assertion in aarch64-dis.c on some SVE encodings), and |
| 8 | # writes the result. xcheck.py --subset then compares leakscan's listing with objdump's. |
| 9 | # Usage: fuzz.py OMC_OM SEED COUNT OUT.bin |
| 10 | import random |
| 11 | import re |
| 12 | import struct |
| 13 | import subprocess |
| 14 | import sys |
| 15 | import tempfile |
| 16 | |
| 17 | |
| 18 | def objdump_ok(words, tmp): |
| 19 | with open(tmp, "wb") as f: |
| 20 | f.write(b"".join(struct.pack("<I", w) for w in words)) |
| 21 | return subprocess.run(["objdump", "-D", "-b", "binary", "-m", "aarch64", tmp], |
| 22 | capture_output=True).returncode == 0 |
| 23 | |
| 24 | |
| 25 | def clean(words, tmp): |
| 26 | if objdump_ok(words, tmp): |
| 27 | return words |
| 28 | if len(words) == 1: |
| 29 | return [] |
| 30 | h = len(words) // 2 |
| 31 | return clean(words[:h], tmp) + clean(words[h:], tmp) |
| 32 | |
| 33 | |
| 34 | def bases(omc_om, tmp): |
| 35 | nums = set() |
| 36 | for line in open(omc_om): |
| 37 | if line.startswith("note"): |
| 38 | continue |
| 39 | for m in re.finditer(r"\b(\d{8,10})\b", line): |
| 40 | v = int(m.group(1)) |
| 41 | if 1 << 24 <= v < 1 << 32: |
| 42 | nums.add(v) |
| 43 | nums = sorted(nums) |
| 44 | with open(tmp, "wb") as f: |
| 45 | f.write(b"".join(struct.pack("<I", w) for w in nums)) |
| 46 | out = subprocess.run(["objdump", "-D", "-b", "binary", "-m", "aarch64", tmp], |
| 47 | check=True, capture_output=True, text=True).stdout |
| 48 | keep = [] |
| 49 | for line in out.splitlines(): |
| 50 | m = re.match(r"^\s*[0-9a-f]+:\t([0-9a-f]{8}) \t(\S+)", line) |
| 51 | if m and m.group(2) != ".inst" and "undefined" not in line: |
| 52 | keep.append(int(m.group(1), 16)) |
| 53 | return keep |
| 54 | |
| 55 | |
| 56 | def main(): |
| 57 | if len(sys.argv) != 5: |
| 58 | print("usage: fuzz.py OMC_OM SEED COUNT OUT.bin", file=sys.stderr) |
| 59 | return 90 |
| 60 | random.seed(int(sys.argv[2])) |
| 61 | with tempfile.TemporaryDirectory() as d: |
| 62 | tmp = d + "/t.bin" |
| 63 | base = bases(sys.argv[1], tmp) |
| 64 | masks = [0x00ffffff, 0x03ffffff, 0x40ffffff, 0x0000ffff, 0x001fffff, 0xffffffff] |
| 65 | words = [random.choice(base) ^ (random.getrandbits(32) & random.choice(masks)) |
| 66 | for _ in range(int(sys.argv[3]))] |
| 67 | out = [] |
| 68 | for i in range(0, len(words), 2000): |
| 69 | out += clean(words[i:i + 2000], tmp) |
| 70 | if out and out[0] == 0x464C457F: |
| 71 | out[0] = 0 |
| 72 | with open(sys.argv[4], "wb") as f: |
| 73 | f.write(b"".join(struct.pack("<I", w) for w in out)) |
| 74 | print(f"FUZZ bases={len(base)} words={len(out)} seed={sys.argv[2]}") |
| 75 | return 0 |
| 76 | |
| 77 | |
| 78 | if __name__ == "__main__": |
| 79 | sys.exit(main()) |