main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. static int app_argc;
  11. static char **app_argv;
  12. #define MODULE_PATH ("--module-path=")
  13. /* clang-format off */
  14. static int
  15. print_help()
  16. {
  17. printf("Usage: iwasm [-options] wasm_file [args...]\n");
  18. printf("options:\n");
  19. printf(" -f|--function name Specify a function name of the module to run rather\n"
  20. " than main\n");
  21. #if WASM_ENABLE_LOG != 0
  22. printf(" -v=n Set log verbose level (0 to 5, default is 2) larger\n"
  23. " level with more log\n");
  24. #endif
  25. printf(" --stack-size=n Set maximum stack size in bytes, default is 16 KB\n");
  26. printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB\n");
  27. printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
  28. " that runs commands in the form of `FUNC ARG...`\n");
  29. #if WASM_ENABLE_LIBC_WASI != 0
  30. printf(" --env=<env> Pass wasi environment variables with \"key=value\"\n");
  31. printf(" to the program, for example:\n");
  32. printf(" --env=\"key1=value1\" --env=\"key2=value2\"\n");
  33. printf(" --dir=<dir> Grant wasi access to the given host directories\n");
  34. printf(" to the program, for example:\n");
  35. printf(" --dir=<dir1> --dir=<dir2>\n");
  36. #endif
  37. #if WASM_ENABLE_MULTI_MODULE != 0
  38. printf(" --module-path= Indicate a module search path. default is current\n"
  39. " directory('./')\n");
  40. #endif
  41. #if WASM_ENABLE_LIB_PTHREAD != 0
  42. printf(" --max-threads=n Set maximum thread number per cluster, default is 4\n");
  43. #endif
  44. return 1;
  45. }
  46. /* clang-format on */
  47. static void *
  48. app_instance_main(wasm_module_inst_t module_inst)
  49. {
  50. const char *exception;
  51. wasm_application_execute_main(module_inst, app_argc, app_argv);
  52. if ((exception = wasm_runtime_get_exception(module_inst)))
  53. printf("%s\n", exception);
  54. return NULL;
  55. }
  56. static void *
  57. app_instance_func(wasm_module_inst_t module_inst, const char *func_name)
  58. {
  59. wasm_application_execute_func(module_inst, func_name, app_argc - 1,
  60. app_argv + 1);
  61. /* The result of wasm function or exception info was output inside
  62. wasm_application_execute_func(), here we don't output them again. */
  63. return NULL;
  64. }
  65. /**
  66. * Split a space separated strings into an array of strings
  67. * Returns NULL on failure
  68. * Memory must be freed by caller
  69. * Based on: http://stackoverflow.com/a/11198630/471795
  70. */
  71. static char **
  72. split_string(char *str, int *count)
  73. {
  74. char **res = NULL;
  75. char *p, *next_token;
  76. int idx = 0;
  77. /* split string and append tokens to 'res' */
  78. do {
  79. p = strtok_s(str, " ", &next_token);
  80. str = NULL;
  81. res = (char **)realloc(res, sizeof(char *) * (uint32)(idx + 1));
  82. if (res == NULL) {
  83. return NULL;
  84. }
  85. res[idx++] = p;
  86. } while (p);
  87. /**
  88. * since the function name,
  89. * res[0] might be contains a '\' to indicate a space
  90. * func\name -> func name
  91. */
  92. p = strchr(res[0], '\\');
  93. while (p) {
  94. *p = ' ';
  95. p = strchr(p, '\\');
  96. }
  97. if (count) {
  98. *count = idx - 1;
  99. }
  100. return res;
  101. }
  102. static void *
  103. app_instance_repl(wasm_module_inst_t module_inst)
  104. {
  105. char buffer[4096];
  106. char *cmd;
  107. size_t n;
  108. while ((printf("webassembly> "), cmd = fgets(buffer, sizeof(buffer), stdin))
  109. != NULL) {
  110. bh_assert(cmd);
  111. n = strlen(cmd);
  112. if (cmd[n - 1] == '\n') {
  113. if (n == 1)
  114. continue;
  115. else
  116. cmd[n - 1] = '\0';
  117. }
  118. if (!strcmp(cmd, "__exit__")) {
  119. printf("exit repl mode\n");
  120. break;
  121. }
  122. app_argv = split_string(cmd, &app_argc);
  123. if (app_argv == NULL) {
  124. LOG_ERROR("Wasm prepare param failed: split string failed.\n");
  125. break;
  126. }
  127. if (app_argc != 0) {
  128. wasm_application_execute_func(module_inst, app_argv[0],
  129. app_argc - 1, app_argv + 1);
  130. }
  131. free(app_argv);
  132. }
  133. return NULL;
  134. }
  135. #if WASM_ENABLE_LIBC_WASI != 0
  136. static bool
  137. validate_env_str(char *env)
  138. {
  139. char *p = env;
  140. int key_len = 0;
  141. while (*p != '\0' && *p != '=') {
  142. key_len++;
  143. p++;
  144. }
  145. if (*p != '=' || key_len == 0)
  146. return false;
  147. return true;
  148. }
  149. #endif
  150. #define USE_GLOBAL_HEAP_BUF 0
  151. #if USE_GLOBAL_HEAP_BUF != 0
  152. static char global_heap_buf[10 * 1024 * 1024] = { 0 };
  153. #endif
  154. #if WASM_ENABLE_MULTI_MODULE != 0
  155. static char *
  156. handle_module_path(const char *module_path)
  157. {
  158. /* next character after '=' */
  159. return (strchr(module_path, '=')) + 1;
  160. }
  161. static char *module_search_path = ".";
  162. static bool
  163. module_reader_callback(const char *module_name, uint8 **p_buffer,
  164. uint32 *p_size)
  165. {
  166. const char *format = "%s/%s.wasm";
  167. uint32 sz = (uint32)(strlen(module_search_path) + strlen("/")
  168. + strlen(module_name) + strlen(".wasm") + 1);
  169. char *wasm_file_name = BH_MALLOC(sz);
  170. if (!wasm_file_name) {
  171. return false;
  172. }
  173. snprintf(wasm_file_name, sz, format, module_search_path, module_name);
  174. *p_buffer = (uint8 *)bh_read_file_to_buffer(wasm_file_name, p_size);
  175. wasm_runtime_free(wasm_file_name);
  176. return *p_buffer != NULL;
  177. }
  178. static void
  179. moudle_destroyer(uint8 *buffer, uint32 size)
  180. {
  181. if (!buffer) {
  182. return;
  183. }
  184. wasm_runtime_free(buffer);
  185. buffer = NULL;
  186. }
  187. #endif /* WASM_ENABLE_MULTI_MODULE */
  188. int
  189. main(int argc, char *argv[])
  190. {
  191. char *wasm_file = NULL;
  192. const char *func_name = NULL;
  193. uint8 *wasm_file_buf = NULL;
  194. uint32 wasm_file_size;
  195. uint32 stack_size = 16 * 1024, heap_size = 16 * 1024;
  196. wasm_module_t wasm_module = NULL;
  197. wasm_module_inst_t wasm_module_inst = NULL;
  198. RuntimeInitArgs init_args;
  199. char error_buf[128] = { 0 };
  200. #if WASM_ENABLE_LOG != 0
  201. int log_verbose_level = 2;
  202. #endif
  203. bool is_repl_mode = false;
  204. #if WASM_ENABLE_LIBC_WASI != 0
  205. const char *dir_list[8] = { NULL };
  206. uint32 dir_list_size = 0;
  207. const char *env_list[8] = { NULL };
  208. uint32 env_list_size = 0;
  209. #endif
  210. /* Process options. */
  211. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  212. if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--function")) {
  213. argc--, argv++;
  214. if (argc < 2) {
  215. print_help();
  216. return 0;
  217. }
  218. func_name = argv[0];
  219. }
  220. #if WASM_ENABLE_LOG != 0
  221. else if (!strncmp(argv[0], "-v=", 3)) {
  222. log_verbose_level = atoi(argv[0] + 3);
  223. if (log_verbose_level < 0 || log_verbose_level > 5)
  224. return print_help();
  225. }
  226. #endif
  227. else if (!strcmp(argv[0], "--repl")) {
  228. is_repl_mode = true;
  229. }
  230. else if (!strncmp(argv[0], "--stack-size=", 13)) {
  231. if (argv[0][13] == '\0')
  232. return print_help();
  233. stack_size = atoi(argv[0] + 13);
  234. }
  235. else if (!strncmp(argv[0], "--heap-size=", 12)) {
  236. if (argv[0][12] == '\0')
  237. return print_help();
  238. heap_size = atoi(argv[0] + 12);
  239. }
  240. #if WASM_ENABLE_LIBC_WASI != 0
  241. else if (!strncmp(argv[0], "--dir=", 6)) {
  242. if (argv[0][6] == '\0')
  243. return print_help();
  244. if (dir_list_size >= sizeof(dir_list) / sizeof(char *)) {
  245. printf("Only allow max dir number %d\n",
  246. (int)(sizeof(dir_list) / sizeof(char *)));
  247. return -1;
  248. }
  249. dir_list[dir_list_size++] = argv[0] + 6;
  250. }
  251. else if (!strncmp(argv[0], "--env=", 6)) {
  252. char *tmp_env;
  253. if (argv[0][6] == '\0')
  254. return print_help();
  255. if (env_list_size >= sizeof(env_list) / sizeof(char *)) {
  256. printf("Only allow max env number %d\n",
  257. (int)(sizeof(env_list) / sizeof(char *)));
  258. return -1;
  259. }
  260. tmp_env = argv[0] + 6;
  261. if (validate_env_str(tmp_env))
  262. env_list[env_list_size++] = tmp_env;
  263. else {
  264. printf("Wasm parse env string failed: expect \"key=value\", "
  265. "got \"%s\"\n",
  266. tmp_env);
  267. return print_help();
  268. }
  269. }
  270. #endif /* WASM_ENABLE_LIBC_WASI */
  271. #if WASM_ENABLE_MULTI_MODULE != 0
  272. else if (!strncmp(argv[0], MODULE_PATH, strlen(MODULE_PATH))) {
  273. module_search_path = handle_module_path(argv[0]);
  274. if (!strlen(module_search_path)) {
  275. return print_help();
  276. }
  277. }
  278. #endif
  279. #if WASM_ENABLE_LIB_PTHREAD != 0
  280. else if (!strncmp(argv[0], "--max-threads=", 14)) {
  281. if (argv[0][14] == '\0')
  282. return print_help();
  283. wasm_runtime_set_max_thread_num(atoi(argv[0] + 14));
  284. }
  285. #endif
  286. else
  287. return print_help();
  288. }
  289. if (argc == 0)
  290. return print_help();
  291. wasm_file = argv[0];
  292. app_argc = argc;
  293. app_argv = argv;
  294. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  295. #if USE_GLOBAL_HEAP_BUF != 0
  296. init_args.mem_alloc_type = Alloc_With_Pool;
  297. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  298. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  299. #else
  300. init_args.mem_alloc_type = Alloc_With_Allocator;
  301. init_args.mem_alloc_option.allocator.malloc_func = malloc;
  302. init_args.mem_alloc_option.allocator.realloc_func = realloc;
  303. init_args.mem_alloc_option.allocator.free_func = free;
  304. #endif
  305. /* initialize runtime environment */
  306. if (!wasm_runtime_full_init(&init_args)) {
  307. printf("Init runtime environment failed.\n");
  308. return -1;
  309. }
  310. #if WASM_ENABLE_LOG != 0
  311. bh_log_set_verbose_level(log_verbose_level);
  312. #endif
  313. /* load WASM byte buffer from WASM bin file */
  314. if (!(wasm_file_buf =
  315. (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size)))
  316. goto fail1;
  317. #if WASM_ENABLE_MULTI_MODULE != 0
  318. wasm_runtime_set_module_reader(module_reader_callback, moudle_destroyer);
  319. #endif
  320. /* load WASM module */
  321. if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
  322. error_buf, sizeof(error_buf)))) {
  323. printf("%s\n", error_buf);
  324. goto fail2;
  325. }
  326. #if WASM_ENABLE_LIBC_WASI != 0
  327. wasm_runtime_set_wasi_args(wasm_module, dir_list, dir_list_size, NULL, 0,
  328. env_list, env_list_size, argv, argc);
  329. #endif
  330. /* instantiate the module */
  331. if (!(wasm_module_inst =
  332. wasm_runtime_instantiate(wasm_module, stack_size, heap_size,
  333. error_buf, sizeof(error_buf)))) {
  334. printf("%s\n", error_buf);
  335. goto fail3;
  336. }
  337. if (is_repl_mode)
  338. app_instance_repl(wasm_module_inst);
  339. else if (func_name)
  340. app_instance_func(wasm_module_inst, func_name);
  341. else
  342. app_instance_main(wasm_module_inst);
  343. /* destroy the module instance */
  344. wasm_runtime_deinstantiate(wasm_module_inst);
  345. fail3:
  346. /* unload the module */
  347. wasm_runtime_unload(wasm_module);
  348. fail2:
  349. /* free the file buffer */
  350. wasm_runtime_free(wasm_file_buf);
  351. fail1:
  352. /* destroy runtime environment */
  353. wasm_runtime_destroy();
  354. return 0;
  355. }