|
| 1 | +import itertools |
| 2 | +from dataclasses import astuple, dataclass |
| 3 | + |
| 4 | +# Workaround for python <3.10, escape characters can't appear in f-strings. |
| 5 | +# Although we require 3.10 in some places, the formatter complains without this. |
| 6 | +newline = "\n" |
| 7 | + |
| 8 | +backslash = '\\' |
| 9 | + |
| 10 | + |
| 11 | +@dataclass |
| 12 | +class instruction_test: |
| 13 | + op: str |
| 14 | + arg: str |
| 15 | + should_drop: bool |
| 16 | + bin: bytes |
| 17 | + |
| 18 | + |
| 19 | +ALL_OPS = [ |
| 20 | + instruction_test("i32.atomic.load", "(i32.const 51)", True, b"\x41\x33\xfe\x10%(align)s%(memidx)s%(ordering)s\x00\x1a"), |
| 21 | + instruction_test("i32.atomic.store", "(i32.const 51) (i32.const 51)", False, b"\x41\x33\x41\x33\xfe\x17%(align)s%(memidx)s%(ordering)s\x00"), |
| 22 | + instruction_test("i32.atomic.rmw.add", "(i32.const 51) (i32.const 51)", True, b"\x41\x33\x41\x33\xfe\x1e%(align)s%(memidx)s%(ordering)s\x00\x1a"), |
| 23 | +] |
| 24 | + |
| 25 | + |
| 26 | +def indent(s): |
| 27 | + return "\n".join(f" {line}" if line else "" for line in s.split("\n")) |
| 28 | + |
| 29 | + |
| 30 | +# skips None for convenience |
| 31 | +def instruction(*args): |
| 32 | + return f"({' '.join(arg for arg in args if arg is not None)})" |
| 33 | + |
| 34 | + |
| 35 | +def atomic_instruction(op, memid, ordering, /, *args, drop): |
| 36 | + if drop: |
| 37 | + return f"(drop {instruction(op, memid, ordering, *args)})" |
| 38 | + return instruction(op, memid, ordering, *args) |
| 39 | + |
| 40 | + |
| 41 | +def func(memid, ordering): |
| 42 | + return f'''(func ${ordering if ordering is not None else "no_ordering"}{"_with_memid" if memid is not None else "_without_memid"} |
| 43 | +{indent(newline.join(atomic_instruction(op, memid, ordering, arg, drop=should_drop) for op, arg, should_drop, _ in map(astuple, ALL_OPS)))} |
| 44 | +)''' |
| 45 | + |
| 46 | + |
| 47 | +def module(*statements): |
| 48 | + return f'''(module |
| 49 | +{newline.join(map(indent, statements))} |
| 50 | +)''' |
| 51 | + |
| 52 | + |
| 53 | +def module_binary(bin): |
| 54 | + return f'''(module binary "{''.join(f'{backslash}{byte:02x}' for byte in bin)}")''' |
| 55 | + |
| 56 | + |
| 57 | +def assert_invalid(module, reason): |
| 58 | + return f'''(assert_invalid {module} "{reason}")''' |
| 59 | + |
| 60 | + |
| 61 | +def text_test(): |
| 62 | + # Declare two memories so we have control over whether the memory idx is printed |
| 63 | + # A memory idx of 0 is allowed to be omitted. |
| 64 | + return module( |
| 65 | + "(memory 1 1 shared)", |
| 66 | + "(memory 1 1 shared)", |
| 67 | + "", |
| 68 | + "\n\n".join([f'{func(memid, ordering)}' for memid in [None, "1"] for ordering in [None, "acqrel", "seqcst"]])) |
| 69 | + |
| 70 | + |
| 71 | +def to_unsigned_leb(num): |
| 72 | + ret = bytearray() |
| 73 | + |
| 74 | + if num == 0: |
| 75 | + ret = bytearray() |
| 76 | + ret.append(0) |
| 77 | + return ret |
| 78 | + ret = bytearray() |
| 79 | + while num > 0: |
| 80 | + rem = num >> 7 |
| 81 | + ret.append((num & 0x7F) | (bool(rem) << 7)) |
| 82 | + |
| 83 | + num = rem |
| 84 | + return ret |
| 85 | + |
| 86 | + |
| 87 | +def bin_to_str(bin): |
| 88 | + return ''.join(f'{backslash}{byte:02x}' for byte in bin) |
| 89 | + |
| 90 | + |
| 91 | +@dataclass |
| 92 | +class statement: |
| 93 | + bin: bytes |
| 94 | + text: str |
| 95 | + |
| 96 | + |
| 97 | +@dataclass |
| 98 | +class function: |
| 99 | + body: [statement] |
| 100 | + memidx: bytes |
| 101 | + ordering: bytes |
| 102 | + |
| 103 | + |
| 104 | +def normalize_spaces(s): |
| 105 | + return " ".join(s.split()) |
| 106 | + |
| 107 | + |
| 108 | +def binary_line(bin): |
| 109 | + return f'"{bin_to_str(bin)}"\n' |
| 110 | + |
| 111 | + |
| 112 | +def binary_test_example(): |
| 113 | + return r'''(module binary |
| 114 | + "\00asm\01\00\00\00" ;; header + version |
| 115 | + "\01\05\01\60\00\01\7f\03\02\01\00\05\05\01\03\17\80\02" ;; other sections |
| 116 | + "\0a\0c\01" ;; code section |
| 117 | + "\0a\00" ;; func size + decl count |
| 118 | + "\41\33" ;; i32.const 51 |
| 119 | + "\fe\10" ;; i32.atomic.load |
| 120 | + "\62" ;; 2 | (1<<5) | (1<<6): Alignment of 2 (32-bit load), with bit 5 set indicating that the next byte is a memory ordering |
| 121 | + "\00" ;; memory index |
| 122 | + "\01" ;; acqrel ordering |
| 123 | + "\00" ;; offset |
| 124 | + "\0b" ;; end |
| 125 | +)''' |
| 126 | + |
| 127 | + |
| 128 | +def binary_tests(): |
| 129 | + funcs: [function] = [] |
| 130 | + for (memidx_bytes, memidx), (ordering_bytes, ordering) in itertools.product([(b'', None), (b'\x01', "1")], [(b'', None), (b'\x00', "seqcst"), (b'\x01', "acqrel")]): |
| 131 | + func = function([], memidx, ordering) |
| 132 | + for test_case in ALL_OPS: |
| 133 | + align = 2 | (bool(memidx_bytes) << 6) | (bool(ordering_bytes) << 5) |
| 134 | + s = statement( |
| 135 | + bin=test_case.bin % {b'align': int.to_bytes(align), b'ordering': ordering_bytes if "rmw" not in test_case.op else int.to_bytes(ordering_bytes[0] | ordering_bytes[0] << 4) if ordering_bytes else b'', b'memidx': memidx_bytes}, |
| 136 | + text=atomic_instruction(test_case.op, memidx, ordering, test_case.arg, drop=test_case.should_drop)) |
| 137 | + func.body.append(s) |
| 138 | + |
| 139 | + # Functions end with 0x0b. |
| 140 | + func.body[-1].bin += b'\x0b' |
| 141 | + funcs.append(func) |
| 142 | + |
| 143 | + str_builder = [] |
| 144 | + |
| 145 | + for func in funcs: |
| 146 | + bin_size = sum(len(statement.bin) for statement in func.body) |
| 147 | + # body size plus 1 byte for the number of locals (0) |
| 148 | + func_bytes = to_unsigned_leb(bin_size + 1) |
| 149 | + # number of locals, none in our case |
| 150 | + func_bytes.append(0x00) |
| 151 | + str_builder.append(f'"{bin_to_str(func_bytes)}" ;; func\n') |
| 152 | + for stmt in func.body: |
| 153 | + str_builder.append(f'"{bin_to_str(stmt.bin)}" ;; {stmt.text}\n') |
| 154 | + |
| 155 | + section_size = ( |
| 156 | + # function body size |
| 157 | + sum(len(statement.bin) for func in funcs for statement in func.body) + |
| 158 | + # function count byte |
| 159 | + 1 + |
| 160 | + # num locals per function (always 0) |
| 161 | + len(funcs) + |
| 162 | + # each function declares its size, add bytes for the LEB encoding of each function's size |
| 163 | + sum(len(to_unsigned_leb(sum(len(statement.bin) for statement in func.body))) for func in funcs)) |
| 164 | + |
| 165 | + code_section = bytearray(b"\x0a") + to_unsigned_leb(section_size) + to_unsigned_leb(len(funcs)) |
| 166 | + |
| 167 | + '''(module |
| 168 | + (memory 1 1 shared) |
| 169 | + (memory 1 1 shared) |
| 170 | + ) |
| 171 | + ''' |
| 172 | + module = b"\x00\x61\x73\x6d\x01\x00\x00\x00\x01\x04\01\x60\x00\x00\x03\x07\06\x00\x00\x00\x00\x00\x00\x05\07\x02\x03\x01\x01\x03\x01\x01" |
| 173 | + with open("asdf.wasm", "wb") as f: |
| 174 | + f.write(module + code_section) |
| 175 | + |
| 176 | + str_builder = [binary_line(module), f'"{bin_to_str(code_section)}" ;; code section\n'] + str_builder |
| 177 | + |
| 178 | + return f"(module binary\n{indent(''.join(str_builder))})" |
| 179 | + |
| 180 | + |
| 181 | +def failing_test(instruction, arg, /, memidx, drop): |
| 182 | + """Module assertion that sets a memory ordering immediate for a non-atomic instruction""" |
| 183 | + |
| 184 | + func = f"(func ${''.join(filter(str.isalnum, instruction))} {atomic_instruction(instruction, memidx, 'acqrel', arg, drop=drop)})" |
| 185 | + return assert_invalid(module("(memory 1 1 shared)", "", func), f"Can't set memory ordering for non-atomic {instruction}") |
| 186 | + |
| 187 | + |
| 188 | +def drop_atomic(instruction): |
| 189 | + first, atomic, last = instruction.partition(".atomic") |
| 190 | + return first + last |
| 191 | + |
| 192 | + |
| 193 | +def failing_tests(): |
| 194 | + inst = ALL_OPS[0] |
| 195 | + op = drop_atomic(inst.op) |
| 196 | + |
| 197 | + return failing_test(op, inst.arg, memidx=None, drop=inst.should_drop) |
| 198 | + |
| 199 | + |
| 200 | +def main(): |
| 201 | + print(text_test()) |
| 202 | + print() |
| 203 | + print(binary_test_example()) |
| 204 | + print() |
| 205 | + print(binary_tests()) |
| 206 | + print() |
| 207 | + print(failing_tests()) |
| 208 | + print() |
| 209 | + |
| 210 | + |
| 211 | +if __name__ == "__main__": |
| 212 | + main() |
0 commit comments