oxedyne/fe2o3/fe2o3_austenite/tools/plugin/mkwasm.py
7.4 KiB, 1 run
created by r1870400018:73537, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | #!/usr/bin/env python3 |
| 2 | """Writes the WebAssembly plugin fixtures for Austenite's plugin tests, std-only. |
| 3 | |
| 4 | Each module is described by the table `MODULES` below: its imports, memory, functions with their |
| 5 | instruction bytes, exports and start function. The assembler is a hand encoder of the WebAssembly |
| 6 | binary format (version 1), enough for the fixtures and nothing more. Every `.wasm` is written beside a |
| 7 | `.wat`-like listing of its source, so a reader can see what a binary does without a disassembler. |
| 8 | |
| 9 | python3 -I fe2o3_austenite/tools/plugin/mkwasm.py [outdir] |
| 10 | |
| 11 | The default outdir is `tests/fixtures/eval/plugin/` under the crate. The output is deterministic. |
| 12 | """ |
| 13 | |
| 14 | import os |
| 15 | import sys |
| 16 | |
| 17 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 18 | CRATE = os.path.dirname(os.path.dirname(HERE)) |
| 19 | OUT = os.path.join(CRATE, "tests", "fixtures", "eval", "plugin") |
| 20 | |
| 21 | I32, I64 = 0x7f, 0x7e |
| 22 | |
| 23 | # Instructions, as (mnemonic, bytes) so the listing names them. |
| 24 | def uleb(n): |
| 25 | out = bytearray() |
| 26 | while True: |
| 27 | b = n & 0x7f |
| 28 | n >>= 7 |
| 29 | if n: |
| 30 | out.append(b | 0x80) |
| 31 | else: |
| 32 | out.append(b) |
| 33 | return bytes(out) |
| 34 | |
| 35 | def sleb(n): |
| 36 | out = bytearray() |
| 37 | while True: |
| 38 | b = n & 0x7f |
| 39 | n >>= 7 |
| 40 | done = (n == 0 and not b & 0x40) or (n == -1 and b & 0x40) |
| 41 | out.append(b if done else b | 0x80) |
| 42 | if done: |
| 43 | return bytes(out) |
| 44 | |
| 45 | def i32_const(n): return ("i32.const %d" % n, b"\x41" + sleb(n)) |
| 46 | def local_get(i): return ("local.get %d" % i, b"\x20" + uleb(i)) |
| 47 | def call(i): return ("call %d" % i, b"\x10" + uleb(i)) |
| 48 | def br(d): return ("br %d" % d, b"\x0c" + uleb(d)) |
| 49 | def br_if(d): return ("br_if %d" % d, b"\x0d" + uleb(d)) |
| 50 | def load8(off=0): return ("i32.load8_u offset=%d" % off, b"\x2d\x00" + uleb(off)) |
| 51 | def store8(off=0): return ("i32.store8 offset=%d" % off, b"\x3a\x00" + uleb(off)) |
| 52 | LOOP = ("loop", b"\x03\x40") |
| 53 | END = ("end", b"\x0b") |
| 54 | DROP = ("drop", b"\x1a") |
| 55 | ADD = ("i32.add", b"\x6a") |
| 56 | NE = ("i32.ne", b"\x47") |
| 57 | UNREACHABLE = ("unreachable", b"\x00") |
| 58 | GROW = ("memory.grow", b"\x40\x00") |
| 59 | |
| 60 | # The two host imports, function indices 0 and 1 when a module imports both. |
| 61 | WRITE_ARGS = ("typst_env", "wasm_minimal_protocol_write_args_to_buffer", [I32], []) |
| 62 | SEND_RESULT = ("typst_env", "wasm_minimal_protocol_send_result_to_host", [I32, I32], []) |
| 63 | HOST = [WRITE_ARGS, SEND_RESULT] |
| 64 | W, S = 0, 1 |
| 65 | |
| 66 | def body_return(code, sum_params): |
| 67 | """Reads the arguments to address 0 and sends back the concatenation, returning `code`.""" |
| 68 | ins = [i32_const(0), call(W), i32_const(0)] |
| 69 | if sum_params == 0: |
| 70 | ins.append(i32_const(0)) |
| 71 | else: |
| 72 | ins.append(local_get(0)) |
| 73 | for i in range(1, sum_params): |
| 74 | ins += [local_get(i), ADD] |
| 75 | ins += [call(S), i32_const(code)] |
| 76 | return ins |
| 77 | |
| 78 | MODULES = { |
| 79 | # Returns its arguments joined; `two` takes two, `none` none. |
| 80 | "echo": dict(imports=HOST, memory=(1, None), funcs=[ |
| 81 | ("echo", [I32], [I32], body_return(0, 1)), |
| 82 | ("two", [I32, I32], [I32], body_return(0, 2)), |
| 83 | ("none", [], [I32], body_return(0, 0)), |
| 84 | ]), |
| 85 | # Returns 1 with its argument as the message. |
| 86 | "err": dict(imports=HOST, memory=(1, None), funcs=[ |
| 87 | ("fail", [I32], [I32], body_return(1, 1)), |
| 88 | ]), |
| 89 | # Returns 2, which the protocol does not define. |
| 90 | "bad_proto": dict(imports=HOST, memory=(1, None), funcs=[ |
| 91 | ("odd", [I32], [I32], body_return(2, 1)), |
| 92 | ]), |
| 93 | # Traps. |
| 94 | "trap": dict(imports=HOST, memory=(1, None), funcs=[ |
| 95 | ("boom", [], [I32], [UNREACHABLE]), |
| 96 | ]), |
| 97 | # A parameter that is not a 32-bit integer, and a function returning nothing. |
| 98 | "nonint": dict(imports=HOST, memory=(1, None), funcs=[ |
| 99 | ("wide", [I64], [I32], [i32_const(0)]), |
| 100 | ("void", [I32], [], []), |
| 101 | ]), |
| 102 | # Exports no memory. |
| 103 | "no_memory": dict(imports=[], memory=None, funcs=[ |
| 104 | ("f", [], [I32], [i32_const(0)]), |
| 105 | ]), |
| 106 | # A byte of state at address 0: `inc` adds one, `get` returns it. |
| 107 | "counter": dict(imports=HOST, memory=(1, None), funcs=[ |
| 108 | ("inc", [], [I32], [i32_const(0), i32_const(0), load8(), i32_const(1), ADD, store8(), |
| 109 | i32_const(0), i32_const(1), call(S), i32_const(0)]), |
| 110 | ("get", [], [I32], [i32_const(0), i32_const(1), call(S), i32_const(0)]), |
| 111 | ]), |
| 112 | # Sends a result from beyond its memory. |
| 113 | "oob": dict(imports=HOST, memory=(1, None), funcs=[ |
| 114 | ("far", [], [I32], [i32_const(0x20000), i32_const(4), call(S), i32_const(0)]), |
| 115 | ]), |
| 116 | # Hostile: a loop that never ends. |
| 117 | "spin": dict(imports=HOST, memory=(1, None), funcs=[ |
| 118 | ("spin", [], [I32], [LOOP, br(0), END, i32_const(0)]), |
| 119 | ]), |
| 120 | # Hostile: grows its memory a page at a time until refused, then traps. |
| 121 | "grow": dict(imports=HOST, memory=(1, None), funcs=[ |
| 122 | ("grow", [], [I32], [LOOP, i32_const(1), GROW, i32_const(-1), NE, br_if(0), END, UNREACHABLE]), |
| 123 | ]), |
| 124 | # Hostile: a start function that never ends, which runs when the module loads. |
| 125 | "spin_start": dict(imports=HOST, memory=(1, None), start="spin", funcs=[ |
| 126 | ("spin", [], [], [LOOP, br(0), END]), |
| 127 | ("f", [], [I32], [i32_const(0)]), |
| 128 | ]), |
| 129 | # Hostile: an initial memory of 32768 pages (2 GiB), above every cap. |
| 130 | "huge_initial": dict(imports=HOST, memory=(32768, None), funcs=[ |
| 131 | ("f", [], [I32], [i32_const(0)]), |
| 132 | ]), |
| 133 | } |
| 134 | |
| 135 | def vec(items): |
| 136 | return uleb(len(items)) + b"".join(items) |
| 137 | |
| 138 | def name(s): |
| 139 | b = s.encode() |
| 140 | return uleb(len(b)) + b |
| 141 | |
| 142 | def section(sid, payload): |
| 143 | return bytes([sid]) + uleb(len(payload)) + payload |
| 144 | |
| 145 | def functype(params, results): |
| 146 | return b"\x60" + vec([bytes([p]) for p in params]) + vec([bytes([r]) for r in results]) |
| 147 | |
| 148 | def limits(lo, hi): |
| 149 | return (b"\x00" + uleb(lo)) if hi is None else (b"\x01" + uleb(lo) + uleb(hi)) |
| 150 | |
| 151 | def assemble(m): |
| 152 | types = [] |
| 153 | def tyidx(params, results): |
| 154 | t = functype(params, results) |
| 155 | if t not in types: |
| 156 | types.append(t) |
| 157 | return types.index(t) |
| 158 | imports = [name(mod) + name(fld) + b"\x00" + uleb(tyidx(p, r)) for (mod, fld, p, r) in m["imports"]] |
| 159 | nimp = len(m["imports"]) |
| 160 | fidx = [uleb(tyidx(p, r)) for (_, p, r, _) in m["funcs"]] |
| 161 | exports = [name(n) + b"\x00" + uleb(nimp + i) for i, (n, _, _, _) in enumerate(m["funcs"])] |
| 162 | if m["memory"] is not None: |
| 163 | exports.append(name("memory") + b"\x02" + uleb(0)) |
| 164 | codes = [] |
| 165 | for (_, _, _, ins) in m["funcs"]: |
| 166 | body = b"\x00" + b"".join(b for (_, b) in ins) + END[1] |
| 167 | codes.append(uleb(len(body)) + body) |
| 168 | out = b"\x00asm\x01\x00\x00\x00" |
| 169 | out += section(1, vec(types)) |
| 170 | if imports: |
| 171 | out += section(2, vec(imports)) |
| 172 | out += section(3, vec(fidx)) |
| 173 | if m["memory"] is not None: |
| 174 | out += section(5, vec([limits(*m["memory"])])) |
| 175 | out += section(7, vec(exports)) |
| 176 | if "start" in m: |
| 177 | names = [n for (n, _, _, _) in m["funcs"]] |
| 178 | out += section(8, uleb(nimp + names.index(m["start"]))) |
| 179 | out += section(10, vec(codes)) |
| 180 | return out |
| 181 | |
| 182 | def listing(key, m): |
| 183 | ty = {I32: "i32", I64: "i64"} |
| 184 | lines = ["(module $%s" % key] |
| 185 | for (mod, fld, p, r) in m["imports"]: |
| 186 | lines.append(' (import "%s" "%s" (func (param %s) (result %s)))' % (mod, fld, |
| 187 | " ".join(ty[x] for x in p), " ".join(ty[x] for x in r))) |
| 188 | if m["memory"] is not None: |
| 189 | lines.append(' (memory (export "memory") %d)' % m["memory"][0]) |
| 190 | for (n, p, r, ins) in m["funcs"]: |
| 191 | lines.append(' (func (export "%s") (param %s) (result %s)' % (n, |
| 192 | " ".join(ty[x] for x in p), " ".join(ty[x] for x in r))) |
| 193 | for (mn, _) in ins: |
| 194 | lines.append(" " + mn) |
| 195 | lines.append(" )") |
| 196 | if "start" in m: |
| 197 | lines.append(" (start $%s)" % m["start"]) |
| 198 | lines.append(")") |
| 199 | return "\n".join(lines) + "\n" |
| 200 | |
| 201 | def main(): |
| 202 | out = sys.argv[1] if len(sys.argv) > 1 else OUT |
| 203 | os.makedirs(out, exist_ok=True) |
| 204 | for key, m in MODULES.items(): |
| 205 | with open(os.path.join(out, key + ".wasm"), "wb") as f: |
| 206 | f.write(assemble(m)) |
| 207 | with open(os.path.join(out, key + ".wat"), "w") as f: |
| 208 | f.write(listing(key, m)) |
| 209 | print("wrote %d modules to %s" % (len(MODULES), out)) |
| 210 | |
| 211 | if __name__ == "__main__": |
| 212 | main() |