all.py 18 KB

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