main.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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(void)
  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. %s", name,
  277. dlerror());
  278. goto fail;
  279. }
  280. lib->init_native_lib = dlsym(lib->handle, "init_native_lib");
  281. lib->get_native_lib = dlsym(lib->handle, "get_native_lib");
  282. lib->deinit_native_lib = dlsym(lib->handle, "deinit_native_lib");
  283. if (!lib->get_native_lib) {
  284. LOG_WARNING("warning: failed to lookup `get_native_lib` function "
  285. "from native lib %s",
  286. name);
  287. goto fail;
  288. }
  289. if (lib->init_native_lib) {
  290. int ret = lib->init_native_lib();
  291. if (ret != 0) {
  292. LOG_WARNING("warning: `init_native_lib` function from native "
  293. "lib %s failed with %d",
  294. name, ret);
  295. goto fail;
  296. }
  297. }
  298. lib->n_native_symbols =
  299. lib->get_native_lib(&lib->module_name, &lib->native_symbols);
  300. /* register native symbols */
  301. if (!(lib->n_native_symbols > 0 && lib->module_name && lib->native_symbols
  302. && wasm_runtime_register_natives(
  303. lib->module_name, lib->native_symbols, lib->n_native_symbols))) {
  304. LOG_WARNING("warning: failed to register native lib %s", name);
  305. if (lib->deinit_native_lib) {
  306. lib->deinit_native_lib();
  307. }
  308. goto fail;
  309. }
  310. return lib;
  311. fail:
  312. if (lib != NULL) {
  313. if (lib->handle != NULL) {
  314. dlclose(lib->handle);
  315. }
  316. wasm_runtime_free(lib);
  317. }
  318. return NULL;
  319. }
  320. static uint32
  321. load_and_register_native_libs(const char **native_lib_list,
  322. uint32 native_lib_count,
  323. struct native_lib **native_lib_loaded_list)
  324. {
  325. uint32 i, native_lib_loaded_count = 0;
  326. for (i = 0; i < native_lib_count; i++) {
  327. struct native_lib *lib = load_native_lib(native_lib_list[i]);
  328. if (lib == NULL) {
  329. continue;
  330. }
  331. native_lib_loaded_list[native_lib_loaded_count++] = lib;
  332. }
  333. return native_lib_loaded_count;
  334. }
  335. static void
  336. unregister_and_unload_native_libs(uint32 native_lib_count,
  337. struct native_lib **native_lib_loaded_list)
  338. {
  339. uint32 i;
  340. for (i = 0; i < native_lib_count; i++) {
  341. struct native_lib *lib = native_lib_loaded_list[i];
  342. /* unregister native symbols */
  343. if (!wasm_runtime_unregister_natives(lib->module_name,
  344. lib->native_symbols)) {
  345. LOG_WARNING("warning: failed to unregister native lib %p",
  346. lib->handle);
  347. continue;
  348. }
  349. if (lib->deinit_native_lib) {
  350. lib->deinit_native_lib();
  351. }
  352. dlclose(lib->handle);
  353. wasm_runtime_free(lib);
  354. }
  355. }
  356. #endif /* BH_HAS_DLFCN */
  357. #if WASM_ENABLE_MULTI_MODULE != 0
  358. static char *
  359. handle_module_path(const char *module_path)
  360. {
  361. /* next character after = */
  362. return (strchr(module_path, '=')) + 1;
  363. }
  364. static char *module_search_path = ".";
  365. static bool
  366. module_reader_callback(package_type_t module_type, const char *module_name,
  367. uint8 **p_buffer, uint32 *p_size)
  368. {
  369. char *file_format = NULL;
  370. #if WASM_ENABLE_INTERP != 0
  371. if (module_type == Wasm_Module_Bytecode)
  372. file_format = ".wasm";
  373. #endif
  374. #if WASM_ENABLE_AOT != 0
  375. if (module_type == Wasm_Module_AoT)
  376. file_format = ".aot";
  377. #endif
  378. bh_assert(file_format);
  379. const char *format = "%s/%s%s";
  380. int sz = strlen(module_search_path) + strlen("/") + strlen(module_name)
  381. + strlen(file_format) + 1;
  382. char *wasm_file_name = wasm_runtime_malloc(sz);
  383. if (!wasm_file_name) {
  384. return false;
  385. }
  386. snprintf(wasm_file_name, sz, format, module_search_path, module_name,
  387. file_format);
  388. *p_buffer = (uint8_t *)bh_read_file_to_buffer(wasm_file_name, p_size);
  389. wasm_runtime_free(wasm_file_name);
  390. return *p_buffer != NULL;
  391. }
  392. static void
  393. module_destroyer_callback(uint8 *buffer, uint32 size)
  394. {
  395. if (!buffer) {
  396. return;
  397. }
  398. wasm_runtime_free(buffer);
  399. buffer = NULL;
  400. }
  401. #endif /* WASM_ENABLE_MULTI_MODULE */
  402. #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
  403. static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
  404. #else
  405. static void *
  406. malloc_func(
  407. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  408. mem_alloc_usage_t usage,
  409. #endif
  410. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  411. void *user_data,
  412. #endif
  413. unsigned int size)
  414. {
  415. return malloc(size);
  416. }
  417. static void *
  418. realloc_func(
  419. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  420. mem_alloc_usage_t usage, bool full_size_mmaped,
  421. #endif
  422. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  423. void *user_data,
  424. #endif
  425. void *ptr, unsigned int size)
  426. {
  427. return realloc(ptr, size);
  428. }
  429. static void
  430. free_func(
  431. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  432. mem_alloc_usage_t usage,
  433. #endif
  434. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  435. void *user_data,
  436. #endif
  437. void *ptr)
  438. {
  439. free(ptr);
  440. }
  441. #endif /* end of WASM_ENABLE_GLOBAL_HEAP_POOL */
  442. #if WASM_ENABLE_STATIC_PGO != 0
  443. static void
  444. dump_pgo_prof_data(wasm_module_inst_t module_inst, const char *path)
  445. {
  446. char *buf;
  447. uint32 len;
  448. FILE *file;
  449. if (!(len = wasm_runtime_get_pgo_prof_data_size(module_inst))) {
  450. printf("failed to get LLVM PGO profile data size\n");
  451. return;
  452. }
  453. if (!(buf = wasm_runtime_malloc(len))) {
  454. printf("allocate memory failed\n");
  455. return;
  456. }
  457. if (len != wasm_runtime_dump_pgo_prof_data_to_buf(module_inst, buf, len)) {
  458. printf("failed to dump LLVM PGO profile data\n");
  459. wasm_runtime_free(buf);
  460. return;
  461. }
  462. if (!(file = fopen(path, "wb"))) {
  463. printf("failed to create file %s", path);
  464. wasm_runtime_free(buf);
  465. return;
  466. }
  467. fwrite(buf, len, 1, file);
  468. fclose(file);
  469. wasm_runtime_free(buf);
  470. printf("LLVM raw profile file %s was generated.\n", path);
  471. }
  472. #endif
  473. #if WASM_ENABLE_THREAD_MGR != 0
  474. struct timeout_arg {
  475. uint32 timeout_ms;
  476. wasm_module_inst_t inst;
  477. #if defined(BH_HAS_STD_ATOMIC)
  478. _Atomic
  479. #endif
  480. bool cancel;
  481. };
  482. void *
  483. timeout_thread(void *vp)
  484. {
  485. const struct timeout_arg *arg = vp;
  486. const uint64 end_time =
  487. os_time_get_boot_us() + (uint64)arg->timeout_ms * 1000;
  488. while (!arg->cancel) {
  489. const uint64 now = os_time_get_boot_us();
  490. if ((int64)(now - end_time) > 0) {
  491. wasm_runtime_terminate(arg->inst);
  492. break;
  493. }
  494. const uint64 left_us = end_time - now;
  495. uint32 us;
  496. if (left_us >= 100 * 1000) {
  497. us = 100 * 1000;
  498. }
  499. else {
  500. us = left_us;
  501. }
  502. os_usleep(us);
  503. }
  504. return NULL;
  505. }
  506. #endif
  507. int
  508. main(int argc, char *argv[])
  509. {
  510. int32 ret = -1;
  511. char *wasm_file = NULL;
  512. const char *func_name = NULL;
  513. uint8 *wasm_file_buf = NULL;
  514. uint32 wasm_file_size;
  515. uint32 stack_size = 64 * 1024;
  516. #if WASM_ENABLE_LIBC_WASI != 0
  517. uint32 heap_size = 0;
  518. #else
  519. uint32 heap_size = 16 * 1024;
  520. #endif
  521. #if WASM_ENABLE_FAST_JIT != 0
  522. uint32 jit_code_cache_size = FAST_JIT_DEFAULT_CODE_CACHE_SIZE;
  523. #endif
  524. #if WASM_ENABLE_GC != 0
  525. uint32 gc_heap_size = GC_HEAP_SIZE_DEFAULT;
  526. #endif
  527. #if WASM_ENABLE_JIT != 0
  528. uint32 llvm_jit_size_level = 3;
  529. uint32 llvm_jit_opt_level = 3;
  530. uint32 segue_flags = 0;
  531. #endif
  532. #if WASM_ENABLE_LINUX_PERF != 0
  533. bool enable_linux_perf = false;
  534. #endif
  535. wasm_module_t wasm_module = NULL;
  536. wasm_module_inst_t wasm_module_inst = NULL;
  537. RunningMode running_mode = 0;
  538. RuntimeInitArgs init_args;
  539. char error_buf[128] = { 0 };
  540. #if WASM_ENABLE_LOG != 0
  541. int log_verbose_level = 2;
  542. #endif
  543. bool is_repl_mode = false;
  544. bool is_xip_file = false;
  545. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  546. bool disable_bounds_checks = false;
  547. #endif
  548. #if WASM_ENABLE_LIBC_WASI != 0
  549. libc_wasi_parse_context_t wasi_parse_ctx;
  550. #endif
  551. #if BH_HAS_DLFCN
  552. const char *native_lib_list[8] = { NULL };
  553. uint32 native_lib_count = 0;
  554. struct native_lib *native_lib_loaded_list[8];
  555. uint32 native_lib_loaded_count = 0;
  556. #endif
  557. #if WASM_ENABLE_DEBUG_INTERP != 0
  558. char *ip_addr = NULL;
  559. int instance_port = 0;
  560. #endif
  561. #if WASM_ENABLE_STATIC_PGO != 0
  562. const char *gen_prof_file = NULL;
  563. #endif
  564. #if WASM_ENABLE_THREAD_MGR != 0
  565. int timeout_ms = -1;
  566. #endif
  567. #if WASM_ENABLE_LIBC_WASI != 0
  568. memset(&wasi_parse_ctx, 0, sizeof(wasi_parse_ctx));
  569. #endif
  570. /* Process options. */
  571. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  572. if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--function")) {
  573. argc--, argv++;
  574. if (argc < 2) {
  575. return print_help();
  576. }
  577. func_name = argv[0];
  578. }
  579. #if WASM_ENABLE_INTERP != 0
  580. else if (!strcmp(argv[0], "--interp")) {
  581. running_mode = Mode_Interp;
  582. }
  583. #endif
  584. #if WASM_ENABLE_FAST_JIT != 0
  585. else if (!strcmp(argv[0], "--fast-jit")) {
  586. running_mode = Mode_Fast_JIT;
  587. }
  588. #endif
  589. #if WASM_ENABLE_JIT != 0
  590. else if (!strcmp(argv[0], "--llvm-jit")) {
  591. running_mode = Mode_LLVM_JIT;
  592. }
  593. #endif
  594. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 \
  595. && WASM_ENABLE_LAZY_JIT != 0
  596. else if (!strcmp(argv[0], "--multi-tier-jit")) {
  597. running_mode = Mode_Multi_Tier_JIT;
  598. }
  599. #endif
  600. #if WASM_ENABLE_LOG != 0
  601. else if (!strncmp(argv[0], "-v=", 3)) {
  602. log_verbose_level = atoi(argv[0] + 3);
  603. if (log_verbose_level < 0 || log_verbose_level > 5)
  604. return print_help();
  605. }
  606. #endif
  607. else if (!strcmp(argv[0], "--repl")) {
  608. is_repl_mode = true;
  609. }
  610. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  611. else if (!strcmp(argv[0], "--disable-bounds-checks")) {
  612. disable_bounds_checks = true;
  613. }
  614. #endif
  615. else if (!strncmp(argv[0], "--stack-size=", 13)) {
  616. if (argv[0][13] == '\0')
  617. return print_help();
  618. stack_size = atoi(argv[0] + 13);
  619. }
  620. else if (!strncmp(argv[0], "--heap-size=", 12)) {
  621. if (argv[0][12] == '\0')
  622. return print_help();
  623. heap_size = atoi(argv[0] + 12);
  624. }
  625. #if WASM_ENABLE_FAST_JIT != 0
  626. else if (!strncmp(argv[0], "--jit-codecache-size=", 21)) {
  627. if (argv[0][21] == '\0')
  628. return print_help();
  629. jit_code_cache_size = atoi(argv[0] + 21);
  630. }
  631. #endif
  632. #if WASM_ENABLE_GC != 0
  633. else if (!strncmp(argv[0], "--gc-heap-size=", 15)) {
  634. if (argv[0][15] == '\0')
  635. return print_help();
  636. gc_heap_size = atoi(argv[0] + 15);
  637. }
  638. #endif
  639. #if WASM_ENABLE_JIT != 0
  640. else if (!strncmp(argv[0], "--llvm-jit-size-level=", 22)) {
  641. if (argv[0][22] == '\0')
  642. return print_help();
  643. llvm_jit_size_level = atoi(argv[0] + 22);
  644. if (llvm_jit_size_level < 1) {
  645. printf("LLVM JIT size level shouldn't be smaller than 1, "
  646. "setting it to 1\n");
  647. llvm_jit_size_level = 1;
  648. }
  649. else if (llvm_jit_size_level > 3) {
  650. printf("LLVM JIT size level shouldn't be greater than 3, "
  651. "setting it to 3\n");
  652. llvm_jit_size_level = 3;
  653. }
  654. }
  655. else if (!strncmp(argv[0], "--llvm-jit-opt-level=", 21)) {
  656. if (argv[0][21] == '\0')
  657. return print_help();
  658. llvm_jit_opt_level = atoi(argv[0] + 21);
  659. if (llvm_jit_opt_level < 1) {
  660. printf("LLVM JIT opt level shouldn't be smaller than 1, "
  661. "setting it to 1\n");
  662. llvm_jit_opt_level = 1;
  663. }
  664. else if (llvm_jit_opt_level > 3) {
  665. printf("LLVM JIT opt level shouldn't be greater than 3, "
  666. "setting it to 3\n");
  667. llvm_jit_opt_level = 3;
  668. }
  669. }
  670. else if (!strcmp(argv[0], "--enable-segue")) {
  671. /* all flags are enabled */
  672. segue_flags = 0x1F1F;
  673. }
  674. else if (!strncmp(argv[0], "--enable-segue=", 15)) {
  675. segue_flags = resolve_segue_flags(argv[0] + 15);
  676. if (segue_flags == (uint32)-1)
  677. return print_help();
  678. }
  679. #endif /* end of WASM_ENABLE_JIT != 0 */
  680. #if BH_HAS_DLFCN
  681. else if (!strncmp(argv[0], "--native-lib=", 13)) {
  682. if (argv[0][13] == '\0')
  683. return print_help();
  684. if (native_lib_count >= sizeof(native_lib_list) / sizeof(char *)) {
  685. printf("Only allow max native lib number %d\n",
  686. (int)(sizeof(native_lib_list) / sizeof(char *)));
  687. return 1;
  688. }
  689. native_lib_list[native_lib_count++] = argv[0] + 13;
  690. }
  691. #endif
  692. #if WASM_ENABLE_LINUX_PERF != 0
  693. else if (!strncmp(argv[0], "--enable-linux-perf", 19)) {
  694. enable_linux_perf = true;
  695. }
  696. #endif
  697. #if WASM_ENABLE_MULTI_MODULE != 0
  698. else if (!strncmp(argv[0],
  699. "--module-path=", strlen("--module-path="))) {
  700. module_search_path = handle_module_path(argv[0]);
  701. if (!strlen(module_search_path)) {
  702. return print_help();
  703. }
  704. }
  705. #endif
  706. #if WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
  707. else if (!strncmp(argv[0], "--max-threads=", 14)) {
  708. if (argv[0][14] == '\0')
  709. return print_help();
  710. wasm_runtime_set_max_thread_num(atoi(argv[0] + 14));
  711. }
  712. #endif
  713. #if WASM_ENABLE_THREAD_MGR != 0
  714. else if (!strncmp(argv[0], "--timeout=", 10)) {
  715. if (argv[0][10] == '\0')
  716. return print_help();
  717. timeout_ms = atoi(argv[0] + 10);
  718. }
  719. #endif
  720. #if WASM_ENABLE_DEBUG_INTERP != 0
  721. else if (!strncmp(argv[0], "-g=", 3)) {
  722. char *port_str = strchr(argv[0] + 3, ':');
  723. char *port_end;
  724. if (port_str == NULL)
  725. return print_help();
  726. *port_str = '\0';
  727. instance_port = strtoul(port_str + 1, &port_end, 10);
  728. if (port_str[1] == '\0' || *port_end != '\0')
  729. return print_help();
  730. ip_addr = argv[0] + 3;
  731. }
  732. #endif
  733. #if WASM_ENABLE_STATIC_PGO != 0
  734. else if (!strncmp(argv[0], "--gen-prof-file=", 16)) {
  735. if (argv[0][16] == '\0')
  736. return print_help();
  737. gen_prof_file = argv[0] + 16;
  738. }
  739. #endif
  740. else if (!strcmp(argv[0], "--version")) {
  741. uint32 major, minor, patch;
  742. wasm_runtime_get_version(&major, &minor, &patch);
  743. printf("iwasm %" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n", major, minor,
  744. patch);
  745. return 0;
  746. }
  747. else {
  748. #if WASM_ENABLE_LIBC_WASI != 0
  749. libc_wasi_parse_result_t result =
  750. libc_wasi_parse(argv[0], &wasi_parse_ctx);
  751. switch (result) {
  752. case LIBC_WASI_PARSE_RESULT_OK:
  753. continue;
  754. case LIBC_WASI_PARSE_RESULT_NEED_HELP:
  755. return print_help();
  756. case LIBC_WASI_PARSE_RESULT_BAD_PARAM:
  757. return 1;
  758. }
  759. #else
  760. return print_help();
  761. #endif
  762. }
  763. }
  764. if (argc == 0)
  765. return print_help();
  766. wasm_file = argv[0];
  767. app_argc = argc;
  768. app_argv = argv;
  769. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  770. init_args.running_mode = running_mode;
  771. #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
  772. init_args.mem_alloc_type = Alloc_With_Pool;
  773. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  774. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  775. #else
  776. init_args.mem_alloc_type = Alloc_With_Allocator;
  777. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  778. /* Set user data for the allocator is needed */
  779. /* init_args.mem_alloc_option.allocator.user_data = user_data; */
  780. #endif
  781. init_args.mem_alloc_option.allocator.malloc_func = malloc_func;
  782. init_args.mem_alloc_option.allocator.realloc_func = realloc_func;
  783. init_args.mem_alloc_option.allocator.free_func = free_func;
  784. #endif
  785. #if WASM_ENABLE_FAST_JIT != 0
  786. init_args.fast_jit_code_cache_size = jit_code_cache_size;
  787. #endif
  788. #if WASM_ENABLE_GC != 0
  789. init_args.gc_heap_size = gc_heap_size;
  790. #endif
  791. #if WASM_ENABLE_JIT != 0
  792. init_args.llvm_jit_size_level = llvm_jit_size_level;
  793. init_args.llvm_jit_opt_level = llvm_jit_opt_level;
  794. init_args.segue_flags = segue_flags;
  795. #endif
  796. #if WASM_ENABLE_LINUX_PERF != 0
  797. init_args.enable_linux_perf = enable_linux_perf;
  798. #endif
  799. #if WASM_ENABLE_DEBUG_INTERP != 0
  800. init_args.instance_port = instance_port;
  801. if (ip_addr)
  802. /* ensure that init_args.ip_addr is null terminated */
  803. strncpy(init_args.ip_addr, ip_addr, sizeof(init_args.ip_addr) - 1);
  804. #endif
  805. /* initialize runtime environment */
  806. if (!wasm_runtime_full_init(&init_args)) {
  807. printf("Init runtime environment failed.\n");
  808. return -1;
  809. }
  810. #if WASM_ENABLE_LOG != 0
  811. bh_log_set_verbose_level(log_verbose_level);
  812. #endif
  813. #if BH_HAS_DLFCN
  814. native_lib_loaded_count = load_and_register_native_libs(
  815. native_lib_list, native_lib_count, native_lib_loaded_list);
  816. #endif
  817. /* load WASM byte buffer from WASM bin file */
  818. if (!(wasm_file_buf =
  819. (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size)))
  820. goto fail1;
  821. #if WASM_ENABLE_AOT != 0
  822. if (wasm_runtime_is_xip_file(wasm_file_buf, wasm_file_size)) {
  823. uint8 *wasm_file_mapped;
  824. uint8 *daddr;
  825. int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
  826. int map_flags = MMAP_MAP_32BIT;
  827. if (!(wasm_file_mapped = os_mmap(NULL, (uint32)wasm_file_size, map_prot,
  828. map_flags, os_get_invalid_handle()))) {
  829. printf("mmap memory failed\n");
  830. wasm_runtime_free(wasm_file_buf);
  831. goto fail1;
  832. }
  833. #if (WASM_MEM_DUAL_BUS_MIRROR != 0)
  834. daddr = os_get_dbus_mirror(wasm_file_mapped);
  835. #else
  836. daddr = wasm_file_mapped;
  837. #endif
  838. bh_memcpy_s(daddr, wasm_file_size, wasm_file_buf, wasm_file_size);
  839. #if (WASM_MEM_DUAL_BUS_MIRROR != 0)
  840. os_dcache_flush();
  841. #endif
  842. wasm_runtime_free(wasm_file_buf);
  843. wasm_file_buf = wasm_file_mapped;
  844. is_xip_file = true;
  845. }
  846. #endif
  847. #if WASM_ENABLE_MULTI_MODULE != 0
  848. wasm_runtime_set_module_reader(module_reader_callback,
  849. module_destroyer_callback);
  850. #endif
  851. /* load WASM module */
  852. if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
  853. error_buf, sizeof(error_buf)))) {
  854. printf("%s\n", error_buf);
  855. goto fail2;
  856. }
  857. #if WASM_ENABLE_DYNAMIC_AOT_DEBUG != 0
  858. if (!wasm_runtime_set_module_name(wasm_module, wasm_file, error_buf,
  859. sizeof(error_buf))) {
  860. printf("set aot module name failed in dynamic aot debug mode, %s\n",
  861. error_buf);
  862. goto fail3;
  863. }
  864. #endif
  865. #if WASM_ENABLE_LIBC_WASI != 0
  866. libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx);
  867. #endif
  868. /* instantiate the module */
  869. if (!(wasm_module_inst =
  870. wasm_runtime_instantiate(wasm_module, stack_size, heap_size,
  871. error_buf, sizeof(error_buf)))) {
  872. printf("%s\n", error_buf);
  873. goto fail3;
  874. }
  875. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  876. if (disable_bounds_checks) {
  877. wasm_runtime_set_bounds_checks(wasm_module_inst, false);
  878. }
  879. #endif
  880. #if WASM_ENABLE_DEBUG_INTERP != 0
  881. if (ip_addr != NULL) {
  882. wasm_exec_env_t exec_env =
  883. wasm_runtime_get_exec_env_singleton(wasm_module_inst);
  884. uint32_t debug_port;
  885. if (exec_env == NULL) {
  886. printf("%s\n", wasm_runtime_get_exception(wasm_module_inst));
  887. goto fail4;
  888. }
  889. debug_port = wasm_runtime_start_debug_instance(exec_env);
  890. if (debug_port == 0) {
  891. printf("Failed to start debug instance\n");
  892. goto fail4;
  893. }
  894. }
  895. #endif
  896. #if WASM_ENABLE_THREAD_MGR != 0
  897. struct timeout_arg timeout_arg;
  898. korp_tid timeout_tid;
  899. if (timeout_ms >= 0) {
  900. timeout_arg.timeout_ms = timeout_ms;
  901. timeout_arg.inst = wasm_module_inst;
  902. timeout_arg.cancel = false;
  903. ret = os_thread_create(&timeout_tid, timeout_thread, &timeout_arg,
  904. APP_THREAD_STACK_SIZE_DEFAULT);
  905. if (ret != 0) {
  906. printf("Failed to start timeout\n");
  907. goto fail5;
  908. }
  909. }
  910. #endif
  911. ret = 0;
  912. const char *exception = NULL;
  913. if (is_repl_mode) {
  914. app_instance_repl(wasm_module_inst);
  915. }
  916. else if (func_name) {
  917. exception = app_instance_func(wasm_module_inst, func_name);
  918. if (exception) {
  919. /* got an exception */
  920. ret = 1;
  921. }
  922. }
  923. else {
  924. exception = app_instance_main(wasm_module_inst);
  925. if (exception) {
  926. /* got an exception */
  927. ret = 1;
  928. }
  929. }
  930. #if WASM_ENABLE_LIBC_WASI != 0
  931. if (ret == 0) {
  932. /* propagate wasi exit code. */
  933. ret = wasm_runtime_get_wasi_exit_code(wasm_module_inst);
  934. }
  935. #endif
  936. if (exception)
  937. printf("%s\n", exception);
  938. #if WASM_ENABLE_STATIC_PGO != 0 && WASM_ENABLE_AOT != 0
  939. if (get_package_type(wasm_file_buf, wasm_file_size) == Wasm_Module_AoT
  940. && gen_prof_file)
  941. dump_pgo_prof_data(wasm_module_inst, gen_prof_file);
  942. #endif
  943. #if WASM_ENABLE_THREAD_MGR != 0
  944. if (timeout_ms >= 0) {
  945. timeout_arg.cancel = true;
  946. os_thread_join(timeout_tid, NULL);
  947. }
  948. #endif
  949. #if WASM_ENABLE_THREAD_MGR != 0
  950. fail5:
  951. #endif
  952. #if WASM_ENABLE_DEBUG_INTERP != 0
  953. fail4:
  954. #endif
  955. /* destroy the module instance */
  956. wasm_runtime_deinstantiate(wasm_module_inst);
  957. fail3:
  958. /* unload the module */
  959. wasm_runtime_unload(wasm_module);
  960. fail2:
  961. /* free the file buffer */
  962. if (!is_xip_file)
  963. wasm_runtime_free(wasm_file_buf);
  964. else
  965. os_munmap(wasm_file_buf, wasm_file_size);
  966. fail1:
  967. #if BH_HAS_DLFCN
  968. /* unload the native libraries */
  969. unregister_and_unload_native_libs(native_lib_loaded_count,
  970. native_lib_loaded_list);
  971. #endif
  972. /* destroy runtime environment */
  973. wasm_runtime_destroy();
  974. return ret;
  975. }