wasm_application.c 32 KB

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