main.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _GNU_SOURCE
  6. #define _GNU_SOURCE
  7. #endif
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "bh_platform.h"
  12. #include "bh_read_file.h"
  13. #include "wasm_export.h"
  14. #if WASM_ENABLE_LIBC_WASI != 0
  15. #include "../common/libc_wasi.c"
  16. #endif
  17. #if BH_HAS_DLFCN
  18. #include <dlfcn.h>
  19. #endif
  20. static int app_argc;
  21. static char **app_argv;
  22. /* clang-format off */
  23. static int
  24. print_help()
  25. {
  26. printf("Usage: iwasm [-options] wasm_file [args...]\n");
  27. printf("options:\n");
  28. printf(" -f|--function name Specify a function name of the module to run rather\n"
  29. " than main\n");
  30. #if WASM_ENABLE_LOG != 0
  31. printf(" -v=n Set log verbose level (0 to 5, default is 2) larger\n"
  32. " level with more log\n");
  33. #endif
  34. #if WASM_ENABLE_INTERP != 0
  35. printf(" --interp Run the wasm app with interpreter mode\n");
  36. #endif
  37. #if WASM_ENABLE_FAST_JIT != 0
  38. printf(" --fast-jit Run the wasm app with fast jit mode\n");
  39. #endif
  40. #if WASM_ENABLE_JIT != 0
  41. printf(" --llvm-jit Run the wasm app with llvm jit mode\n");
  42. #endif
  43. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  44. printf(" --multi-tier-jit Run the wasm app with multi-tier jit mode\n");
  45. #endif
  46. printf(" --stack-size=n Set maximum stack size in bytes, default is 64 KB\n");
  47. printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB\n");
  48. #if WASM_ENABLE_FAST_JIT != 0
  49. printf(" --jit-codecache-size=n Set fast jit maximum code cache size in bytes,\n");
  50. printf(" default is %u KB\n", FAST_JIT_DEFAULT_CODE_CACHE_SIZE / 1024);
  51. #endif
  52. #if WASM_ENABLE_GC != 0
  53. printf(" --gc-heap-size=n Set maximum gc heap size in bytes,\n");
  54. printf(" default is %u KB\n", GC_HEAP_SIZE_DEFAULT / 1024);
  55. #endif
  56. #if WASM_ENABLE_JIT != 0
  57. printf(" --llvm-jit-size-level=n Set LLVM JIT size level, default is 3\n");
  58. printf(" --llvm-jit-opt-level=n Set LLVM JIT optimization level, default is 3\n");
  59. #if defined(os_writegsbase)
  60. printf(" --enable-segue[=<flags>] Enable using segment register GS as the base address of\n");
  61. printf(" linear memory, which may improve performance, flags can be:\n");
  62. printf(" i32.load, i64.load, f32.load, f64.load, v128.load,\n");
  63. printf(" i32.store, i64.store, f32.store, f64.store, v128.store\n");
  64. printf(" Use comma to separate, e.g. --enable-segue=i32.load,i64.store\n");
  65. printf(" and --enable-segue means all flags are added.\n");
  66. #endif
  67. #endif /* WASM_ENABLE_JIT != 0*/
  68. #if WASM_ENABLE_LINUX_PERF != 0
  69. printf(" --enable-linux-perf Enable linux perf support. It works in aot and llvm-jit.\n");
  70. #endif
  71. printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
  72. " that runs commands in the form of \"FUNC ARG...\"\n");
  73. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  74. printf(" --disable-bounds-checks Disable bounds checks for memory accesses\n");
  75. #endif
  76. #if WASM_ENABLE_LIBC_WASI != 0
  77. libc_wasi_print_help();
  78. #endif
  79. #if BH_HAS_DLFCN
  80. printf(" --native-lib=<lib> Register native libraries to the WASM module, which\n");
  81. printf(" are shared object (.so) files, for example:\n");
  82. printf(" --native-lib=test1.so --native-lib=test2.so\n");
  83. #endif
  84. #if WASM_ENABLE_MULTI_MODULE != 0
  85. printf(" --module-path=<path> Indicate a module search path. default is current\n"
  86. " directory('./')\n");
  87. #endif
  88. #if WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
  89. printf(" --max-threads=n Set maximum thread number per cluster, default is 4\n");
  90. #endif
  91. #if WASM_ENABLE_THREAD_MGR != 0
  92. printf(" --timeout=ms Set the maximum execution time in ms.\n");
  93. printf(" If it expires, the runtime aborts the execution\n");
  94. printf(" with a trap.\n");
  95. #endif
  96. #if WASM_ENABLE_DEBUG_INTERP != 0
  97. printf(" -g=ip:port Set the debug sever address, default is debug disabled\n");
  98. printf(" if port is 0, then a random port will be used\n");
  99. #endif
  100. #if WASM_ENABLE_STATIC_PGO != 0
  101. printf(" --gen-prof-file=<path> Generate LLVM PGO (Profile-Guided Optimization) profile file\n");
  102. #endif
  103. printf(" --version Show version information\n");
  104. return 1;
  105. }
  106. /* clang-format on */
  107. static const void *
  108. app_instance_main(wasm_module_inst_t module_inst)
  109. {
  110. const char *exception;
  111. wasm_application_execute_main(module_inst, app_argc, app_argv);
  112. exception = wasm_runtime_get_exception(module_inst);
  113. return exception;
  114. }
  115. static const void *
  116. app_instance_func(wasm_module_inst_t module_inst, const char *func_name)
  117. {
  118. wasm_application_execute_func(module_inst, func_name, app_argc - 1,
  119. app_argv + 1);
  120. /* The result of wasm function or exception info was output inside
  121. wasm_application_execute_func(), here we don't output them again. */
  122. return wasm_runtime_get_exception(module_inst);
  123. }
  124. /**
  125. * Split a string into an array of strings
  126. * Returns NULL on failure
  127. * Memory must be freed by caller
  128. * Based on: http://stackoverflow.com/a/11198630/471795
  129. */
  130. static char **
  131. split_string(char *str, int *count, const char *delimer)
  132. {
  133. char **res = NULL, **res1;
  134. char *p;
  135. int idx = 0;
  136. /* split string and append tokens to 'res' */
  137. do {
  138. p = strtok(str, delimer);
  139. str = NULL;
  140. res1 = res;
  141. res = (char **)realloc(res1, sizeof(char *) * (uint32)(idx + 1));
  142. if (res == NULL) {
  143. free(res1);
  144. return NULL;
  145. }
  146. res[idx++] = p;
  147. } while (p);
  148. /**
  149. * Due to the function name,
  150. * res[0] might contain a '\' to indicate a space
  151. * func\name -> func name
  152. */
  153. p = strchr(res[0], '\\');
  154. while (p) {
  155. *p = ' ';
  156. p = strchr(p, '\\');
  157. }
  158. if (count) {
  159. *count = idx - 1;
  160. }
  161. return res;
  162. }
  163. static void *
  164. app_instance_repl(wasm_module_inst_t module_inst)
  165. {
  166. char *cmd = NULL;
  167. size_t len = 0;
  168. ssize_t n;
  169. while ((printf("webassembly> "), fflush(stdout),
  170. n = getline(&cmd, &len, stdin))
  171. != -1) {
  172. bh_assert(n > 0);
  173. if (cmd[n - 1] == '\n') {
  174. if (n == 1)
  175. continue;
  176. else
  177. cmd[n - 1] = '\0';
  178. }
  179. if (!strcmp(cmd, "__exit__")) {
  180. printf("exit repl mode\n");
  181. break;
  182. }
  183. app_argv = split_string(cmd, &app_argc, " ");
  184. if (app_argv == NULL) {
  185. LOG_ERROR("Wasm prepare param failed: split string failed.\n");
  186. break;
  187. }
  188. if (app_argc != 0) {
  189. const char *exception;
  190. wasm_application_execute_func(module_inst, app_argv[0],
  191. app_argc - 1, app_argv + 1);
  192. if ((exception = wasm_runtime_get_exception(module_inst)))
  193. printf("%s\n", exception);
  194. }
  195. free(app_argv);
  196. }
  197. free(cmd);
  198. return NULL;
  199. }
  200. #if WASM_ENABLE_JIT != 0
  201. static uint32
  202. resolve_segue_flags(char *str_flags)
  203. {
  204. uint32 segue_flags = 0;
  205. int32 flag_count, i;
  206. char **flag_list;
  207. flag_list = split_string(str_flags, &flag_count, ",");
  208. if (flag_list) {
  209. for (i = 0; i < flag_count; i++) {
  210. if (!strcmp(flag_list[i], "i32.load")) {
  211. segue_flags |= 1 << 0;
  212. }
  213. else if (!strcmp(flag_list[i], "i64.load")) {
  214. segue_flags |= 1 << 1;
  215. }
  216. else if (!strcmp(flag_list[i], "f32.load")) {
  217. segue_flags |= 1 << 2;
  218. }
  219. else if (!strcmp(flag_list[i], "f64.load")) {
  220. segue_flags |= 1 << 3;
  221. }
  222. else if (!strcmp(flag_list[i], "v128.load")) {
  223. segue_flags |= 1 << 4;
  224. }
  225. else if (!strcmp(flag_list[i], "i32.store")) {
  226. segue_flags |= 1 << 8;
  227. }
  228. else if (!strcmp(flag_list[i], "i64.store")) {
  229. segue_flags |= 1 << 9;
  230. }
  231. else if (!strcmp(flag_list[i], "f32.store")) {
  232. segue_flags |= 1 << 10;
  233. }
  234. else if (!strcmp(flag_list[i], "f64.store")) {
  235. segue_flags |= 1 << 11;
  236. }
  237. else if (!strcmp(flag_list[i], "v128.store")) {
  238. segue_flags |= 1 << 12;
  239. }
  240. else {
  241. /* invalid flag */
  242. segue_flags = (uint32)-1;
  243. break;
  244. }
  245. }
  246. free(flag_list);
  247. }
  248. return segue_flags;
  249. }
  250. #endif /* end of WASM_ENABLE_JIT != 0 */
  251. #if BH_HAS_DLFCN
  252. struct native_lib {
  253. void *handle;
  254. uint32 (*get_native_lib)(char **p_module_name,
  255. NativeSymbol **p_native_symbols);
  256. int (*init_native_lib)(void);
  257. void (*deinit_native_lib)(void);
  258. char *module_name;
  259. NativeSymbol *native_symbols;
  260. uint32 n_native_symbols;
  261. };
  262. struct native_lib *
  263. load_native_lib(const char *name)
  264. {
  265. struct native_lib *lib = wasm_runtime_malloc(sizeof(*lib));
  266. if (lib == NULL) {
  267. LOG_WARNING("warning: failed to load native library %s because of "
  268. "allocation failure",
  269. name);
  270. goto fail;
  271. }
  272. memset(lib, 0, sizeof(*lib));
  273. /* open the native library */
  274. if (!(lib->handle = dlopen(name, RTLD_NOW | RTLD_GLOBAL))
  275. && !(lib->handle = dlopen(name, RTLD_LAZY))) {
  276. LOG_WARNING("warning: failed to load native library %s", name);
  277. goto fail;
  278. }
  279. lib->init_native_lib = dlsym(lib->handle, "init_native_lib");
  280. lib->get_native_lib = dlsym(lib->handle, "get_native_lib");
  281. lib->deinit_native_lib = dlsym(lib->handle, "deinit_native_lib");
  282. if (!lib->get_native_lib) {
  283. LOG_WARNING("warning: failed to lookup `get_native_lib` function "
  284. "from native lib %s",
  285. name);
  286. goto fail;
  287. }
  288. if (lib->init_native_lib) {
  289. int ret = lib->init_native_lib();
  290. if (ret != 0) {
  291. LOG_WARNING("warning: `init_native_lib` function from native "
  292. "lib %s failed with %d",
  293. name, ret);
  294. goto fail;
  295. }
  296. }
  297. lib->n_native_symbols =
  298. lib->get_native_lib(&lib->module_name, &lib->native_symbols);
  299. /* register native symbols */
  300. if (!(lib->n_native_symbols > 0 && lib->module_name && lib->native_symbols
  301. && wasm_runtime_register_natives(
  302. lib->module_name, lib->native_symbols, lib->n_native_symbols))) {
  303. LOG_WARNING("warning: failed to register native lib %s", name);
  304. if (lib->deinit_native_lib) {
  305. lib->deinit_native_lib();
  306. }
  307. goto fail;
  308. }
  309. return lib;
  310. fail:
  311. if (lib != NULL) {
  312. if (lib->handle != NULL) {
  313. dlclose(lib->handle);
  314. }
  315. wasm_runtime_free(lib);
  316. }
  317. return NULL;
  318. }
  319. static uint32
  320. load_and_register_native_libs(const char **native_lib_list,
  321. uint32 native_lib_count,
  322. struct native_lib **native_lib_loaded_list)
  323. {
  324. uint32 i, native_lib_loaded_count = 0;
  325. for (i = 0; i < native_lib_count; i++) {
  326. struct native_lib *lib = load_native_lib(native_lib_list[i]);
  327. if (lib == NULL) {
  328. continue;
  329. }
  330. native_lib_loaded_list[native_lib_loaded_count++] = lib;
  331. }
  332. return native_lib_loaded_count;
  333. }
  334. static void
  335. unregister_and_unload_native_libs(uint32 native_lib_count,
  336. struct native_lib **native_lib_loaded_list)
  337. {
  338. uint32 i;
  339. for (i = 0; i < native_lib_count; i++) {
  340. struct native_lib *lib = native_lib_loaded_list[i];
  341. /* unregister native symbols */
  342. if (!wasm_runtime_unregister_natives(lib->module_name,
  343. lib->native_symbols)) {
  344. LOG_WARNING("warning: failed to unregister native lib %p",
  345. lib->handle);
  346. continue;
  347. }
  348. if (lib->deinit_native_lib) {
  349. lib->deinit_native_lib();
  350. }
  351. dlclose(lib->handle);
  352. wasm_runtime_free(lib);
  353. }
  354. }
  355. #endif /* BH_HAS_DLFCN */
  356. #if WASM_ENABLE_MULTI_MODULE != 0
  357. static char *
  358. handle_module_path(const char *module_path)
  359. {
  360. /* next character after = */
  361. return (strchr(module_path, '=')) + 1;
  362. }
  363. static char *module_search_path = ".";
  364. static bool
  365. module_reader_callback(package_type_t module_type, const char *module_name,
  366. uint8 **p_buffer, uint32 *p_size)
  367. {
  368. char *file_format = NULL;
  369. #if WASM_ENABLE_INTERP != 0
  370. if (module_type == Wasm_Module_Bytecode)
  371. file_format = ".wasm";
  372. #endif
  373. #if WASM_ENABLE_AOT != 0
  374. if (module_type == Wasm_Module_AoT)
  375. file_format = ".aot";
  376. #endif
  377. bh_assert(file_format);
  378. const char *format = "%s/%s%s";
  379. int sz = strlen(module_search_path) + strlen("/") + strlen(module_name)
  380. + strlen(file_format) + 1;
  381. char *wasm_file_name = wasm_runtime_malloc(sz);
  382. if (!wasm_file_name) {
  383. return false;
  384. }
  385. snprintf(wasm_file_name, sz, format, module_search_path, module_name,
  386. file_format);
  387. *p_buffer = (uint8_t *)bh_read_file_to_buffer(wasm_file_name, p_size);
  388. wasm_runtime_free(wasm_file_name);
  389. return *p_buffer != NULL;
  390. }
  391. static void
  392. moudle_destroyer(uint8 *buffer, uint32 size)
  393. {
  394. if (!buffer) {
  395. return;
  396. }
  397. wasm_runtime_free(buffer);
  398. buffer = NULL;
  399. }
  400. #endif /* WASM_ENABLE_MULTI_MODULE */
  401. #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
  402. static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
  403. #else
  404. static void *
  405. malloc_func(
  406. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  407. mem_alloc_usage_t usage,
  408. #endif
  409. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  410. void *user_data,
  411. #endif
  412. unsigned int size)
  413. {
  414. return malloc(size);
  415. }
  416. static void *
  417. realloc_func(
  418. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  419. mem_alloc_usage_t usage, bool full_size_mmaped,
  420. #endif
  421. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  422. void *user_data,
  423. #endif
  424. void *ptr, unsigned int size)
  425. {
  426. return realloc(ptr, size);
  427. }
  428. static void
  429. free_func(
  430. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  431. mem_alloc_usage_t usage,
  432. #endif
  433. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  434. void *user_data,
  435. #endif
  436. void *ptr)
  437. {
  438. free(ptr);
  439. }
  440. #endif /* end of WASM_ENABLE_GLOBAL_HEAP_POOL */
  441. #if WASM_ENABLE_STATIC_PGO != 0
  442. static void
  443. dump_pgo_prof_data(wasm_module_inst_t module_inst, const char *path)
  444. {
  445. char *buf;
  446. uint32 len;
  447. FILE *file;
  448. if (!(len = wasm_runtime_get_pgo_prof_data_size(module_inst))) {
  449. printf("failed to get LLVM PGO profile data size\n");
  450. return;
  451. }
  452. if (!(buf = wasm_runtime_malloc(len))) {
  453. printf("allocate memory failed\n");
  454. return;
  455. }
  456. if (len != wasm_runtime_dump_pgo_prof_data_to_buf(module_inst, buf, len)) {
  457. printf("failed to dump LLVM PGO profile data\n");
  458. wasm_runtime_free(buf);
  459. return;
  460. }
  461. if (!(file = fopen(path, "wb"))) {
  462. printf("failed to create file %s", path);
  463. wasm_runtime_free(buf);
  464. return;
  465. }
  466. fwrite(buf, len, 1, file);
  467. fclose(file);
  468. wasm_runtime_free(buf);
  469. printf("LLVM raw profile file %s was generated.\n", path);
  470. }
  471. #endif
  472. #if WASM_ENABLE_THREAD_MGR != 0
  473. struct timeout_arg {
  474. uint32 timeout_ms;
  475. wasm_module_inst_t inst;
  476. #if defined(BH_HAS_STD_ATOMIC)
  477. _Atomic
  478. #endif
  479. bool cancel;
  480. };
  481. void *
  482. timeout_thread(void *vp)
  483. {
  484. const struct timeout_arg *arg = vp;
  485. uint32 left = arg->timeout_ms;
  486. while (!arg->cancel) {
  487. uint32 ms;
  488. if (left >= 100) {
  489. ms = 100;
  490. }
  491. else {
  492. ms = left;
  493. }
  494. os_usleep((uint64)ms * 1000);
  495. left -= ms;
  496. if (left == 0) {
  497. wasm_runtime_terminate(arg->inst);
  498. break;
  499. }
  500. }
  501. return NULL;
  502. }
  503. #endif
  504. int
  505. main(int argc, char *argv[])
  506. {
  507. int32 ret = -1;
  508. char *wasm_file = NULL;
  509. const char *func_name = NULL;
  510. uint8 *wasm_file_buf = NULL;
  511. uint32 wasm_file_size;
  512. uint32 stack_size = 64 * 1024;
  513. #if WASM_ENABLE_LIBC_WASI != 0
  514. uint32 heap_size = 0;
  515. #else
  516. uint32 heap_size = 16 * 1024;
  517. #endif
  518. #if WASM_ENABLE_FAST_JIT != 0
  519. uint32 jit_code_cache_size = FAST_JIT_DEFAULT_CODE_CACHE_SIZE;
  520. #endif
  521. #if WASM_ENABLE_GC != 0
  522. uint32 gc_heap_size = GC_HEAP_SIZE_DEFAULT;
  523. #endif
  524. #if WASM_ENABLE_JIT != 0
  525. uint32 llvm_jit_size_level = 3;
  526. uint32 llvm_jit_opt_level = 3;
  527. uint32 segue_flags = 0;
  528. #endif
  529. #if WASM_ENABLE_LINUX_PERF != 0
  530. bool enable_linux_perf = false;
  531. #endif
  532. wasm_module_t wasm_module = NULL;
  533. wasm_module_inst_t wasm_module_inst = NULL;
  534. RunningMode running_mode = 0;
  535. RuntimeInitArgs init_args;
  536. char error_buf[128] = { 0 };
  537. #if WASM_ENABLE_LOG != 0
  538. int log_verbose_level = 2;
  539. #endif
  540. bool is_repl_mode = false;
  541. bool is_xip_file = false;
  542. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  543. bool disable_bounds_checks = false;
  544. #endif
  545. #if WASM_ENABLE_LIBC_WASI != 0
  546. libc_wasi_parse_context_t wasi_parse_ctx;
  547. #endif
  548. #if BH_HAS_DLFCN
  549. const char *native_lib_list[8] = { NULL };
  550. uint32 native_lib_count = 0;
  551. struct native_lib *native_lib_loaded_list[8];
  552. uint32 native_lib_loaded_count = 0;
  553. #endif
  554. #if WASM_ENABLE_DEBUG_INTERP != 0
  555. char *ip_addr = NULL;
  556. int instance_port = 0;
  557. #endif
  558. #if WASM_ENABLE_STATIC_PGO != 0
  559. const char *gen_prof_file = NULL;
  560. #endif
  561. #if WASM_ENABLE_THREAD_MGR != 0
  562. int timeout_ms = -1;
  563. #endif
  564. #if WASM_ENABLE_LIBC_WASI != 0
  565. memset(&wasi_parse_ctx, 0, sizeof(wasi_parse_ctx));
  566. #endif
  567. /* Process options. */
  568. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  569. if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--function")) {
  570. argc--, argv++;
  571. if (argc < 2) {
  572. return print_help();
  573. }
  574. func_name = argv[0];
  575. }
  576. #if WASM_ENABLE_INTERP != 0
  577. else if (!strcmp(argv[0], "--interp")) {
  578. running_mode = Mode_Interp;
  579. }
  580. #endif
  581. #if WASM_ENABLE_FAST_JIT != 0
  582. else if (!strcmp(argv[0], "--fast-jit")) {
  583. running_mode = Mode_Fast_JIT;
  584. }
  585. #endif
  586. #if WASM_ENABLE_JIT != 0
  587. else if (!strcmp(argv[0], "--llvm-jit")) {
  588. running_mode = Mode_LLVM_JIT;
  589. }
  590. #endif
  591. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 \
  592. && WASM_ENABLE_LAZY_JIT != 0
  593. else if (!strcmp(argv[0], "--multi-tier-jit")) {
  594. running_mode = Mode_Multi_Tier_JIT;
  595. }
  596. #endif
  597. #if WASM_ENABLE_LOG != 0
  598. else if (!strncmp(argv[0], "-v=", 3)) {
  599. log_verbose_level = atoi(argv[0] + 3);
  600. if (log_verbose_level < 0 || log_verbose_level > 5)
  601. return print_help();
  602. }
  603. #endif
  604. else if (!strcmp(argv[0], "--repl")) {
  605. is_repl_mode = true;
  606. }
  607. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  608. else if (!strcmp(argv[0], "--disable-bounds-checks")) {
  609. disable_bounds_checks = true;
  610. }
  611. #endif
  612. else if (!strncmp(argv[0], "--stack-size=", 13)) {
  613. if (argv[0][13] == '\0')
  614. return print_help();
  615. stack_size = atoi(argv[0] + 13);
  616. }
  617. else if (!strncmp(argv[0], "--heap-size=", 12)) {
  618. if (argv[0][12] == '\0')
  619. return print_help();
  620. heap_size = atoi(argv[0] + 12);
  621. }
  622. #if WASM_ENABLE_FAST_JIT != 0
  623. else if (!strncmp(argv[0], "--jit-codecache-size=", 21)) {
  624. if (argv[0][21] == '\0')
  625. return print_help();
  626. jit_code_cache_size = atoi(argv[0] + 21);
  627. }
  628. #endif
  629. #if WASM_ENABLE_GC != 0
  630. else if (!strncmp(argv[0], "--gc-heap-size=", 15)) {
  631. if (argv[0][15] == '\0')
  632. return print_help();
  633. gc_heap_size = atoi(argv[0] + 15);
  634. }
  635. #endif
  636. #if WASM_ENABLE_JIT != 0
  637. else if (!strncmp(argv[0], "--llvm-jit-size-level=", 22)) {
  638. if (argv[0][22] == '\0')
  639. return print_help();
  640. llvm_jit_size_level = atoi(argv[0] + 22);
  641. if (llvm_jit_size_level < 1) {
  642. printf("LLVM JIT size level shouldn't be smaller than 1, "
  643. "setting it to 1\n");
  644. llvm_jit_size_level = 1;
  645. }
  646. else if (llvm_jit_size_level > 3) {
  647. printf("LLVM JIT size level shouldn't be greater than 3, "
  648. "setting it to 3\n");
  649. llvm_jit_size_level = 3;
  650. }
  651. }
  652. else if (!strncmp(argv[0], "--llvm-jit-opt-level=", 21)) {
  653. if (argv[0][21] == '\0')
  654. return print_help();
  655. llvm_jit_opt_level = atoi(argv[0] + 21);
  656. if (llvm_jit_opt_level < 1) {
  657. printf("LLVM JIT opt level shouldn't be smaller than 1, "
  658. "setting it to 1\n");
  659. llvm_jit_opt_level = 1;
  660. }
  661. else if (llvm_jit_opt_level > 3) {
  662. printf("LLVM JIT opt level shouldn't be greater than 3, "
  663. "setting it to 3\n");
  664. llvm_jit_opt_level = 3;
  665. }
  666. }
  667. else if (!strcmp(argv[0], "--enable-segue")) {
  668. /* all flags are enabled */
  669. segue_flags = 0x1F1F;
  670. }
  671. else if (!strncmp(argv[0], "--enable-segue=", 15)) {
  672. segue_flags = resolve_segue_flags(argv[0] + 15);
  673. if (segue_flags == (uint32)-1)
  674. return print_help();
  675. }
  676. #endif /* end of WASM_ENABLE_JIT != 0 */
  677. #if BH_HAS_DLFCN
  678. else if (!strncmp(argv[0], "--native-lib=", 13)) {
  679. if (argv[0][13] == '\0')
  680. return print_help();
  681. if (native_lib_count >= sizeof(native_lib_list) / sizeof(char *)) {
  682. printf("Only allow max native lib number %d\n",
  683. (int)(sizeof(native_lib_list) / sizeof(char *)));
  684. return 1;
  685. }
  686. native_lib_list[native_lib_count++] = argv[0] + 13;
  687. }
  688. #endif
  689. #if WASM_ENABLE_LINUX_PERF != 0
  690. else if (!strncmp(argv[0], "--enable-linux-perf", 19)) {
  691. enable_linux_perf = true;
  692. }
  693. #endif
  694. #if WASM_ENABLE_MULTI_MODULE != 0
  695. else if (!strncmp(argv[0],
  696. "--module-path=", strlen("--module-path="))) {
  697. module_search_path = handle_module_path(argv[0]);
  698. if (!strlen(module_search_path)) {
  699. return print_help();
  700. }
  701. }
  702. #endif
  703. #if WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
  704. else if (!strncmp(argv[0], "--max-threads=", 14)) {
  705. if (argv[0][14] == '\0')
  706. return print_help();
  707. wasm_runtime_set_max_thread_num(atoi(argv[0] + 14));
  708. }
  709. #endif
  710. #if WASM_ENABLE_THREAD_MGR != 0
  711. else if (!strncmp(argv[0], "--timeout=", 10)) {
  712. if (argv[0][10] == '\0')
  713. return print_help();
  714. timeout_ms = atoi(argv[0] + 10);
  715. }
  716. #endif
  717. #if WASM_ENABLE_DEBUG_INTERP != 0
  718. else if (!strncmp(argv[0], "-g=", 3)) {
  719. char *port_str = strchr(argv[0] + 3, ':');
  720. char *port_end;
  721. if (port_str == NULL)
  722. return print_help();
  723. *port_str = '\0';
  724. instance_port = strtoul(port_str + 1, &port_end, 10);
  725. if (port_str[1] == '\0' || *port_end != '\0')
  726. return print_help();
  727. ip_addr = argv[0] + 3;
  728. }
  729. #endif
  730. #if WASM_ENABLE_STATIC_PGO != 0
  731. else if (!strncmp(argv[0], "--gen-prof-file=", 16)) {
  732. if (argv[0][16] == '\0')
  733. return print_help();
  734. gen_prof_file = argv[0] + 16;
  735. }
  736. #endif
  737. else if (!strcmp(argv[0], "--version")) {
  738. uint32 major, minor, patch;
  739. wasm_runtime_get_version(&major, &minor, &patch);
  740. printf("iwasm %" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n", major, minor,
  741. patch);
  742. return 0;
  743. }
  744. else {
  745. #if WASM_ENABLE_LIBC_WASI != 0
  746. libc_wasi_parse_result_t result =
  747. libc_wasi_parse(argv[0], &wasi_parse_ctx);
  748. switch (result) {
  749. case LIBC_WASI_PARSE_RESULT_OK:
  750. continue;
  751. case LIBC_WASI_PARSE_RESULT_NEED_HELP:
  752. return print_help();
  753. case LIBC_WASI_PARSE_RESULT_BAD_PARAM:
  754. return 1;
  755. }
  756. #else
  757. return print_help();
  758. #endif
  759. }
  760. }
  761. if (argc == 0)
  762. return print_help();
  763. wasm_file = argv[0];
  764. app_argc = argc;
  765. app_argv = argv;
  766. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  767. init_args.running_mode = running_mode;
  768. #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
  769. init_args.mem_alloc_type = Alloc_With_Pool;
  770. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  771. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  772. #else
  773. init_args.mem_alloc_type = Alloc_With_Allocator;
  774. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  775. /* Set user data for the allocator is needed */
  776. /* init_args.mem_alloc_option.allocator.user_data = user_data; */
  777. #endif
  778. init_args.mem_alloc_option.allocator.malloc_func = malloc_func;
  779. init_args.mem_alloc_option.allocator.realloc_func = realloc_func;
  780. init_args.mem_alloc_option.allocator.free_func = free_func;
  781. #endif
  782. #if WASM_ENABLE_FAST_JIT != 0
  783. init_args.fast_jit_code_cache_size = jit_code_cache_size;
  784. #endif
  785. #if WASM_ENABLE_GC != 0
  786. init_args.gc_heap_size = gc_heap_size;
  787. #endif
  788. #if WASM_ENABLE_JIT != 0
  789. init_args.llvm_jit_size_level = llvm_jit_size_level;
  790. init_args.llvm_jit_opt_level = llvm_jit_opt_level;
  791. init_args.segue_flags = segue_flags;
  792. #endif
  793. #if WASM_ENABLE_LINUX_PERF != 0
  794. init_args.enable_linux_perf = enable_linux_perf;
  795. #endif
  796. #if WASM_ENABLE_DEBUG_INTERP != 0
  797. init_args.instance_port = instance_port;
  798. if (ip_addr)
  799. /* ensure that init_args.ip_addr is null terminated */
  800. strncpy(init_args.ip_addr, ip_addr, sizeof(init_args.ip_addr) - 1);
  801. #endif
  802. /* initialize runtime environment */
  803. if (!wasm_runtime_full_init(&init_args)) {
  804. printf("Init runtime environment failed.\n");
  805. return -1;
  806. }
  807. #if WASM_ENABLE_LOG != 0
  808. bh_log_set_verbose_level(log_verbose_level);
  809. #endif
  810. #if BH_HAS_DLFCN
  811. native_lib_loaded_count = load_and_register_native_libs(
  812. native_lib_list, native_lib_count, native_lib_loaded_list);
  813. #endif
  814. /* load WASM byte buffer from WASM bin file */
  815. if (!(wasm_file_buf =
  816. (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size)))
  817. goto fail1;
  818. #if WASM_ENABLE_AOT != 0
  819. if (wasm_runtime_is_xip_file(wasm_file_buf, wasm_file_size)) {
  820. uint8 *wasm_file_mapped;
  821. int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
  822. int map_flags = MMAP_MAP_32BIT;
  823. if (!(wasm_file_mapped = os_mmap(NULL, (uint32)wasm_file_size, map_prot,
  824. map_flags, os_get_invalid_handle()))) {
  825. printf("mmap memory failed\n");
  826. wasm_runtime_free(wasm_file_buf);
  827. goto fail1;
  828. }
  829. bh_memcpy_s(wasm_file_mapped, wasm_file_size, wasm_file_buf,
  830. wasm_file_size);
  831. wasm_runtime_free(wasm_file_buf);
  832. wasm_file_buf = wasm_file_mapped;
  833. is_xip_file = true;
  834. }
  835. #endif
  836. #if WASM_ENABLE_MULTI_MODULE != 0
  837. wasm_runtime_set_module_reader(module_reader_callback, moudle_destroyer);
  838. #endif
  839. /* load WASM module */
  840. if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
  841. error_buf, sizeof(error_buf)))) {
  842. printf("%s\n", error_buf);
  843. goto fail2;
  844. }
  845. #if WASM_ENABLE_LIBC_WASI != 0
  846. libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx);
  847. #endif
  848. /* instantiate the module */
  849. if (!(wasm_module_inst =
  850. wasm_runtime_instantiate(wasm_module, stack_size, heap_size,
  851. error_buf, sizeof(error_buf)))) {
  852. printf("%s\n", error_buf);
  853. goto fail3;
  854. }
  855. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  856. if (disable_bounds_checks) {
  857. wasm_runtime_set_bounds_checks(wasm_module_inst, false);
  858. }
  859. #endif
  860. #if WASM_ENABLE_DEBUG_INTERP != 0
  861. if (ip_addr != NULL) {
  862. wasm_exec_env_t exec_env =
  863. wasm_runtime_get_exec_env_singleton(wasm_module_inst);
  864. uint32_t debug_port;
  865. if (exec_env == NULL) {
  866. printf("%s\n", wasm_runtime_get_exception(wasm_module_inst));
  867. goto fail4;
  868. }
  869. debug_port = wasm_runtime_start_debug_instance(exec_env);
  870. if (debug_port == 0) {
  871. printf("Failed to start debug instance\n");
  872. goto fail4;
  873. }
  874. }
  875. #endif
  876. #if WASM_ENABLE_THREAD_MGR != 0
  877. struct timeout_arg timeout_arg;
  878. korp_tid timeout_tid;
  879. if (timeout_ms >= 0) {
  880. timeout_arg.timeout_ms = timeout_ms;
  881. timeout_arg.inst = wasm_module_inst;
  882. timeout_arg.cancel = false;
  883. ret = os_thread_create(&timeout_tid, timeout_thread, &timeout_arg,
  884. APP_THREAD_STACK_SIZE_DEFAULT);
  885. if (ret != 0) {
  886. printf("Failed to start timeout\n");
  887. goto fail5;
  888. }
  889. }
  890. #endif
  891. ret = 0;
  892. const char *exception = NULL;
  893. if (is_repl_mode) {
  894. app_instance_repl(wasm_module_inst);
  895. }
  896. else if (func_name) {
  897. exception = app_instance_func(wasm_module_inst, func_name);
  898. if (exception) {
  899. /* got an exception */
  900. ret = 1;
  901. }
  902. }
  903. else {
  904. exception = app_instance_main(wasm_module_inst);
  905. if (exception) {
  906. /* got an exception */
  907. ret = 1;
  908. }
  909. }
  910. #if WASM_ENABLE_LIBC_WASI != 0
  911. if (ret == 0) {
  912. /* propagate wasi exit code. */
  913. ret = wasm_runtime_get_wasi_exit_code(wasm_module_inst);
  914. }
  915. #endif
  916. if (exception)
  917. printf("%s\n", exception);
  918. #if WASM_ENABLE_STATIC_PGO != 0 && WASM_ENABLE_AOT != 0
  919. if (get_package_type(wasm_file_buf, wasm_file_size) == Wasm_Module_AoT
  920. && gen_prof_file)
  921. dump_pgo_prof_data(wasm_module_inst, gen_prof_file);
  922. #endif
  923. #if WASM_ENABLE_THREAD_MGR != 0
  924. if (timeout_ms >= 0) {
  925. timeout_arg.cancel = true;
  926. os_thread_join(timeout_tid, NULL);
  927. }
  928. #endif
  929. #if WASM_ENABLE_THREAD_MGR != 0
  930. fail5:
  931. #endif
  932. #if WASM_ENABLE_DEBUG_INTERP != 0
  933. fail4:
  934. #endif
  935. /* destroy the module instance */
  936. wasm_runtime_deinstantiate(wasm_module_inst);
  937. fail3:
  938. /* unload the module */
  939. wasm_runtime_unload(wasm_module);
  940. fail2:
  941. /* free the file buffer */
  942. if (!is_xip_file)
  943. wasm_runtime_free(wasm_file_buf);
  944. else
  945. os_munmap(wasm_file_buf, wasm_file_size);
  946. fail1:
  947. #if BH_HAS_DLFCN
  948. /* unload the native libraries */
  949. unregister_and_unload_native_libs(native_lib_loaded_count,
  950. native_lib_loaded_list);
  951. #endif
  952. /* destroy runtime environment */
  953. wasm_runtime_destroy();
  954. return ret;
  955. }