all.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (C) 2019 Intel Corporation. All rights reserved.
  4. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5. #
  6. import argparse
  7. import multiprocessing as mp
  8. import platform
  9. import pathlib
  10. import subprocess
  11. import sys
  12. import time
  13. """
  14. The script itself has to be put under the same directory with the "spec".
  15. To run a single non-GC case with interpreter mode:
  16. cd workspace
  17. python3 runtest.py --wast2wasm wabt/bin/wat2wasm --interpreter iwasm \
  18. spec/test/core/xxx.wast
  19. To run a single non-GC case with aot mode:
  20. cd workspace
  21. python3 runtest.py --aot --wast2wasm wabt/bin/wat2wasm --interpreter iwasm \
  22. --aot-compiler wamrc spec/test/core/xxx.wast
  23. To run a single GC case case:
  24. cd workspace
  25. python3 runtest.py --wast2wasm spec/interpreter/wasm --interpreter iwasm \
  26. --aot-compiler wamrc --gc spec/test/core/xxx.wast
  27. """
  28. def exe_file_path(base_path: str) -> str:
  29. if platform.system().lower() == "windows":
  30. base_path += ".exe"
  31. return base_path
  32. def get_iwasm_cmd(platform: str) -> str:
  33. build_path = "../../../product-mini/platforms/" + platform + "/build/"
  34. exe_name = "iwasm"
  35. if platform == "windows":
  36. build_path += "RelWithDebInfo/"
  37. return exe_file_path(build_path + exe_name)
  38. PLATFORM_NAME = platform.uname().system.lower()
  39. IWASM_CMD = get_iwasm_cmd(PLATFORM_NAME)
  40. IWASM_SGX_CMD = "../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm"
  41. IWASM_QEMU_CMD = "iwasm"
  42. SPEC_TEST_DIR = "spec/test/core"
  43. WAST2WASM_CMD = exe_file_path("./wabt/out/gcc/Release/wat2wasm")
  44. SPEC_INTERPRETER_CMD = "spec/interpreter/wasm"
  45. WAMRC_CMD = "../../../wamr-compiler/build/wamrc"
  46. AVAILABLE_TARGETS = [
  47. "I386",
  48. "X86_32",
  49. "X86_64",
  50. "AARCH64",
  51. "AARCH64_VFP",
  52. "ARMV7",
  53. "ARMV7_VFP",
  54. "RISCV32",
  55. "RISCV32_ILP32F",
  56. "RISCV32_ILP32D",
  57. "RISCV64",
  58. "RISCV64_LP64F",
  59. "RISCV64_LP64D",
  60. "THUMBV7",
  61. "THUMBV7_VFP",
  62. "XTENSA",
  63. ]
  64. def ignore_the_case(
  65. case_name,
  66. target,
  67. aot_flag=False,
  68. sgx_flag=False,
  69. multi_module_flag=False,
  70. multi_thread_flag=False,
  71. simd_flag=False,
  72. gc_flag=False,
  73. memory64_flag=False,
  74. multi_memory_flag=False,
  75. xip_flag=False,
  76. eh_flag=False,
  77. qemu_flag=False,
  78. ):
  79. if case_name in ["comments", "inline-module", "names"]:
  80. return True
  81. if not multi_module_flag and case_name in ["imports", "linking", "simd_linking"]:
  82. return True
  83. # Note: x87 doesn't preserve sNaN and makes some relevant tests fail.
  84. if "i386" == target and case_name in ["float_exprs", "conversions"]:
  85. return True
  86. # esp32s3 qemu doesn't have PSRAM emulation
  87. if qemu_flag and target == 'xtensa' and case_name in ["memory_size"]:
  88. return True
  89. if gc_flag:
  90. if case_name in [
  91. "array_init_elem",
  92. "array_init_data",
  93. "array_new_data",
  94. "array_new_elem"
  95. ]:
  96. return True
  97. if sgx_flag:
  98. if case_name in ["conversions", "f32_bitwise", "f64_bitwise"]:
  99. return True
  100. if aot_flag and case_name in [
  101. "call_indirect",
  102. "call",
  103. "fac",
  104. "skip-stack-guard-page",
  105. ]:
  106. return True
  107. if qemu_flag:
  108. if case_name in [
  109. "f32_bitwise",
  110. "f64_bitwise",
  111. "loop",
  112. "f64",
  113. "f64_cmp",
  114. "conversions",
  115. "f32",
  116. "f32_cmp",
  117. "float_exprs",
  118. "float_misc",
  119. "select",
  120. "memory_grow",
  121. # Skip the test case for now, restore it after fixing the issue
  122. "skip-stack-guard-page",
  123. ]:
  124. return True
  125. return False
  126. def preflight_check(aot_flag, aot_compiler, eh_flag):
  127. if not pathlib.Path(SPEC_TEST_DIR).resolve().exists():
  128. print(f"Can not find {SPEC_TEST_DIR}")
  129. return False
  130. if not pathlib.Path(WAST2WASM_CMD).resolve().exists():
  131. print(f"Can not find {WAST2WASM_CMD}")
  132. return False
  133. if aot_flag and not pathlib.Path(aot_compiler).resolve().exists():
  134. print(f"Can not find {aot_compiler}")
  135. return False
  136. return True
  137. def test_case(
  138. case_path,
  139. target,
  140. aot_flag=False,
  141. aot_compiler=WAMRC_CMD,
  142. sgx_flag=False,
  143. multi_module_flag=False,
  144. multi_thread_flag=False,
  145. simd_flag=False,
  146. xip_flag=False,
  147. eh_flag=False,
  148. clean_up_flag=True,
  149. verbose_flag=True,
  150. gc_flag=False,
  151. extended_const_flag=False,
  152. memory64_flag=False,
  153. multi_memory_flag=False,
  154. qemu_flag=False,
  155. qemu_firmware="",
  156. log="",
  157. no_pty=False
  158. ):
  159. CMD = [sys.executable, "runtest.py"]
  160. CMD.append("--wast2wasm")
  161. CMD.append(WAST2WASM_CMD if not gc_flag else SPEC_INTERPRETER_CMD)
  162. CMD.append("--interpreter")
  163. if sgx_flag:
  164. CMD.append(IWASM_SGX_CMD)
  165. elif qemu_flag:
  166. CMD.append(IWASM_QEMU_CMD)
  167. else:
  168. CMD.append(IWASM_CMD)
  169. if no_pty:
  170. CMD.append("--no-pty")
  171. CMD.append("--aot-compiler")
  172. CMD.append(aot_compiler)
  173. if aot_flag:
  174. CMD.append("--aot")
  175. CMD.append("--target")
  176. CMD.append(target)
  177. if multi_module_flag:
  178. CMD.append("--multi-module")
  179. if multi_thread_flag:
  180. CMD.append("--multi-thread")
  181. if sgx_flag:
  182. CMD.append("--sgx")
  183. if simd_flag:
  184. CMD.append("--simd")
  185. if xip_flag:
  186. CMD.append("--xip")
  187. if eh_flag:
  188. CMD.append("--eh")
  189. if qemu_flag:
  190. CMD.append("--qemu")
  191. CMD.append("--qemu-firmware")
  192. CMD.append(qemu_firmware)
  193. if not clean_up_flag:
  194. CMD.append("--no_cleanup")
  195. if gc_flag:
  196. CMD.append("--gc")
  197. if extended_const_flag:
  198. CMD.append("--extended-const")
  199. if memory64_flag:
  200. CMD.append("--memory64")
  201. if multi_memory_flag:
  202. CMD.append("--multi-memory")
  203. if log != "":
  204. CMD.append("--log-dir")
  205. CMD.append(log)
  206. case_path = pathlib.Path(case_path).resolve()
  207. case_name = case_path.stem
  208. CMD.append(str(case_path))
  209. # print(f"============> use {' '.join(CMD)}")
  210. print(f"============> run {case_name} ", end="")
  211. with subprocess.Popen(
  212. CMD,
  213. bufsize=1,
  214. stdout=subprocess.PIPE,
  215. stderr=subprocess.STDOUT,
  216. universal_newlines=True,
  217. ) as p:
  218. try:
  219. case_last_words = []
  220. while not p.poll():
  221. output = p.stdout.readline()
  222. if not output:
  223. break
  224. if verbose_flag:
  225. print(output, end="")
  226. else:
  227. if len(case_last_words) == 1024:
  228. case_last_words.pop(0)
  229. case_last_words.append(output)
  230. p.wait(60)
  231. if p.returncode:
  232. print(f"failed with a non-zero return code {p.returncode}")
  233. if not verbose_flag:
  234. print(
  235. f"\n==================== LAST LOG of {case_name} ====================\n"
  236. )
  237. print("".join(case_last_words))
  238. print("\n==================== LAST LOG END ====================\n")
  239. raise Exception(case_name)
  240. else:
  241. print("successful")
  242. return True
  243. except subprocess.CalledProcessError:
  244. print("failed with CalledProcessError")
  245. raise Exception(case_name)
  246. except subprocess.TimeoutExpired:
  247. print("failed with TimeoutExpired")
  248. raise Exception(case_name)
  249. except Exception as e:
  250. print(f"An unexpected error occurred: {e}")
  251. raise e
  252. def test_suite(
  253. target,
  254. aot_flag=False,
  255. aot_compiler=WAMRC_CMD,
  256. sgx_flag=False,
  257. multi_module_flag=False,
  258. multi_thread_flag=False,
  259. simd_flag=False,
  260. xip_flag=False,
  261. eh_flag=False,
  262. clean_up_flag=True,
  263. verbose_flag=True,
  264. gc_flag=False,
  265. extended_const_flag=False,
  266. memory64_flag=False,
  267. multi_memory_flag=False,
  268. parl_flag=False,
  269. qemu_flag=False,
  270. qemu_firmware="",
  271. log="",
  272. no_pty=False,
  273. ):
  274. suite_path = pathlib.Path(SPEC_TEST_DIR).resolve()
  275. if not suite_path.exists():
  276. print(f"can not find spec test cases at {suite_path}")
  277. return False
  278. case_list = sorted(suite_path.glob("*.wast"))
  279. if simd_flag:
  280. simd_case_list = sorted(suite_path.glob("simd/*.wast"))
  281. case_list.extend(simd_case_list)
  282. if gc_flag:
  283. gc_case_list = sorted(suite_path.glob("gc/*.wast"))
  284. case_list.extend(gc_case_list)
  285. if eh_flag:
  286. eh_case_list = sorted(suite_path.glob("*.wast"))
  287. eh_case_list_include = [test for test in eh_case_list if test.stem in ["throw", "tag", "try_catch", "rethrow", "try_delegate"]]
  288. case_list.extend(eh_case_list_include)
  289. if multi_memory_flag:
  290. multi_memory_list = sorted(suite_path.glob("multi-memory/*.wast"))
  291. case_list.extend(multi_memory_list)
  292. # ignore based on command line options
  293. filtered_case_list = []
  294. for case_path in case_list:
  295. case_name = case_path.stem
  296. if not ignore_the_case(
  297. case_name,
  298. target,
  299. aot_flag,
  300. sgx_flag,
  301. multi_module_flag,
  302. multi_thread_flag,
  303. simd_flag,
  304. gc_flag,
  305. memory64_flag,
  306. multi_memory_flag,
  307. xip_flag,
  308. eh_flag,
  309. qemu_flag,
  310. ):
  311. filtered_case_list.append(case_path)
  312. else:
  313. print(f"---> skip {case_name}")
  314. print(f"---> {len(case_list)} ---filter--> {len(filtered_case_list)}")
  315. case_list = filtered_case_list
  316. case_count = len(case_list)
  317. failed_case = 0
  318. successful_case = 0
  319. if parl_flag:
  320. print(f"----- Run the whole spec test suite on {mp.cpu_count()} cores -----")
  321. with mp.Pool() as pool:
  322. results = {}
  323. for case_path in case_list:
  324. results[case_path.stem] = pool.apply_async(
  325. test_case,
  326. [
  327. str(case_path),
  328. target,
  329. aot_flag,
  330. aot_compiler,
  331. sgx_flag,
  332. multi_module_flag,
  333. multi_thread_flag,
  334. simd_flag,
  335. xip_flag,
  336. eh_flag,
  337. clean_up_flag,
  338. verbose_flag,
  339. gc_flag,
  340. extended_const_flag,
  341. memory64_flag,
  342. multi_memory_flag,
  343. qemu_flag,
  344. qemu_firmware,
  345. log,
  346. no_pty,
  347. ],
  348. )
  349. for case_name, result in results.items():
  350. try:
  351. if qemu_flag:
  352. # 60 min / case, testing on QEMU may be very slow
  353. result.wait(7200)
  354. else:
  355. # 5 min / case
  356. result.wait(300)
  357. if not result.successful():
  358. failed_case += 1
  359. else:
  360. successful_case += 1
  361. except mp.TimeoutError:
  362. print(f"{case_name} meets TimeoutError")
  363. failed_case += 1
  364. else:
  365. print(f"----- Run the whole spec test suite -----")
  366. for case_path in case_list:
  367. print(case_path)
  368. try:
  369. test_case(
  370. str(case_path),
  371. target,
  372. aot_flag,
  373. aot_compiler,
  374. sgx_flag,
  375. multi_module_flag,
  376. multi_thread_flag,
  377. simd_flag,
  378. xip_flag,
  379. eh_flag,
  380. clean_up_flag,
  381. verbose_flag,
  382. gc_flag,
  383. extended_const_flag,
  384. memory64_flag,
  385. multi_memory_flag,
  386. qemu_flag,
  387. qemu_firmware,
  388. log,
  389. no_pty,
  390. )
  391. successful_case += 1
  392. except Exception as e:
  393. failed_case += 1
  394. raise e
  395. print(
  396. f"IN ALL {case_count} cases: {successful_case} PASS, {failed_case} FAIL, {case_count - successful_case - failed_case} SKIP"
  397. )
  398. return 0 == failed_case
  399. def main():
  400. parser = argparse.ArgumentParser(description="run the whole spec test suite")
  401. parser.add_argument(
  402. "-M",
  403. action="store_true",
  404. default=False,
  405. dest="multi_module_flag",
  406. help="Running with the Multi-Module feature",
  407. )
  408. parser.add_argument(
  409. "-m",
  410. choices=AVAILABLE_TARGETS,
  411. type=str,
  412. dest="target",
  413. default="X86_64",
  414. help="Specify Target ",
  415. )
  416. parser.add_argument(
  417. "-p",
  418. action="store_true",
  419. default=False,
  420. dest="multi_thread_flag",
  421. help="Running with the Multi-Thread feature",
  422. )
  423. parser.add_argument(
  424. "-S",
  425. action="store_true",
  426. default=False,
  427. dest="simd_flag",
  428. help="Running with the SIMD feature",
  429. )
  430. parser.add_argument(
  431. "-X",
  432. action="store_true",
  433. default=False,
  434. dest="xip_flag",
  435. help="Running with the XIP feature",
  436. )
  437. # added to support WASM_ENABLE_EXCE_HANDLING
  438. parser.add_argument(
  439. "-e",
  440. action="store_true",
  441. default=False,
  442. dest="eh_flag",
  443. help="Running with the exception-handling feature",
  444. )
  445. parser.add_argument(
  446. "-t",
  447. action="store_true",
  448. default=False,
  449. dest="aot_flag",
  450. help="Running with AOT mode",
  451. )
  452. parser.add_argument(
  453. "--aot-compiler",
  454. default=WAMRC_CMD,
  455. dest="aot_compiler",
  456. help="AOT compiler",
  457. )
  458. parser.add_argument(
  459. "-x",
  460. action="store_true",
  461. default=False,
  462. dest="sgx_flag",
  463. help="Running with SGX environment",
  464. )
  465. parser.add_argument(
  466. "--no_clean_up",
  467. action="store_false",
  468. default=True,
  469. dest="clean_up_flag",
  470. help="Does not remove tmpfiles. But it will be enabled while running parallelly",
  471. )
  472. parser.add_argument(
  473. "--parl",
  474. action="store_true",
  475. default=False,
  476. dest="parl_flag",
  477. help="To run whole test suite parallelly",
  478. )
  479. parser.add_argument(
  480. "--qemu",
  481. action="store_true",
  482. default=False,
  483. dest="qemu_flag",
  484. help="To run whole test suite in qemu",
  485. )
  486. parser.add_argument(
  487. "--qemu-firmware",
  488. default="",
  489. dest="qemu_firmware",
  490. help="Firmware required by qemu",
  491. )
  492. parser.add_argument(
  493. "--log",
  494. default="",
  495. dest="log",
  496. help="Log directory",
  497. )
  498. parser.add_argument(
  499. "--quiet",
  500. action="store_false",
  501. default=True,
  502. dest="verbose_flag",
  503. help="Close real time output while running cases, only show last words of failed ones",
  504. )
  505. parser.add_argument(
  506. "--gc",
  507. action="store_true",
  508. default=False,
  509. dest="gc_flag",
  510. help="Running with GC feature",
  511. )
  512. parser.add_argument(
  513. "--enable-extended-const",
  514. action="store_true",
  515. default=False,
  516. dest="extended_const_flag",
  517. help="Running with extended const expression feature",
  518. )
  519. parser.add_argument(
  520. "--memory64",
  521. action="store_true",
  522. default=False,
  523. dest="memory64_flag",
  524. help="Running with memory64 feature",
  525. )
  526. parser.add_argument(
  527. "--multi-memory",
  528. action="store_true",
  529. default=False,
  530. dest="multi_memory_flag",
  531. help="Running with multi-memory feature",
  532. )
  533. parser.add_argument(
  534. "cases",
  535. metavar="path_to__case",
  536. type=str,
  537. nargs="*",
  538. help=f"Specify all wanted cases. If not the script will go through all cases under {SPEC_TEST_DIR}",
  539. )
  540. parser.add_argument('--no-pty', action='store_true',
  541. help="Use direct pipes instead of pseudo-tty")
  542. options = parser.parse_args()
  543. # Convert target to lower case for internal use, e.g. X86_64 -> x86_64
  544. # target is always exist, so no need to check it
  545. options.target = options.target.lower()
  546. if options.target == "x86_32":
  547. options.target = "i386"
  548. if not preflight_check(options.aot_flag, options.aot_compiler, options.eh_flag):
  549. return False
  550. if not options.cases:
  551. if options.parl_flag:
  552. # several cases might share the same workspace/tempfile at the same time
  553. # so, disable it while running parallelly
  554. if options.multi_module_flag:
  555. options.clean_up_flag = False
  556. options.verbose_flag = False
  557. start = time.time_ns()
  558. ret = test_suite(
  559. options.target,
  560. options.aot_flag,
  561. options.aot_compiler,
  562. options.sgx_flag,
  563. options.multi_module_flag,
  564. options.multi_thread_flag,
  565. options.simd_flag,
  566. options.xip_flag,
  567. options.eh_flag,
  568. options.clean_up_flag,
  569. options.verbose_flag,
  570. options.gc_flag,
  571. options.extended_const_flag,
  572. options.memory64_flag,
  573. options.multi_memory_flag,
  574. options.parl_flag,
  575. options.qemu_flag,
  576. options.qemu_firmware,
  577. options.log,
  578. options.no_pty
  579. )
  580. end = time.time_ns()
  581. print(
  582. f"It takes {((end - start) / 1000000):,} ms to run test_suite {'parallelly' if options.parl_flag else ''}"
  583. )
  584. else:
  585. try:
  586. for case in options.cases:
  587. test_case(
  588. case,
  589. options.target,
  590. options.aot_flag,
  591. options.aot_compiler,
  592. options.sgx_flag,
  593. options.multi_module_flag,
  594. options.multi_thread_flag,
  595. options.simd_flag,
  596. options.xip_flag,
  597. options.eh_flag,
  598. options.clean_up_flag,
  599. options.verbose_flag,
  600. options.gc_flag,
  601. options.extended_const_flag,
  602. options.memory64_flag,
  603. options.multi_memory_flag,
  604. options.qemu_flag,
  605. options.qemu_firmware,
  606. options.log,
  607. options.no_pty,
  608. )
  609. else:
  610. ret = True
  611. except Exception:
  612. ret = False
  613. return ret
  614. if __name__ == "__main__":
  615. sys.exit(0 if main() else 1)