wasm_application.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. #if WASM_ENABLE_STRINGREF != 0
  18. #include "string_object.h"
  19. #endif
  20. #if WASM_ENABLE_GC_PERF_PROFILING != 0
  21. #include "../../shared/mem-alloc/mem_alloc.h"
  22. #endif
  23. #endif
  24. static void
  25. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  26. {
  27. if (error_buf != NULL)
  28. snprintf(error_buf, error_buf_size, "%s", string);
  29. }
  30. static void *
  31. runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst,
  32. char *error_buf, uint32 error_buf_size)
  33. {
  34. void *mem;
  35. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  36. if (module_inst != NULL) {
  37. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  38. }
  39. else if (error_buf != NULL) {
  40. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  41. }
  42. return NULL;
  43. }
  44. memset(mem, 0, (uint32)size);
  45. return mem;
  46. }
  47. static union {
  48. int a;
  49. char b;
  50. } __ue = { .a = 1 };
  51. #define is_little_endian() (__ue.b == 1) /* NOLINT */
  52. /**
  53. * Implementation of wasm_application_execute_main()
  54. */
  55. static bool
  56. check_main_func_type(const WASMFuncType *type)
  57. {
  58. if (!(type->param_count == 0 || type->param_count == 2)
  59. || type->result_count > 1) {
  60. LOG_ERROR(
  61. "WASM execute application failed: invalid main function type.\n");
  62. return false;
  63. }
  64. if (type->param_count == 2
  65. && !(type->types[0] == VALUE_TYPE_I32
  66. && type->types[1] == VALUE_TYPE_I32)) {
  67. LOG_ERROR(
  68. "WASM execute application failed: invalid main function type.\n");
  69. return false;
  70. }
  71. if (type->result_count
  72. && type->types[type->param_count] != VALUE_TYPE_I32) {
  73. LOG_ERROR(
  74. "WASM execute application failed: invalid main function type.\n");
  75. return false;
  76. }
  77. return true;
  78. }
  79. static bool
  80. execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
  81. {
  82. WASMFunctionInstanceCommon *func;
  83. WASMFuncType *func_type = NULL;
  84. WASMExecEnv *exec_env = NULL;
  85. uint32 argc1 = 0, argv1[2] = { 0 };
  86. uint32 total_argv_size = 0;
  87. uint64 total_size;
  88. uint32 argv_buf_offset = 0;
  89. int32 i;
  90. char *argv_buf, *p, *p_end;
  91. uint32 *argv_offsets, module_type;
  92. bool ret, is_import_func = true;
  93. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  94. if (!exec_env) {
  95. wasm_runtime_set_exception(module_inst,
  96. "create singleton exec_env failed");
  97. return false;
  98. }
  99. #if WASM_ENABLE_LIBC_WASI != 0
  100. /* In wasi mode, we should call the function named "_start"
  101. which initializes the wasi envrionment and then calls
  102. the actual main function. Directly calling main function
  103. may cause exception thrown. */
  104. if ((func = wasm_runtime_lookup_wasi_start_function(module_inst))) {
  105. const char *wasi_proc_exit_exception = "wasi proc exit";
  106. ret = wasm_runtime_call_wasm(exec_env, func, 0, NULL);
  107. #if WASM_ENABLE_THREAD_MGR != 0
  108. if (ret) {
  109. /* On a successful return from the `_start` function,
  110. we terminate other threads by mimicing wasi:proc_exit(0).
  111. Note:
  112. - A return from the `main` function is an equivalent of
  113. exit(). (C standard)
  114. - When exit code is 0, wasi-libc's `_start` function just
  115. returns w/o calling `proc_exit`.
  116. - A process termination should terminate threads in
  117. the process. */
  118. wasm_runtime_set_exception(module_inst, wasi_proc_exit_exception);
  119. /* exit_code is zero-initialized */
  120. ret = false;
  121. }
  122. #endif
  123. /* report wasm proc exit as a success */
  124. WASMModuleInstance *inst = (WASMModuleInstance *)module_inst;
  125. if (!ret && strstr(inst->cur_exception, wasi_proc_exit_exception)) {
  126. inst->cur_exception[0] = 0;
  127. ret = true;
  128. }
  129. return ret;
  130. }
  131. #endif /* end of WASM_ENABLE_LIBC_WASI */
  132. if (!(func = wasm_runtime_lookup_function(module_inst, "main", NULL))
  133. && !(func = wasm_runtime_lookup_function(module_inst,
  134. "__main_argc_argv", NULL))
  135. && !(func = wasm_runtime_lookup_function(module_inst, "_main", NULL))) {
  136. #if WASM_ENABLE_LIBC_WASI != 0
  137. wasm_runtime_set_exception(
  138. module_inst, "lookup the entry point symbol (like _start, main, "
  139. "_main, __main_argc_argv) failed");
  140. #else
  141. wasm_runtime_set_exception(module_inst,
  142. "lookup the entry point symbol (like main, "
  143. "_main, __main_argc_argv) failed");
  144. #endif
  145. return false;
  146. }
  147. #if WASM_ENABLE_INTERP != 0
  148. if (module_inst->module_type == Wasm_Module_Bytecode) {
  149. is_import_func = ((WASMFunctionInstance *)func)->is_import_func;
  150. }
  151. #endif
  152. #if WASM_ENABLE_AOT != 0
  153. if (module_inst->module_type == Wasm_Module_AoT) {
  154. is_import_func = ((AOTFunctionInstance *)func)->is_import_func;
  155. }
  156. #endif
  157. if (is_import_func) {
  158. wasm_runtime_set_exception(module_inst, "lookup main function failed");
  159. return false;
  160. }
  161. module_type = module_inst->module_type;
  162. func_type = wasm_runtime_get_function_type(func, module_type);
  163. if (!func_type) {
  164. LOG_ERROR("invalid module instance type");
  165. return false;
  166. }
  167. if (!check_main_func_type(func_type)) {
  168. wasm_runtime_set_exception(module_inst,
  169. "invalid function type of main function");
  170. return false;
  171. }
  172. if (func_type->param_count) {
  173. for (i = 0; i < argc; i++)
  174. total_argv_size += (uint32)(strlen(argv[i]) + 1);
  175. total_argv_size = align_uint(total_argv_size, 4);
  176. total_size = (uint64)total_argv_size + sizeof(int32) * (uint64)argc;
  177. if (total_size >= UINT32_MAX
  178. || !(argv_buf_offset = wasm_runtime_module_malloc(
  179. module_inst, (uint32)total_size, (void **)&argv_buf))) {
  180. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  181. return false;
  182. }
  183. p = argv_buf;
  184. argv_offsets = (uint32 *)(p + total_argv_size);
  185. p_end = p + total_size;
  186. for (i = 0; i < argc; i++) {
  187. bh_memcpy_s(p, (uint32)(p_end - p), argv[i],
  188. (uint32)(strlen(argv[i]) + 1));
  189. argv_offsets[i] = argv_buf_offset + (uint32)(p - argv_buf);
  190. p += strlen(argv[i]) + 1;
  191. }
  192. argc1 = 2;
  193. argv1[0] = (uint32)argc;
  194. argv1[1] =
  195. (uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
  196. }
  197. ret = wasm_runtime_call_wasm(exec_env, func, argc1, argv1);
  198. if (ret && func_type->result_count > 0 && argc > 0 && argv)
  199. /* copy the return value */
  200. *(int *)argv = (int)argv1[0];
  201. if (argv_buf_offset)
  202. wasm_runtime_module_free(module_inst, argv_buf_offset);
  203. return ret;
  204. }
  205. bool
  206. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
  207. char *argv[])
  208. {
  209. bool ret;
  210. #if (WASM_ENABLE_MEMORY_PROFILING != 0)
  211. WASMExecEnv *exec_env;
  212. #endif
  213. ret = execute_main(module_inst, argc, argv);
  214. #if WASM_ENABLE_MEMORY_PROFILING != 0
  215. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  216. if (exec_env) {
  217. wasm_runtime_dump_mem_consumption(exec_env);
  218. }
  219. #endif
  220. #if WASM_ENABLE_GC_PERF_PROFILING != 0
  221. void *handle = wasm_runtime_get_gc_heap_handle(module_inst);
  222. mem_allocator_dump_perf_profiling(handle);
  223. #endif
  224. #if WASM_ENABLE_PERF_PROFILING != 0
  225. wasm_runtime_dump_perf_profiling(module_inst);
  226. #endif
  227. if (ret)
  228. ret = wasm_runtime_get_exception(module_inst) == NULL;
  229. return ret;
  230. }
  231. /**
  232. * Implementation of wasm_application_execute_func()
  233. */
  234. union ieee754_float {
  235. float f;
  236. /* This is the IEEE 754 single-precision format. */
  237. union {
  238. struct {
  239. unsigned int negative : 1;
  240. unsigned int exponent : 8;
  241. unsigned int mantissa : 23;
  242. } ieee_big_endian;
  243. struct {
  244. unsigned int mantissa : 23;
  245. unsigned int exponent : 8;
  246. unsigned int negative : 1;
  247. } ieee_little_endian;
  248. } ieee;
  249. };
  250. union ieee754_double {
  251. double d;
  252. /* This is the IEEE 754 double-precision format. */
  253. union {
  254. struct {
  255. unsigned int negative : 1;
  256. unsigned int exponent : 11;
  257. /* Together these comprise the mantissa. */
  258. unsigned int mantissa0 : 20;
  259. unsigned int mantissa1 : 32;
  260. } ieee_big_endian;
  261. struct {
  262. /* Together these comprise the mantissa. */
  263. unsigned int mantissa1 : 32;
  264. unsigned int mantissa0 : 20;
  265. unsigned int exponent : 11;
  266. unsigned int negative : 1;
  267. } ieee_little_endian;
  268. } ieee;
  269. };
  270. static bool
  271. execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
  272. int32 argc, char *argv[])
  273. {
  274. WASMFunctionInstanceCommon *target_func;
  275. WASMFuncType *type = NULL;
  276. WASMExecEnv *exec_env = NULL;
  277. #if WASM_ENABLE_GC != 0
  278. WASMRefTypeMap *ref_type_map;
  279. WASMLocalObjectRef *local_ref;
  280. uint32 num_local_ref_pushed = 0;
  281. #endif
  282. uint32 argc1, *argv1 = NULL, cell_num = 0, j, k = 0;
  283. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  284. uint32 param_size_in_double_world = 0, result_size_in_double_world = 0;
  285. #endif
  286. int32 i, p, module_type;
  287. uint64 total_size;
  288. char buf[128];
  289. bh_assert(argc >= 0);
  290. LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc);
  291. if (!(target_func =
  292. wasm_runtime_lookup_function(module_inst, name, NULL))) {
  293. snprintf(buf, sizeof(buf), "lookup function %s failed", name);
  294. wasm_runtime_set_exception(module_inst, buf);
  295. goto fail;
  296. }
  297. module_type = module_inst->module_type;
  298. type = wasm_runtime_get_function_type(target_func, module_type);
  299. if (!type) {
  300. LOG_ERROR("invalid module instance type");
  301. return false;
  302. }
  303. if (type->param_count != (uint32)argc) {
  304. wasm_runtime_set_exception(module_inst, "invalid input argument count");
  305. goto fail;
  306. }
  307. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  308. if (!exec_env) {
  309. wasm_runtime_set_exception(module_inst,
  310. "create singleton exec_env failed");
  311. goto fail;
  312. }
  313. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  314. for (i = 0; i < type->param_count; i++) {
  315. param_size_in_double_world +=
  316. wasm_value_type_cell_num_outside(type->types[i]);
  317. }
  318. for (i = 0; i < type->result_count; i++) {
  319. result_size_in_double_world += wasm_value_type_cell_num_outside(
  320. type->types[type->param_count + i]);
  321. }
  322. argc1 = param_size_in_double_world;
  323. cell_num = (param_size_in_double_world >= result_size_in_double_world)
  324. ? param_size_in_double_world
  325. : result_size_in_double_world;
  326. #else
  327. argc1 = type->param_cell_num;
  328. cell_num = (argc1 > type->ret_cell_num) ? argc1 : type->ret_cell_num;
  329. #endif
  330. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  331. if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst, NULL, 0)))) {
  332. goto fail;
  333. }
  334. #if WASM_ENABLE_GC != 0
  335. ref_type_map = type->ref_type_maps;
  336. #endif
  337. /* Parse arguments */
  338. for (i = 0, p = 0; i < argc; i++) {
  339. char *endptr = NULL;
  340. bh_assert(argv[i] != NULL);
  341. if (argv[i][0] == '\0') {
  342. snprintf(buf, sizeof(buf), "invalid input argument %" PRId32, i);
  343. wasm_runtime_set_exception(module_inst, buf);
  344. goto fail;
  345. }
  346. switch (type->types[i]) {
  347. case VALUE_TYPE_I32:
  348. argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
  349. break;
  350. case VALUE_TYPE_I64:
  351. {
  352. union {
  353. uint64 val;
  354. uint32 parts[2];
  355. } u;
  356. u.val = strtoull(argv[i], &endptr, 0);
  357. argv1[p++] = u.parts[0];
  358. argv1[p++] = u.parts[1];
  359. break;
  360. }
  361. case VALUE_TYPE_F32:
  362. {
  363. float32 f32 = strtof(argv[i], &endptr);
  364. if (isnan(f32)) {
  365. #ifdef _MSC_VER
  366. /*
  367. * Spec tests require the binary representation of NaN to be
  368. * 0x7fc00000 for float and 0x7ff8000000000000 for float;
  369. * however, in MSVC compiler, strtof doesn't return this
  370. * exact value, causing some of the spec test failures. We
  371. * use the value returned by nan/nanf as it is the one
  372. * expected by spec tests.
  373. *
  374. */
  375. f32 = nanf("");
  376. #endif
  377. if (argv[i][0] == '-') {
  378. union ieee754_float u;
  379. u.f = f32;
  380. if (is_little_endian())
  381. u.ieee.ieee_little_endian.negative = 1;
  382. else
  383. u.ieee.ieee_big_endian.negative = 1;
  384. bh_memcpy_s(&f32, sizeof(float), &u.f, sizeof(float));
  385. }
  386. if (endptr[0] == ':') {
  387. uint32 sig;
  388. union ieee754_float u;
  389. sig = (uint32)strtoul(endptr + 1, &endptr, 0);
  390. u.f = f32;
  391. if (is_little_endian())
  392. u.ieee.ieee_little_endian.mantissa = sig;
  393. else
  394. u.ieee.ieee_big_endian.mantissa = sig;
  395. bh_memcpy_s(&f32, sizeof(float), &u.f, sizeof(float));
  396. }
  397. }
  398. bh_memcpy_s(&argv1[p], (uint32)total_size - p, &f32,
  399. (uint32)sizeof(float));
  400. p++;
  401. break;
  402. }
  403. case VALUE_TYPE_F64:
  404. {
  405. union {
  406. float64 val;
  407. uint32 parts[2];
  408. } u;
  409. u.val = strtod(argv[i], &endptr);
  410. if (isnan(u.val)) {
  411. #ifdef _MSC_VER
  412. u.val = nan("");
  413. #endif
  414. if (argv[i][0] == '-') {
  415. union ieee754_double ud;
  416. ud.d = u.val;
  417. if (is_little_endian())
  418. ud.ieee.ieee_little_endian.negative = 1;
  419. else
  420. ud.ieee.ieee_big_endian.negative = 1;
  421. bh_memcpy_s(&u.val, sizeof(double), &ud.d,
  422. sizeof(double));
  423. }
  424. if (endptr[0] == ':') {
  425. uint64 sig;
  426. union ieee754_double ud;
  427. sig = strtoull(endptr + 1, &endptr, 0);
  428. ud.d = u.val;
  429. if (is_little_endian()) {
  430. ud.ieee.ieee_little_endian.mantissa0 = sig >> 32;
  431. ud.ieee.ieee_little_endian.mantissa1 = (uint32)sig;
  432. }
  433. else {
  434. ud.ieee.ieee_big_endian.mantissa0 = sig >> 32;
  435. ud.ieee.ieee_big_endian.mantissa1 = (uint32)sig;
  436. }
  437. bh_memcpy_s(&u.val, sizeof(double), &ud.d,
  438. sizeof(double));
  439. }
  440. }
  441. argv1[p++] = u.parts[0];
  442. argv1[p++] = u.parts[1];
  443. break;
  444. }
  445. #if WASM_ENABLE_SIMD != 0
  446. case VALUE_TYPE_V128:
  447. {
  448. /* it likes 0x123\0x234 or 123\234 */
  449. /* retrive first i64 */
  450. *(uint64 *)(argv1 + p) = strtoull(argv[i], &endptr, 0);
  451. /* skip \ */
  452. endptr++;
  453. /* retrive second i64 */
  454. *(uint64 *)(argv1 + p + 2) = strtoull(endptr, &endptr, 0);
  455. p += 4;
  456. break;
  457. }
  458. #endif /* WASM_ENABLE_SIMD != 0 */
  459. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  460. case VALUE_TYPE_FUNCREF:
  461. #if UINTPTR_MAX == UINT32_MAX
  462. case VALUE_TYPE_EXTERNREF:
  463. #endif
  464. {
  465. if (strncasecmp(argv[i], "null", 4) == 0) {
  466. argv1[p++] = (uint32)-1;
  467. }
  468. else {
  469. argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
  470. }
  471. break;
  472. }
  473. #if UINTPTR_MAX == UINT64_MAX
  474. case VALUE_TYPE_EXTERNREF:
  475. {
  476. union {
  477. uintptr_t val;
  478. uint32 parts[2];
  479. } u;
  480. if (strncasecmp(argv[i], "null", 4) == 0) {
  481. u.val = (uintptr_t)-1LL;
  482. }
  483. else {
  484. u.val = strtoull(argv[i], &endptr, 0);
  485. }
  486. argv1[p++] = u.parts[0];
  487. argv1[p++] = u.parts[1];
  488. break;
  489. }
  490. #endif
  491. #endif /* WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 */
  492. default:
  493. {
  494. #if WASM_ENABLE_GC != 0
  495. bool is_extern_ref = false;
  496. bool is_anyref = false;
  497. if (wasm_is_type_reftype(type->types[i])) {
  498. if (strncasecmp(argv[i], "null", 4) == 0) {
  499. PUT_REF_TO_ADDR(argv1 + p, NULL_REF);
  500. p += REF_CELL_NUM;
  501. break;
  502. }
  503. else if (type->types[i] == VALUE_TYPE_EXTERNREF) {
  504. is_extern_ref = true;
  505. }
  506. else if (type->types[i] == VALUE_TYPE_ANYREF) {
  507. is_anyref = true;
  508. }
  509. if (wasm_is_type_multi_byte_type(
  510. type->types[type->param_count + i])) {
  511. WASMRefType *ref_type = ref_type_map->ref_type;
  512. if (wasm_is_refheaptype_common(
  513. &ref_type->ref_ht_common)) {
  514. int32 heap_type = ref_type->ref_ht_common.heap_type;
  515. if (heap_type == HEAP_TYPE_EXTERN) {
  516. is_extern_ref = true;
  517. }
  518. else if (heap_type == HEAP_TYPE_ANY) {
  519. is_anyref = true;
  520. }
  521. }
  522. ref_type_map++;
  523. }
  524. if (is_extern_ref) {
  525. WASMExternrefObjectRef gc_obj;
  526. void *extern_obj =
  527. (void *)(uintptr_t)strtoull(argv[i], &endptr, 0);
  528. gc_obj = wasm_externref_obj_new(exec_env, extern_obj);
  529. if (!gc_obj) {
  530. wasm_runtime_set_exception(
  531. module_inst, "create extern object failed");
  532. goto fail;
  533. }
  534. if (!(local_ref =
  535. runtime_malloc(sizeof(WASMLocalObjectRef),
  536. module_inst, NULL, 0))) {
  537. goto fail;
  538. }
  539. wasm_runtime_push_local_obj_ref(exec_env, local_ref);
  540. local_ref->val = (WASMObjectRef)gc_obj;
  541. num_local_ref_pushed++;
  542. PUT_REF_TO_ADDR(argv1 + p, gc_obj);
  543. p += REF_CELL_NUM;
  544. }
  545. else if (is_anyref) {
  546. /* If a parameter type is (ref null? any) and its value
  547. * is not null, then we treat the value as host ptr */
  548. WASMAnyrefObjectRef gc_obj;
  549. void *host_obj =
  550. (void *)(uintptr_t)strtoull(argv[i], &endptr, 0);
  551. gc_obj = wasm_anyref_obj_new(exec_env, host_obj);
  552. if (!gc_obj) {
  553. wasm_runtime_set_exception(
  554. module_inst, "create anyref object failed");
  555. goto fail;
  556. }
  557. if (!(local_ref =
  558. runtime_malloc(sizeof(WASMLocalObjectRef),
  559. module_inst, NULL, 0))) {
  560. goto fail;
  561. }
  562. wasm_runtime_push_local_obj_ref(exec_env, local_ref);
  563. local_ref->val = (WASMObjectRef)gc_obj;
  564. num_local_ref_pushed++;
  565. PUT_REF_TO_ADDR(argv1 + p, gc_obj);
  566. p += REF_CELL_NUM;
  567. }
  568. break;
  569. }
  570. #endif /* end of WASM_ENABLE_GC != 0 */
  571. bh_assert(0);
  572. break;
  573. }
  574. }
  575. if (endptr && *endptr != '\0' && *endptr != '_') {
  576. snprintf(buf, sizeof(buf), "invalid input argument %" PRId32 ": %s",
  577. i, argv[i]);
  578. wasm_runtime_set_exception(module_inst, buf);
  579. goto fail;
  580. }
  581. }
  582. wasm_runtime_set_exception(module_inst, NULL);
  583. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  584. bh_assert(p == (int32)argc1);
  585. #endif
  586. if (!wasm_runtime_call_wasm(exec_env, target_func, argc1, argv1)) {
  587. goto fail;
  588. }
  589. #if WASM_ENABLE_GC != 0
  590. ref_type_map = type->result_ref_type_maps;
  591. #endif
  592. /* print return value */
  593. for (j = 0; j < type->result_count; j++) {
  594. switch (type->types[type->param_count + j]) {
  595. case VALUE_TYPE_I32:
  596. {
  597. os_printf("0x%" PRIx32 ":i32", argv1[k]);
  598. k++;
  599. break;
  600. }
  601. case VALUE_TYPE_I64:
  602. {
  603. union {
  604. uint64 val;
  605. uint32 parts[2];
  606. } u;
  607. u.parts[0] = argv1[k];
  608. u.parts[1] = argv1[k + 1];
  609. k += 2;
  610. os_printf("0x%" PRIx64 ":i64", u.val);
  611. break;
  612. }
  613. case VALUE_TYPE_F32:
  614. {
  615. os_printf("%.7g:f32", *(float32 *)(argv1 + k));
  616. k++;
  617. break;
  618. }
  619. case VALUE_TYPE_F64:
  620. {
  621. union {
  622. float64 val;
  623. uint32 parts[2];
  624. } u;
  625. u.parts[0] = argv1[k];
  626. u.parts[1] = argv1[k + 1];
  627. k += 2;
  628. os_printf("%.7g:f64", u.val);
  629. break;
  630. }
  631. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  632. case VALUE_TYPE_FUNCREF:
  633. {
  634. if (argv1[k] != NULL_REF)
  635. os_printf("%" PRIu32 ":ref.func", argv1[k]);
  636. else
  637. os_printf("func:ref.null");
  638. k++;
  639. break;
  640. }
  641. case VALUE_TYPE_EXTERNREF:
  642. {
  643. #if UINTPTR_MAX == UINT32_MAX
  644. if (argv1[k] != 0 && argv1[k] != (uint32)-1)
  645. os_printf("0x%" PRIxPTR ":ref.extern", (uintptr_t)argv1[k]);
  646. else
  647. os_printf("extern:ref.null");
  648. k++;
  649. #else
  650. union {
  651. uintptr_t val;
  652. uint32 parts[2];
  653. } u;
  654. u.parts[0] = argv1[k];
  655. u.parts[1] = argv1[k + 1];
  656. k += 2;
  657. if (u.val && u.val != (uintptr_t)-1LL)
  658. os_printf("0x%" PRIxPTR ":ref.extern", u.val);
  659. else
  660. os_printf("extern:ref.null");
  661. #endif
  662. break;
  663. }
  664. #endif /* end of WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 */
  665. #if WASM_ENABLE_SIMD != 0
  666. case VALUE_TYPE_V128:
  667. {
  668. uint64 *v = (uint64 *)(argv1 + k);
  669. os_printf("<0x%016" PRIx64 " 0x%016" PRIx64 ">:v128", *v,
  670. *(v + 1));
  671. k += 4;
  672. break;
  673. }
  674. #endif /* WASM_ENABLE_SIMD != 0 */
  675. default:
  676. {
  677. #if WASM_ENABLE_GC != 0
  678. if (wasm_is_type_reftype(type->types[type->param_count + j])) {
  679. void *gc_obj = GET_REF_FROM_ADDR(argv1 + k);
  680. k += REF_CELL_NUM;
  681. if (!gc_obj) {
  682. uint8 type1 = type->types[type->param_count + j];
  683. WASMRefType *ref_type1 = NULL;
  684. WASMType **types = NULL;
  685. uint32 type_count = 0;
  686. if (wasm_is_type_multi_byte_type(
  687. type->types[type->param_count + j]))
  688. ref_type1 = ref_type_map->ref_type;
  689. #if WASM_ENABLE_INTERP != 0
  690. if (module_inst->module_type == Wasm_Module_Bytecode) {
  691. WASMModule *module =
  692. ((WASMModuleInstance *)module_inst)->module;
  693. types = module->types;
  694. type_count = module->type_count;
  695. }
  696. #endif
  697. #if WASM_ENABLE_AOT != 0
  698. if (module_inst->module_type == Wasm_Module_AoT) {
  699. AOTModule *module =
  700. (AOTModule *)((AOTModuleInstance *)module_inst)
  701. ->module;
  702. types = module->types;
  703. type_count = module->type_count;
  704. }
  705. #endif
  706. bh_assert(type);
  707. if (wasm_reftype_is_subtype_of(type1, ref_type1,
  708. REF_TYPE_ANYREF, NULL,
  709. types, type_count))
  710. os_printf("any:");
  711. else if (wasm_reftype_is_subtype_of(
  712. type1, ref_type1, REF_TYPE_FUNCREF, NULL,
  713. types, type_count))
  714. os_printf("func:");
  715. if (wasm_reftype_is_subtype_of(type1, ref_type1,
  716. REF_TYPE_EXTERNREF, NULL,
  717. types, type_count))
  718. os_printf("extern:");
  719. os_printf("ref.null");
  720. }
  721. else if (wasm_obj_is_func_obj(gc_obj))
  722. os_printf("ref.func");
  723. #if WASM_ENABLE_STRINGREF != 0
  724. else if (wasm_obj_is_stringref_obj(gc_obj)
  725. || wasm_obj_is_stringview_wtf8_obj(gc_obj)) {
  726. wasm_string_dump(
  727. (WASMString)wasm_stringref_obj_get_value(gc_obj));
  728. }
  729. else if (wasm_obj_is_stringview_wtf16_obj(gc_obj)) {
  730. wasm_string_dump(
  731. (WASMString)wasm_stringview_wtf16_obj_get_value(
  732. gc_obj));
  733. }
  734. #endif
  735. else if (wasm_obj_is_externref_obj(gc_obj)) {
  736. #if WASM_ENABLE_SPEC_TEST != 0
  737. WASMObjectRef obj = wasm_externref_obj_to_internal_obj(
  738. (WASMExternrefObjectRef)gc_obj);
  739. if (wasm_obj_is_anyref_obj(obj))
  740. os_printf("0x%" PRIxPTR ":ref.extern",
  741. (uintptr_t)wasm_anyref_obj_get_value(
  742. (WASMAnyrefObjectRef)obj));
  743. else
  744. #endif
  745. os_printf("ref.extern");
  746. }
  747. else if (wasm_obj_is_i31_obj(gc_obj))
  748. os_printf("ref.i31");
  749. else if (wasm_obj_is_array_obj(gc_obj))
  750. os_printf("ref.array");
  751. else if (wasm_obj_is_struct_obj(gc_obj))
  752. os_printf("ref.struct");
  753. else if (wasm_obj_is_eq_obj(gc_obj))
  754. os_printf("ref.eq");
  755. else if (wasm_obj_is_anyref_obj(gc_obj))
  756. os_printf("0x%" PRIxPTR ":ref.host",
  757. (uintptr_t)wasm_anyref_obj_get_value(
  758. (WASMAnyrefObjectRef)gc_obj));
  759. else if (wasm_obj_is_internal_obj(gc_obj))
  760. os_printf("ref.any");
  761. if (wasm_is_type_multi_byte_type(
  762. type->types[type->param_count + j]))
  763. ref_type_map++;
  764. break;
  765. }
  766. #endif /* endof WASM_ENABLE_GC != 0 */
  767. bh_assert(0);
  768. break;
  769. }
  770. }
  771. if (j < (uint32)(type->result_count - 1))
  772. os_printf(",");
  773. }
  774. os_printf("\n");
  775. #if WASM_ENABLE_GC != 0
  776. for (j = 0; j < num_local_ref_pushed; j++) {
  777. local_ref = wasm_runtime_pop_local_obj_ref(exec_env);
  778. wasm_runtime_free(local_ref);
  779. }
  780. #endif
  781. wasm_runtime_free(argv1);
  782. return true;
  783. fail:
  784. if (argv1)
  785. wasm_runtime_free(argv1);
  786. #if WASM_ENABLE_GC != 0
  787. for (j = 0; j < num_local_ref_pushed; j++) {
  788. local_ref = wasm_runtime_pop_local_obj_ref(exec_env);
  789. wasm_runtime_free(local_ref);
  790. }
  791. #endif
  792. bh_assert(wasm_runtime_get_exception(module_inst));
  793. return false;
  794. }
  795. bool
  796. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  797. const char *name, int32 argc, char *argv[])
  798. {
  799. bool ret;
  800. #if WASM_ENABLE_MEMORY_PROFILING != 0
  801. WASMExecEnv *exec_env;
  802. #endif
  803. ret = execute_func(module_inst, name, argc, argv);
  804. #if WASM_ENABLE_MEMORY_PROFILING != 0
  805. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  806. if (exec_env) {
  807. wasm_runtime_dump_mem_consumption(exec_env);
  808. }
  809. #endif
  810. #if WASM_ENABLE_GC_PERF_PROFILING != 0
  811. void *handle = wasm_runtime_get_gc_heap_handle(module_inst);
  812. mem_allocator_dump_perf_profiling(handle);
  813. #endif
  814. #if WASM_ENABLE_PERF_PROFILING != 0
  815. wasm_runtime_dump_perf_profiling(module_inst);
  816. #endif
  817. return (ret && !wasm_runtime_get_exception(module_inst)) ? true : false;
  818. }