main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "bh_platform.h"
  8. #include "bh_read_file.h"
  9. #include "wasm_export.h"
  10. #if WASM_ENABLE_LIBC_WASI != 0
  11. #include "../common/libc_wasi.c"
  12. #endif
  13. static int app_argc;
  14. static char **app_argv;
  15. #define MODULE_PATH ("--module-path=")
  16. /* clang-format off */
  17. static int
  18. print_help()
  19. {
  20. printf("Usage: iwasm [-options] wasm_file [args...]\n");
  21. printf("options:\n");
  22. printf(" -f|--function name Specify a function name of the module to run rather\n"
  23. " than main\n");
  24. #if WASM_ENABLE_LOG != 0
  25. printf(" -v=n Set log verbose level (0 to 5, default is 2) larger\n"
  26. " level with more log\n");
  27. #endif
  28. #if WASM_ENABLE_INTERP != 0
  29. printf(" --interp Run the wasm app with interpreter mode\n");
  30. #endif
  31. #if WASM_ENABLE_FAST_JIT != 0
  32. printf(" --fast-jit Run the wasm app with fast jit mode\n");
  33. #endif
  34. #if WASM_ENABLE_JIT != 0
  35. printf(" --llvm-jit Run the wasm app with llvm jit mode\n");
  36. #endif
  37. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  38. printf(" --multi-tier-jit Run the wasm app with multi-tier jit mode\n");
  39. #endif
  40. printf(" --stack-size=n Set maximum stack size in bytes, default is 64 KB\n");
  41. #if WASM_ENABLE_LIBC_WASI !=0
  42. printf(" --heap-size=n Set maximum heap size in bytes, default is 0 KB when libc wasi is enabled\n");
  43. #else
  44. printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB when libc wasi is diabled\n");
  45. #endif
  46. #if WASM_ENABLE_GC != 0
  47. printf(" --gc-heap-size=n Set maximum gc heap size in bytes,\n");
  48. printf(" default is %u KB\n", GC_HEAP_SIZE_DEFAULT / 1024);
  49. #endif
  50. #if WASM_ENABLE_SHARED_HEAP != 0
  51. printf(" --shared-heap-size=n Create shared heap of n bytes and attach to the wasm app.\n");
  52. printf(" The size n will be adjusted to a minumum number aligned to page size\n");
  53. #endif
  54. #if WASM_ENABLE_JIT != 0
  55. printf(" --llvm-jit-size-level=n Set LLVM JIT size level, default is 3\n");
  56. printf(" --llvm-jit-opt-level=n Set LLVM JIT optimization level, default is 3\n");
  57. #endif
  58. printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
  59. " that runs commands in the form of `FUNC ARG...`\n");
  60. #if WASM_ENABLE_LIBC_WASI != 0
  61. libc_wasi_print_help();
  62. #endif
  63. #if WASM_ENABLE_MULTI_MODULE != 0
  64. printf(" --module-path=<path> Indicate a module search path. default is current\n"
  65. " directory('./')\n");
  66. #endif
  67. #if WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
  68. printf(" --max-threads=n Set maximum thread number per cluster, default is 4\n");
  69. #endif
  70. #if WASM_ENABLE_DEBUG_INTERP != 0
  71. printf(" -g=ip:port Set the debug sever address, default is debug disabled\n");
  72. printf(" if port is 0, then a random port will be used\n");
  73. #endif
  74. printf(" --version Show version information\n");
  75. return 1;
  76. }
  77. /* clang-format on */
  78. static const void *
  79. app_instance_main(wasm_module_inst_t module_inst)
  80. {
  81. const char *exception;
  82. wasm_application_execute_main(module_inst, app_argc, app_argv);
  83. exception = wasm_runtime_get_exception(module_inst);
  84. return exception;
  85. }
  86. static const void *
  87. app_instance_func(wasm_module_inst_t module_inst, const char *func_name)
  88. {
  89. wasm_application_execute_func(module_inst, func_name, app_argc - 1,
  90. app_argv + 1);
  91. /* The result of wasm function or exception info was output inside
  92. wasm_application_execute_func(), here we don't output them again. */
  93. return wasm_runtime_get_exception(module_inst);
  94. }
  95. /**
  96. * Split a space separated strings into an array of strings
  97. * Returns NULL on failure
  98. * Memory must be freed by caller
  99. * Based on: http://stackoverflow.com/a/11198630/471795
  100. */
  101. static char **
  102. split_string(char *str, int *count)
  103. {
  104. char **res = NULL, **res1;
  105. char *p, *next_token;
  106. int idx = 0;
  107. /* split string and append tokens to 'res' */
  108. do {
  109. p = strtok_s(str, " ", &next_token);
  110. str = NULL;
  111. res1 = res;
  112. res = (char **)realloc(res1, sizeof(char *) * (uint32)(idx + 1));
  113. if (res == NULL) {
  114. free(res1);
  115. return NULL;
  116. }
  117. res[idx++] = p;
  118. } while (p);
  119. /**
  120. * Due to the function name,
  121. * res[0] might contain a '\' to indicate a space
  122. * func\name -> func name
  123. */
  124. p = strchr(res[0], '\\');
  125. while (p) {
  126. *p = ' ';
  127. p = strchr(p, '\\');
  128. }
  129. if (count) {
  130. *count = idx - 1;
  131. }
  132. return res;
  133. }
  134. static void *
  135. app_instance_repl(wasm_module_inst_t module_inst)
  136. {
  137. char buffer[4096];
  138. char *cmd;
  139. size_t n;
  140. while ((printf("webassembly> "), fflush(stdout),
  141. cmd = fgets(buffer, sizeof(buffer), stdin))
  142. != NULL) {
  143. bh_assert(cmd);
  144. n = strlen(cmd);
  145. if (cmd[n - 1] == '\n') {
  146. if (n == 1)
  147. continue;
  148. else
  149. cmd[n - 1] = '\0';
  150. }
  151. if (!strcmp(cmd, "__exit__")) {
  152. printf("exit repl mode\n");
  153. break;
  154. }
  155. app_argv = split_string(cmd, &app_argc);
  156. if (app_argv == NULL) {
  157. LOG_ERROR("Wasm prepare param failed: split string failed.\n");
  158. break;
  159. }
  160. if (app_argc != 0) {
  161. const char *exception;
  162. wasm_application_execute_func(module_inst, app_argv[0],
  163. app_argc - 1, app_argv + 1);
  164. if ((exception = wasm_runtime_get_exception(module_inst)))
  165. printf("%s\n", exception);
  166. }
  167. free(app_argv);
  168. }
  169. return NULL;
  170. }
  171. #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
  172. static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
  173. #else
  174. static void *
  175. malloc_func(
  176. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  177. void *user_data,
  178. #endif
  179. unsigned int size)
  180. {
  181. return malloc(size);
  182. }
  183. static void *
  184. realloc_func(
  185. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  186. void *user_data,
  187. #endif
  188. void *ptr, unsigned int size)
  189. {
  190. return realloc(ptr, size);
  191. }
  192. static void
  193. free_func(
  194. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  195. void *user_data,
  196. #endif
  197. void *ptr)
  198. {
  199. free(ptr);
  200. }
  201. #endif /* end of WASM_ENABLE_GLOBAL_HEAP_POOL */
  202. #if WASM_ENABLE_MULTI_MODULE != 0
  203. static char *
  204. handle_module_path(const char *module_path)
  205. {
  206. /* next character after '=' */
  207. return (strchr(module_path, '=')) + 1;
  208. }
  209. static char *module_search_path = ".";
  210. static bool
  211. module_reader_callback(package_type_t module_type, const char *module_name,
  212. uint8 **p_buffer, uint32 *p_size)
  213. {
  214. char *file_format = NULL;
  215. #if WASM_ENABLE_INTERP != 0
  216. if (module_type == Wasm_Module_Bytecode)
  217. file_format = ".wasm";
  218. #endif
  219. #if WASM_ENABLE_AOT != 0
  220. if (module_type == Wasm_Module_AoT)
  221. file_format = ".aot";
  222. #endif
  223. bh_assert(file_format);
  224. const char *format = "%s/%s%s";
  225. int sz = strlen(module_search_path) + strlen("/") + strlen(module_name)
  226. + strlen(file_format) + 1;
  227. char *wasm_file_name = wasm_runtime_malloc(sz);
  228. if (!wasm_file_name) {
  229. return false;
  230. }
  231. snprintf(wasm_file_name, sz, format, module_search_path, module_name,
  232. file_format);
  233. *p_buffer = (uint8_t *)bh_read_file_to_buffer(wasm_file_name, p_size);
  234. wasm_runtime_free(wasm_file_name);
  235. return *p_buffer != NULL;
  236. }
  237. static void
  238. module_destroyer_callback(uint8 *buffer, uint32 size)
  239. {
  240. if (!buffer) {
  241. return;
  242. }
  243. wasm_runtime_free(buffer);
  244. buffer = NULL;
  245. }
  246. #endif /* WASM_ENABLE_MULTI_MODULE */
  247. int
  248. main(int argc, char *argv[])
  249. {
  250. int32 ret = -1;
  251. char *wasm_file = NULL;
  252. const char *func_name = NULL;
  253. uint8 *wasm_file_buf = NULL;
  254. uint32 wasm_file_size;
  255. uint32 stack_size = 64 * 1024;
  256. #if WASM_ENABLE_LIBC_WASI != 0
  257. uint32 heap_size = 0;
  258. #else
  259. uint32 heap_size = 16 * 1024;
  260. #endif
  261. #if WASM_ENABLE_GC != 0
  262. uint32 gc_heap_size = GC_HEAP_SIZE_DEFAULT;
  263. #endif
  264. #if WASM_ENABLE_JIT != 0
  265. uint32 llvm_jit_size_level = 3;
  266. uint32 llvm_jit_opt_level = 3;
  267. #endif
  268. wasm_module_t wasm_module = NULL;
  269. wasm_module_inst_t wasm_module_inst = NULL;
  270. RunningMode running_mode = 0;
  271. RuntimeInitArgs init_args;
  272. char error_buf[128] = { 0 };
  273. #if WASM_ENABLE_LOG != 0
  274. int log_verbose_level = 2;
  275. #endif
  276. bool is_repl_mode = false;
  277. bool is_xip_file = false;
  278. #if WASM_ENABLE_LIBC_WASI != 0
  279. libc_wasi_parse_context_t wasi_parse_ctx;
  280. #endif
  281. #if WASM_ENABLE_DEBUG_INTERP != 0
  282. char *ip_addr = NULL;
  283. int instance_port = 0;
  284. #endif
  285. #if WASM_ENABLE_LIBC_WASI != 0
  286. memset(&wasi_parse_ctx, 0, sizeof(wasi_parse_ctx));
  287. #endif
  288. /* Process options. */
  289. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  290. if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--function")) {
  291. argc--, argv++;
  292. if (argc < 2) {
  293. return print_help();
  294. }
  295. func_name = argv[0];
  296. }
  297. #if WASM_ENABLE_INTERP != 0
  298. else if (!strcmp(argv[0], "--interp")) {
  299. running_mode = Mode_Interp;
  300. }
  301. #endif
  302. #if WASM_ENABLE_FAST_JIT != 0
  303. else if (!strcmp(argv[0], "--fast-jit")) {
  304. running_mode = Mode_Fast_JIT;
  305. }
  306. #endif
  307. #if WASM_ENABLE_JIT != 0
  308. else if (!strcmp(argv[0], "--llvm-jit")) {
  309. running_mode = Mode_LLVM_JIT;
  310. }
  311. #endif
  312. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_FAST_JIT != 0
  313. else if (!strcmp(argv[0], "--multi-tier-jit")) {
  314. running_mode = Mode_Multi_Tier_JIT;
  315. }
  316. #endif
  317. #if WASM_ENABLE_LOG != 0
  318. else if (!strncmp(argv[0], "-v=", 3)) {
  319. log_verbose_level = atoi(argv[0] + 3);
  320. if (log_verbose_level < 0 || log_verbose_level > 5)
  321. return print_help();
  322. }
  323. #endif
  324. else if (!strcmp(argv[0], "--repl")) {
  325. is_repl_mode = true;
  326. }
  327. else if (!strncmp(argv[0], "--stack-size=", 13)) {
  328. if (argv[0][13] == '\0')
  329. return print_help();
  330. stack_size = atoi(argv[0] + 13);
  331. }
  332. else if (!strncmp(argv[0], "--heap-size=", 12)) {
  333. if (argv[0][12] == '\0')
  334. return print_help();
  335. heap_size = atoi(argv[0] + 12);
  336. }
  337. #if WASM_ENABLE_GC != 0
  338. else if (!strncmp(argv[0], "--gc-heap-size=", 15)) {
  339. if (argv[0][15] == '\0')
  340. return print_help();
  341. gc_heap_size = atoi(argv[0] + 15);
  342. }
  343. #endif
  344. #if WASM_ENABLE_SHARED_HEAP != 0
  345. else if (!strncmp(argv[0], "--shared-heap-size=", 19)) {
  346. if (argv[0][19] == '\0')
  347. return print_help();
  348. shared_heap_size = atoi(argv[0] + 19);
  349. }
  350. #endif
  351. #if WASM_ENABLE_JIT != 0
  352. else if (!strncmp(argv[0], "--llvm-jit-size-level=", 22)) {
  353. if (argv[0][22] == '\0')
  354. return print_help();
  355. llvm_jit_size_level = atoi(argv[0] + 22);
  356. if (llvm_jit_size_level < 1) {
  357. printf("LLVM JIT size level shouldn't be smaller than 1, "
  358. "setting it to 1\n");
  359. llvm_jit_size_level = 1;
  360. }
  361. else if (llvm_jit_size_level > 3) {
  362. printf("LLVM JIT size level shouldn't be greater than 3, "
  363. "setting it to 3\n");
  364. llvm_jit_size_level = 3;
  365. }
  366. }
  367. else if (!strncmp(argv[0], "--llvm-jit-opt-level=", 21)) {
  368. if (argv[0][21] == '\0')
  369. return print_help();
  370. llvm_jit_opt_level = atoi(argv[0] + 21);
  371. if (llvm_jit_opt_level < 1) {
  372. printf("LLVM JIT opt level shouldn't be smaller than 1, "
  373. "setting it to 1\n");
  374. llvm_jit_opt_level = 1;
  375. }
  376. else if (llvm_jit_opt_level > 3) {
  377. printf("LLVM JIT opt level shouldn't be greater than 3, "
  378. "setting it to 3\n");
  379. llvm_jit_opt_level = 3;
  380. }
  381. }
  382. #endif
  383. #if WASM_ENABLE_MULTI_MODULE != 0
  384. else if (!strncmp(argv[0], MODULE_PATH, strlen(MODULE_PATH))) {
  385. module_search_path = handle_module_path(argv[0]);
  386. if (!strlen(module_search_path)) {
  387. return print_help();
  388. }
  389. }
  390. #endif
  391. #if WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
  392. else if (!strncmp(argv[0], "--max-threads=", 14)) {
  393. if (argv[0][14] == '\0')
  394. return print_help();
  395. wasm_runtime_set_max_thread_num(atoi(argv[0] + 14));
  396. }
  397. #endif
  398. #if WASM_ENABLE_DEBUG_INTERP != 0
  399. else if (!strncmp(argv[0], "-g=", 3)) {
  400. char *port_str = strchr(argv[0] + 3, ':');
  401. char *port_end;
  402. if (port_str == NULL)
  403. return print_help();
  404. *port_str = '\0';
  405. instance_port = strtoul(port_str + 1, &port_end, 10);
  406. if (port_str[1] == '\0' || *port_end != '\0')
  407. return print_help();
  408. ip_addr = argv[0] + 3;
  409. }
  410. #endif
  411. else if (!strcmp(argv[0], "--version")) {
  412. uint32 major, minor, patch;
  413. wasm_runtime_get_version(&major, &minor, &patch);
  414. printf("iwasm %" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n", major, minor,
  415. patch);
  416. return 0;
  417. }
  418. else {
  419. #if WASM_ENABLE_LIBC_WASI != 0
  420. libc_wasi_parse_result_t result =
  421. libc_wasi_parse(argv[0], &wasi_parse_ctx);
  422. switch (result) {
  423. case LIBC_WASI_PARSE_RESULT_OK:
  424. continue;
  425. case LIBC_WASI_PARSE_RESULT_NEED_HELP:
  426. return print_help();
  427. case LIBC_WASI_PARSE_RESULT_BAD_PARAM:
  428. return 1;
  429. }
  430. #else
  431. return print_help();
  432. #endif
  433. }
  434. }
  435. if (argc == 0)
  436. return print_help();
  437. wasm_file = argv[0];
  438. app_argc = argc;
  439. app_argv = argv;
  440. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  441. init_args.running_mode = running_mode;
  442. #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
  443. init_args.mem_alloc_type = Alloc_With_Pool;
  444. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  445. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  446. #else
  447. init_args.mem_alloc_type = Alloc_With_Allocator;
  448. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  449. /* Set user data for the allocator is needed */
  450. /* init_args.mem_alloc_option.allocator.user_data = user_data; */
  451. #endif
  452. init_args.mem_alloc_option.allocator.malloc_func = malloc_func;
  453. init_args.mem_alloc_option.allocator.realloc_func = realloc_func;
  454. init_args.mem_alloc_option.allocator.free_func = free_func;
  455. #endif
  456. #if WASM_ENABLE_GC != 0
  457. init_args.gc_heap_size = gc_heap_size;
  458. #endif
  459. #if WASM_ENABLE_JIT != 0
  460. init_args.llvm_jit_size_level = llvm_jit_size_level;
  461. init_args.llvm_jit_opt_level = llvm_jit_opt_level;
  462. #endif
  463. #if WASM_ENABLE_DEBUG_INTERP != 0
  464. init_args.instance_port = instance_port;
  465. if (ip_addr)
  466. /* ensure that init_args.ip_addr is null terminated */
  467. strncpy_s(init_args.ip_addr, sizeof(init_args.ip_addr) - 1, ip_addr,
  468. strlen(ip_addr));
  469. #endif
  470. /* initialize runtime environment */
  471. if (!wasm_runtime_full_init(&init_args)) {
  472. printf("Init runtime environment failed.\n");
  473. return -1;
  474. }
  475. #if WASM_ENABLE_LOG != 0
  476. bh_log_set_verbose_level(log_verbose_level);
  477. #endif
  478. /* load WASM byte buffer from WASM bin file */
  479. if (!(wasm_file_buf =
  480. (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size)))
  481. goto fail1;
  482. #if WASM_ENABLE_AOT != 0
  483. if (wasm_runtime_is_xip_file(wasm_file_buf, wasm_file_size)) {
  484. uint8 *wasm_file_mapped;
  485. int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
  486. int map_flags = MMAP_MAP_32BIT;
  487. if (!(wasm_file_mapped = os_mmap(NULL, (uint32)wasm_file_size, map_prot,
  488. map_flags, os_get_invalid_handle()))) {
  489. printf("mmap memory failed\n");
  490. wasm_runtime_free(wasm_file_buf);
  491. goto fail1;
  492. }
  493. bh_memcpy_s(wasm_file_mapped, wasm_file_size, wasm_file_buf,
  494. wasm_file_size);
  495. wasm_runtime_free(wasm_file_buf);
  496. wasm_file_buf = wasm_file_mapped;
  497. is_xip_file = true;
  498. }
  499. #endif
  500. #if WASM_ENABLE_MULTI_MODULE != 0
  501. wasm_runtime_set_module_reader(module_reader_callback,
  502. module_destroyer_callback);
  503. #endif
  504. /* load WASM module */
  505. if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
  506. error_buf, sizeof(error_buf)))) {
  507. printf("%s\n", error_buf);
  508. goto fail2;
  509. }
  510. #if WASM_ENABLE_LIBC_WASI != 0
  511. libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx);
  512. #endif
  513. /* instantiate the module */
  514. if (!(wasm_module_inst =
  515. wasm_runtime_instantiate(wasm_module, stack_size, heap_size,
  516. error_buf, sizeof(error_buf)))) {
  517. printf("%s\n", error_buf);
  518. goto fail3;
  519. }
  520. #if WASM_ENABLE_SHARED_HEAP != 0
  521. if (shared_heap_size > 0) {
  522. memset(&shared_heap_init_args, 0, sizeof(shared_heap_init_args));
  523. shared_heap_init_args.size = shared_heap_size;
  524. shared_heap = wasm_runtime_create_shared_heap(&shared_heap_init_args);
  525. if (!shared_heap) {
  526. printf("Create shared heap failed.\n");
  527. goto fail5;
  528. }
  529. if (!wasm_runtime_attach_shared_heap(wasm_module_inst, shared_heap)) {
  530. printf("Attach shared heap failed.\n");
  531. goto fail5;
  532. }
  533. }
  534. #endif
  535. #if WASM_ENABLE_DEBUG_INTERP != 0
  536. if (ip_addr != NULL) {
  537. wasm_exec_env_t exec_env =
  538. wasm_runtime_get_exec_env_singleton(wasm_module_inst);
  539. uint32_t debug_port;
  540. if (exec_env == NULL) {
  541. printf("%s\n", wasm_runtime_get_exception(wasm_module_inst));
  542. goto fail4;
  543. }
  544. debug_port = wasm_runtime_start_debug_instance(exec_env);
  545. if (debug_port == 0) {
  546. printf("Failed to start debug instance\n");
  547. goto fail4;
  548. }
  549. }
  550. #endif
  551. ret = 0;
  552. const char *exception = NULL;
  553. if (is_repl_mode) {
  554. app_instance_repl(wasm_module_inst);
  555. }
  556. else if (func_name) {
  557. exception = app_instance_func(wasm_module_inst, func_name);
  558. if (exception) {
  559. /* got an exception */
  560. ret = 1;
  561. }
  562. }
  563. else {
  564. exception = app_instance_main(wasm_module_inst);
  565. if (exception) {
  566. /* got an exception */
  567. ret = 1;
  568. }
  569. }
  570. #if WASM_ENABLE_LIBC_WASI != 0
  571. if (ret == 0) {
  572. /* propagate wasi exit code. */
  573. ret = wasm_runtime_get_wasi_exit_code(wasm_module_inst);
  574. }
  575. #endif
  576. if (exception)
  577. printf("%s\n", exception);
  578. #if WASM_ENABLE_SHARED_HEAP != 0
  579. fail5:
  580. #endif
  581. #if WASM_ENABLE_DEBUG_INTERP != 0
  582. fail4:
  583. #endif
  584. /* destroy the module instance */
  585. wasm_runtime_deinstantiate(wasm_module_inst);
  586. fail3:
  587. /* unload the module */
  588. wasm_runtime_unload(wasm_module);
  589. fail2:
  590. /* free the file buffer */
  591. if (!is_xip_file)
  592. wasm_runtime_free(wasm_file_buf);
  593. else
  594. os_munmap(wasm_file_buf, wasm_file_size);
  595. fail1:
  596. /* destroy runtime environment */
  597. wasm_runtime_destroy();
  598. return ret;
  599. }