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) || (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. #if WASM_ENABLE_GC != 0
  453. if (wasm_is_type_reftype(type->types[i])) {
  454. if (strncasecmp(argv[i], "null", 4) == 0) {
  455. PUT_REF_TO_ADDR(argv1 + p, NULL_REF);
  456. p += REF_CELL_NUM;
  457. break;
  458. }
  459. else if (type->types[i] == VALUE_TYPE_EXTERNREF) {
  460. WASMExternrefObjectRef gc_obj;
  461. void *extern_obj =
  462. (void *)(uintptr_t)strtoull(argv[i], &endptr, 0);
  463. gc_obj = wasm_externref_obj_new(exec_env, extern_obj);
  464. if (!gc_obj) {
  465. wasm_runtime_set_exception(
  466. module_inst, "create extern object failed");
  467. goto fail;
  468. }
  469. if (!(local_ref =
  470. runtime_malloc(sizeof(WASMLocalObjectRef),
  471. module_inst, NULL, 0))) {
  472. goto fail;
  473. }
  474. wasm_runtime_push_local_object_ref(exec_env, local_ref);
  475. local_ref->val = (WASMObjectRef)gc_obj;
  476. num_local_ref_pushed++;
  477. PUT_REF_TO_ADDR(argv1 + p, gc_obj);
  478. p += REF_CELL_NUM;
  479. }
  480. if (wasm_is_type_multi_byte_type(
  481. type->types[type->param_count + i]))
  482. ref_type_map++;
  483. break;
  484. }
  485. #endif /* end of WASM_ENABLE_GC != 0 */
  486. bh_assert(0);
  487. break;
  488. }
  489. if (endptr && *endptr != '\0' && *endptr != '_') {
  490. snprintf(buf, sizeof(buf), "invalid input argument %" PRId32 ": %s",
  491. i, argv[i]);
  492. wasm_runtime_set_exception(module_inst, buf);
  493. goto fail;
  494. }
  495. }
  496. wasm_runtime_set_exception(module_inst, NULL);
  497. #if !(WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0)
  498. bh_assert(p == (int32)argc1);
  499. #endif
  500. if (!wasm_runtime_call_wasm(exec_env, target_func, argc1, argv1)) {
  501. goto fail;
  502. }
  503. #if WASM_ENABLE_GC != 0
  504. ref_type_map = type->result_ref_type_maps;
  505. #endif
  506. /* print return value */
  507. for (j = 0; j < type->result_count; j++) {
  508. switch (type->types[type->param_count + j]) {
  509. case VALUE_TYPE_I32:
  510. {
  511. os_printf("0x%" PRIx32 ":i32", argv1[k]);
  512. k++;
  513. break;
  514. }
  515. case VALUE_TYPE_I64:
  516. {
  517. union {
  518. uint64 val;
  519. uint32 parts[2];
  520. } u;
  521. u.parts[0] = argv1[k];
  522. u.parts[1] = argv1[k + 1];
  523. k += 2;
  524. os_printf("0x%" PRIx64 ":i64", u.val);
  525. break;
  526. }
  527. case VALUE_TYPE_F32:
  528. {
  529. os_printf("%.7g:f32", *(float32 *)(argv1 + k));
  530. k++;
  531. break;
  532. }
  533. case VALUE_TYPE_F64:
  534. {
  535. union {
  536. float64 val;
  537. uint32 parts[2];
  538. } u;
  539. u.parts[0] = argv1[k];
  540. u.parts[1] = argv1[k + 1];
  541. k += 2;
  542. os_printf("%.7g:f64", u.val);
  543. break;
  544. }
  545. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  546. case VALUE_TYPE_FUNCREF:
  547. {
  548. if (argv1[k] != NULL_REF)
  549. os_printf("%" PRIu32 ":ref.func", argv1[k]);
  550. else
  551. os_printf("func:ref.null");
  552. k++;
  553. break;
  554. }
  555. case VALUE_TYPE_EXTERNREF:
  556. {
  557. #if UINTPTR_MAX == UINT32_MAX
  558. if (argv1[k] != 0 && argv1[k] != (uint32)-1)
  559. os_printf("%p:ref.extern", (void *)argv1[k]);
  560. else
  561. os_printf("extern:ref.null");
  562. k++;
  563. #else
  564. union {
  565. uintptr_t val;
  566. uint32 parts[2];
  567. } u;
  568. u.parts[0] = argv1[k];
  569. u.parts[1] = argv1[k + 1];
  570. k += 2;
  571. if (u.val && u.val != (uintptr_t)-1LL)
  572. os_printf("%p:ref.extern", (void *)u.val);
  573. else
  574. os_printf("extern:ref.null");
  575. #endif
  576. break;
  577. }
  578. #endif /* end of WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 */
  579. #if WASM_ENABLE_SIMD != 0
  580. case VALUE_TYPE_V128:
  581. {
  582. uint64 *v = (uint64 *)(argv1 + k);
  583. os_printf("<0x%016" PRIx64 " 0x%016" PRIx64 ">:v128", *v,
  584. *(v + 1));
  585. k += 4;
  586. break;
  587. }
  588. #endif /* WASM_ENABLE_SIMD != 0 */
  589. default:
  590. {
  591. #if WASM_ENABLE_GC != 0
  592. if (wasm_is_type_reftype(type->types[type->param_count + j])) {
  593. void *gc_obj = GET_REF_FROM_ADDR(argv1 + k);
  594. k += REF_CELL_NUM;
  595. if (!gc_obj) {
  596. uint8 type1 = type->types[type->param_count + j];
  597. WASMRefType *ref_type1 = NULL;
  598. WASMType **types = NULL;
  599. uint32 type_count = 0;
  600. if (wasm_is_type_multi_byte_type(
  601. type->types[type->param_count + j]))
  602. ref_type1 = ref_type_map->ref_type;
  603. #if WASM_ENABLE_INTERP != 0
  604. if (module_inst->module_type == Wasm_Module_Bytecode) {
  605. WASMModule *module =
  606. ((WASMModuleInstance *)module_inst)->module;
  607. types = module->types;
  608. type_count = module->type_count;
  609. }
  610. #endif
  611. if (wasm_reftype_is_subtype_of(type1, ref_type1,
  612. REF_TYPE_ANYREF, NULL,
  613. types, type_count))
  614. os_printf("any:");
  615. else if (wasm_reftype_is_subtype_of(
  616. type1, ref_type1, REF_TYPE_FUNCREF, NULL,
  617. types, type_count))
  618. os_printf("func:");
  619. if (wasm_reftype_is_subtype_of(type1, ref_type1,
  620. REF_TYPE_EXTERNREF, NULL,
  621. types, type_count))
  622. os_printf("extern:");
  623. os_printf("ref.null");
  624. }
  625. else if (wasm_obj_is_func_obj(gc_obj))
  626. os_printf("ref.func");
  627. else if (wasm_obj_is_externref_obj(gc_obj)) {
  628. WASMObjectRef obj = wasm_externref_obj_to_internal_obj(
  629. (WASMExternrefObjectRef)gc_obj);
  630. if (wasm_obj_is_anyref_obj(obj))
  631. os_printf("0x%" PRIxPTR ":ref.extern",
  632. (uintptr_t)wasm_anyref_obj_get_value(
  633. (WASMAnyrefObjectRef)obj));
  634. else
  635. os_printf("ref.extern");
  636. }
  637. else if (wasm_obj_is_i31_obj(gc_obj))
  638. os_printf("ref.i31");
  639. else if (wasm_obj_is_array_obj(gc_obj))
  640. os_printf("ref.array");
  641. else if (wasm_obj_is_struct_obj(gc_obj))
  642. os_printf("ref.struct");
  643. else if (wasm_obj_is_eq_obj(gc_obj))
  644. os_printf("ref.eq");
  645. else if (wasm_obj_is_anyref_obj(gc_obj))
  646. os_printf("0x%" PRIxPTR ":ref.host",
  647. (uintptr_t)wasm_anyref_obj_get_value(
  648. (WASMAnyrefObjectRef)gc_obj));
  649. else if (wasm_obj_is_internal_obj(gc_obj))
  650. os_printf("ref.any");
  651. if (wasm_is_type_multi_byte_type(
  652. type->types[type->param_count + j]))
  653. ref_type_map++;
  654. break;
  655. }
  656. #endif /* endof WASM_ENABLE_GC != 0 */
  657. bh_assert(0);
  658. break;
  659. }
  660. }
  661. if (j < (uint32)(type->result_count - 1))
  662. os_printf(",");
  663. }
  664. os_printf("\n");
  665. #if WASM_ENABLE_GC != 0
  666. for (j = 0; j < num_local_ref_pushed; j++) {
  667. local_ref = wasm_runtime_pop_local_object_ref(exec_env);
  668. wasm_runtime_free(local_ref);
  669. }
  670. #endif
  671. wasm_runtime_free(argv1);
  672. return true;
  673. fail:
  674. if (argv1)
  675. wasm_runtime_free(argv1);
  676. #if WASM_ENABLE_GC != 0
  677. for (j = 0; j < num_local_ref_pushed; j++) {
  678. local_ref = wasm_runtime_pop_local_object_ref(exec_env);
  679. wasm_runtime_free(local_ref);
  680. }
  681. #endif
  682. exception = wasm_runtime_get_exception(module_inst);
  683. bh_assert(exception);
  684. os_printf("%s\n", exception);
  685. return false;
  686. }
  687. bool
  688. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  689. const char *name, int32 argc, char *argv[])
  690. {
  691. bool ret;
  692. #if WASM_ENABLE_MEMORY_PROFILING != 0
  693. WASMExecEnv *exec_env;
  694. #endif
  695. ret = execute_func(module_inst, name, argc, argv);
  696. #if WASM_ENABLE_MEMORY_PROFILING != 0
  697. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  698. if (exec_env) {
  699. wasm_runtime_dump_mem_consumption(exec_env);
  700. }
  701. #endif
  702. #if WASM_ENABLE_PERF_PROFILING != 0
  703. wasm_runtime_dump_perf_profiling(module_inst);
  704. #endif
  705. return (ret && !wasm_runtime_get_exception(module_inst)) ? true : false;
  706. }