wasm_application.c 20 KB

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