wasm_application.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. static void
  13. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  14. {
  15. if (error_buf != NULL)
  16. snprintf(error_buf, error_buf_size, "%s", string);
  17. }
  18. static void *
  19. runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst,
  20. char *error_buf, uint32 error_buf_size)
  21. {
  22. void *mem;
  23. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  24. if (module_inst != NULL) {
  25. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  26. }
  27. else if (error_buf != NULL) {
  28. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  29. }
  30. return NULL;
  31. }
  32. memset(mem, 0, (uint32)size);
  33. return mem;
  34. }
  35. static union {
  36. int a;
  37. char b;
  38. } __ue = { .a = 1 };
  39. #define is_little_endian() (__ue.b == 1)
  40. /**
  41. * Implementation of wasm_application_execute_main()
  42. */
  43. static WASMFunctionInstanceCommon *
  44. resolve_function(const WASMModuleInstanceCommon *module_inst, const char *name);
  45. static bool
  46. check_main_func_type(const WASMType *type)
  47. {
  48. if (!(type->param_count == 0 || type->param_count == 2)
  49. || type->result_count > 1) {
  50. LOG_ERROR(
  51. "WASM execute application failed: invalid main function type.\n");
  52. return false;
  53. }
  54. if (type->param_count == 2
  55. && !(type->types[0] == VALUE_TYPE_I32
  56. && type->types[1] == VALUE_TYPE_I32)) {
  57. LOG_ERROR(
  58. "WASM execute application failed: invalid main function type.\n");
  59. return false;
  60. }
  61. if (type->result_count
  62. && type->types[type->param_count] != VALUE_TYPE_I32) {
  63. LOG_ERROR(
  64. "WASM execute application failed: invalid main function type.\n");
  65. return false;
  66. }
  67. return true;
  68. }
  69. bool
  70. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
  71. char *argv[])
  72. {
  73. WASMFunctionInstanceCommon *func;
  74. WASMType *func_type = NULL;
  75. uint32 argc1 = 0, argv1[2] = { 0 };
  76. uint32 total_argv_size = 0;
  77. uint64 total_size;
  78. uint32 argv_buf_offset = 0;
  79. int32 i;
  80. char *argv_buf, *p, *p_end;
  81. uint32 *argv_offsets, module_type;
  82. bool ret, is_import_func = true;
  83. #if WASM_ENABLE_LIBC_WASI != 0
  84. if (wasm_runtime_is_wasi_mode(module_inst)) {
  85. /* In wasi mode, we should call function named "_start"
  86. which initializes the wasi envrionment and then calls
  87. the actual main function. Directly call main function
  88. may cause exception thrown. */
  89. if ((func = wasm_runtime_lookup_wasi_start_function(module_inst)))
  90. return wasm_runtime_create_exec_env_and_call_wasm(module_inst, func,
  91. 0, NULL);
  92. /* If no start function was found, we execute
  93. the main function as normal */
  94. }
  95. #endif /* end of WASM_ENABLE_LIBC_WASI */
  96. if (!(func = resolve_function(module_inst, "main"))
  97. && !(func = resolve_function(module_inst, "__main_argc_argv"))
  98. && !(func = resolve_function(module_inst, "_main"))) {
  99. wasm_runtime_set_exception(module_inst, "lookup main function failed");
  100. return false;
  101. }
  102. #if WASM_ENABLE_INTERP != 0
  103. if (module_inst->module_type == Wasm_Module_Bytecode) {
  104. is_import_func = ((WASMFunctionInstance *)func)->is_import_func;
  105. }
  106. #endif
  107. #if WASM_ENABLE_AOT != 0
  108. if (module_inst->module_type == Wasm_Module_AoT) {
  109. is_import_func = ((AOTExportFunctionInstance *)func)->is_import_func;
  110. }
  111. #endif
  112. if (is_import_func) {
  113. wasm_runtime_set_exception(module_inst, "lookup main function failed");
  114. return false;
  115. }
  116. module_type = module_inst->module_type;
  117. func_type = wasm_runtime_get_function_type(func, module_type);
  118. if (!func_type) {
  119. LOG_ERROR("invalid module instance type");
  120. return false;
  121. }
  122. if (!check_main_func_type(func_type)) {
  123. wasm_runtime_set_exception(module_inst,
  124. "invalid function type of main function");
  125. return false;
  126. }
  127. if (func_type->param_count) {
  128. for (i = 0; i < argc; i++)
  129. total_argv_size += (uint32)(strlen(argv[i]) + 1);
  130. total_argv_size = align_uint(total_argv_size, 4);
  131. total_size = (uint64)total_argv_size + sizeof(int32) * (uint64)argc;
  132. if (total_size >= UINT32_MAX
  133. || !(argv_buf_offset = wasm_runtime_module_malloc(
  134. module_inst, (uint32)total_size, (void **)&argv_buf))) {
  135. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  136. return false;
  137. }
  138. p = argv_buf;
  139. argv_offsets = (uint32 *)(p + total_argv_size);
  140. p_end = p + total_size;
  141. for (i = 0; i < argc; i++) {
  142. bh_memcpy_s(p, (uint32)(p_end - p), argv[i],
  143. (uint32)(strlen(argv[i]) + 1));
  144. argv_offsets[i] = argv_buf_offset + (uint32)(p - argv_buf);
  145. p += strlen(argv[i]) + 1;
  146. }
  147. argc1 = 2;
  148. argv1[0] = (uint32)argc;
  149. argv1[1] =
  150. (uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
  151. }
  152. ret = wasm_runtime_create_exec_env_and_call_wasm(module_inst, func, argc1,
  153. argv1);
  154. if (ret && func_type->result_count > 0 && argc > 0 && argv)
  155. /* copy the return value */
  156. *(int *)argv = (int)argv1[0];
  157. if (argv_buf_offset)
  158. wasm_runtime_module_free(module_inst, argv_buf_offset);
  159. return ret;
  160. }
  161. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  162. bool
  163. wasm_application_execute_program_main(WASMProgramCommon *program,
  164. int32 argc, char *argv[])
  165. {
  166. WASMProgramInstance * program_inst = (WASMProgramInstance *)program;
  167. if (!program_inst)
  168. return false;
  169. return wasm_application_execute_main((WASMModuleInstanceCommon*)program_inst->root_module_inst, argc, argv);
  170. }
  171. #endif
  172. #if WASM_ENABLE_MULTI_MODULE != 0
  173. static WASMModuleInstance *
  174. get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
  175. const char *sub_module_name)
  176. {
  177. WASMSubModInstNode *node =
  178. bh_list_first_elem(parent_module_inst->sub_module_inst_list);
  179. while (node && strcmp(node->module_name, sub_module_name)) {
  180. node = bh_list_elem_next(node);
  181. }
  182. return node ? node->module_inst : NULL;
  183. }
  184. static bool
  185. parse_function_name(char *orig_function_name, char **p_module_name,
  186. char **p_function_name)
  187. {
  188. if (orig_function_name[0] != '$') {
  189. *p_module_name = NULL;
  190. *p_function_name = orig_function_name;
  191. return true;
  192. }
  193. /**
  194. * $module_name$function_name\0
  195. * ===>
  196. * module_name\0function_name\0
  197. * ===>
  198. * module_name
  199. * function_name
  200. */
  201. char *p1 = orig_function_name;
  202. char *p2 = strchr(p1 + 1, '$');
  203. if (!p2) {
  204. LOG_DEBUG("can not parse the incoming function name");
  205. return false;
  206. }
  207. *p_module_name = p1 + 1;
  208. *p2 = '\0';
  209. *p_function_name = p2 + 1;
  210. return strlen(*p_module_name) && strlen(*p_function_name);
  211. }
  212. #endif
  213. /**
  214. * Implementation of wasm_application_execute_func()
  215. */
  216. static WASMFunctionInstanceCommon *
  217. resolve_function(const WASMModuleInstanceCommon *module_inst, const char *name)
  218. {
  219. uint32 i = 0;
  220. WASMFunctionInstanceCommon *ret = NULL;
  221. #if WASM_ENABLE_MULTI_MODULE != 0
  222. WASMModuleInstance *sub_module_inst = NULL;
  223. char *orig_name = NULL;
  224. char *sub_module_name = NULL;
  225. char *function_name = NULL;
  226. uint32 length = (uint32)(strlen(name) + 1);
  227. orig_name = runtime_malloc(sizeof(char) * length, NULL, NULL, 0);
  228. if (!orig_name) {
  229. return NULL;
  230. }
  231. strncpy(orig_name, name, length);
  232. if (!parse_function_name(orig_name, &sub_module_name, &function_name)) {
  233. goto LEAVE;
  234. }
  235. LOG_DEBUG("%s -> %s and %s", name, sub_module_name, function_name);
  236. if (sub_module_name) {
  237. sub_module_inst = get_sub_module_inst((WASMModuleInstance *)module_inst,
  238. sub_module_name);
  239. if (!sub_module_inst) {
  240. LOG_DEBUG("can not find a sub module named %s", sub_module_name);
  241. goto LEAVE;
  242. }
  243. }
  244. #else
  245. const char *function_name = name;
  246. #endif
  247. #if WASM_ENABLE_INTERP != 0
  248. if (module_inst->module_type == Wasm_Module_Bytecode) {
  249. WASMModuleInstance *wasm_inst = (WASMModuleInstance *)module_inst;
  250. #if WASM_ENABLE_MULTI_MODULE != 0
  251. wasm_inst = sub_module_inst ? sub_module_inst : wasm_inst;
  252. #endif /* WASM_ENABLE_MULTI_MODULE */
  253. for (i = 0; i < wasm_inst->export_func_count; i++) {
  254. if (!strcmp(wasm_inst->export_functions[i].name, function_name)) {
  255. ret = wasm_inst->export_functions[i].function;
  256. break;
  257. }
  258. }
  259. }
  260. #endif /* WASM_ENABLE_INTERP */
  261. #if WASM_ENABLE_AOT != 0
  262. if (module_inst->module_type == Wasm_Module_AoT) {
  263. AOTModuleInstance *aot_inst = (AOTModuleInstance*)module_inst;
  264. AOTModule * aot_module = (AOTModule *)aot_inst->aot_module.ptr;
  265. AOTExportFunctionInstance *export_funcs = (AOTExportFunctionInstance *)
  266. aot_inst->export_funcs.ptr;
  267. for (i = 0; i < aot_module->export_func_count; i++) {
  268. if (!strcmp(export_funcs[i].func_name, function_name)) {
  269. ret = &export_funcs[i];
  270. break;
  271. }
  272. }
  273. }
  274. #endif
  275. #if WASM_ENABLE_MULTI_MODULE != 0
  276. LEAVE:
  277. wasm_runtime_free(orig_name);
  278. #endif
  279. return ret;
  280. }
  281. union ieee754_float {
  282. float f;
  283. /* This is the IEEE 754 single-precision format. */
  284. union {
  285. struct {
  286. unsigned int negative : 1;
  287. unsigned int exponent : 8;
  288. unsigned int mantissa : 23;
  289. } ieee_big_endian;
  290. struct {
  291. unsigned int mantissa : 23;
  292. unsigned int exponent : 8;
  293. unsigned int negative : 1;
  294. } ieee_little_endian;
  295. } ieee;
  296. };
  297. union ieee754_double {
  298. double d;
  299. /* This is the IEEE 754 double-precision format. */
  300. union {
  301. struct {
  302. unsigned int negative : 1;
  303. unsigned int exponent : 11;
  304. /* Together these comprise the mantissa. */
  305. unsigned int mantissa0 : 20;
  306. unsigned int mantissa1 : 32;
  307. } ieee_big_endian;
  308. struct {
  309. /* Together these comprise the mantissa. */
  310. unsigned int mantissa1 : 32;
  311. unsigned int mantissa0 : 20;
  312. unsigned int exponent : 11;
  313. unsigned int negative : 1;
  314. } ieee_little_endian;
  315. } ieee;
  316. };
  317. bool
  318. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  319. const char *name, int32 argc, char *argv[])
  320. {
  321. WASMFunctionInstanceCommon *func;
  322. WASMType *type = NULL;
  323. uint32 argc1, *argv1 = NULL, cell_num = 0, j, k = 0;
  324. int32 i, p, module_type;
  325. uint64 total_size;
  326. const char *exception;
  327. char buf[128];
  328. bh_assert(argc >= 0);
  329. LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc);
  330. func = resolve_function(module_inst, name);
  331. if (!func) {
  332. snprintf(buf, sizeof(buf), "lookup function %s failed", name);
  333. wasm_runtime_set_exception(module_inst, buf);
  334. goto fail;
  335. }
  336. #if WASM_ENABLE_INTERP != 0
  337. if (module_inst->module_type == Wasm_Module_Bytecode) {
  338. WASMFunctionInstance *wasm_func = (WASMFunctionInstance *)func;
  339. if (wasm_func->is_import_func
  340. #if WASM_ENABLE_MULTI_MODULE != 0
  341. && !wasm_func->import_func_inst
  342. #endif
  343. ) {
  344. snprintf(buf, sizeof(buf), "lookup function %s failed", name);
  345. wasm_runtime_set_exception(module_inst, buf);
  346. goto fail;
  347. }
  348. }
  349. #endif
  350. module_type = module_inst->module_type;
  351. type = wasm_runtime_get_function_type(func, module_type);
  352. if (!type) {
  353. LOG_ERROR("invalid module instance type");
  354. return false;
  355. }
  356. if (type->param_count != (uint32)argc) {
  357. wasm_runtime_set_exception(module_inst, "invalid input argument count");
  358. goto fail;
  359. }
  360. argc1 = type->param_cell_num;
  361. cell_num = (argc1 > type->ret_cell_num) ? argc1 : type->ret_cell_num;
  362. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  363. if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst, NULL, 0)))) {
  364. goto fail;
  365. }
  366. /* Parse arguments */
  367. for (i = 0, p = 0; i < argc; i++) {
  368. char *endptr = NULL;
  369. bh_assert(argv[i] != NULL);
  370. if (argv[i][0] == '\0') {
  371. snprintf(buf, sizeof(buf), "invalid input argument %d", i);
  372. wasm_runtime_set_exception(module_inst, buf);
  373. goto fail;
  374. }
  375. switch (type->types[i]) {
  376. case VALUE_TYPE_I32:
  377. argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
  378. break;
  379. case VALUE_TYPE_I64:
  380. {
  381. union {
  382. uint64 val;
  383. uint32 parts[2];
  384. } u;
  385. u.val = strtoull(argv[i], &endptr, 0);
  386. argv1[p++] = u.parts[0];
  387. argv1[p++] = u.parts[1];
  388. break;
  389. }
  390. case VALUE_TYPE_F32:
  391. {
  392. float32 f32 = strtof(argv[i], &endptr);
  393. if (isnan(f32)) {
  394. if (argv[i][0] == '-') {
  395. union ieee754_float u;
  396. u.f = f32;
  397. if (is_little_endian())
  398. u.ieee.ieee_little_endian.negative = 1;
  399. else
  400. u.ieee.ieee_big_endian.negative = 1;
  401. memcpy(&f32, &u.f, sizeof(float));
  402. }
  403. if (endptr[0] == ':') {
  404. uint32 sig;
  405. union ieee754_float u;
  406. sig = (uint32)strtoul(endptr + 1, &endptr, 0);
  407. u.f = f32;
  408. if (is_little_endian())
  409. u.ieee.ieee_little_endian.mantissa = sig;
  410. else
  411. u.ieee.ieee_big_endian.mantissa = sig;
  412. memcpy(&f32, &u.f, sizeof(float));
  413. }
  414. }
  415. memcpy(&argv1[p++], &f32, sizeof(float));
  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. if (argv[i][0] == '-') {
  427. union ieee754_double ud;
  428. ud.d = u.val;
  429. if (is_little_endian())
  430. ud.ieee.ieee_little_endian.negative = 1;
  431. else
  432. ud.ieee.ieee_big_endian.negative = 1;
  433. memcpy(&u.val, &ud.d, sizeof(double));
  434. }
  435. if (endptr[0] == ':') {
  436. uint64 sig;
  437. union ieee754_double ud;
  438. sig = strtoull(endptr + 1, &endptr, 0);
  439. ud.d = u.val;
  440. if (is_little_endian()) {
  441. ud.ieee.ieee_little_endian.mantissa0 = sig >> 32;
  442. ud.ieee.ieee_little_endian.mantissa1 = (uint32)sig;
  443. }
  444. else {
  445. ud.ieee.ieee_big_endian.mantissa0 = sig >> 32;
  446. ud.ieee.ieee_big_endian.mantissa1 = (uint32)sig;
  447. }
  448. memcpy(&u.val, &ud.d, sizeof(double));
  449. }
  450. }
  451. argv1[p++] = u.parts[0];
  452. argv1[p++] = u.parts[1];
  453. break;
  454. }
  455. #if WASM_ENABLE_SIMD != 0
  456. case VALUE_TYPE_V128:
  457. {
  458. /* it likes 0x123\0x234 or 123\234 */
  459. /* retrive first i64 */
  460. *(uint64 *)(argv1 + p) = strtoull(argv[i], &endptr, 0);
  461. /* skip \ */
  462. endptr++;
  463. /* retrive second i64 */
  464. *(uint64 *)(argv1 + p + 2) = strtoull(endptr, &endptr, 0);
  465. p += 4;
  466. break;
  467. }
  468. #endif /* WASM_ENABLE_SIMD != 0 */
  469. #if WASM_ENABLE_REF_TYPES != 0
  470. case VALUE_TYPE_FUNCREF:
  471. {
  472. if (strncmp(argv[i], "null", 4) == 0
  473. || strncmp(argv[i], "NULL", 4) == 0) {
  474. argv1[p++] = NULL_REF;
  475. }
  476. else {
  477. argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
  478. }
  479. break;
  480. }
  481. case VALUE_TYPE_EXTERNREF:
  482. {
  483. if (strncmp(argv[i], "null", 4) == 0
  484. || strncmp(argv[i], "NULL", 4) == 0) {
  485. argv1[p++] = NULL_REF;
  486. }
  487. else {
  488. uint64 val = strtoull(argv[i], &endptr, 0);
  489. void *extern_obj = (void *)(uintptr_t)val;
  490. uint32 externref_idx;
  491. if (!wasm_externref_obj2ref(module_inst, extern_obj,
  492. &externref_idx)) {
  493. wasm_runtime_set_exception(
  494. module_inst, "map extern object to ref failed");
  495. goto fail;
  496. }
  497. argv1[p++] = externref_idx;
  498. }
  499. break;
  500. }
  501. #endif /* WASM_ENABLE_REF_TYPES */
  502. default:
  503. bh_assert(0);
  504. break;
  505. }
  506. if (endptr && *endptr != '\0' && *endptr != '_') {
  507. snprintf(buf, sizeof(buf), "invalid input argument %d: %s", i,
  508. argv[i]);
  509. wasm_runtime_set_exception(module_inst, buf);
  510. goto fail;
  511. }
  512. }
  513. bh_assert(p == (int32)argc1);
  514. wasm_runtime_set_exception(module_inst, NULL);
  515. if (!wasm_runtime_create_exec_env_and_call_wasm(module_inst, func, argc1,
  516. argv1)) {
  517. goto fail;
  518. }
  519. /* print return value */
  520. for (j = 0; j < type->result_count; j++) {
  521. switch (type->types[type->param_count + j]) {
  522. case VALUE_TYPE_I32:
  523. {
  524. os_printf("0x%x:i32", argv1[k]);
  525. k++;
  526. break;
  527. }
  528. case VALUE_TYPE_I64:
  529. {
  530. union {
  531. uint64 val;
  532. uint32 parts[2];
  533. } u;
  534. u.parts[0] = argv1[k];
  535. u.parts[1] = argv1[k + 1];
  536. k += 2;
  537. #ifdef PRIx64
  538. os_printf("0x%" PRIx64 ":i64", u.val);
  539. #else
  540. char buf[16];
  541. if (sizeof(long) == 4)
  542. snprintf(buf, sizeof(buf), "%s", "0x%llx:i64");
  543. else
  544. snprintf(buf, sizeof(buf), "%s", "0x%lx:i64");
  545. os_printf(buf, u.val);
  546. #endif
  547. break;
  548. }
  549. case VALUE_TYPE_F32:
  550. {
  551. os_printf("%.7g:f32", *(float32 *)(argv1 + k));
  552. k++;
  553. break;
  554. }
  555. case VALUE_TYPE_F64:
  556. {
  557. union {
  558. float64 val;
  559. uint32 parts[2];
  560. } u;
  561. u.parts[0] = argv1[k];
  562. u.parts[1] = argv1[k + 1];
  563. k += 2;
  564. os_printf("%.7g:f64", u.val);
  565. break;
  566. }
  567. #if WASM_ENABLE_REF_TYPES
  568. case VALUE_TYPE_FUNCREF:
  569. {
  570. if (argv1[k] != NULL_REF)
  571. os_printf("%u:ref.func", argv1[k]);
  572. else
  573. os_printf("func:ref.null");
  574. k++;
  575. break;
  576. }
  577. case VALUE_TYPE_EXTERNREF:
  578. {
  579. if (argv1[k] != NULL_REF) {
  580. void *extern_obj = NULL;
  581. bool ret = wasm_externref_ref2obj(argv1[k], &extern_obj);
  582. bh_assert(ret);
  583. (void)ret;
  584. os_printf("%p:ref.extern", extern_obj);
  585. }
  586. else
  587. os_printf("extern:ref.null");
  588. k++;
  589. break;
  590. }
  591. #endif
  592. #if WASM_ENABLE_SIMD != 0
  593. case VALUE_TYPE_V128:
  594. {
  595. uint64 *v = (uint64 *)(argv1 + k);
  596. #if defined(PRIx64)
  597. os_printf("<0x%016" PRIx64 " 0x%016" PRIx64 ">:v128", *v,
  598. *(v + 1));
  599. #else
  600. if (4 == sizeof(long)) {
  601. os_printf("<0x%016llx 0x%016llx>:v128", *v, *(v + 1));
  602. }
  603. else {
  604. os_printf("<0x%016lx 0x%016lx>:v128", *v, *(v + 1));
  605. }
  606. #endif /* PRIx64 */
  607. k += 4;
  608. break;
  609. }
  610. #endif /* WASM_ENABLE_SIMD != 0 */
  611. default:
  612. bh_assert(0);
  613. break;
  614. }
  615. if (j < (uint32)(type->result_count - 1))
  616. os_printf(",");
  617. }
  618. os_printf("\n");
  619. wasm_runtime_free(argv1);
  620. return true;
  621. fail:
  622. if (argv1)
  623. wasm_runtime_free(argv1);
  624. exception = wasm_runtime_get_exception(module_inst);
  625. bh_assert(exception);
  626. os_printf("%s\n", exception);
  627. return false;
  628. }
  629. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  630. bool
  631. wasm_application_execute_program_func(WASMProgramCommon *program,
  632. const char *name, int32 argc, char *argv[])
  633. {
  634. WASMProgramInstance * program_inst = (WASMProgramInstance *)program;
  635. if (!program_inst)
  636. return false;
  637. return wasm_application_execute_func((WASMModuleInstanceCommon*)program_inst->root_module_inst, name, argc, argv);
  638. }
  639. #endif