wasm_application.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. #if WASM_ENABLE_GC != 0
  16. #include "gc/gc_object.h"
  17. #endif
  18. static void
  19. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  20. {
  21. if (error_buf != NULL)
  22. snprintf(error_buf, error_buf_size, "%s", string);
  23. }
  24. static void *
  25. runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst,
  26. char *error_buf, uint32 error_buf_size)
  27. {
  28. void *mem;
  29. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  30. if (module_inst != NULL) {
  31. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  32. }
  33. else if (error_buf != NULL) {
  34. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  35. }
  36. return NULL;
  37. }
  38. memset(mem, 0, (uint32)size);
  39. return mem;
  40. }
  41. static union {
  42. int a;
  43. char b;
  44. } __ue = { .a = 1 };
  45. #define is_little_endian() (__ue.b == 1)
  46. /**
  47. * Implementation of wasm_application_execute_main()
  48. */
  49. static bool
  50. check_main_func_type(const WASMFuncType *type)
  51. {
  52. if (!(type->param_count == 0 || type->param_count == 2)
  53. || type->result_count > 1) {
  54. LOG_ERROR(
  55. "WASM execute application failed: invalid main function type.\n");
  56. return false;
  57. }
  58. if (type->param_count == 2
  59. && !(type->types[0] == VALUE_TYPE_I32
  60. && type->types[1] == VALUE_TYPE_I32)) {
  61. LOG_ERROR(
  62. "WASM execute application failed: invalid main function type.\n");
  63. return false;
  64. }
  65. if (type->result_count
  66. && type->types[type->param_count] != VALUE_TYPE_I32) {
  67. LOG_ERROR(
  68. "WASM execute application failed: invalid main function type.\n");
  69. return false;
  70. }
  71. return true;
  72. }
  73. static bool
  74. execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
  75. {
  76. WASMFunctionInstanceCommon *func;
  77. WASMFuncType *func_type = NULL;
  78. WASMExecEnv *exec_env = NULL;
  79. uint32 argc1 = 0, argv1[2] = { 0 };
  80. uint32 total_argv_size = 0;
  81. uint64 total_size;
  82. uint32 argv_buf_offset = 0;
  83. int32 i;
  84. char *argv_buf, *p, *p_end;
  85. uint32 *argv_offsets, module_type;
  86. bool ret, is_import_func = true;
  87. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  88. if (!exec_env) {
  89. wasm_runtime_set_exception(module_inst,
  90. "create singleton exec_env failed");
  91. return false;
  92. }
  93. #if WASM_ENABLE_LIBC_WASI != 0
  94. /* In wasi mode, we should call the function named "_start"
  95. which initializes the wasi envrionment and then calls
  96. the actual main function. Directly calling main function
  97. may cause exception thrown. */
  98. if ((func = wasm_runtime_lookup_wasi_start_function(module_inst))) {
  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. ret = wasm_runtime_call_wasm(exec_env, func, argc1, argv1);
  168. if (ret && func_type->result_count > 0 && argc > 0 && argv)
  169. /* copy the return value */
  170. *(int *)argv = (int)argv1[0];
  171. if (argv_buf_offset)
  172. wasm_runtime_module_free(module_inst, argv_buf_offset);
  173. return ret;
  174. }
  175. bool
  176. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
  177. char *argv[])
  178. {
  179. bool ret;
  180. #if WASM_ENABLE_MEMORY_PROFILING != 0
  181. WASMExecEnv *exec_env;
  182. #endif
  183. ret = execute_main(module_inst, argc, argv);
  184. #if WASM_ENABLE_MEMORY_PROFILING != 0
  185. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  186. if (exec_env) {
  187. wasm_runtime_dump_mem_consumption(exec_env);
  188. }
  189. #endif
  190. #if WASM_ENABLE_PERF_PROFILING != 0
  191. wasm_runtime_dump_perf_profiling(module_inst);
  192. #endif
  193. return (ret && !wasm_runtime_get_exception(module_inst)) ? true : false;
  194. }
  195. /**
  196. * Implementation of wasm_application_execute_func()
  197. */
  198. union ieee754_float {
  199. float f;
  200. /* This is the IEEE 754 single-precision format. */
  201. union {
  202. struct {
  203. unsigned int negative : 1;
  204. unsigned int exponent : 8;
  205. unsigned int mantissa : 23;
  206. } ieee_big_endian;
  207. struct {
  208. unsigned int mantissa : 23;
  209. unsigned int exponent : 8;
  210. unsigned int negative : 1;
  211. } ieee_little_endian;
  212. } ieee;
  213. };
  214. union ieee754_double {
  215. double d;
  216. /* This is the IEEE 754 double-precision format. */
  217. union {
  218. struct {
  219. unsigned int negative : 1;
  220. unsigned int exponent : 11;
  221. /* Together these comprise the mantissa. */
  222. unsigned int mantissa0 : 20;
  223. unsigned int mantissa1 : 32;
  224. } ieee_big_endian;
  225. struct {
  226. /* Together these comprise the mantissa. */
  227. unsigned int mantissa1 : 32;
  228. unsigned int mantissa0 : 20;
  229. unsigned int exponent : 11;
  230. unsigned int negative : 1;
  231. } ieee_little_endian;
  232. } ieee;
  233. };
  234. static bool
  235. execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
  236. int32 argc, char *argv[])
  237. {
  238. WASMFunctionInstanceCommon *target_func;
  239. WASMFuncType *type = NULL;
  240. WASMExecEnv *exec_env = NULL;
  241. #if WASM_ENABLE_GC != 0
  242. WASMRefTypeMap *ref_type_map;
  243. WASMLocalObjectRef *local_ref;
  244. uint32 num_local_ref_pushed = 0;
  245. #endif
  246. uint32 argc1, *argv1 = NULL, cell_num = 0, j, k = 0;
  247. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  248. uint32 param_size_in_double_world = 0, result_size_in_double_world = 0;
  249. #endif
  250. int32 i, p, module_type;
  251. uint64 total_size;
  252. const char *exception;
  253. char buf[128];
  254. bh_assert(argc >= 0);
  255. LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc);
  256. if (!(target_func =
  257. wasm_runtime_lookup_function(module_inst, name, NULL))) {
  258. snprintf(buf, sizeof(buf), "lookup function %s failed", name);
  259. wasm_runtime_set_exception(module_inst, buf);
  260. goto fail;
  261. }
  262. module_type = module_inst->module_type;
  263. type = wasm_runtime_get_function_type(target_func, module_type);
  264. if (!type) {
  265. LOG_ERROR("invalid module instance type");
  266. return false;
  267. }
  268. if (type->param_count != (uint32)argc) {
  269. wasm_runtime_set_exception(module_inst, "invalid input argument count");
  270. goto fail;
  271. }
  272. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  273. if (!exec_env) {
  274. wasm_runtime_set_exception(module_inst,
  275. "create singleton exec_env failed");
  276. goto fail;
  277. }
  278. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  279. for (i = 0; i < type->param_count; i++) {
  280. param_size_in_double_world +=
  281. wasm_value_type_cell_num_outside(type->types[i]);
  282. }
  283. for (i = 0; i < type->result_count; i++) {
  284. result_size_in_double_world += wasm_value_type_cell_num_outside(
  285. type->types[type->param_count + i]);
  286. }
  287. argc1 = param_size_in_double_world;
  288. cell_num = (param_size_in_double_world >= result_size_in_double_world)
  289. ? param_size_in_double_world
  290. : result_size_in_double_world;
  291. #else
  292. argc1 = type->param_cell_num;
  293. cell_num = (argc1 > type->ret_cell_num) ? argc1 : type->ret_cell_num;
  294. #endif
  295. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  296. if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst, NULL, 0)))) {
  297. goto fail;
  298. }
  299. #if WASM_ENABLE_GC != 0
  300. ref_type_map = type->ref_type_maps;
  301. #endif
  302. /* Parse arguments */
  303. for (i = 0, p = 0; i < argc; i++) {
  304. char *endptr = NULL;
  305. bh_assert(argv[i] != NULL);
  306. if (argv[i][0] == '\0') {
  307. snprintf(buf, sizeof(buf), "invalid input argument %" PRId32, i);
  308. wasm_runtime_set_exception(module_inst, buf);
  309. goto fail;
  310. }
  311. switch (type->types[i]) {
  312. case VALUE_TYPE_I32:
  313. argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
  314. break;
  315. case VALUE_TYPE_I64:
  316. {
  317. union {
  318. uint64 val;
  319. uint32 parts[2];
  320. } u;
  321. u.val = strtoull(argv[i], &endptr, 0);
  322. argv1[p++] = u.parts[0];
  323. argv1[p++] = u.parts[1];
  324. break;
  325. }
  326. case VALUE_TYPE_F32:
  327. {
  328. float32 f32 = strtof(argv[i], &endptr);
  329. if (isnan(f32)) {
  330. if (argv[i][0] == '-') {
  331. union ieee754_float u;
  332. u.f = f32;
  333. if (is_little_endian())
  334. u.ieee.ieee_little_endian.negative = 1;
  335. else
  336. u.ieee.ieee_big_endian.negative = 1;
  337. bh_memcpy_s(&f32, sizeof(float), &u.f, sizeof(float));
  338. }
  339. if (endptr[0] == ':') {
  340. uint32 sig;
  341. union ieee754_float u;
  342. sig = (uint32)strtoul(endptr + 1, &endptr, 0);
  343. u.f = f32;
  344. if (is_little_endian())
  345. u.ieee.ieee_little_endian.mantissa = sig;
  346. else
  347. u.ieee.ieee_big_endian.mantissa = sig;
  348. bh_memcpy_s(&f32, sizeof(float), &u.f, sizeof(float));
  349. }
  350. }
  351. bh_memcpy_s(&argv1[p], (uint32)total_size - p, &f32,
  352. (uint32)sizeof(float));
  353. p++;
  354. break;
  355. }
  356. case VALUE_TYPE_F64:
  357. {
  358. union {
  359. float64 val;
  360. uint32 parts[2];
  361. } u;
  362. u.val = strtod(argv[i], &endptr);
  363. if (isnan(u.val)) {
  364. if (argv[i][0] == '-') {
  365. union ieee754_double ud;
  366. ud.d = u.val;
  367. if (is_little_endian())
  368. ud.ieee.ieee_little_endian.negative = 1;
  369. else
  370. ud.ieee.ieee_big_endian.negative = 1;
  371. bh_memcpy_s(&u.val, sizeof(double), &ud.d,
  372. sizeof(double));
  373. }
  374. if (endptr[0] == ':') {
  375. uint64 sig;
  376. union ieee754_double ud;
  377. sig = strtoull(endptr + 1, &endptr, 0);
  378. ud.d = u.val;
  379. if (is_little_endian()) {
  380. ud.ieee.ieee_little_endian.mantissa0 = sig >> 32;
  381. ud.ieee.ieee_little_endian.mantissa1 = (uint32)sig;
  382. }
  383. else {
  384. ud.ieee.ieee_big_endian.mantissa0 = sig >> 32;
  385. ud.ieee.ieee_big_endian.mantissa1 = (uint32)sig;
  386. }
  387. bh_memcpy_s(&u.val, sizeof(double), &ud.d,
  388. sizeof(double));
  389. }
  390. }
  391. argv1[p++] = u.parts[0];
  392. argv1[p++] = u.parts[1];
  393. break;
  394. }
  395. #if WASM_ENABLE_SIMD != 0
  396. case VALUE_TYPE_V128:
  397. {
  398. /* it likes 0x123\0x234 or 123\234 */
  399. /* retrive first i64 */
  400. *(uint64 *)(argv1 + p) = strtoull(argv[i], &endptr, 0);
  401. /* skip \ */
  402. endptr++;
  403. /* retrive second i64 */
  404. *(uint64 *)(argv1 + p + 2) = strtoull(endptr, &endptr, 0);
  405. p += 4;
  406. break;
  407. }
  408. #endif /* WASM_ENABLE_SIMD != 0 */
  409. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  410. case VALUE_TYPE_FUNCREF:
  411. #if UINTPTR_MAX == UINT32_MAX
  412. case VALUE_TYPE_EXTERNREF:
  413. #endif
  414. {
  415. if (strncasecmp(argv[i], "null", 4) == 0) {
  416. argv1[p++] = (uint32)-1;
  417. }
  418. else {
  419. argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
  420. }
  421. break;
  422. }
  423. #if UINTPTR_MAX == UINT64_MAX
  424. case VALUE_TYPE_EXTERNREF:
  425. {
  426. union {
  427. uintptr_t val;
  428. uint32 parts[2];
  429. } u;
  430. if (strncasecmp(argv[i], "null", 4) == 0) {
  431. u.val = (uintptr_t)-1LL;
  432. }
  433. else {
  434. u.val = strtoull(argv[i], &endptr, 0);
  435. }
  436. argv1[p++] = u.parts[0];
  437. argv1[p++] = u.parts[1];
  438. break;
  439. }
  440. #endif
  441. #endif /* WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 */
  442. default:
  443. #if WASM_ENABLE_GC != 0
  444. if (wasm_is_type_reftype(type->types[i])) {
  445. if (strncasecmp(argv[i], "null", 4) == 0) {
  446. PUT_REF_TO_ADDR(argv1 + p, NULL_REF);
  447. p += REF_CELL_NUM;
  448. break;
  449. }
  450. else if (type->types[i] == VALUE_TYPE_EXTERNREF) {
  451. WASMExternrefObjectRef gc_obj;
  452. void *gc_heap_handle = NULL;
  453. void *extern_obj =
  454. (void *)(uintptr_t)strtoull(argv[i], &endptr, 0);
  455. #if WASM_ENABLE_INTERP != 0
  456. if (module_inst->module_type == Wasm_Module_Bytecode)
  457. gc_heap_handle = ((WASMModuleInstance *)module_inst)
  458. ->e->gc_heap_handle;
  459. #endif
  460. #if WASM_ENABLE_AOT != 0
  461. if (module_inst->module_type == Wasm_Module_AoT)
  462. gc_heap_handle = NULL; /* TODO */
  463. #endif
  464. bh_assert(gc_heap_handle);
  465. gc_obj = wasm_externref_obj_new(
  466. exec_env, gc_heap_handle, extern_obj);
  467. if (!gc_obj) {
  468. wasm_runtime_set_exception(
  469. module_inst, "create extern object failed");
  470. goto fail;
  471. }
  472. if (!(local_ref =
  473. runtime_malloc(sizeof(WASMLocalObjectRef),
  474. module_inst, NULL, 0))) {
  475. goto fail;
  476. }
  477. wasm_runtime_push_local_object_ref(exec_env, local_ref);
  478. local_ref->val = (WASMObjectRef)gc_obj;
  479. num_local_ref_pushed++;
  480. PUT_REF_TO_ADDR(argv1 + p, gc_obj);
  481. p += REF_CELL_NUM;
  482. }
  483. if (wasm_is_type_multi_byte_type(
  484. type->types[type->param_count + i]))
  485. ref_type_map++;
  486. break;
  487. }
  488. #endif /* end of WASM_ENABLE_GC != 0 */
  489. bh_assert(0);
  490. break;
  491. }
  492. if (endptr && *endptr != '\0' && *endptr != '_') {
  493. snprintf(buf, sizeof(buf), "invalid input argument %" PRId32 ": %s",
  494. i, argv[i]);
  495. wasm_runtime_set_exception(module_inst, buf);
  496. goto fail;
  497. }
  498. }
  499. wasm_runtime_set_exception(module_inst, NULL);
  500. #if !(WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0)
  501. bh_assert(p == (int32)argc1);
  502. #endif
  503. if (!wasm_runtime_call_wasm(exec_env, target_func, argc1, argv1)) {
  504. goto fail;
  505. }
  506. #if WASM_ENABLE_GC != 0
  507. ref_type_map = type->result_ref_type_maps;
  508. #endif
  509. /* print return value */
  510. for (j = 0; j < type->result_count; j++) {
  511. switch (type->types[type->param_count + j]) {
  512. case VALUE_TYPE_I32:
  513. {
  514. os_printf("0x%" PRIx32 ":i32", argv1[k]);
  515. k++;
  516. break;
  517. }
  518. case VALUE_TYPE_I64:
  519. {
  520. union {
  521. uint64 val;
  522. uint32 parts[2];
  523. } u;
  524. u.parts[0] = argv1[k];
  525. u.parts[1] = argv1[k + 1];
  526. k += 2;
  527. os_printf("0x%" PRIx64 ":i64", u.val);
  528. break;
  529. }
  530. case VALUE_TYPE_F32:
  531. {
  532. os_printf("%.7g:f32", *(float32 *)(argv1 + k));
  533. k++;
  534. break;
  535. }
  536. case VALUE_TYPE_F64:
  537. {
  538. union {
  539. float64 val;
  540. uint32 parts[2];
  541. } u;
  542. u.parts[0] = argv1[k];
  543. u.parts[1] = argv1[k + 1];
  544. k += 2;
  545. os_printf("%.7g:f64", u.val);
  546. break;
  547. }
  548. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  549. case VALUE_TYPE_FUNCREF:
  550. {
  551. if (argv1[k] != NULL_REF)
  552. os_printf("%" PRIu32 ":ref.func", argv1[k]);
  553. else
  554. os_printf("func:ref.null");
  555. k++;
  556. break;
  557. }
  558. case VALUE_TYPE_EXTERNREF:
  559. {
  560. #if UINTPTR_MAX == UINT32_MAX
  561. if (argv1[k] != 0 && argv1[k] != (uint32)-1)
  562. os_printf("%p:ref.extern", (void *)argv1[k]);
  563. else
  564. os_printf("extern:ref.null");
  565. k++;
  566. #else
  567. union {
  568. uintptr_t val;
  569. uint32 parts[2];
  570. } u;
  571. u.parts[0] = argv1[k];
  572. u.parts[1] = argv1[k + 1];
  573. k += 2;
  574. if (u.val && u.val != (uintptr_t)-1LL)
  575. os_printf("%p:ref.extern", (void *)u.val);
  576. else
  577. os_printf("extern:ref.null");
  578. #endif
  579. break;
  580. }
  581. #endif /* end of WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 */
  582. #if WASM_ENABLE_SIMD != 0
  583. case VALUE_TYPE_V128:
  584. {
  585. uint64 *v = (uint64 *)(argv1 + k);
  586. os_printf("<0x%016" PRIx64 " 0x%016" PRIx64 ">:v128", *v,
  587. *(v + 1));
  588. k += 4;
  589. break;
  590. }
  591. #endif /* WASM_ENABLE_SIMD != 0 */
  592. default:
  593. {
  594. #if WASM_ENABLE_GC != 0
  595. if (wasm_is_type_reftype(type->types[type->param_count + j])) {
  596. void *gc_obj = GET_REF_FROM_ADDR(argv1 + k);
  597. k += REF_CELL_NUM;
  598. if (!gc_obj) {
  599. uint8 type1 = type->types[type->param_count + j];
  600. WASMRefType *ref_type1 = NULL;
  601. WASMType **types = NULL;
  602. uint32 type_count = 0;
  603. if (wasm_is_type_multi_byte_type(
  604. type->types[type->param_count + j]))
  605. ref_type1 = ref_type_map->ref_type;
  606. #if WASM_ENABLE_INTERP != 0
  607. if (module_inst->module_type == Wasm_Module_Bytecode) {
  608. WASMModule *module =
  609. ((WASMModuleInstance *)module_inst)->module;
  610. types = module->types;
  611. type_count = module->type_count;
  612. }
  613. #endif
  614. if (wasm_reftype_is_subtype_of(type1, ref_type1,
  615. REF_TYPE_ANYREF, NULL,
  616. types, type_count))
  617. os_printf("any:");
  618. else if (wasm_reftype_is_subtype_of(
  619. type1, ref_type1, REF_TYPE_FUNCREF, NULL,
  620. types, type_count))
  621. os_printf("func:");
  622. if (wasm_reftype_is_subtype_of(type1, ref_type1,
  623. REF_TYPE_EXTERNREF, NULL,
  624. types, type_count))
  625. os_printf("extern:");
  626. os_printf("ref.null");
  627. }
  628. else if (wasm_obj_is_func_obj(gc_obj))
  629. os_printf("ref.func");
  630. else if (wasm_obj_is_externref_obj(gc_obj)) {
  631. WASMObjectRef obj = wasm_externref_obj_to_internal_obj(
  632. (WASMExternrefObjectRef)gc_obj);
  633. if (wasm_obj_is_anyref_obj(obj))
  634. os_printf("0x%" PRIxPTR ":ref.extern",
  635. wasm_anyref_obj_get_value(
  636. (WASMAnyrefObjectRef)obj));
  637. else
  638. os_printf("ref.extern");
  639. }
  640. else if (wasm_obj_is_i31_obj(gc_obj))
  641. os_printf("ref.i31");
  642. else if (wasm_obj_is_array_obj(gc_obj))
  643. os_printf("ref.array");
  644. else if (wasm_obj_is_struct_obj(gc_obj))
  645. os_printf("ref.struct");
  646. else if (wasm_obj_is_eq_obj(gc_obj))
  647. os_printf("ref.eq");
  648. else if (wasm_obj_is_anyref_obj(gc_obj))
  649. os_printf("0x%" PRIxPTR ":ref.host",
  650. wasm_anyref_obj_get_value(
  651. (WASMAnyrefObjectRef)gc_obj));
  652. else if (wasm_obj_is_internal_obj(gc_obj))
  653. os_printf("ref.any");
  654. if (wasm_is_type_multi_byte_type(
  655. type->types[type->param_count + j]))
  656. ref_type_map++;
  657. break;
  658. }
  659. #endif /* endof WASM_ENABLE_GC != 0 */
  660. bh_assert(0);
  661. break;
  662. }
  663. }
  664. if (j < (uint32)(type->result_count - 1))
  665. os_printf(",");
  666. }
  667. os_printf("\n");
  668. #if WASM_ENABLE_GC != 0
  669. for (j = 0; j < num_local_ref_pushed; j++) {
  670. local_ref = wasm_runtime_pop_local_object_ref(exec_env);
  671. wasm_runtime_free(local_ref);
  672. }
  673. #endif
  674. wasm_runtime_free(argv1);
  675. return true;
  676. fail:
  677. if (argv1)
  678. wasm_runtime_free(argv1);
  679. #if WASM_ENABLE_GC != 0
  680. for (j = 0; j < num_local_ref_pushed; j++) {
  681. local_ref = wasm_runtime_pop_local_object_ref(exec_env);
  682. wasm_runtime_free(local_ref);
  683. }
  684. #endif
  685. exception = wasm_runtime_get_exception(module_inst);
  686. bh_assert(exception);
  687. os_printf("%s\n", exception);
  688. return false;
  689. }
  690. bool
  691. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  692. const char *name, int32 argc, char *argv[])
  693. {
  694. bool ret;
  695. #if WASM_ENABLE_MEMORY_PROFILING != 0
  696. WASMExecEnv *exec_env;
  697. #endif
  698. ret = execute_func(module_inst, name, argc, argv);
  699. #if WASM_ENABLE_MEMORY_PROFILING != 0
  700. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  701. if (exec_env) {
  702. wasm_runtime_dump_mem_consumption(exec_env);
  703. }
  704. #endif
  705. #if WASM_ENABLE_PERF_PROFILING != 0
  706. wasm_runtime_dump_perf_profiling(module_inst);
  707. #endif
  708. return (ret && !wasm_runtime_get_exception(module_inst)) ? true : false;
  709. }