wasm_application.c 26 KB

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