all.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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:
  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. ]
  63. def ignore_the_case(
  64. case_name,
  65. target,
  66. aot_flag=False,
  67. sgx_flag=False,
  68. multi_module_flag=False,
  69. multi_thread_flag=False,
  70. simd_flag=False,
  71. gc_flag=False,
  72. xip_flag=False,
  73. qemu_flag=False,
  74. ):
  75. if case_name in ["comments", "inline-module", "names"]:
  76. return True
  77. if not multi_module_flag and case_name in ["imports", "linking"]:
  78. return True
  79. # Note: x87 doesn't preserve sNaN and makes some relevant tests fail.
  80. if "i386" == target and case_name in ["float_exprs", "conversions"]:
  81. return True
  82. if gc_flag:
  83. if case_name in ["type-canon", "type-equivalence", "type-rec"]:
  84. return True
  85. if sgx_flag:
  86. if case_name in ["conversions", "f32_bitwise", "f64_bitwise"]:
  87. return True
  88. if aot_flag and case_name in [
  89. "call_indirect",
  90. "call",
  91. "fac",
  92. "skip-stack-guard-page",
  93. ]:
  94. return True
  95. if qemu_flag:
  96. if case_name in [
  97. "f32_bitwise",
  98. "f64_bitwise",
  99. "loop",
  100. "f64",
  101. "f64_cmp",
  102. "conversions",
  103. "f32",
  104. "f32_cmp",
  105. "float_exprs",
  106. "float_misc",
  107. "select",
  108. "memory_grow",
  109. ]:
  110. return True
  111. return False
  112. def preflight_check(aot_flag):
  113. if not pathlib.Path(SPEC_TEST_DIR).resolve().exists():
  114. print(f"Can not find {SPEC_TEST_DIR}")
  115. return False
  116. if not pathlib.Path(WAST2WASM_CMD).resolve().exists():
  117. print(f"Can not find {WAST2WASM_CMD}")
  118. return False
  119. if aot_flag and not pathlib.Path(WAMRC_CMD).resolve().exists():
  120. print(f"Can not find {WAMRC_CMD}")
  121. return False
  122. return True
  123. def test_case(
  124. case_path,
  125. target,
  126. aot_flag=False,
  127. sgx_flag=False,
  128. multi_module_flag=False,
  129. multi_thread_flag=False,
  130. simd_flag=False,
  131. xip_flag=False,
  132. clean_up_flag=True,
  133. verbose_flag=True,
  134. gc_flag=False,
  135. qemu_flag=False,
  136. qemu_firmware="",
  137. log="",
  138. no_pty=False
  139. ):
  140. CMD = [sys.executable, "runtest.py"]
  141. CMD.append("--wast2wasm")
  142. CMD.append(WAST2WASM_CMD if not gc_flag else SPEC_INTERPRETER_CMD)
  143. CMD.append("--interpreter")
  144. if sgx_flag:
  145. CMD.append(IWASM_SGX_CMD)
  146. elif qemu_flag:
  147. CMD.append(IWASM_QEMU_CMD)
  148. else:
  149. CMD.append(IWASM_CMD)
  150. if no_pty:
  151. CMD.append("--no-pty")
  152. CMD.append("--aot-compiler")
  153. CMD.append(WAMRC_CMD)
  154. if aot_flag:
  155. CMD.append("--aot")
  156. CMD.append("--target")
  157. CMD.append(target)
  158. if multi_module_flag:
  159. CMD.append("--multi-module")
  160. if multi_thread_flag:
  161. CMD.append("--multi-thread")
  162. if sgx_flag:
  163. CMD.append("--sgx")
  164. if simd_flag:
  165. CMD.append("--simd")
  166. if xip_flag:
  167. CMD.append("--xip")
  168. if qemu_flag:
  169. CMD.append("--qemu")
  170. CMD.append("--qemu-firmware")
  171. CMD.append(qemu_firmware)
  172. if not clean_up_flag:
  173. CMD.append("--no_cleanup")
  174. if gc_flag:
  175. CMD.append("--gc")
  176. if log != "":
  177. CMD.append("--log-dir")
  178. CMD.append(log)
  179. case_path = pathlib.Path(case_path).resolve()
  180. case_name = case_path.stem
  181. CMD.append(str(case_path))
  182. # print(f"============> use {' '.join(CMD)}")
  183. print(f"============> run {case_name} ", end="")
  184. with subprocess.Popen(
  185. CMD,
  186. bufsize=1,
  187. stdout=subprocess.PIPE,
  188. stderr=subprocess.PIPE,
  189. universal_newlines=True,
  190. ) as p:
  191. try:
  192. case_last_words = []
  193. while not p.poll():
  194. output = p.stdout.readline()
  195. if not output:
  196. break
  197. if verbose_flag:
  198. print(output, end="")
  199. else:
  200. if len(case_last_words) == 16:
  201. case_last_words.pop(0)
  202. case_last_words.append(output)
  203. p.wait(60)
  204. if p.returncode:
  205. print(f"failed with a non-zero return code {p.returncode}")
  206. if not verbose_flag:
  207. print(
  208. f"\n==================== LAST LOG of {case_name} ====================\n"
  209. )
  210. print("".join(case_last_words))
  211. print("\n==================== LAST LOG END ====================\n")
  212. raise Exception(case_name)
  213. else:
  214. print("successful")
  215. return True
  216. except subprocess.CalledProcessError:
  217. print("failed with CalledProcessError")
  218. raise Exception(case_name)
  219. except subprocess.TimeoutExpired:
  220. print("failed with TimeoutExpired")
  221. raise Exception(case_name)
  222. def test_suite(
  223. target,
  224. aot_flag=False,
  225. sgx_flag=False,
  226. multi_module_flag=False,
  227. multi_thread_flag=False,
  228. simd_flag=False,
  229. xip_flag=False,
  230. clean_up_flag=True,
  231. verbose_flag=True,
  232. gc_flag=False,
  233. parl_flag=False,
  234. qemu_flag=False,
  235. qemu_firmware="",
  236. log="",
  237. no_pty=False,
  238. ):
  239. suite_path = pathlib.Path(SPEC_TEST_DIR).resolve()
  240. if not suite_path.exists():
  241. print(f"can not find spec test cases at {suite_path}")
  242. return False
  243. case_list = sorted(suite_path.glob("*.wast"))
  244. if simd_flag:
  245. simd_case_list = sorted(suite_path.glob("simd/*.wast"))
  246. case_list.extend(simd_case_list)
  247. if gc_flag:
  248. gc_case_list = sorted(suite_path.glob("gc/*.wast"))
  249. case_list.extend(gc_case_list)
  250. # ignore based on command line options
  251. filtered_case_list = []
  252. for case_path in case_list:
  253. case_name = case_path.stem
  254. if not ignore_the_case(
  255. case_name,
  256. target,
  257. aot_flag,
  258. sgx_flag,
  259. multi_module_flag,
  260. multi_thread_flag,
  261. simd_flag,
  262. gc_flag,
  263. xip_flag,
  264. qemu_flag,
  265. ):
  266. filtered_case_list.append(case_path)
  267. print(f"---> {len(case_list)} --filter--> {len(filtered_case_list)}")
  268. case_list = filtered_case_list
  269. case_count = len(case_list)
  270. failed_case = 0
  271. successful_case = 0
  272. if parl_flag:
  273. print(f"----- Run the whole spec test suite on {mp.cpu_count()} cores -----")
  274. with mp.Pool() as pool:
  275. results = {}
  276. for case_path in case_list:
  277. results[case_path.stem] = pool.apply_async(
  278. test_case,
  279. [
  280. str(case_path),
  281. target,
  282. aot_flag,
  283. sgx_flag,
  284. multi_module_flag,
  285. multi_thread_flag,
  286. simd_flag,
  287. xip_flag,
  288. clean_up_flag,
  289. verbose_flag,
  290. gc_flag,
  291. qemu_flag,
  292. qemu_firmware,
  293. log,
  294. no_pty,
  295. ],
  296. )
  297. for case_name, result in results.items():
  298. try:
  299. if qemu_flag:
  300. # 60 min / case, testing on QEMU may be very slow
  301. result.wait(7200)
  302. else:
  303. # 5 min / case
  304. result.wait(300)
  305. if not result.successful():
  306. failed_case += 1
  307. else:
  308. successful_case += 1
  309. except mp.TimeoutError:
  310. print(f"{case_name} meets TimeoutError")
  311. failed_case += 1
  312. else:
  313. print(f"----- Run the whole spec test suite -----")
  314. for case_path in case_list:
  315. try:
  316. test_case(
  317. str(case_path),
  318. target,
  319. aot_flag,
  320. sgx_flag,
  321. multi_module_flag,
  322. multi_thread_flag,
  323. simd_flag,
  324. xip_flag,
  325. clean_up_flag,
  326. verbose_flag,
  327. gc_flag,
  328. qemu_flag,
  329. qemu_firmware,
  330. log,
  331. no_pty,
  332. )
  333. successful_case += 1
  334. except Exception as e:
  335. failed_case += 1
  336. raise e
  337. print(
  338. f"IN ALL {case_count} cases: {successful_case} PASS, {failed_case} FAIL, {case_count - successful_case - failed_case} SKIP"
  339. )
  340. return 0 == failed_case
  341. def main():
  342. parser = argparse.ArgumentParser(description="run the whole spec test suite")
  343. parser.add_argument(
  344. "-M",
  345. action="store_true",
  346. default=False,
  347. dest="multi_module_flag",
  348. help="Running with the Multi-Module feature",
  349. )
  350. parser.add_argument(
  351. "-m",
  352. choices=AVAILABLE_TARGETS,
  353. type=str,
  354. dest="target",
  355. default="X86_64",
  356. help="Specify Target ",
  357. )
  358. parser.add_argument(
  359. "-p",
  360. action="store_true",
  361. default=False,
  362. dest="multi_thread_flag",
  363. help="Running with the Multi-Thread feature",
  364. )
  365. parser.add_argument(
  366. "-S",
  367. action="store_true",
  368. default=False,
  369. dest="simd_flag",
  370. help="Running with the SIMD feature",
  371. )
  372. parser.add_argument(
  373. "-X",
  374. action="store_true",
  375. default=False,
  376. dest="xip_flag",
  377. help="Running with the XIP feature",
  378. )
  379. parser.add_argument(
  380. "-t",
  381. action="store_true",
  382. default=False,
  383. dest="aot_flag",
  384. help="Running with AOT mode",
  385. )
  386. parser.add_argument(
  387. "-x",
  388. action="store_true",
  389. default=False,
  390. dest="sgx_flag",
  391. help="Running with SGX environment",
  392. )
  393. parser.add_argument(
  394. "--no_clean_up",
  395. action="store_false",
  396. default=True,
  397. dest="clean_up_flag",
  398. help="Does not remove tmpfiles. But it will be enabled while running parallelly",
  399. )
  400. parser.add_argument(
  401. "--parl",
  402. action="store_true",
  403. default=False,
  404. dest="parl_flag",
  405. help="To run whole test suite parallelly",
  406. )
  407. parser.add_argument(
  408. "--qemu",
  409. action="store_true",
  410. default=False,
  411. dest="qemu_flag",
  412. help="To run whole test suite in qemu",
  413. )
  414. parser.add_argument(
  415. "--qemu-firmware",
  416. default="",
  417. dest="qemu_firmware",
  418. help="Firmware required by qemu",
  419. )
  420. parser.add_argument(
  421. "--log",
  422. default="",
  423. dest="log",
  424. help="Log directory",
  425. )
  426. parser.add_argument(
  427. "--quiet",
  428. action="store_false",
  429. default=True,
  430. dest="verbose_flag",
  431. help="Close real time output while running cases, only show last words of failed ones",
  432. )
  433. parser.add_argument(
  434. "--gc",
  435. action="store_true",
  436. default=False,
  437. dest="gc_flag",
  438. help="Running with GC feature",
  439. )
  440. parser.add_argument(
  441. "cases",
  442. metavar="path_to__case",
  443. type=str,
  444. nargs="*",
  445. help=f"Specify all wanted cases. If not the script will go through all cases under {SPEC_TEST_DIR}",
  446. )
  447. parser.add_argument('--no-pty', action='store_true',
  448. help="Use direct pipes instead of pseudo-tty")
  449. options = parser.parse_args()
  450. # Convert target to lower case for internal use, e.g. X86_64 -> x86_64
  451. # target is always exist, so no need to check it
  452. options.target = options.target.lower()
  453. if options.target == "x86_32":
  454. options.target = "i386"
  455. if not preflight_check(options.aot_flag):
  456. return False
  457. if not options.cases:
  458. if options.parl_flag:
  459. # several cases might share the same workspace/tempfile at the same time
  460. # so, disable it while running parallelly
  461. options.clean_up_flag = False
  462. options.verbose_flag = False
  463. start = time.time_ns()
  464. ret = test_suite(
  465. options.target,
  466. options.aot_flag,
  467. options.sgx_flag,
  468. options.multi_module_flag,
  469. options.multi_thread_flag,
  470. options.simd_flag,
  471. options.xip_flag,
  472. options.clean_up_flag,
  473. options.verbose_flag,
  474. options.gc_flag,
  475. options.parl_flag,
  476. options.qemu_flag,
  477. options.qemu_firmware,
  478. options.log,
  479. options.no_pty
  480. )
  481. end = time.time_ns()
  482. print(
  483. f"It takes {((end - start) / 1000000):,} ms to run test_suite {'parallelly' if options.parl_flag else ''}"
  484. )
  485. else:
  486. try:
  487. for case in options.cases:
  488. test_case(
  489. case,
  490. options.target,
  491. options.aot_flag,
  492. options.sgx_flag,
  493. options.multi_module_flag,
  494. options.multi_thread_flag,
  495. options.simd_flag,
  496. options.xip_flag,
  497. options.clean_up_flag,
  498. options.verbose_flag,
  499. options.gc_flag,
  500. options.qemu_flag,
  501. options.qemu_firmware,
  502. options.log,
  503. options.no_pty,
  504. )
  505. else:
  506. ret = True
  507. except Exception:
  508. ret = False
  509. return ret
  510. if __name__ == "__main__":
  511. sys.exit(0 if main() else 1)