wasm_application.c 32 KB

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