generate-libwasm-spec-test.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. import json
  2. import sys
  3. import struct
  4. import subprocess
  5. from dataclasses import dataclass
  6. from pathlib import Path
  7. from typing import Union, Literal, Any
  8. class ParseException(Exception):
  9. pass
  10. class GenerateException(Exception):
  11. pass
  12. @dataclass
  13. class WasmPrimitiveValue:
  14. kind: Literal["i32", "i64", "f32", "f64", "externref", "funcref"]
  15. value: str
  16. @dataclass
  17. class WasmVector:
  18. lanes: list[str]
  19. num_bits: int
  20. WasmValue = Union[WasmPrimitiveValue, WasmVector]
  21. @dataclass
  22. class ModuleCommand:
  23. line: int
  24. file_name: Path
  25. name: str | None
  26. @dataclass
  27. class Invoke:
  28. field: str
  29. args: list[WasmValue]
  30. module: str | None
  31. @dataclass
  32. class Get:
  33. field: str
  34. module: str | None
  35. Action = Union[Invoke, Get]
  36. @dataclass
  37. class Register:
  38. line: int
  39. name: str | None
  40. as_: str
  41. @dataclass
  42. class AssertReturn:
  43. line: int
  44. action: Action
  45. expected: WasmValue | None
  46. @dataclass
  47. class AssertTrap:
  48. line: int
  49. messsage: str
  50. action: Action
  51. @dataclass
  52. class ActionCommand:
  53. line: int
  54. action: Action
  55. @dataclass
  56. class AssertInvalid:
  57. line: int
  58. filename: str
  59. message: str
  60. Command = Union[
  61. ModuleCommand,
  62. AssertReturn,
  63. AssertTrap,
  64. ActionCommand,
  65. AssertInvalid,
  66. Register,
  67. ]
  68. @dataclass
  69. class ArithmeticNan:
  70. num_bits: int
  71. @dataclass
  72. class CanonicalNan:
  73. num_bits: int
  74. @dataclass
  75. class GeneratedVector:
  76. repr: str
  77. num_bits: int
  78. GeneratedValue = Union[str, ArithmeticNan, CanonicalNan, GeneratedVector]
  79. @dataclass
  80. class WastDescription:
  81. source_filename: str
  82. commands: list[Command]
  83. @dataclass
  84. class Context:
  85. current_module_name: str
  86. has_unclosed: bool
  87. def parse_value(arg: dict[str, str]) -> WasmValue:
  88. type_ = arg["type"]
  89. match type_:
  90. case "i32" | "i64" | "f32" | "f64" | "externref" | "funcref":
  91. return WasmPrimitiveValue(type_, arg["value"])
  92. case "v128":
  93. if not isinstance(arg["value"], list):
  94. raise ParseException("Got unknown type for Wasm value")
  95. num_bits = int(arg["lane_type"][1:])
  96. return WasmVector(arg["value"], num_bits)
  97. case _:
  98. raise ParseException(f"Unknown value type: {type_}")
  99. def parse_args(raw_args: list[dict[str, str]]) -> list[WasmValue]:
  100. return [parse_value(arg) for arg in raw_args]
  101. def parse_action(action: dict[str, Any]) -> Action:
  102. match action["type"]:
  103. case "invoke":
  104. return Invoke(
  105. action["field"], parse_args(action["args"]), action.get("module")
  106. )
  107. case "get":
  108. return Get(action["field"], action.get("module"))
  109. case _:
  110. raise ParseException(f"Action not implemented: {action['type']}")
  111. def parse(raw: dict[str, Any]) -> WastDescription:
  112. commands: list[Command] = []
  113. for raw_cmd in raw["commands"]:
  114. line = raw_cmd["line"]
  115. cmd: Command
  116. match raw_cmd["type"]:
  117. case "module":
  118. cmd = ModuleCommand(
  119. line, Path(raw_cmd["filename"]), raw_cmd.get("name")
  120. )
  121. case "action":
  122. cmd = ActionCommand(line, parse_action(raw_cmd["action"]))
  123. case "register":
  124. cmd = Register(line, raw_cmd.get("name"), raw_cmd["as"])
  125. case "assert_return":
  126. cmd = AssertReturn(
  127. line,
  128. parse_action(raw_cmd["action"]),
  129. parse_value(raw_cmd["expected"][0])
  130. if len(raw_cmd["expected"]) == 1
  131. else None,
  132. )
  133. case "assert_trap" | "assert_exhaustion":
  134. cmd = AssertTrap(line, raw_cmd["text"], parse_action(raw_cmd["action"]))
  135. case "assert_invalid" | "assert_malformed" | "assert_uninstantiable" | "assert_unlinkable":
  136. if raw_cmd.get("module_type") == "text":
  137. continue
  138. cmd = AssertInvalid(line, raw_cmd["filename"], raw_cmd["text"])
  139. case _:
  140. raise ParseException(f"Unknown command type: {raw_cmd['type']}")
  141. commands.append(cmd)
  142. return WastDescription(raw["source_filename"], commands)
  143. def escape(s: str) -> str:
  144. return s.replace('"', '\\"')
  145. def make_description(input_path: Path, name: str, out_path: Path) -> WastDescription:
  146. out_json_path = out_path / f"{name}.json"
  147. result = subprocess.run(
  148. ["wast2json", input_path, f"--output={out_json_path}", "--no-check"],
  149. )
  150. result.check_returncode()
  151. with open(out_json_path, "r") as f:
  152. description = json.load(f)
  153. return parse(description)
  154. def gen_vector(vec: WasmVector, *, array=False) -> str:
  155. addition = "n" if vec.num_bits == 64 else ""
  156. vals = ", ".join(v + addition if v.isdigit() else f'"{v}"' for v in vec.lanes)
  157. if not array:
  158. type_ = "BigUint64Array" if vec.num_bits == 64 else f"Uint{vec.num_bits}Array"
  159. return f"new {type_}([{vals}])"
  160. return f"[{vals}]"
  161. def gen_value_arg(value: WasmValue) -> str:
  162. if isinstance(value, WasmVector):
  163. return gen_vector(value)
  164. def unsigned_to_signed(uint: int, bits: int) -> int:
  165. max_value = 2**bits
  166. if uint >= 2 ** (bits - 1):
  167. signed_int = uint - max_value
  168. else:
  169. signed_int = uint
  170. return signed_int
  171. def int_to_float_bitcast(uint: int) -> float:
  172. b = struct.pack("I", uint)
  173. f = struct.unpack("f", b)[0]
  174. return f
  175. def int_to_float64_bitcast(uint: int) -> float:
  176. uint64 = uint & 0xFFFFFFFFFFFFFFFF
  177. b = struct.pack("Q", uint64)
  178. f = struct.unpack("d", b)[0]
  179. return f
  180. def float_to_str(bits: int, *, double=False) -> str:
  181. f = int_to_float64_bitcast(bits) if double else int_to_float_bitcast(bits)
  182. return str(f)
  183. if value.value.startswith("nan"):
  184. raise GenerateException("Should not get indeterminate nan value as an argument")
  185. if value.value == "inf":
  186. return "Infinity"
  187. if value.value == "-inf":
  188. return "-Infinity"
  189. match value.kind:
  190. case "i32":
  191. return str(unsigned_to_signed(int(value.value), 32))
  192. case "i64":
  193. return str(unsigned_to_signed(int(value.value), 64)) + "n"
  194. case "f32":
  195. return str(int(value.value)) + f" /* {float_to_str(int(value.value))} */"
  196. case "f64":
  197. return (
  198. str(int(value.value))
  199. + f"n /* {float_to_str(int(value.value), double=True)} */"
  200. )
  201. case "externref" | "funcref" | "v128":
  202. return value.value
  203. case _:
  204. raise GenerateException(f"Not implemented: {value.kind}")
  205. def gen_value_result(value: WasmValue) -> GeneratedValue:
  206. if isinstance(value, WasmVector):
  207. return GeneratedVector(gen_vector(value, array=True), value.num_bits)
  208. if (value.kind == "f32" or value.kind == "f64") and value.value.startswith("nan"):
  209. num_bits = int(value.kind[1:])
  210. match value.value:
  211. case "nan:canonical":
  212. return CanonicalNan(num_bits)
  213. case "nan:arithmetic":
  214. return ArithmeticNan(num_bits)
  215. case _:
  216. raise GenerateException(f"Unknown indeterminate nan: {value.value}")
  217. return gen_value_arg(value)
  218. def gen_args(args: list[WasmValue]) -> str:
  219. return ",".join(gen_value_arg(arg) for arg in args)
  220. def gen_module_command(command: ModuleCommand, ctx: Context):
  221. if ctx.has_unclosed:
  222. print("});")
  223. print(
  224. f"""describe("{command.file_name.stem}", () => {{
  225. let _test = test;
  226. let content, module;
  227. try {{
  228. content = readBinaryWasmFile("Fixtures/SpecTests/{command.file_name}");
  229. module = parseWebAssemblyModule(content, globalImportObject);
  230. }} catch (e) {{
  231. _test("parse", () => expect().fail(e));
  232. _test = test.skip;
  233. _test.skip = test.skip;
  234. }}
  235. """
  236. )
  237. if command.name is not None:
  238. print(f'namedModules["{command.name}"] = module;')
  239. ctx.current_module_name = command.file_name.stem
  240. ctx.has_unclosed = True
  241. def gen_invalid(invalid: AssertInvalid, ctx: Context):
  242. # TODO: Remove this once the multiple memories proposal is standardized.
  243. # We support the multiple memories proposal, so spec-tests that check that
  244. # we don't do not make any sense to include right now.
  245. if invalid.message == "multiple memories":
  246. return
  247. if ctx.has_unclosed:
  248. print("});")
  249. ctx.has_unclosed = False
  250. stem = Path(invalid.filename).stem
  251. print(
  252. f"""
  253. describe("{stem}", () => {{
  254. let _test = test;
  255. _test("parse of {stem} (line {invalid.line})", () => {{
  256. content = readBinaryWasmFile("Fixtures/SpecTests/{invalid.filename}");
  257. expect(() => parseWebAssemblyModule(content, globalImportObject)).toThrow(Error, "{invalid.message}");
  258. }});
  259. }});"""
  260. )
  261. def gen_pretty_expect(expr: str, got: str, expect: str):
  262. print(
  263. f"if (!{expr}) {{ expect().fail(`Failed with ${{{got}}}, expected {expect}`); }}"
  264. )
  265. def gen_invoke(
  266. line: int,
  267. invoke: Invoke,
  268. result: WasmValue | None,
  269. ctx: Context,
  270. *,
  271. fail_msg: str | None = None,
  272. ):
  273. if not ctx.has_unclosed:
  274. print(f'describe("inline (line {line}))", () => {{\nlet _test = test;\n')
  275. module = "module"
  276. if invoke.module is not None:
  277. module = f'namedModules["{invoke.module}"]'
  278. utf8 = (
  279. str(invoke.field.encode("utf8"))[2:-1]
  280. .replace("\\'", "'")
  281. .replace("`", "${'`'}")
  282. )
  283. print(
  284. f"""_test(`execution of {ctx.current_module_name}: {utf8} (line {line})`, () => {{
  285. let _field = {module}.getExport(decodeURIComponent(escape(`{utf8}`)));
  286. expect(_field).not.toBeUndefined();"""
  287. )
  288. if fail_msg is not None:
  289. print(f'expect(() => {module}.invoke(_field)).toThrow(Error, "{fail_msg}");')
  290. else:
  291. print(f"let _result = {module}.invoke(_field, {gen_args(invoke.args)});")
  292. if result is not None:
  293. gen_result = gen_value_result(result)
  294. match gen_result:
  295. case str():
  296. print(f"expect(_result).toBe({gen_result});")
  297. case ArithmeticNan():
  298. gen_pretty_expect(
  299. f"isArithmeticNaN{gen_result.num_bits}(_result)",
  300. "_result",
  301. "nan:arithmetic",
  302. )
  303. case CanonicalNan():
  304. gen_pretty_expect(
  305. f"isCanonicalNaN{gen_result.num_bits}(_result)",
  306. "_result",
  307. "nan:canonical",
  308. )
  309. case GeneratedVector():
  310. if gen_result.num_bits == 64:
  311. array = "new BigUint64Array(_result)"
  312. else:
  313. array = f"new Uint{gen_result.num_bits}Array(_result)"
  314. gen_pretty_expect(
  315. f"testSIMDVector({gen_result.repr}, {array})",
  316. array,
  317. gen_result.repr,
  318. )
  319. print("});")
  320. if not ctx.has_unclosed:
  321. print("});")
  322. def gen_get(line: int, get: Get, result: WasmValue | None, ctx: Context):
  323. module = "module"
  324. if get.module is not None:
  325. module = f'namedModules["{get.module}"]'
  326. print(
  327. f"""_test("execution of {ctx.current_module_name}: get-{get.field} (line {line})", () => {{
  328. let _field = {module}.getExport("{get.field}");"""
  329. )
  330. if result is not None:
  331. print(f"expect(_field).toBe({gen_value_result(result)});")
  332. print("});")
  333. def gen_register(register: Register, _: Context):
  334. module = "module"
  335. if register.name is not None:
  336. module = f'namedModules["{register.name}"]'
  337. print(f'globalImportObject["{register.as_}"] = {module};')
  338. def gen_command(command: Command, ctx: Context):
  339. match command:
  340. case ModuleCommand():
  341. gen_module_command(command, ctx)
  342. case ActionCommand():
  343. if isinstance(command.action, Invoke):
  344. gen_invoke(command.line, command.action, None, ctx)
  345. else:
  346. raise GenerateException(
  347. f"Not implemented: top-level {type(command.action)}"
  348. )
  349. case AssertInvalid():
  350. gen_invalid(command, ctx)
  351. case Register():
  352. gen_register(command, ctx)
  353. case AssertReturn():
  354. match command.action:
  355. case Invoke():
  356. gen_invoke(command.line, command.action, command.expected, ctx)
  357. case Get():
  358. gen_get(command.line, command.action, command.expected, ctx)
  359. case AssertTrap():
  360. if not isinstance(command.action, Invoke):
  361. raise GenerateException(f"Not implemented: {type(command.action)}")
  362. gen_invoke(
  363. command.line, command.action, None, ctx, fail_msg=command.messsage
  364. )
  365. def generate(description: WastDescription):
  366. print("let globalImportObject = {};\nlet namedModules = {};\n")
  367. ctx = Context("", False)
  368. for command in description.commands:
  369. gen_command(command, ctx)
  370. if ctx.has_unclosed:
  371. print("});")
  372. def clean_up(path: Path):
  373. for file in path.iterdir():
  374. if file.suffix in ("wat", "json"):
  375. file.unlink()
  376. def main():
  377. input_path = Path(sys.argv[1])
  378. name = sys.argv[2]
  379. out_path = Path(sys.argv[3])
  380. description = make_description(input_path, name, out_path)
  381. generate(description)
  382. clean_up(out_path)
  383. if __name__ == "__main__":
  384. main()