main.c 32 KB

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