all.py 14 KB

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