main.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. char error_buf[128] = { 0 };
  545. #if WASM_ENABLE_LOG != 0
  546. int log_verbose_level = 2;
  547. #endif
  548. bool is_repl_mode = false;
  549. bool is_xip_file = false;
  550. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  551. bool disable_bounds_checks = false;
  552. #endif
  553. #if WASM_ENABLE_LIBC_WASI != 0
  554. libc_wasi_parse_context_t wasi_parse_ctx;
  555. #endif
  556. #if BH_HAS_DLFCN
  557. const char *native_lib_list[8] = { NULL };
  558. uint32 native_lib_count = 0;
  559. struct native_lib *native_lib_loaded_list[8];
  560. uint32 native_lib_loaded_count = 0;
  561. #endif
  562. #if WASM_ENABLE_DEBUG_INTERP != 0
  563. char *ip_addr = NULL;
  564. int instance_port = 0;
  565. #endif
  566. #if WASM_ENABLE_STATIC_PGO != 0
  567. const char *gen_prof_file = NULL;
  568. #endif
  569. #if WASM_ENABLE_THREAD_MGR != 0
  570. int timeout_ms = -1;
  571. #endif
  572. #if WASM_ENABLE_LIBC_WASI != 0
  573. memset(&wasi_parse_ctx, 0, sizeof(wasi_parse_ctx));
  574. #endif
  575. /* Process options. */
  576. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  577. if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--function")) {
  578. argc--, argv++;
  579. if (argc < 2) {
  580. return print_help();
  581. }
  582. func_name = argv[0];
  583. }
  584. #if WASM_ENABLE_INTERP != 0
  585. else if (!strcmp(argv[0], "--interp")) {
  586. running_mode = Mode_Interp;
  587. }
  588. #endif
  589. #if WASM_ENABLE_FAST_JIT != 0
  590. else if (!strcmp(argv[0], "--fast-jit")) {
  591. running_mode = Mode_Fast_JIT;
  592. }
  593. #endif
  594. #if WASM_ENABLE_JIT != 0
  595. else if (!strcmp(argv[0], "--llvm-jit")) {
  596. running_mode = Mode_LLVM_JIT;
  597. }
  598. #endif
  599. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 \
  600. && WASM_ENABLE_LAZY_JIT != 0
  601. else if (!strcmp(argv[0], "--multi-tier-jit")) {
  602. running_mode = Mode_Multi_Tier_JIT;
  603. }
  604. #endif
  605. #if WASM_ENABLE_LOG != 0
  606. else if (!strncmp(argv[0], "-v=", 3)) {
  607. log_verbose_level = atoi(argv[0] + 3);
  608. if (log_verbose_level < 0 || log_verbose_level > 5)
  609. return print_help();
  610. }
  611. #endif
  612. else if (!strcmp(argv[0], "--repl")) {
  613. is_repl_mode = true;
  614. }
  615. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  616. else if (!strcmp(argv[0], "--disable-bounds-checks")) {
  617. disable_bounds_checks = true;
  618. }
  619. #endif
  620. else if (!strncmp(argv[0], "--stack-size=", 13)) {
  621. if (argv[0][13] == '\0')
  622. return print_help();
  623. stack_size = atoi(argv[0] + 13);
  624. }
  625. else if (!strncmp(argv[0], "--heap-size=", 12)) {
  626. if (argv[0][12] == '\0')
  627. return print_help();
  628. heap_size = atoi(argv[0] + 12);
  629. }
  630. #if WASM_ENABLE_FAST_JIT != 0
  631. else if (!strncmp(argv[0], "--jit-codecache-size=", 21)) {
  632. if (argv[0][21] == '\0')
  633. return print_help();
  634. jit_code_cache_size = atoi(argv[0] + 21);
  635. }
  636. #endif
  637. #if WASM_ENABLE_GC != 0
  638. else if (!strncmp(argv[0], "--gc-heap-size=", 15)) {
  639. if (argv[0][15] == '\0')
  640. return print_help();
  641. gc_heap_size = atoi(argv[0] + 15);
  642. }
  643. #endif
  644. #if WASM_ENABLE_JIT != 0
  645. else if (!strncmp(argv[0], "--llvm-jit-size-level=", 22)) {
  646. if (argv[0][22] == '\0')
  647. return print_help();
  648. llvm_jit_size_level = atoi(argv[0] + 22);
  649. if (llvm_jit_size_level < 1) {
  650. printf("LLVM JIT size level shouldn't be smaller than 1, "
  651. "setting it to 1\n");
  652. llvm_jit_size_level = 1;
  653. }
  654. else if (llvm_jit_size_level > 3) {
  655. printf("LLVM JIT size level shouldn't be greater than 3, "
  656. "setting it to 3\n");
  657. llvm_jit_size_level = 3;
  658. }
  659. }
  660. else if (!strncmp(argv[0], "--llvm-jit-opt-level=", 21)) {
  661. if (argv[0][21] == '\0')
  662. return print_help();
  663. llvm_jit_opt_level = atoi(argv[0] + 21);
  664. if (llvm_jit_opt_level < 1) {
  665. printf("LLVM JIT opt level shouldn't be smaller than 1, "
  666. "setting it to 1\n");
  667. llvm_jit_opt_level = 1;
  668. }
  669. else if (llvm_jit_opt_level > 3) {
  670. printf("LLVM JIT opt level shouldn't be greater than 3, "
  671. "setting it to 3\n");
  672. llvm_jit_opt_level = 3;
  673. }
  674. }
  675. else if (!strcmp(argv[0], "--enable-segue")) {
  676. /* all flags are enabled */
  677. segue_flags = 0x1F1F;
  678. }
  679. else if (!strncmp(argv[0], "--enable-segue=", 15)) {
  680. segue_flags = resolve_segue_flags(argv[0] + 15);
  681. if (segue_flags == (uint32)-1)
  682. return print_help();
  683. }
  684. #endif /* end of WASM_ENABLE_JIT != 0 */
  685. #if BH_HAS_DLFCN
  686. else if (!strncmp(argv[0], "--native-lib=", 13)) {
  687. if (argv[0][13] == '\0')
  688. return print_help();
  689. if (native_lib_count >= sizeof(native_lib_list) / sizeof(char *)) {
  690. printf("Only allow max native lib number %d\n",
  691. (int)(sizeof(native_lib_list) / sizeof(char *)));
  692. return 1;
  693. }
  694. native_lib_list[native_lib_count++] = argv[0] + 13;
  695. }
  696. #endif
  697. #if WASM_ENABLE_LINUX_PERF != 0
  698. else if (!strncmp(argv[0], "--enable-linux-perf", 19)) {
  699. enable_linux_perf = true;
  700. }
  701. #endif
  702. #if WASM_ENABLE_MULTI_MODULE != 0
  703. else if (!strncmp(argv[0],
  704. "--module-path=", strlen("--module-path="))) {
  705. module_search_path = handle_module_path(argv[0]);
  706. if (!strlen(module_search_path)) {
  707. return print_help();
  708. }
  709. }
  710. #endif
  711. #if WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
  712. else if (!strncmp(argv[0], "--max-threads=", 14)) {
  713. if (argv[0][14] == '\0')
  714. return print_help();
  715. wasm_runtime_set_max_thread_num(atoi(argv[0] + 14));
  716. }
  717. #endif
  718. #if WASM_ENABLE_THREAD_MGR != 0
  719. else if (!strncmp(argv[0], "--timeout=", 10)) {
  720. if (argv[0][10] == '\0')
  721. return print_help();
  722. timeout_ms = atoi(argv[0] + 10);
  723. }
  724. #endif
  725. #if WASM_ENABLE_DEBUG_INTERP != 0
  726. else if (!strncmp(argv[0], "-g=", 3)) {
  727. char *port_str = strchr(argv[0] + 3, ':');
  728. char *port_end;
  729. if (port_str == NULL)
  730. return print_help();
  731. *port_str = '\0';
  732. instance_port = strtoul(port_str + 1, &port_end, 10);
  733. if (port_str[1] == '\0' || *port_end != '\0')
  734. return print_help();
  735. ip_addr = argv[0] + 3;
  736. }
  737. #endif
  738. #if WASM_ENABLE_STATIC_PGO != 0
  739. else if (!strncmp(argv[0], "--gen-prof-file=", 16)) {
  740. if (argv[0][16] == '\0')
  741. return print_help();
  742. gen_prof_file = argv[0] + 16;
  743. }
  744. #endif
  745. else if (!strcmp(argv[0], "--version")) {
  746. uint32 major, minor, patch;
  747. wasm_runtime_get_version(&major, &minor, &patch);
  748. printf("iwasm %" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n", major, minor,
  749. patch);
  750. printf("\n");
  751. wasm_proposal_print_status();
  752. return 0;
  753. }
  754. else {
  755. #if WASM_ENABLE_LIBC_WASI != 0
  756. libc_wasi_parse_result_t result =
  757. libc_wasi_parse(argv[0], &wasi_parse_ctx);
  758. switch (result) {
  759. case LIBC_WASI_PARSE_RESULT_OK:
  760. continue;
  761. case LIBC_WASI_PARSE_RESULT_NEED_HELP:
  762. return print_help();
  763. case LIBC_WASI_PARSE_RESULT_BAD_PARAM:
  764. return 1;
  765. }
  766. #else
  767. return print_help();
  768. #endif
  769. }
  770. }
  771. if (argc == 0)
  772. return print_help();
  773. wasm_file = argv[0];
  774. app_argc = argc;
  775. app_argv = argv;
  776. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  777. init_args.running_mode = running_mode;
  778. #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
  779. init_args.mem_alloc_type = Alloc_With_Pool;
  780. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  781. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  782. #else
  783. init_args.mem_alloc_type = Alloc_With_Allocator;
  784. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  785. /* Set user data for the allocator is needed */
  786. /* init_args.mem_alloc_option.allocator.user_data = user_data; */
  787. #endif
  788. init_args.mem_alloc_option.allocator.malloc_func = malloc_func;
  789. init_args.mem_alloc_option.allocator.realloc_func = realloc_func;
  790. init_args.mem_alloc_option.allocator.free_func = free_func;
  791. #endif
  792. #if WASM_ENABLE_FAST_JIT != 0
  793. init_args.fast_jit_code_cache_size = jit_code_cache_size;
  794. #endif
  795. #if WASM_ENABLE_GC != 0
  796. init_args.gc_heap_size = gc_heap_size;
  797. #endif
  798. #if WASM_ENABLE_JIT != 0
  799. init_args.llvm_jit_size_level = llvm_jit_size_level;
  800. init_args.llvm_jit_opt_level = llvm_jit_opt_level;
  801. init_args.segue_flags = segue_flags;
  802. #endif
  803. #if WASM_ENABLE_LINUX_PERF != 0
  804. init_args.enable_linux_perf = enable_linux_perf;
  805. #endif
  806. #if WASM_ENABLE_DEBUG_INTERP != 0
  807. init_args.instance_port = instance_port;
  808. if (ip_addr)
  809. /* ensure that init_args.ip_addr is null terminated */
  810. strncpy(init_args.ip_addr, ip_addr, sizeof(init_args.ip_addr) - 1);
  811. #endif
  812. /* initialize runtime environment */
  813. if (!wasm_runtime_full_init(&init_args)) {
  814. printf("Init runtime environment failed.\n");
  815. return -1;
  816. }
  817. #if WASM_ENABLE_LOG != 0
  818. bh_log_set_verbose_level(log_verbose_level);
  819. #endif
  820. #if BH_HAS_DLFCN
  821. native_lib_loaded_count = load_and_register_native_libs(
  822. native_lib_list, native_lib_count, native_lib_loaded_list);
  823. #endif
  824. /* load WASM byte buffer from WASM bin file */
  825. if (!(wasm_file_buf =
  826. (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size)))
  827. goto fail1;
  828. #if WASM_ENABLE_AOT != 0
  829. if (wasm_runtime_is_xip_file(wasm_file_buf, wasm_file_size)) {
  830. uint8 *wasm_file_mapped;
  831. uint8 *daddr;
  832. int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
  833. int map_flags = MMAP_MAP_32BIT;
  834. if (!(wasm_file_mapped = os_mmap(NULL, (uint32)wasm_file_size, map_prot,
  835. map_flags, os_get_invalid_handle()))) {
  836. printf("mmap memory failed\n");
  837. wasm_runtime_free(wasm_file_buf);
  838. goto fail1;
  839. }
  840. #if (WASM_MEM_DUAL_BUS_MIRROR != 0)
  841. daddr = os_get_dbus_mirror(wasm_file_mapped);
  842. #else
  843. daddr = wasm_file_mapped;
  844. #endif
  845. bh_memcpy_s(daddr, wasm_file_size, wasm_file_buf, wasm_file_size);
  846. #if (WASM_MEM_DUAL_BUS_MIRROR != 0)
  847. os_dcache_flush();
  848. #endif
  849. wasm_runtime_free(wasm_file_buf);
  850. wasm_file_buf = wasm_file_mapped;
  851. is_xip_file = true;
  852. }
  853. #endif
  854. #if WASM_ENABLE_MULTI_MODULE != 0
  855. wasm_runtime_set_module_reader(module_reader_callback,
  856. module_destroyer_callback);
  857. #endif
  858. /* load WASM module */
  859. if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
  860. error_buf, sizeof(error_buf)))) {
  861. printf("%s\n", error_buf);
  862. goto fail2;
  863. }
  864. #if WASM_ENABLE_DYNAMIC_AOT_DEBUG != 0
  865. if (!wasm_runtime_set_module_name(wasm_module, wasm_file, error_buf,
  866. sizeof(error_buf))) {
  867. printf("set aot module name failed in dynamic aot debug mode, %s\n",
  868. error_buf);
  869. goto fail3;
  870. }
  871. #endif
  872. #if WASM_ENABLE_LIBC_WASI != 0
  873. libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx);
  874. #endif
  875. /* instantiate the module */
  876. if (!(wasm_module_inst =
  877. wasm_runtime_instantiate(wasm_module, stack_size, heap_size,
  878. error_buf, sizeof(error_buf)))) {
  879. printf("%s\n", error_buf);
  880. goto fail3;
  881. }
  882. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  883. if (disable_bounds_checks) {
  884. wasm_runtime_set_bounds_checks(wasm_module_inst, false);
  885. }
  886. #endif
  887. #if WASM_ENABLE_DEBUG_INTERP != 0
  888. if (ip_addr != NULL) {
  889. wasm_exec_env_t exec_env =
  890. wasm_runtime_get_exec_env_singleton(wasm_module_inst);
  891. uint32_t debug_port;
  892. if (exec_env == NULL) {
  893. printf("%s\n", wasm_runtime_get_exception(wasm_module_inst));
  894. goto fail4;
  895. }
  896. debug_port = wasm_runtime_start_debug_instance(exec_env);
  897. if (debug_port == 0) {
  898. printf("Failed to start debug instance\n");
  899. goto fail4;
  900. }
  901. }
  902. #endif
  903. #if WASM_ENABLE_THREAD_MGR != 0
  904. struct timeout_arg timeout_arg;
  905. korp_tid timeout_tid;
  906. if (timeout_ms >= 0) {
  907. timeout_arg.timeout_ms = timeout_ms;
  908. timeout_arg.inst = wasm_module_inst;
  909. timeout_arg.cancel = false;
  910. ret = os_thread_create(&timeout_tid, timeout_thread, &timeout_arg,
  911. APP_THREAD_STACK_SIZE_DEFAULT);
  912. if (ret != 0) {
  913. printf("Failed to start timeout\n");
  914. goto fail5;
  915. }
  916. }
  917. #endif
  918. ret = 0;
  919. const char *exception = NULL;
  920. if (is_repl_mode) {
  921. app_instance_repl(wasm_module_inst);
  922. }
  923. else if (func_name) {
  924. exception = app_instance_func(wasm_module_inst, func_name);
  925. if (exception) {
  926. /* got an exception */
  927. ret = 1;
  928. }
  929. }
  930. else {
  931. exception = app_instance_main(wasm_module_inst);
  932. if (exception) {
  933. /* got an exception */
  934. ret = 1;
  935. }
  936. }
  937. #if WASM_ENABLE_LIBC_WASI != 0
  938. if (ret == 0) {
  939. /* propagate wasi exit code. */
  940. ret = wasm_runtime_get_wasi_exit_code(wasm_module_inst);
  941. }
  942. #endif
  943. if (exception)
  944. printf("%s\n", exception);
  945. #if WASM_ENABLE_STATIC_PGO != 0 && WASM_ENABLE_AOT != 0
  946. if (get_package_type(wasm_file_buf, wasm_file_size) == Wasm_Module_AoT
  947. && gen_prof_file)
  948. dump_pgo_prof_data(wasm_module_inst, gen_prof_file);
  949. #endif
  950. #if WASM_ENABLE_THREAD_MGR != 0
  951. if (timeout_ms >= 0) {
  952. timeout_arg.cancel = true;
  953. os_thread_join(timeout_tid, NULL);
  954. }
  955. #endif
  956. #if WASM_ENABLE_THREAD_MGR != 0
  957. fail5:
  958. #endif
  959. #if WASM_ENABLE_DEBUG_INTERP != 0
  960. fail4:
  961. #endif
  962. /* destroy the module instance */
  963. wasm_runtime_deinstantiate(wasm_module_inst);
  964. fail3:
  965. /* unload the module */
  966. wasm_runtime_unload(wasm_module);
  967. fail2:
  968. /* free the file buffer */
  969. if (!is_xip_file)
  970. wasm_runtime_free(wasm_file_buf);
  971. else
  972. os_munmap(wasm_file_buf, wasm_file_size);
  973. fail1:
  974. #if BH_HAS_DLFCN
  975. /* unload the native libraries */
  976. unregister_and_unload_native_libs(native_lib_loaded_count,
  977. native_lib_loaded_list);
  978. #endif
  979. /* destroy runtime environment */
  980. wasm_runtime_destroy();
  981. return ret;
  982. }