main.c 18 KB

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