main.c 33 KB

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