all.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. memory64_flag=False,
  152. multi_memory_flag=False,
  153. qemu_flag=False,
  154. qemu_firmware="",
  155. log="",
  156. no_pty=False
  157. ):
  158. CMD = [sys.executable, "runtest.py"]
  159. CMD.append("--wast2wasm")
  160. CMD.append(WAST2WASM_CMD if not gc_flag else SPEC_INTERPRETER_CMD)
  161. CMD.append("--interpreter")
  162. if sgx_flag:
  163. CMD.append(IWASM_SGX_CMD)
  164. elif qemu_flag:
  165. CMD.append(IWASM_QEMU_CMD)
  166. else:
  167. CMD.append(IWASM_CMD)
  168. if no_pty:
  169. CMD.append("--no-pty")
  170. CMD.append("--aot-compiler")
  171. CMD.append(aot_compiler)
  172. if aot_flag:
  173. CMD.append("--aot")
  174. CMD.append("--target")
  175. CMD.append(target)
  176. if multi_module_flag:
  177. CMD.append("--multi-module")
  178. if multi_thread_flag:
  179. CMD.append("--multi-thread")
  180. if sgx_flag:
  181. CMD.append("--sgx")
  182. if simd_flag:
  183. CMD.append("--simd")
  184. if xip_flag:
  185. CMD.append("--xip")
  186. if eh_flag:
  187. CMD.append("--eh")
  188. if qemu_flag:
  189. CMD.append("--qemu")
  190. CMD.append("--qemu-firmware")
  191. CMD.append(qemu_firmware)
  192. if not clean_up_flag:
  193. CMD.append("--no_cleanup")
  194. if gc_flag:
  195. CMD.append("--gc")
  196. if memory64_flag:
  197. CMD.append("--memory64")
  198. if multi_memory_flag:
  199. CMD.append("--multi-memory")
  200. if log != "":
  201. CMD.append("--log-dir")
  202. CMD.append(log)
  203. case_path = pathlib.Path(case_path).resolve()
  204. case_name = case_path.stem
  205. CMD.append(str(case_path))
  206. # print(f"============> use {' '.join(CMD)}")
  207. print(f"============> run {case_name} ", end="")
  208. with subprocess.Popen(
  209. CMD,
  210. bufsize=1,
  211. stdout=subprocess.PIPE,
  212. stderr=subprocess.STDOUT,
  213. universal_newlines=True,
  214. ) as p:
  215. try:
  216. case_last_words = []
  217. while not p.poll():
  218. output = p.stdout.readline()
  219. if not output:
  220. break
  221. if verbose_flag:
  222. print(output, end="")
  223. else:
  224. if len(case_last_words) == 1024:
  225. case_last_words.pop(0)
  226. case_last_words.append(output)
  227. p.wait(60)
  228. if p.returncode:
  229. print(f"failed with a non-zero return code {p.returncode}")
  230. if not verbose_flag:
  231. print(
  232. f"\n==================== LAST LOG of {case_name} ====================\n"
  233. )
  234. print("".join(case_last_words))
  235. print("\n==================== LAST LOG END ====================\n")
  236. raise Exception(case_name)
  237. else:
  238. print("successful")
  239. return True
  240. except subprocess.CalledProcessError:
  241. print("failed with CalledProcessError")
  242. raise Exception(case_name)
  243. except subprocess.TimeoutExpired:
  244. print("failed with TimeoutExpired")
  245. raise Exception(case_name)
  246. except Exception as e:
  247. print(f"An unexpected error occurred: {e}")
  248. raise e
  249. def test_suite(
  250. target,
  251. aot_flag=False,
  252. aot_compiler=WAMRC_CMD,
  253. sgx_flag=False,
  254. multi_module_flag=False,
  255. multi_thread_flag=False,
  256. simd_flag=False,
  257. xip_flag=False,
  258. eh_flag=False,
  259. clean_up_flag=True,
  260. verbose_flag=True,
  261. gc_flag=False,
  262. memory64_flag=False,
  263. multi_memory_flag=False,
  264. parl_flag=False,
  265. qemu_flag=False,
  266. qemu_firmware="",
  267. log="",
  268. no_pty=False,
  269. ):
  270. suite_path = pathlib.Path(SPEC_TEST_DIR).resolve()
  271. if not suite_path.exists():
  272. print(f"can not find spec test cases at {suite_path}")
  273. return False
  274. case_list = sorted(suite_path.glob("*.wast"))
  275. if simd_flag:
  276. simd_case_list = sorted(suite_path.glob("simd/*.wast"))
  277. case_list.extend(simd_case_list)
  278. if gc_flag:
  279. gc_case_list = sorted(suite_path.glob("gc/*.wast"))
  280. case_list.extend(gc_case_list)
  281. if eh_flag:
  282. eh_case_list = sorted(suite_path.glob("*.wast"))
  283. eh_case_list_include = [test for test in eh_case_list if test.stem in ["throw", "tag", "try_catch", "rethrow", "try_delegate"]]
  284. case_list.extend(eh_case_list_include)
  285. if multi_memory_flag:
  286. multi_memory_list = sorted(suite_path.glob("multi-memory/*.wast"))
  287. case_list.extend(multi_memory_list)
  288. # ignore based on command line options
  289. filtered_case_list = []
  290. for case_path in case_list:
  291. case_name = case_path.stem
  292. if not ignore_the_case(
  293. case_name,
  294. target,
  295. aot_flag,
  296. sgx_flag,
  297. multi_module_flag,
  298. multi_thread_flag,
  299. simd_flag,
  300. gc_flag,
  301. memory64_flag,
  302. multi_memory_flag,
  303. xip_flag,
  304. eh_flag,
  305. qemu_flag,
  306. ):
  307. filtered_case_list.append(case_path)
  308. else:
  309. print(f"---> skip {case_name}")
  310. print(f"---> {len(case_list)} ---filter--> {len(filtered_case_list)}")
  311. case_list = filtered_case_list
  312. case_count = len(case_list)
  313. failed_case = 0
  314. successful_case = 0
  315. if parl_flag:
  316. print(f"----- Run the whole spec test suite on {mp.cpu_count()} cores -----")
  317. with mp.Pool() as pool:
  318. results = {}
  319. for case_path in case_list:
  320. results[case_path.stem] = pool.apply_async(
  321. test_case,
  322. [
  323. str(case_path),
  324. target,
  325. aot_flag,
  326. aot_compiler,
  327. sgx_flag,
  328. multi_module_flag,
  329. multi_thread_flag,
  330. simd_flag,
  331. xip_flag,
  332. eh_flag,
  333. clean_up_flag,
  334. verbose_flag,
  335. gc_flag,
  336. memory64_flag,
  337. multi_memory_flag,
  338. qemu_flag,
  339. qemu_firmware,
  340. log,
  341. no_pty,
  342. ],
  343. )
  344. for case_name, result in results.items():
  345. try:
  346. if qemu_flag:
  347. # 60 min / case, testing on QEMU may be very slow
  348. result.wait(7200)
  349. else:
  350. # 5 min / case
  351. result.wait(300)
  352. if not result.successful():
  353. failed_case += 1
  354. else:
  355. successful_case += 1
  356. except mp.TimeoutError:
  357. print(f"{case_name} meets TimeoutError")
  358. failed_case += 1
  359. else:
  360. print(f"----- Run the whole spec test suite -----")
  361. for case_path in case_list:
  362. print(case_path)
  363. try:
  364. test_case(
  365. str(case_path),
  366. target,
  367. aot_flag,
  368. aot_compiler,
  369. sgx_flag,
  370. multi_module_flag,
  371. multi_thread_flag,
  372. simd_flag,
  373. xip_flag,
  374. eh_flag,
  375. clean_up_flag,
  376. verbose_flag,
  377. gc_flag,
  378. memory64_flag,
  379. multi_memory_flag,
  380. qemu_flag,
  381. qemu_firmware,
  382. log,
  383. no_pty,
  384. )
  385. successful_case += 1
  386. except Exception as e:
  387. failed_case += 1
  388. raise e
  389. print(
  390. f"IN ALL {case_count} cases: {successful_case} PASS, {failed_case} FAIL, {case_count - successful_case - failed_case} SKIP"
  391. )
  392. return 0 == failed_case
  393. def main():
  394. parser = argparse.ArgumentParser(description="run the whole spec test suite")
  395. parser.add_argument(
  396. "-M",
  397. action="store_true",
  398. default=False,
  399. dest="multi_module_flag",
  400. help="Running with the Multi-Module feature",
  401. )
  402. parser.add_argument(
  403. "-m",
  404. choices=AVAILABLE_TARGETS,
  405. type=str,
  406. dest="target",
  407. default="X86_64",
  408. help="Specify Target ",
  409. )
  410. parser.add_argument(
  411. "-p",
  412. action="store_true",
  413. default=False,
  414. dest="multi_thread_flag",
  415. help="Running with the Multi-Thread feature",
  416. )
  417. parser.add_argument(
  418. "-S",
  419. action="store_true",
  420. default=False,
  421. dest="simd_flag",
  422. help="Running with the SIMD feature",
  423. )
  424. parser.add_argument(
  425. "-X",
  426. action="store_true",
  427. default=False,
  428. dest="xip_flag",
  429. help="Running with the XIP feature",
  430. )
  431. # added to support WASM_ENABLE_EXCE_HANDLING
  432. parser.add_argument(
  433. "-e",
  434. action="store_true",
  435. default=False,
  436. dest="eh_flag",
  437. help="Running with the exception-handling feature",
  438. )
  439. parser.add_argument(
  440. "-t",
  441. action="store_true",
  442. default=False,
  443. dest="aot_flag",
  444. help="Running with AOT mode",
  445. )
  446. parser.add_argument(
  447. "--aot-compiler",
  448. default=WAMRC_CMD,
  449. dest="aot_compiler",
  450. help="AOT compiler",
  451. )
  452. parser.add_argument(
  453. "-x",
  454. action="store_true",
  455. default=False,
  456. dest="sgx_flag",
  457. help="Running with SGX environment",
  458. )
  459. parser.add_argument(
  460. "--no_clean_up",
  461. action="store_false",
  462. default=True,
  463. dest="clean_up_flag",
  464. help="Does not remove tmpfiles. But it will be enabled while running parallelly",
  465. )
  466. parser.add_argument(
  467. "--parl",
  468. action="store_true",
  469. default=False,
  470. dest="parl_flag",
  471. help="To run whole test suite parallelly",
  472. )
  473. parser.add_argument(
  474. "--qemu",
  475. action="store_true",
  476. default=False,
  477. dest="qemu_flag",
  478. help="To run whole test suite in qemu",
  479. )
  480. parser.add_argument(
  481. "--qemu-firmware",
  482. default="",
  483. dest="qemu_firmware",
  484. help="Firmware required by qemu",
  485. )
  486. parser.add_argument(
  487. "--log",
  488. default="",
  489. dest="log",
  490. help="Log directory",
  491. )
  492. parser.add_argument(
  493. "--quiet",
  494. action="store_false",
  495. default=True,
  496. dest="verbose_flag",
  497. help="Close real time output while running cases, only show last words of failed ones",
  498. )
  499. parser.add_argument(
  500. "--gc",
  501. action="store_true",
  502. default=False,
  503. dest="gc_flag",
  504. help="Running with GC feature",
  505. )
  506. parser.add_argument(
  507. "--memory64",
  508. action="store_true",
  509. default=False,
  510. dest="memory64_flag",
  511. help="Running with memory64 feature",
  512. )
  513. parser.add_argument(
  514. "--multi-memory",
  515. action="store_true",
  516. default=False,
  517. dest="multi_memory_flag",
  518. help="Running with multi-memory feature",
  519. )
  520. parser.add_argument(
  521. "cases",
  522. metavar="path_to__case",
  523. type=str,
  524. nargs="*",
  525. help=f"Specify all wanted cases. If not the script will go through all cases under {SPEC_TEST_DIR}",
  526. )
  527. parser.add_argument('--no-pty', action='store_true',
  528. help="Use direct pipes instead of pseudo-tty")
  529. options = parser.parse_args()
  530. # Convert target to lower case for internal use, e.g. X86_64 -> x86_64
  531. # target is always exist, so no need to check it
  532. options.target = options.target.lower()
  533. if options.target == "x86_32":
  534. options.target = "i386"
  535. if not preflight_check(options.aot_flag, options.aot_compiler, options.eh_flag):
  536. return False
  537. if not options.cases:
  538. if options.parl_flag:
  539. # several cases might share the same workspace/tempfile at the same time
  540. # so, disable it while running parallelly
  541. if options.multi_module_flag:
  542. options.clean_up_flag = False
  543. options.verbose_flag = False
  544. start = time.time_ns()
  545. ret = test_suite(
  546. options.target,
  547. options.aot_flag,
  548. options.aot_compiler,
  549. options.sgx_flag,
  550. options.multi_module_flag,
  551. options.multi_thread_flag,
  552. options.simd_flag,
  553. options.xip_flag,
  554. options.eh_flag,
  555. options.clean_up_flag,
  556. options.verbose_flag,
  557. options.gc_flag,
  558. options.memory64_flag,
  559. options.multi_memory_flag,
  560. options.parl_flag,
  561. options.qemu_flag,
  562. options.qemu_firmware,
  563. options.log,
  564. options.no_pty
  565. )
  566. end = time.time_ns()
  567. print(
  568. f"It takes {((end - start) / 1000000):,} ms to run test_suite {'parallelly' if options.parl_flag else ''}"
  569. )
  570. else:
  571. try:
  572. for case in options.cases:
  573. test_case(
  574. case,
  575. options.target,
  576. options.aot_flag,
  577. options.aot_compiler,
  578. options.sgx_flag,
  579. options.multi_module_flag,
  580. options.multi_thread_flag,
  581. options.simd_flag,
  582. options.xip_flag,
  583. options.eh_flag,
  584. options.clean_up_flag,
  585. options.verbose_flag,
  586. options.gc_flag,
  587. options.memory64_flag,
  588. options.multi_memory_flag,
  589. options.qemu_flag,
  590. options.qemu_firmware,
  591. options.log,
  592. options.no_pty,
  593. )
  594. else:
  595. ret = True
  596. except Exception:
  597. ret = False
  598. return ret
  599. if __name__ == "__main__":
  600. sys.exit(0 if main() else 1)