generate-libwasm-spec-test.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. #!/usr/bin/env python3
  2. import struct
  3. from sys import argv, stderr
  4. from os import path
  5. from string import whitespace
  6. import re
  7. import math
  8. from tempfile import NamedTemporaryFile
  9. from subprocess import call
  10. import json
  11. import array
  12. atom_end = set('()"' + whitespace)
  13. def parse(sexp):
  14. sexp = re.sub(r'(?m)\(;.*;\)', '', re.sub(r'(;;.*)', '', sexp))
  15. stack, i, length = [[]], 0, len(sexp)
  16. while i < length:
  17. c = sexp[i]
  18. kind = type(stack[-1])
  19. if kind == list:
  20. if c == '(':
  21. stack.append([])
  22. elif c == ')':
  23. stack[-2].append(stack.pop())
  24. elif c == '"':
  25. stack.append('')
  26. elif c in whitespace:
  27. pass
  28. else:
  29. stack.append((c,))
  30. elif kind == str:
  31. if c == '"':
  32. stack[-2].append(stack.pop())
  33. elif c == '\\':
  34. i += 1
  35. if sexp[i] != '"':
  36. stack[-1] += '\\'
  37. stack[-1] += sexp[i]
  38. else:
  39. stack[-1] += c
  40. elif kind == tuple:
  41. if c in atom_end:
  42. atom = stack.pop()
  43. stack[-1].append(atom)
  44. continue
  45. else:
  46. stack[-1] = ((stack[-1][0] + c),)
  47. i += 1
  48. return stack.pop()
  49. class TestGenerationError(Exception):
  50. def __init__(self, message):
  51. self.msg = message
  52. def parse_typed_value(ast):
  53. types = {
  54. 'i32.const': 'i32',
  55. 'i64.const': 'i64',
  56. 'f32.const': 'float',
  57. 'f64.const': 'double',
  58. 'ref.null': 'null',
  59. 'ref.extern': 'i32',
  60. 'ref.func': 'i32',
  61. 'v128.const': 'bigint',
  62. }
  63. v128_sizes = {
  64. 'i8x16': 1,
  65. 'i16x8': 2,
  66. 'i32x4': 4,
  67. 'i64x2': 8,
  68. 'f32x4': 4,
  69. 'f64x2': 8,
  70. }
  71. v128_format_names = {
  72. 'i8x16': 'b',
  73. 'i16x8': 'h',
  74. 'i32x4': 'i',
  75. 'i64x2': 'q',
  76. 'f32x4': 'f',
  77. 'f64x2': 'd',
  78. }
  79. v128_format_names_unsigned = {
  80. 'i8x16': 'B',
  81. 'i16x8': 'H',
  82. 'i32x4': 'I',
  83. 'i64x2': 'Q',
  84. }
  85. def parse_v128_chunk(num, type) -> array:
  86. negative = 1
  87. if num.startswith('-'):
  88. negative = -1
  89. num = num[1:]
  90. elif num.startswith('+'):
  91. num = num[1:]
  92. # wtf spec test, split your wast tests already
  93. while num.startswith('0') and not num.startswith('0x'):
  94. num = num[1:]
  95. if num == '':
  96. num = '0'
  97. if type.startswith('f'):
  98. def generate():
  99. if num == 'nan:canonical':
  100. return float.fromhex('0x7fc00000')
  101. if num == 'nan:arithmetic':
  102. return float.fromhex('0x7ff00000')
  103. if num == 'nan:signaling':
  104. return float.fromhex('0x7ff80000')
  105. if num.startswith('nan:'):
  106. # FIXME: I have no idea if this is actually correct :P
  107. rest = num[4:]
  108. return float.fromhex('0x7ff80000') + int(rest, base=16)
  109. if num.lower() == 'infinity':
  110. return float.fromhex('0x7ff00000') * negative
  111. try:
  112. return float(num) * negative
  113. except ValueError:
  114. return float.fromhex(num) * negative
  115. value = generate()
  116. return struct.pack(f'={v128_format_names[type]}', value)
  117. value = negative * int(num.replace('_', ''), base=0)
  118. try:
  119. return struct.pack(f'={v128_format_names[type]}', value)
  120. except struct.error:
  121. # The test format uses signed and unsigned values interchangeably, this is probably an unsigned value.
  122. return struct.pack(f'={v128_format_names_unsigned[type]}', value)
  123. if len(ast) >= 2 and ast[0][0] in types:
  124. if ast[0][0] == 'v128.const':
  125. value = array.array('b')
  126. for i, num in enumerate(ast[2:]):
  127. size = v128_sizes[ast[1][0]]
  128. s = len(value)
  129. value.frombytes(parse_v128_chunk(num[0], ast[1][0]))
  130. assert len(value) - s == size, f'Expected {size} bytes, got {len(value) - s} bytes'
  131. assert len(value) == 16, f'Expected 16 bytes, got {len(value)} bytes'
  132. return {
  133. 'type': types[ast[0][0]],
  134. 'value': value.tobytes().hex()
  135. }
  136. return {"type": types[ast[0][0]], "value": ast[1][0]}
  137. return {"type": "error"}
  138. def generate_module_source_for_compilation(entries):
  139. s = '('
  140. for entry in entries:
  141. if type(entry) is tuple and len(entry) == 1 and type(entry[0]) is str:
  142. s += entry[0] + ' '
  143. elif type(entry) is str:
  144. s += json.dumps(entry).replace('\\\\', '\\') + ' '
  145. elif type(entry) is list:
  146. s += generate_module_source_for_compilation(entry)
  147. else:
  148. raise Exception("wat? I dunno how to pretty print " + str(type(entry)))
  149. while s.endswith(' '):
  150. s = s[:len(s) - 1]
  151. return s + ')'
  152. def generate_binary_source(chunks):
  153. res = b''
  154. for chunk in chunks:
  155. i = 0
  156. while i < len(chunk):
  157. c = chunk[i]
  158. if c == '\\':
  159. res += bytes.fromhex(chunk[i + 1: i + 3])
  160. i += 3
  161. continue
  162. res += c.encode('utf-8')
  163. i += 1
  164. return res
  165. named_modules = {}
  166. named_modules_inverse = {}
  167. registered_modules = {}
  168. module_output_path: str
  169. def generate_module(ast):
  170. # (module ...)
  171. name = None
  172. mode = 'ast' # binary, quote
  173. start_index = 1
  174. if len(ast) > 1:
  175. if isinstance(ast[1], tuple) and isinstance(ast[1][0], str) and ast[1][0].startswith('$'):
  176. name = ast[1][0]
  177. if len(ast) > 2:
  178. if isinstance(ast[2], tuple) and ast[2][0] in ('binary', 'quote'):
  179. mode = ast[2][0]
  180. start_index = 3
  181. else:
  182. start_index = 2
  183. elif isinstance(ast[1][0], str):
  184. mode = ast[1][0]
  185. start_index = 2
  186. result = {
  187. 'ast': lambda: ('parse', generate_module_source_for_compilation(ast)),
  188. 'binary': lambda: ('literal', generate_binary_source(ast[start_index:])),
  189. # FIXME: Make this work when we have a WAT parser
  190. 'quote': lambda: ('literal', ast[start_index]),
  191. }[mode]()
  192. return {
  193. 'module': result,
  194. 'name': name
  195. }
  196. def generate(ast):
  197. global named_modules, named_modules_inverse, registered_modules
  198. if type(ast) is not list:
  199. return []
  200. tests = []
  201. for entry in ast:
  202. if len(entry) > 0 and entry[0] == ('module',):
  203. gen = generate_module(entry)
  204. module, name = gen['module'], gen['name']
  205. tests.append({
  206. "module": module,
  207. "tests": []
  208. })
  209. if name is not None:
  210. named_modules[name] = len(tests) - 1
  211. named_modules_inverse[len(tests) - 1] = (name, None)
  212. elif entry[0] == ('assert_unlinkable',):
  213. # (assert_unlinkable module message)
  214. if len(entry) < 2 or not isinstance(entry[1], list) or entry[1][0] != ('module',):
  215. print(f"Invalid argument to assert_unlinkable: {entry[1]}", file=stderr)
  216. continue
  217. result = generate_module(entry[1])
  218. tests.append({
  219. 'module': None,
  220. 'tests': [{
  221. "kind": "unlinkable",
  222. "module": result['module'],
  223. }]
  224. })
  225. elif entry[0] in (('assert_malformed',), ('assert_invalid',)):
  226. # (assert_malformed/invalid module message)
  227. if len(entry) < 2 or not isinstance(entry[1], list) or entry[1][0] != ('module',):
  228. print(f"Invalid argument to assert_malformed: {entry[1]}", file=stderr)
  229. continue
  230. result = generate_module(entry[1])
  231. kind = entry[0][0][len('assert_'):]
  232. tests.append({
  233. 'module': None,
  234. 'kind': kind,
  235. 'tests': [{
  236. "kind": kind,
  237. "module": result['module'],
  238. }]
  239. })
  240. elif len(entry) in [2, 3] and entry[0][0].startswith('assert_'):
  241. if entry[1][0] == ('invoke',):
  242. arg, name, module = 0, None, None
  243. if isinstance(entry[1][1], str):
  244. name = entry[1][1]
  245. else:
  246. name = entry[1][2]
  247. module = named_modules[entry[1][1][0]]
  248. arg = 1
  249. kind = entry[0][0][len('assert_'):]
  250. tests[-1]["tests"].append({
  251. "kind": kind,
  252. "function": {
  253. "module": module,
  254. "name": name,
  255. "args": list(parse_typed_value(x) for x in entry[1][arg + 2:])
  256. },
  257. "result": parse_typed_value(entry[2]) if len(entry) == 3 + arg and kind != 'exhaustion' else None
  258. })
  259. elif entry[1][0] == ('get',):
  260. arg, name, module = 0, None, None
  261. if isinstance(entry[1][1], str):
  262. name = entry[1][1]
  263. else:
  264. name = entry[1][2]
  265. module = named_modules[entry[1][1][0]]
  266. arg = 1
  267. tests[-1]["tests"].append({
  268. "kind": entry[0][0][len('assert_'):],
  269. "get": {
  270. "name": name,
  271. "module": module,
  272. },
  273. "result": parse_typed_value(entry[2]) if len(entry) == 3 + arg else None
  274. })
  275. else:
  276. if not len(tests):
  277. tests.append({
  278. "module": ('literal', b""),
  279. "tests": []
  280. })
  281. tests[-1]["tests"].append({
  282. "kind": "testgen_fail",
  283. "function": {
  284. "module": None,
  285. "name": "<unknown>",
  286. "args": []
  287. },
  288. "reason": f"Unknown assertion {entry[0][0][len('assert_'):]}"
  289. })
  290. elif len(entry) >= 2 and entry[0][0] == 'invoke':
  291. # toplevel invoke :shrug:
  292. arg, name, module = 0, None, None
  293. if not isinstance(entry[1], str) and isinstance(entry[1][1], str):
  294. name = entry[1][1]
  295. elif isinstance(entry[1], str):
  296. name = entry[1]
  297. else:
  298. name = entry[1][2]
  299. module = named_modules[entry[1][1][0]]
  300. arg = 1
  301. tests[-1]["tests"].append({
  302. "kind": "ignore",
  303. "function": {
  304. "module": module,
  305. "name": name,
  306. "args": [parse_typed_value(entry[2])] if len(entry) == 3 else []
  307. },
  308. "result": None
  309. })
  310. elif len(entry) > 1 and entry[0][0] == 'register':
  311. if len(entry) == 3:
  312. registered_modules[entry[1]] = named_modules[entry[2][0]]
  313. x = named_modules_inverse[named_modules[entry[2][0]]]
  314. named_modules_inverse[named_modules[entry[2][0]]] = (x[0], entry[1])
  315. else:
  316. index = len(tests) - 1
  317. registered_modules[entry[1]] = index
  318. named_modules_inverse[index] = (":" + entry[1], entry[1])
  319. else:
  320. if not len(tests):
  321. tests.append({
  322. "module": ('literal', b""),
  323. "tests": []
  324. })
  325. tests[-1]["tests"].append({
  326. "kind": "testgen_fail",
  327. "function": {
  328. "module": None,
  329. "name": "<unknown>",
  330. "args": []
  331. },
  332. "reason": f"Unknown command {entry[0][0]}"
  333. })
  334. return tests
  335. def genarg(spec):
  336. if spec['type'] == 'error':
  337. return '0'
  338. def gen():
  339. x = spec['value']
  340. if spec['type'] == 'bigint':
  341. return f"0x{x}n"
  342. if spec['type'] == 'null':
  343. return 'null'
  344. if spec['type'] in ('i32', 'i64'):
  345. if x.startswith('0x'):
  346. if spec['type'] == 'i32':
  347. # cast back to i32 to get the correct sign
  348. return str(struct.unpack('>i', struct.pack('>Q', int(x, 16))[4:])[0])
  349. # cast back to i64 to get the correct sign
  350. return str(struct.unpack('>q', struct.pack('>Q', int(x, 16)))[0]) + 'n'
  351. if spec['type'] == 'i64':
  352. # Make a bigint instead, since `double' cannot fit all i64 values.
  353. if x.startswith('0'):
  354. x = x.lstrip('0')
  355. if x == '':
  356. x = '0'
  357. return x + 'n'
  358. return x
  359. if x == 'nan':
  360. return 'NaN'
  361. if x == '-nan':
  362. return '-NaN'
  363. try:
  364. x = float(x)
  365. if math.isnan(x):
  366. # FIXME: This is going to mess up the different kinds of nan
  367. return '-NaN' if math.copysign(1.0, x) < 0 else 'NaN'
  368. if math.isinf(x):
  369. return 'Infinity' if x > 0 else '-Infinity'
  370. return x
  371. except ValueError:
  372. try:
  373. x = float.fromhex(x)
  374. if math.isnan(x):
  375. # FIXME: This is going to mess up the different kinds of nan
  376. return '-NaN' if math.copysign(1.0, x) < 0 else 'NaN'
  377. if math.isinf(x):
  378. return 'Infinity' if x > 0 else '-Infinity'
  379. return x
  380. except ValueError:
  381. try:
  382. x = int(x, 0)
  383. return x
  384. except ValueError:
  385. return x
  386. x = gen()
  387. if isinstance(x, str):
  388. if x.startswith('nan'):
  389. return 'NaN'
  390. if x.startswith('-nan'):
  391. return '-NaN'
  392. return x
  393. return str(x)
  394. all_names_in_main = {}
  395. def genresult(ident, entry, index):
  396. expectation = None
  397. if "function" in entry:
  398. tmodule = 'module'
  399. if entry['function']['module'] is not None:
  400. tmodule = f'namedModules[{json.dumps(named_modules_inverse[entry["function"]["module"]][0])}]'
  401. expectation = (
  402. f'{tmodule}.invoke({ident}, {", ".join(genarg(x) for x in entry["function"]["args"])})'
  403. )
  404. elif "get" in entry:
  405. expectation = f'module.getExport({ident})'
  406. if entry['kind'] == 'return':
  407. return (
  408. f'let {ident}_result = {expectation};\n ' +
  409. (f'expect({ident}_result).toBe({genarg(entry["result"])})\n ' if entry["result"] is not None else '')
  410. )
  411. if entry['kind'] == 'ignore':
  412. return expectation
  413. if entry['kind'] == 'unlinkable':
  414. name = f'mod-{ident}-{index}.wasm'
  415. outpath = path.join(module_output_path, name)
  416. if not compile_wasm_source(entry['module'], outpath):
  417. return 'throw new Error("Module compilation failed");'
  418. return (
  419. f' expect(() => {{\n'
  420. f' let content = readBinaryWasmFile("Fixtures/SpecTests/{name}");\n'
  421. f' parseWebAssemblyModule(content, globalImportObject);\n'
  422. f' }}).toThrow(TypeError, "Linking failed");'
  423. )
  424. if entry['kind'] in ('exhaustion', 'trap', 'invalid'):
  425. return (
  426. f'expect(() => {expectation}.toThrow(TypeError, "Execution trapped"));\n '
  427. )
  428. if entry['kind'] == 'malformed':
  429. return ''
  430. if entry['kind'] == 'testgen_fail':
  431. raise TestGenerationError(entry["reason"])
  432. if not expectation:
  433. raise TestGenerationError(f"Unknown test result structure in {json.dumps(entry)}")
  434. return expectation
  435. raw_test_number = 0
  436. def gentest(entry, main_name):
  437. global raw_test_number
  438. isfunction = 'function' in entry
  439. name: str
  440. isempty = False
  441. if isfunction or 'get' in entry:
  442. name = json.dumps((entry["function"] if isfunction else entry["get"])["name"])[1:-1]
  443. else:
  444. isempty = True
  445. name = str(f"_inline_test_{raw_test_number}")
  446. raw_test_number += 1
  447. if type(name) is not str:
  448. print("Unsupported test case (call to", name, ")", file=stderr)
  449. return '\n '
  450. ident = '_' + re.sub("[^a-zA-Z_0-9]", "_", name)
  451. count = all_names_in_main.get(name, 0)
  452. all_names_in_main[name] = count + 1
  453. test_name = f'execution of {main_name}: {name} (instance {count})'
  454. tmodule = 'module'
  455. if not isempty:
  456. key = "function" if "function" in entry else "get"
  457. if entry[key]['module'] is not None:
  458. tmodule = f'namedModules[{json.dumps(named_modules_inverse[entry[key]["module"]][0])}]'
  459. test = "_test"
  460. try:
  461. result = genresult(ident, entry, count)
  462. except TestGenerationError as e:
  463. test = f"/* {e.msg} */ _test.skip"
  464. result = ""
  465. return (
  466. f'{test}({json.dumps(test_name)}, () => {{\n' +
  467. (
  468. f'let {ident} = {tmodule}.getExport({json.dumps(name)});\n '
  469. f'expect({ident}).not.toBeUndefined();\n '
  470. if not isempty else ''
  471. ) +
  472. f'{result}'
  473. '});\n\n '
  474. )
  475. def gen_parse_module(name, index):
  476. export_string = ''
  477. if index in named_modules_inverse:
  478. entry = named_modules_inverse[index]
  479. export_string += f'namedModules[{json.dumps(entry[0])}] = module;\n '
  480. if entry[1]:
  481. export_string += f'globalImportObject[{json.dumps(entry[1])}] = module;\n '
  482. return (
  483. 'let content, module;\n '
  484. 'try {\n '
  485. f'content = readBinaryWasmFile("Fixtures/SpecTests/{name}.wasm");\n '
  486. f'module = parseWebAssemblyModule(content, globalImportObject)\n '
  487. '} catch(e) { _test("parse", () => expect().fail(e)); _test = test.skip; _test.skip = test.skip; }\n '
  488. f'{export_string}\n '
  489. )
  490. def nth(a, x, y=None):
  491. if y:
  492. return a[x:y]
  493. return a[x]
  494. def compile_wasm_source(mod, outpath):
  495. if not mod:
  496. return True
  497. if mod[0] == 'literal':
  498. with open(outpath, 'wb+') as f:
  499. f.write(mod[1])
  500. return True
  501. elif mod[0] == 'parse':
  502. with NamedTemporaryFile("w+") as temp:
  503. temp.write(mod[1])
  504. temp.flush()
  505. rc = call(["wat2wasm", "--enable-all", "--no-check", temp.name, "-o", outpath])
  506. return rc == 0
  507. return False
  508. def main():
  509. global module_output_path
  510. with open(argv[1]) as f:
  511. sexp = f.read()
  512. name = argv[2]
  513. module_output_path = argv[3]
  514. ast = parse(sexp)
  515. print('let globalImportObject = {};')
  516. print('let namedModules = {};\n')
  517. for index, description in enumerate(generate(ast)):
  518. testname = f'{name}_{index}'
  519. outpath = path.join(module_output_path, f'{testname}.wasm')
  520. mod = description["module"]
  521. if not compile_wasm_source(mod, outpath) and ('kind' not in description or description["kind"] != "malformed"):
  522. print("Failed to compile", name, "module index", index, "skipping that test", file=stderr)
  523. continue
  524. sep = ""
  525. print(f'''describe({json.dumps(testname)}, () => {{
  526. let _test = test;
  527. {gen_parse_module(testname, index) if mod else ''}
  528. {sep.join(gentest(x, testname) for x in description["tests"])}
  529. }});
  530. ''')
  531. if __name__ == "__main__":
  532. main()