wasm_application.c 31 KB

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