all.py 18 KB

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