wasm_application.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <errno.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "wasm.h"
  9. #include "wasm_application.h"
  10. #include "wasm_interp.h"
  11. #include "wasm_runtime.h"
  12. #include "wasm_thread.h"
  13. #include "wasm_assert.h"
  14. #include "wasm_log.h"
  15. #include "wasm_memory.h"
  16. #include "wasm_platform_log.h"
  17. #include "bh_common.h"
  18. static WASMFunctionInstance*
  19. resolve_main_function(const WASMModuleInstance *module_inst)
  20. {
  21. uint32 i;
  22. for (i = 0; i < module_inst->export_func_count; i++)
  23. if (!strcmp(module_inst->export_functions[i].name, "_main")
  24. || !strcmp(module_inst->export_functions[i].name, "main"))
  25. return module_inst->export_functions[i].function;
  26. LOG_ERROR("WASM execute application failed: main function not found.\n");
  27. return NULL;
  28. }
  29. static bool
  30. check_main_func_type(const WASMType *type)
  31. {
  32. if (!(type->param_count == 0 || type->param_count == 2)
  33. ||type->result_count > 1) {
  34. LOG_ERROR("WASM execute application failed: invalid main function type.\n");
  35. return false;
  36. }
  37. if (type->param_count == 2
  38. && !(type->types[0] == VALUE_TYPE_I32
  39. && type->types[1] == VALUE_TYPE_I32)) {
  40. LOG_ERROR("WASM execute application failed: invalid main function type.\n");
  41. return false;
  42. }
  43. if (type->result_count
  44. && type->types[type->param_count] != VALUE_TYPE_I32) {
  45. LOG_ERROR("WASM execute application failed: invalid main function type.\n");
  46. return false;
  47. }
  48. return true;
  49. }
  50. #if WASM_ENABLE_WASI != 0
  51. static WASMFunctionInstance *
  52. lookup_wasi_start_function(WASMModuleInstance *module_inst)
  53. {
  54. WASMFunctionInstance *func = NULL;
  55. uint32 i;
  56. for (i = 0; i < module_inst->export_func_count; i++) {
  57. if (!strcmp(module_inst->export_functions[i].name, "_start")) {
  58. func = module_inst->export_functions[i].function;
  59. if (func->u.func->func_type->param_count != 0
  60. || func->u.func->func_type->result_count != 0) {
  61. LOG_ERROR("Lookup wasi _start function failed: "
  62. "invalid function type.\n");
  63. return NULL;
  64. }
  65. return func;
  66. }
  67. }
  68. return NULL;
  69. }
  70. #endif
  71. bool
  72. wasm_application_execute_main(WASMModuleInstance *module_inst,
  73. int argc, char *argv[])
  74. {
  75. WASMFunctionInstance *func;
  76. uint32 argc1 = 0, argv1[2] = { 0 };
  77. uint32 total_argv_size = 0;
  78. uint64 total_size;
  79. int32 argv_buf_offset, i;
  80. char *argv_buf, *p, *p_end;
  81. int32 *argv_offsets;
  82. #if WASM_ENABLE_WASI != 0
  83. if (module_inst->module->is_wasi_module) {
  84. /* In wasi mode, we should call function named "_start"
  85. which initializes the wasi envrionment and then calls
  86. the actual main function. Directly call main function
  87. may cause exception thrown. */
  88. if ((func = lookup_wasi_start_function(module_inst)))
  89. return wasm_runtime_call_wasm(module_inst, NULL,
  90. func, 0, NULL);
  91. /* if no start function is found, we execute
  92. the main function as normal */
  93. }
  94. #endif
  95. func = resolve_main_function(module_inst);
  96. if (!func || func->is_import_func) {
  97. wasm_runtime_set_exception(module_inst,
  98. "lookup main function failed.");
  99. return false;
  100. }
  101. if (!check_main_func_type(func->u.func->func_type)) {
  102. wasm_runtime_set_exception(module_inst,
  103. "invalid function type of main function.");
  104. return false;
  105. }
  106. if (func->u.func->func_type->param_count) {
  107. for (i = 0; i < argc; i++)
  108. total_argv_size += (uint32)(strlen(argv[i]) + 1);
  109. total_argv_size = align_uint(total_argv_size, 4);
  110. total_size = (uint64)total_argv_size + sizeof(int32) * (uint64)argc;
  111. if (total_size >= UINT32_MAX
  112. || !(argv_buf_offset =
  113. wasm_runtime_module_malloc(module_inst, (uint32)total_size))) {
  114. wasm_runtime_set_exception(module_inst,
  115. "allocate memory failed.");
  116. return false;
  117. }
  118. argv_buf = p = wasm_runtime_addr_app_to_native(module_inst, argv_buf_offset);
  119. argv_offsets = (int32*)(p + total_argv_size);
  120. p_end = p + total_size;
  121. for (i = 0; i < argc; i++) {
  122. bh_memcpy_s(p, (uint32)(p_end - p), argv[i], (uint32)(strlen(argv[i]) + 1));
  123. argv_offsets[i] = argv_buf_offset + (int32)(p - argv_buf);
  124. p += strlen(argv[i]) + 1;
  125. }
  126. argc1 = 2;
  127. argv1[0] = (uint32)argc;
  128. argv1[1] = (uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
  129. }
  130. return wasm_runtime_call_wasm(module_inst, NULL, func, argc1, argv1);
  131. }
  132. static WASMFunctionInstance*
  133. resolve_function(const WASMModuleInstance *module_inst, char *name)
  134. {
  135. uint32 i;
  136. for (i = 0; i < module_inst->export_func_count; i++)
  137. if (!strcmp(module_inst->export_functions[i].name, name))
  138. return module_inst->export_functions[i].function;
  139. return NULL;
  140. }
  141. union ieee754_float {
  142. float f;
  143. /* This is the IEEE 754 single-precision format. */
  144. union {
  145. struct {
  146. unsigned int negative:1;
  147. unsigned int exponent:8;
  148. unsigned int mantissa:23;
  149. } ieee_big_endian;
  150. struct {
  151. unsigned int mantissa:23;
  152. unsigned int exponent:8;
  153. unsigned int negative:1;
  154. } ieee_little_endian;
  155. } ieee;
  156. };
  157. union ieee754_double {
  158. double d;
  159. /* This is the IEEE 754 double-precision format. */
  160. union {
  161. struct {
  162. unsigned int negative:1;
  163. unsigned int exponent:11;
  164. /* Together these comprise the mantissa. */
  165. unsigned int mantissa0:20;
  166. unsigned int mantissa1:32;
  167. } ieee_big_endian;
  168. struct {
  169. /* Together these comprise the mantissa. */
  170. unsigned int mantissa1:32;
  171. unsigned int mantissa0:20;
  172. unsigned int exponent:11;
  173. unsigned int negative:1;
  174. } ieee_little_endian;
  175. } ieee;
  176. };
  177. static union {
  178. int a;
  179. char b;
  180. } __ue = { .a = 1 };
  181. #define is_little_endian() (__ue.b == 1)
  182. bool
  183. wasm_application_execute_func(WASMModuleInstance *module_inst,
  184. char *name, int argc, char *argv[])
  185. {
  186. WASMFunctionInstance *func;
  187. WASMType *type;
  188. uint32 argc1, *argv1;
  189. int32 i, p;
  190. uint64 total_size;
  191. const char *exception;
  192. char buf[128];
  193. wasm_assert(argc >= 0);
  194. func = resolve_function(module_inst, name);
  195. if (!func || func->is_import_func) {
  196. snprintf(buf, sizeof(buf), "lookup function %s failed.", name);
  197. wasm_runtime_set_exception(module_inst, buf);
  198. return false;
  199. }
  200. type = func->u.func->func_type;
  201. if (type->param_count != (uint32)argc) {
  202. wasm_runtime_set_exception(module_inst,
  203. "invalid input argument count.");
  204. return false;
  205. }
  206. argc1 = func->param_cell_num;
  207. total_size = sizeof(uint32) * (uint64)(argc1 > 2 ? argc1 : 2);
  208. if (total_size >= UINT32_MAX
  209. || (!(argv1 = wasm_malloc((uint32)total_size)))) {
  210. wasm_runtime_set_exception(module_inst, "allocate memory failed.");
  211. return false;
  212. }
  213. /* Parse arguments */
  214. for (i = 0, p = 0; i < argc; i++) {
  215. char *endptr = NULL;
  216. wasm_assert(argv[i] != NULL);
  217. if (argv[i][0] == '\0') {
  218. snprintf(buf, sizeof(buf), "invalid input argument %d.", i);
  219. wasm_runtime_set_exception(module_inst, buf);
  220. goto fail;
  221. }
  222. switch (type->types[i]) {
  223. case VALUE_TYPE_I32:
  224. argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
  225. break;
  226. case VALUE_TYPE_I64:
  227. {
  228. union { uint64 val; uint32 parts[2]; } u;
  229. u.val = strtoull(argv[i], &endptr, 0);
  230. argv1[p++] = u.parts[0];
  231. argv1[p++] = u.parts[1];
  232. break;
  233. }
  234. case VALUE_TYPE_F32:
  235. {
  236. float32 f32 = strtof(argv[i], &endptr);
  237. if (isnan(f32)) {
  238. if (argv[i][0] == '-') {
  239. f32 = -f32;
  240. }
  241. if (endptr[0] == ':') {
  242. uint32 sig;
  243. union ieee754_float u;
  244. sig = (uint32)strtoul(endptr + 1, &endptr, 0);
  245. u.f = f32;
  246. if (is_little_endian())
  247. u.ieee.ieee_little_endian.mantissa = sig;
  248. else
  249. u.ieee.ieee_big_endian.mantissa = sig;
  250. f32 = u.f;
  251. }
  252. }
  253. *(float32*)&argv1[p++] = f32;
  254. break;
  255. }
  256. case VALUE_TYPE_F64:
  257. {
  258. union { float64 val; uint32 parts[2]; } u;
  259. u.val = strtod(argv[i], &endptr);
  260. if (isnan(u.val)) {
  261. if (argv[i][0] == '-') {
  262. u.val = -u.val;
  263. }
  264. if (endptr[0] == ':') {
  265. uint64 sig;
  266. union ieee754_double ud;
  267. sig = strtoull(endptr + 1, &endptr, 0);
  268. ud.d = u.val;
  269. if (is_little_endian()) {
  270. ud.ieee.ieee_little_endian.mantissa0 = sig >> 32;
  271. ud.ieee.ieee_little_endian.mantissa1 = (uint32)sig;
  272. }
  273. else {
  274. ud.ieee.ieee_big_endian.mantissa0 = sig >> 32;
  275. ud.ieee.ieee_big_endian.mantissa1 = (uint32)sig;
  276. }
  277. u.val = ud.d;
  278. }
  279. }
  280. argv1[p++] = u.parts[0];
  281. argv1[p++] = u.parts[1];
  282. break;
  283. }
  284. }
  285. if (endptr && *endptr != '\0' && *endptr != '_') {
  286. snprintf(buf, sizeof(buf), "invalid input argument %d: %s.",
  287. i, argv[i]);
  288. wasm_runtime_set_exception(module_inst, buf);
  289. goto fail;
  290. }
  291. if (errno != 0) {
  292. snprintf(buf, sizeof(buf),
  293. "prepare function argument error, errno: %d.", errno);
  294. wasm_runtime_set_exception(module_inst, buf);
  295. goto fail;
  296. }
  297. }
  298. wasm_assert(p == (int32)argc1);
  299. wasm_runtime_set_exception(module_inst, NULL);
  300. if (!wasm_runtime_call_wasm(module_inst, NULL, func, argc1, argv1)) {
  301. exception = wasm_runtime_get_exception(module_inst);
  302. wasm_printf("%s\n", exception);
  303. goto fail;
  304. }
  305. /* print return value */
  306. switch (type->types[type->param_count]) {
  307. case VALUE_TYPE_I32:
  308. wasm_printf("0x%x:i32", argv1[0]);
  309. break;
  310. case VALUE_TYPE_I64:
  311. {
  312. char buf[16];
  313. union { uint64 val; uint32 parts[2]; } u;
  314. u.parts[0] = argv1[0];
  315. u.parts[1] = argv1[1];
  316. if (sizeof(long) == 4)
  317. snprintf(buf, sizeof(buf), "%s", "0x%llx:i64");
  318. else
  319. snprintf(buf, sizeof(buf), "%s", "0x%lx:i64");
  320. wasm_printf(buf, u.val);
  321. break;
  322. }
  323. case VALUE_TYPE_F32:
  324. wasm_printf("%.7g:f32", *(float32*)argv1);
  325. break;
  326. case VALUE_TYPE_F64:
  327. {
  328. union { float64 val; uint32 parts[2]; } u;
  329. u.parts[0] = argv1[0];
  330. u.parts[1] = argv1[1];
  331. wasm_printf("%.7g:f64", u.val);
  332. break;
  333. }
  334. }
  335. wasm_printf("\n");
  336. wasm_free(argv1);
  337. return true;
  338. fail:
  339. wasm_free(argv1);
  340. return false;
  341. }
  342. static bool
  343. check_type(uint8 type, const char *p)
  344. {
  345. const char *str = "i32";
  346. if (strlen(p) < 3)
  347. return false;
  348. switch (type) {
  349. case VALUE_TYPE_I32:
  350. str = "i32";
  351. break;
  352. case VALUE_TYPE_I64:
  353. str = "i64";
  354. break;
  355. case VALUE_TYPE_F32:
  356. str = "f32";
  357. break;
  358. case VALUE_TYPE_F64:
  359. str = "f64";
  360. break;
  361. }
  362. if (strncmp(p, str, 3))
  363. return false;
  364. return true;
  365. }
  366. static bool
  367. check_function_type(const WASMType *type,
  368. const char *signature)
  369. {
  370. uint32 i;
  371. const char *p = signature;
  372. if (!p || *p++ != '(')
  373. return false;
  374. for (i = 0; i < type->param_count; i++) {
  375. if (!check_type(type->types[i], p))
  376. return false;
  377. p += 3;
  378. }
  379. if (*p++ != ')')
  380. return false;
  381. if (type->result_count) {
  382. if (!check_type(type->types[type->param_count], p))
  383. return false;
  384. p += 3;
  385. }
  386. if (*p != '\0')
  387. return false;
  388. return true;
  389. }
  390. WASMFunctionInstance*
  391. wasm_runtime_lookup_function(const WASMModuleInstance *module_inst,
  392. const char *name,
  393. const char *signature)
  394. {
  395. uint32 i;
  396. for (i = 0; i < module_inst->export_func_count; i++)
  397. if (!strcmp(module_inst->export_functions[i].name, name)
  398. && check_function_type(
  399. module_inst->export_functions[i].function->u.func->func_type,
  400. signature))
  401. return module_inst->export_functions[i].function;
  402. return NULL;
  403. }