wasm_application.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_platform.h"
  6. #if WASM_ENABLE_INTERP != 0
  7. #include "../interpreter/wasm_runtime.h"
  8. #endif
  9. #if WASM_ENABLE_AOT != 0
  10. #include "../aot/aot_runtime.h"
  11. #endif
  12. #if WASM_ENABLE_THREAD_MGR != 0
  13. #include "../libraries/thread-mgr/thread_manager.h"
  14. #endif
  15. static void
  16. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  17. {
  18. if (error_buf != NULL)
  19. snprintf(error_buf, error_buf_size, "%s", string);
  20. }
  21. static void *
  22. runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst,
  23. char *error_buf, uint32 error_buf_size)
  24. {
  25. void *mem;
  26. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  27. if (module_inst != NULL) {
  28. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  29. }
  30. else if (error_buf != NULL) {
  31. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  32. }
  33. return NULL;
  34. }
  35. memset(mem, 0, (uint32)size);
  36. return mem;
  37. }
  38. static union {
  39. int a;
  40. char b;
  41. } __ue = { .a = 1 };
  42. #define is_little_endian() (__ue.b == 1)
  43. /**
  44. * Implementation of wasm_application_execute_main()
  45. */
  46. static bool
  47. check_main_func_type(const WASMType *type)
  48. {
  49. if (!(type->param_count == 0 || type->param_count == 2)
  50. || type->result_count > 1) {
  51. LOG_ERROR(
  52. "WASM execute application failed: invalid main function type.\n");
  53. return false;
  54. }
  55. if (type->param_count == 2
  56. && !(type->types[0] == VALUE_TYPE_I32
  57. && type->types[1] == VALUE_TYPE_I32)) {
  58. LOG_ERROR(
  59. "WASM execute application failed: invalid main function type.\n");
  60. return false;
  61. }
  62. if (type->result_count
  63. && type->types[type->param_count] != VALUE_TYPE_I32) {
  64. LOG_ERROR(
  65. "WASM execute application failed: invalid main function type.\n");
  66. return false;
  67. }
  68. return true;
  69. }
  70. static bool
  71. execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
  72. {
  73. WASMFunctionInstanceCommon *func;
  74. WASMType *func_type = NULL;
  75. WASMExecEnv *exec_env = NULL;
  76. uint32 argc1 = 0, argv1[2] = { 0 };
  77. uint32 total_argv_size = 0;
  78. uint64 total_size;
  79. uint32 argv_buf_offset = 0;
  80. int32 i;
  81. char *argv_buf, *p, *p_end;
  82. uint32 *argv_offsets, module_type;
  83. bool ret, is_import_func = true;
  84. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  85. if (!exec_env) {
  86. wasm_runtime_set_exception(module_inst,
  87. "create singleton exec_env failed");
  88. return false;
  89. }
  90. #if WASM_ENABLE_LIBC_WASI != 0
  91. /* In wasi mode, we should call the function named "_start"
  92. which initializes the wasi envrionment and then calls
  93. the actual main function. Directly calling main function
  94. may cause exception thrown. */
  95. if ((func = wasm_runtime_lookup_wasi_start_function(module_inst))) {
  96. #if WASM_ENABLE_DEBUG_INTERP != 0
  97. wasm_runtime_start_debug_instance(exec_env);
  98. #endif
  99. return wasm_runtime_call_wasm(exec_env, func, 0, NULL);
  100. }
  101. #endif /* end of WASM_ENABLE_LIBC_WASI */
  102. if (!(func = wasm_runtime_lookup_function(module_inst, "main", NULL))
  103. && !(func = wasm_runtime_lookup_function(module_inst,
  104. "__main_argc_argv", NULL))
  105. && !(func = wasm_runtime_lookup_function(module_inst, "_main", NULL))) {
  106. #if WASM_ENABLE_LIBC_WASI != 0
  107. wasm_runtime_set_exception(
  108. module_inst, "lookup the entry point symbol (like _start, main, "
  109. "_main, __main_argc_argv) failed");
  110. #else
  111. wasm_runtime_set_exception(module_inst,
  112. "lookup the entry point symbol (like main, "
  113. "_main, __main_argc_argv) failed");
  114. #endif
  115. return false;
  116. }
  117. #if WASM_ENABLE_INTERP != 0
  118. if (module_inst->module_type == Wasm_Module_Bytecode) {
  119. is_import_func = ((WASMFunctionInstance *)func)->is_import_func;
  120. }
  121. #endif
  122. #if WASM_ENABLE_AOT != 0
  123. if (module_inst->module_type == Wasm_Module_AoT) {
  124. is_import_func = ((AOTFunctionInstance *)func)->is_import_func;
  125. }
  126. #endif
  127. if (is_import_func) {
  128. wasm_runtime_set_exception(module_inst, "lookup main function failed");
  129. return false;
  130. }
  131. module_type = module_inst->module_type;
  132. func_type = wasm_runtime_get_function_type(func, module_type);
  133. if (!func_type) {
  134. LOG_ERROR("invalid module instance type");
  135. return false;
  136. }
  137. if (!check_main_func_type(func_type)) {
  138. wasm_runtime_set_exception(module_inst,
  139. "invalid function type of main function");
  140. return false;
  141. }
  142. if (func_type->param_count) {
  143. for (i = 0; i < argc; i++)
  144. total_argv_size += (uint32)(strlen(argv[i]) + 1);
  145. total_argv_size = align_uint(total_argv_size, 4);
  146. total_size = (uint64)total_argv_size + sizeof(int32) * (uint64)argc;
  147. if (total_size >= UINT32_MAX
  148. || !(argv_buf_offset = wasm_runtime_module_malloc(
  149. module_inst, (uint32)total_size, (void **)&argv_buf))) {
  150. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  151. return false;
  152. }
  153. p = argv_buf;
  154. argv_offsets = (uint32 *)(p + total_argv_size);
  155. p_end = p + total_size;
  156. for (i = 0; i < argc; i++) {
  157. bh_memcpy_s(p, (uint32)(p_end - p), argv[i],
  158. (uint32)(strlen(argv[i]) + 1));
  159. argv_offsets[i] = argv_buf_offset + (uint32)(p - argv_buf);
  160. p += strlen(argv[i]) + 1;
  161. }
  162. argc1 = 2;
  163. argv1[0] = (uint32)argc;
  164. argv1[1] =
  165. (uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
  166. }
  167. #if WASM_ENABLE_DEBUG_INTERP != 0
  168. wasm_runtime_start_debug_instance(exec_env);
  169. #endif
  170. ret = wasm_runtime_call_wasm(exec_env, func, argc1, argv1);
  171. if (ret && func_type->result_count > 0 && argc > 0 && argv)
  172. /* copy the return value */
  173. *(int *)argv = (int)argv1[0];
  174. if (argv_buf_offset)
  175. wasm_runtime_module_free(module_inst, argv_buf_offset);
  176. return ret;
  177. }
  178. bool
  179. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
  180. char *argv[])
  181. {
  182. bool ret;
  183. #if WASM_ENABLE_THREAD_MGR != 0
  184. WASMCluster *cluster;
  185. #endif
  186. #if WASM_ENABLE_THREAD_MGR != 0 || WASM_ENABLE_MEMORY_PROFILING != 0
  187. WASMExecEnv *exec_env;
  188. #endif
  189. ret = execute_main(module_inst, argc, argv);
  190. #if WASM_ENABLE_THREAD_MGR != 0
  191. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  192. if (exec_env && (cluster = wasm_exec_env_get_cluster(exec_env))) {
  193. wasm_cluster_wait_for_all_except_self(cluster, exec_env);
  194. }
  195. #endif
  196. #if WASM_ENABLE_MEMORY_PROFILING != 0
  197. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  198. if (exec_env) {
  199. wasm_runtime_dump_mem_consumption(exec_env);
  200. }
  201. #endif
  202. #if WASM_ENABLE_PERF_PROFILING != 0
  203. wasm_runtime_dump_perf_profiling(module_inst);
  204. #endif
  205. return (ret && !wasm_runtime_get_exception(module_inst)) ? true : false;
  206. }
  207. #if WASM_ENABLE_MULTI_MODULE != 0
  208. static WASMModuleInstance *
  209. get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
  210. const char *sub_module_name)
  211. {
  212. WASMSubModInstNode *node =
  213. bh_list_first_elem(parent_module_inst->sub_module_inst_list);
  214. while (node && strcmp(node->module_name, sub_module_name)) {
  215. node = bh_list_elem_next(node);
  216. }
  217. return node ? node->module_inst : NULL;
  218. }
  219. static bool
  220. parse_function_name(char *orig_function_name, char **p_module_name,
  221. char **p_function_name)
  222. {
  223. if (orig_function_name[0] != '$') {
  224. *p_module_name = NULL;
  225. *p_function_name = orig_function_name;
  226. return true;
  227. }
  228. /**
  229. * $module_name$function_name\0
  230. * ===>
  231. * module_name\0function_name\0
  232. * ===>
  233. * module_name
  234. * function_name
  235. */
  236. char *p1 = orig_function_name;
  237. char *p2 = strchr(p1 + 1, '$');
  238. if (!p2) {
  239. LOG_DEBUG("can not parse the incoming function name");
  240. return false;
  241. }
  242. *p_module_name = p1 + 1;
  243. *p2 = '\0';
  244. *p_function_name = p2 + 1;
  245. return strlen(*p_module_name) && strlen(*p_function_name);
  246. }
  247. #endif
  248. /**
  249. * Implementation of wasm_application_execute_func()
  250. */
  251. static bool
  252. resolve_function(WASMModuleInstanceCommon *module_inst, const char *name,
  253. WASMFunctionInstanceCommon **out_func,
  254. WASMModuleInstanceCommon **out_module_inst)
  255. {
  256. WASMFunctionInstanceCommon *target_func = NULL;
  257. WASMModuleInstanceCommon *target_inst = NULL;
  258. #if WASM_ENABLE_MULTI_MODULE != 0
  259. char *function_name = NULL;
  260. char *orig_name = NULL;
  261. char *sub_module_name = NULL;
  262. uint32 length = (uint32)(strlen(name) + 1);
  263. orig_name = runtime_malloc(sizeof(char) * length, NULL, NULL, 0);
  264. if (!orig_name) {
  265. goto LEAVE;
  266. }
  267. strncpy(orig_name, name, length);
  268. if (!parse_function_name(orig_name, &sub_module_name, &function_name)) {
  269. goto LEAVE;
  270. }
  271. LOG_DEBUG("%s -> %s and %s", name, sub_module_name, function_name);
  272. if (sub_module_name) {
  273. target_inst = (WASMModuleInstanceCommon *)get_sub_module_inst(
  274. (WASMModuleInstance *)module_inst, sub_module_name);
  275. if (!target_inst) {
  276. LOG_DEBUG("can not find a sub module named %s", sub_module_name);
  277. goto LEAVE;
  278. }
  279. }
  280. else {
  281. target_inst = module_inst;
  282. }
  283. #else
  284. const char *function_name = name;
  285. target_inst = module_inst;
  286. #endif
  287. target_func =
  288. wasm_runtime_lookup_function(target_inst, function_name, NULL);
  289. #if WASM_ENABLE_MULTI_MODULE != 0
  290. LEAVE:
  291. if (orig_name)
  292. wasm_runtime_free(orig_name);
  293. #endif
  294. *out_func = target_func;
  295. *out_module_inst = target_inst;
  296. return target_func;
  297. }
  298. union ieee754_float {
  299. float f;
  300. /* This is the IEEE 754 single-precision format. */
  301. union {
  302. struct {
  303. unsigned int negative : 1;
  304. unsigned int exponent : 8;
  305. unsigned int mantissa : 23;
  306. } ieee_big_endian;
  307. struct {
  308. unsigned int mantissa : 23;
  309. unsigned int exponent : 8;
  310. unsigned int negative : 1;
  311. } ieee_little_endian;
  312. } ieee;
  313. };
  314. union ieee754_double {
  315. double d;
  316. /* This is the IEEE 754 double-precision format. */
  317. union {
  318. struct {
  319. unsigned int negative : 1;
  320. unsigned int exponent : 11;
  321. /* Together these comprise the mantissa. */
  322. unsigned int mantissa0 : 20;
  323. unsigned int mantissa1 : 32;
  324. } ieee_big_endian;
  325. struct {
  326. /* Together these comprise the mantissa. */
  327. unsigned int mantissa1 : 32;
  328. unsigned int mantissa0 : 20;
  329. unsigned int exponent : 11;
  330. unsigned int negative : 1;
  331. } ieee_little_endian;
  332. } ieee;
  333. };
  334. static bool
  335. execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
  336. int32 argc, char *argv[])
  337. {
  338. WASMFunctionInstanceCommon *target_func;
  339. WASMModuleInstanceCommon *target_inst;
  340. WASMType *type = NULL;
  341. WASMExecEnv *exec_env = NULL;
  342. uint32 argc1, *argv1 = NULL, cell_num = 0, j, k = 0;
  343. #if WASM_ENABLE_REF_TYPES != 0
  344. uint32 param_size_in_double_world = 0, result_size_in_double_world = 0;
  345. #endif
  346. int32 i, p, module_type;
  347. uint64 total_size;
  348. const char *exception;
  349. char buf[128];
  350. bh_assert(argc >= 0);
  351. LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc);
  352. if (!resolve_function(module_inst, name, &target_func, &target_inst)) {
  353. snprintf(buf, sizeof(buf), "lookup function %s failed", name);
  354. wasm_runtime_set_exception(module_inst, buf);
  355. goto fail;
  356. }
  357. module_type = target_inst->module_type;
  358. type = wasm_runtime_get_function_type(target_func, module_type);
  359. if (!type) {
  360. LOG_ERROR("invalid module instance type");
  361. return false;
  362. }
  363. if (type->param_count != (uint32)argc) {
  364. wasm_runtime_set_exception(module_inst, "invalid input argument count");
  365. goto fail;
  366. }
  367. #if WASM_ENABLE_REF_TYPES != 0
  368. for (i = 0; i < type->param_count; i++) {
  369. param_size_in_double_world +=
  370. wasm_value_type_cell_num_outside(type->types[i]);
  371. }
  372. for (i = 0; i < type->result_count; i++) {
  373. result_size_in_double_world += wasm_value_type_cell_num_outside(
  374. type->types[type->param_count + i]);
  375. }
  376. argc1 = param_size_in_double_world;
  377. cell_num = (param_size_in_double_world >= result_size_in_double_world)
  378. ? param_size_in_double_world
  379. : result_size_in_double_world;
  380. #else
  381. argc1 = type->param_cell_num;
  382. cell_num = (argc1 > type->ret_cell_num) ? argc1 : type->ret_cell_num;
  383. #endif
  384. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  385. if ((!(argv1 = runtime_malloc((uint32)total_size, target_inst, NULL, 0)))) {
  386. goto fail;
  387. }
  388. /* Parse arguments */
  389. for (i = 0, p = 0; i < argc; i++) {
  390. char *endptr = NULL;
  391. bh_assert(argv[i] != NULL);
  392. if (argv[i][0] == '\0') {
  393. snprintf(buf, sizeof(buf), "invalid input argument %" PRId32, i);
  394. wasm_runtime_set_exception(module_inst, buf);
  395. goto fail;
  396. }
  397. switch (type->types[i]) {
  398. case VALUE_TYPE_I32:
  399. argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
  400. break;
  401. case VALUE_TYPE_I64:
  402. {
  403. union {
  404. uint64 val;
  405. uint32 parts[2];
  406. } u;
  407. u.val = strtoull(argv[i], &endptr, 0);
  408. argv1[p++] = u.parts[0];
  409. argv1[p++] = u.parts[1];
  410. break;
  411. }
  412. case VALUE_TYPE_F32:
  413. {
  414. float32 f32 = strtof(argv[i], &endptr);
  415. if (isnan(f32)) {
  416. if (argv[i][0] == '-') {
  417. union ieee754_float u;
  418. u.f = f32;
  419. if (is_little_endian())
  420. u.ieee.ieee_little_endian.negative = 1;
  421. else
  422. u.ieee.ieee_big_endian.negative = 1;
  423. bh_memcpy_s(&f32, sizeof(float), &u.f, sizeof(float));
  424. }
  425. if (endptr[0] == ':') {
  426. uint32 sig;
  427. union ieee754_float u;
  428. sig = (uint32)strtoul(endptr + 1, &endptr, 0);
  429. u.f = f32;
  430. if (is_little_endian())
  431. u.ieee.ieee_little_endian.mantissa = sig;
  432. else
  433. u.ieee.ieee_big_endian.mantissa = sig;
  434. bh_memcpy_s(&f32, sizeof(float), &u.f, sizeof(float));
  435. }
  436. }
  437. bh_memcpy_s(&argv1[p], (uint32)total_size - p, &f32,
  438. (uint32)sizeof(float));
  439. p++;
  440. break;
  441. }
  442. case VALUE_TYPE_F64:
  443. {
  444. union {
  445. float64 val;
  446. uint32 parts[2];
  447. } u;
  448. u.val = strtod(argv[i], &endptr);
  449. if (isnan(u.val)) {
  450. if (argv[i][0] == '-') {
  451. union ieee754_double ud;
  452. ud.d = u.val;
  453. if (is_little_endian())
  454. ud.ieee.ieee_little_endian.negative = 1;
  455. else
  456. ud.ieee.ieee_big_endian.negative = 1;
  457. bh_memcpy_s(&u.val, sizeof(double), &ud.d,
  458. sizeof(double));
  459. }
  460. if (endptr[0] == ':') {
  461. uint64 sig;
  462. union ieee754_double ud;
  463. sig = strtoull(endptr + 1, &endptr, 0);
  464. ud.d = u.val;
  465. if (is_little_endian()) {
  466. ud.ieee.ieee_little_endian.mantissa0 = sig >> 32;
  467. ud.ieee.ieee_little_endian.mantissa1 = (uint32)sig;
  468. }
  469. else {
  470. ud.ieee.ieee_big_endian.mantissa0 = sig >> 32;
  471. ud.ieee.ieee_big_endian.mantissa1 = (uint32)sig;
  472. }
  473. bh_memcpy_s(&u.val, sizeof(double), &ud.d,
  474. sizeof(double));
  475. }
  476. }
  477. argv1[p++] = u.parts[0];
  478. argv1[p++] = u.parts[1];
  479. break;
  480. }
  481. #if WASM_ENABLE_SIMD != 0
  482. case VALUE_TYPE_V128:
  483. {
  484. /* it likes 0x123\0x234 or 123\234 */
  485. /* retrive first i64 */
  486. *(uint64 *)(argv1 + p) = strtoull(argv[i], &endptr, 0);
  487. /* skip \ */
  488. endptr++;
  489. /* retrive second i64 */
  490. *(uint64 *)(argv1 + p + 2) = strtoull(endptr, &endptr, 0);
  491. p += 4;
  492. break;
  493. }
  494. #endif /* WASM_ENABLE_SIMD != 0 */
  495. #if WASM_ENABLE_REF_TYPES != 0
  496. case VALUE_TYPE_FUNCREF:
  497. {
  498. if (strncasecmp(argv[i], "null", 4) == 0) {
  499. argv1[p++] = (uint32)-1;
  500. }
  501. else {
  502. argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
  503. }
  504. break;
  505. }
  506. case VALUE_TYPE_EXTERNREF:
  507. {
  508. #if UINTPTR_MAX == UINT32_MAX
  509. if (strncasecmp(argv[i], "null", 4) == 0) {
  510. argv1[p++] = (uint32)-1;
  511. }
  512. else {
  513. argv1[p++] = strtoul(argv[i], &endptr, 0);
  514. }
  515. #else
  516. union {
  517. uintptr_t val;
  518. uint32 parts[2];
  519. } u;
  520. if (strncasecmp(argv[i], "null", 4) == 0) {
  521. u.val = (uintptr_t)-1LL;
  522. }
  523. else {
  524. u.val = strtoull(argv[i], &endptr, 0);
  525. }
  526. argv1[p++] = u.parts[0];
  527. argv1[p++] = u.parts[1];
  528. #endif
  529. break;
  530. }
  531. #endif /* WASM_ENABLE_REF_TYPES */
  532. default:
  533. bh_assert(0);
  534. break;
  535. }
  536. if (endptr && *endptr != '\0' && *endptr != '_') {
  537. snprintf(buf, sizeof(buf), "invalid input argument %" PRId32 ": %s",
  538. i, argv[i]);
  539. wasm_runtime_set_exception(module_inst, buf);
  540. goto fail;
  541. }
  542. }
  543. wasm_runtime_set_exception(module_inst, NULL);
  544. #if WASM_ENABLE_REF_TYPES == 0
  545. bh_assert(p == (int32)argc1);
  546. #endif
  547. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  548. if (!exec_env) {
  549. wasm_runtime_set_exception(module_inst,
  550. "create singleton exec_env failed");
  551. goto fail;
  552. }
  553. #if WASM_ENABLE_DEBUG_INTERP != 0
  554. wasm_runtime_start_debug_instance(exec_env);
  555. #endif
  556. if (!wasm_runtime_call_wasm(exec_env, target_func, argc1, argv1)) {
  557. goto fail;
  558. }
  559. /* print return value */
  560. for (j = 0; j < type->result_count; j++) {
  561. switch (type->types[type->param_count + j]) {
  562. case VALUE_TYPE_I32:
  563. {
  564. os_printf("0x%" PRIx32 ":i32", argv1[k]);
  565. k++;
  566. break;
  567. }
  568. case VALUE_TYPE_I64:
  569. {
  570. union {
  571. uint64 val;
  572. uint32 parts[2];
  573. } u;
  574. u.parts[0] = argv1[k];
  575. u.parts[1] = argv1[k + 1];
  576. k += 2;
  577. os_printf("0x%" PRIx64 ":i64", u.val);
  578. break;
  579. }
  580. case VALUE_TYPE_F32:
  581. {
  582. os_printf("%.7g:f32", *(float32 *)(argv1 + k));
  583. k++;
  584. break;
  585. }
  586. case VALUE_TYPE_F64:
  587. {
  588. union {
  589. float64 val;
  590. uint32 parts[2];
  591. } u;
  592. u.parts[0] = argv1[k];
  593. u.parts[1] = argv1[k + 1];
  594. k += 2;
  595. os_printf("%.7g:f64", u.val);
  596. break;
  597. }
  598. #if WASM_ENABLE_REF_TYPES != 0
  599. case VALUE_TYPE_FUNCREF:
  600. {
  601. if (argv1[k] != NULL_REF)
  602. os_printf("%u:ref.func", argv1[k]);
  603. else
  604. os_printf("func:ref.null");
  605. k++;
  606. break;
  607. }
  608. case VALUE_TYPE_EXTERNREF:
  609. {
  610. #if UINTPTR_MAX == UINT32_MAX
  611. if (argv1[k] != 0 && argv1[k] != (uint32)-1)
  612. os_printf("%p:ref.extern", (void *)argv1[k]);
  613. else
  614. os_printf("extern:ref.null");
  615. k++;
  616. #else
  617. union {
  618. uintptr_t val;
  619. uint32 parts[2];
  620. } u;
  621. u.parts[0] = argv1[k];
  622. u.parts[1] = argv1[k + 1];
  623. k += 2;
  624. if (u.val && u.val != (uintptr_t)-1LL)
  625. os_printf("%p:ref.extern", (void *)u.val);
  626. else
  627. os_printf("extern:ref.null");
  628. #endif
  629. break;
  630. }
  631. #endif
  632. #if WASM_ENABLE_SIMD != 0
  633. case VALUE_TYPE_V128:
  634. {
  635. uint64 *v = (uint64 *)(argv1 + k);
  636. os_printf("<0x%016" PRIx64 " 0x%016" PRIx64 ">:v128", *v,
  637. *(v + 1));
  638. k += 4;
  639. break;
  640. }
  641. #endif /* WASM_ENABLE_SIMD != 0 */
  642. default:
  643. bh_assert(0);
  644. break;
  645. }
  646. if (j < (uint32)(type->result_count - 1))
  647. os_printf(",");
  648. }
  649. os_printf("\n");
  650. wasm_runtime_free(argv1);
  651. return true;
  652. fail:
  653. if (argv1)
  654. wasm_runtime_free(argv1);
  655. exception = wasm_runtime_get_exception(module_inst);
  656. bh_assert(exception);
  657. os_printf("%s\n", exception);
  658. return false;
  659. }
  660. bool
  661. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  662. const char *name, int32 argc, char *argv[])
  663. {
  664. bool ret;
  665. #if WASM_ENABLE_THREAD_MGR != 0
  666. WASMCluster *cluster;
  667. #endif
  668. #if WASM_ENABLE_THREAD_MGR != 0 || WASM_ENABLE_MEMORY_PROFILING != 0
  669. WASMExecEnv *exec_env;
  670. #endif
  671. ret = execute_func(module_inst, name, argc, argv);
  672. #if WASM_ENABLE_THREAD_MGR != 0
  673. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  674. if (exec_env && (cluster = wasm_exec_env_get_cluster(exec_env))) {
  675. wasm_cluster_wait_for_all_except_self(cluster, exec_env);
  676. }
  677. #endif
  678. #if WASM_ENABLE_MEMORY_PROFILING != 0
  679. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  680. if (exec_env) {
  681. wasm_runtime_dump_mem_consumption(exec_env);
  682. }
  683. #endif
  684. #if WASM_ENABLE_PERF_PROFILING != 0
  685. wasm_runtime_dump_perf_profiling(module_inst);
  686. #endif
  687. return (ret && !wasm_runtime_get_exception(module_inst)) ? true : false;
  688. }