wasm_application.c 27 KB

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