wasm_application.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. return false;
  98. if (!check_main_func_type(func->u.func->func_type))
  99. return false;
  100. if (func->u.func->func_type->param_count) {
  101. for (i = 0; i < argc; i++)
  102. total_argv_size += (uint32)(strlen(argv[i]) + 1);
  103. total_argv_size = align_uint(total_argv_size, 4);
  104. total_size = (uint64)total_argv_size + sizeof(int32) * (uint64)argc;
  105. if (total_size >= UINT32_MAX
  106. || !(argv_buf_offset =
  107. wasm_runtime_module_malloc(module_inst, (uint32)total_size)))
  108. return false;
  109. argv_buf = p = wasm_runtime_addr_app_to_native(module_inst, argv_buf_offset);
  110. argv_offsets = (int32*)(p + total_argv_size);
  111. p_end = p + total_size;
  112. for (i = 0; i < argc; i++) {
  113. bh_memcpy_s(p, (uint32)(p_end - p), argv[i], (uint32)(strlen(argv[i]) + 1));
  114. argv_offsets[i] = argv_buf_offset + (int32)(p - argv_buf);
  115. p += strlen(argv[i]) + 1;
  116. }
  117. argc1 = 2;
  118. argv1[0] = (uint32)argc;
  119. argv1[1] = (uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
  120. }
  121. return wasm_runtime_call_wasm(module_inst, NULL, func, argc1, argv1);
  122. }
  123. static WASMFunctionInstance*
  124. resolve_function(const WASMModuleInstance *module_inst, char *name)
  125. {
  126. uint32 i;
  127. for (i = 0; i < module_inst->export_func_count; i++)
  128. if (!strcmp(module_inst->export_functions[i].name, name))
  129. return module_inst->export_functions[i].function;
  130. return NULL;
  131. }
  132. union ieee754_float {
  133. float f;
  134. /* This is the IEEE 754 single-precision format. */
  135. union {
  136. struct {
  137. unsigned int negative:1;
  138. unsigned int exponent:8;
  139. unsigned int mantissa:23;
  140. } ieee_big_endian;
  141. struct {
  142. unsigned int mantissa:23;
  143. unsigned int exponent:8;
  144. unsigned int negative:1;
  145. } ieee_little_endian;
  146. } ieee;
  147. };
  148. union ieee754_double {
  149. double d;
  150. /* This is the IEEE 754 double-precision format. */
  151. union {
  152. struct {
  153. unsigned int negative:1;
  154. unsigned int exponent:11;
  155. /* Together these comprise the mantissa. */
  156. unsigned int mantissa0:20;
  157. unsigned int mantissa1:32;
  158. } ieee_big_endian;
  159. struct {
  160. /* Together these comprise the mantissa. */
  161. unsigned int mantissa1:32;
  162. unsigned int mantissa0:20;
  163. unsigned int exponent:11;
  164. unsigned int negative:1;
  165. } ieee_little_endian;
  166. } ieee;
  167. };
  168. static union {
  169. int a;
  170. char b;
  171. } __ue = { .a = 1 };
  172. #define is_little_endian() (__ue.b == 1)
  173. bool
  174. wasm_application_execute_func(WASMModuleInstance *module_inst,
  175. char *name, int argc, char *argv[])
  176. {
  177. WASMFunctionInstance *func;
  178. WASMType *type;
  179. uint32 argc1, *argv1;
  180. int32 i, p;
  181. uint64 total_size;
  182. const char *exception;
  183. wasm_assert(argc >= 0);
  184. func = resolve_function(module_inst, name);
  185. if (!func || func->is_import_func) {
  186. LOG_ERROR("Wasm lookup function %s failed.\n", name);
  187. return false;
  188. }
  189. type = func->u.func->func_type;
  190. if (type->param_count != (uint32)argc) {
  191. LOG_ERROR("Wasm prepare param failed: invalid param count.\n");
  192. return false;
  193. }
  194. argc1 = func->param_cell_num;
  195. total_size = sizeof(uint32) * (uint64)(argc1 > 2 ? argc1 : 2);
  196. if (total_size >= UINT32_MAX
  197. || (!(argv1 = wasm_malloc((uint32)total_size)))) {
  198. LOG_ERROR("Wasm prepare param failed: malloc failed.\n");
  199. return false;
  200. }
  201. /* Parse arguments */
  202. for (i = 0, p = 0; i < argc; i++) {
  203. char *endptr = NULL;
  204. wasm_assert(argv[i] != NULL);
  205. if (argv[i][0] == '\0') {
  206. LOG_ERROR("Wasm prepare param failed: invalid num (%s).\n", argv[i]);
  207. goto fail;
  208. }
  209. switch (type->types[i]) {
  210. case VALUE_TYPE_I32:
  211. argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
  212. break;
  213. case VALUE_TYPE_I64:
  214. {
  215. union { uint64 val; uint32 parts[2]; } u;
  216. u.val = strtoull(argv[i], &endptr, 0);
  217. argv1[p++] = u.parts[0];
  218. argv1[p++] = u.parts[1];
  219. break;
  220. }
  221. case VALUE_TYPE_F32:
  222. {
  223. float32 f32 = strtof(argv[i], &endptr);
  224. if (isnan(f32)) {
  225. if (argv[i][0] == '-') {
  226. f32 = -f32;
  227. }
  228. if (endptr[0] == ':') {
  229. uint32 sig;
  230. union ieee754_float u;
  231. sig = (uint32)strtoul(endptr + 1, &endptr, 0);
  232. u.f = f32;
  233. if (is_little_endian())
  234. u.ieee.ieee_little_endian.mantissa = sig;
  235. else
  236. u.ieee.ieee_big_endian.mantissa = sig;
  237. f32 = u.f;
  238. }
  239. }
  240. *(float32*)&argv1[p++] = f32;
  241. break;
  242. }
  243. case VALUE_TYPE_F64:
  244. {
  245. union { float64 val; uint32 parts[2]; } u;
  246. u.val = strtod(argv[i], &endptr);
  247. if (isnan(u.val)) {
  248. if (argv[i][0] == '-') {
  249. u.val = -u.val;
  250. }
  251. if (endptr[0] == ':') {
  252. uint64 sig;
  253. union ieee754_double ud;
  254. sig = strtoull(endptr + 1, &endptr, 0);
  255. ud.d = u.val;
  256. if (is_little_endian()) {
  257. ud.ieee.ieee_little_endian.mantissa0 = sig >> 32;
  258. ud.ieee.ieee_little_endian.mantissa1 = (uint32)sig;
  259. }
  260. else {
  261. ud.ieee.ieee_big_endian.mantissa0 = sig >> 32;
  262. ud.ieee.ieee_big_endian.mantissa1 = (uint32)sig;
  263. }
  264. u.val = ud.d;
  265. }
  266. }
  267. argv1[p++] = u.parts[0];
  268. argv1[p++] = u.parts[1];
  269. break;
  270. }
  271. }
  272. if (endptr && *endptr != '\0' && *endptr != '_') {
  273. LOG_ERROR("Wasm prepare param failed: invalid num (%s).\n", argv[i]);
  274. goto fail;
  275. }
  276. if (errno != 0) {
  277. LOG_ERROR("Wasm prepare param failed: errno %d.\n", errno);
  278. goto fail;
  279. }
  280. }
  281. wasm_assert(p == (int32)argc1);
  282. wasm_runtime_set_exception(module_inst, NULL);
  283. if (!wasm_runtime_call_wasm(module_inst, NULL, func, argc1, argv1)) {
  284. exception = wasm_runtime_get_exception(module_inst);
  285. wasm_printf("%s\n", exception);
  286. goto fail;
  287. }
  288. /* print return value */
  289. switch (type->types[type->param_count]) {
  290. case VALUE_TYPE_I32:
  291. wasm_printf("0x%x:i32", argv1[0]);
  292. break;
  293. case VALUE_TYPE_I64:
  294. {
  295. char buf[16];
  296. union { uint64 val; uint32 parts[2]; } u;
  297. u.parts[0] = argv1[0];
  298. u.parts[1] = argv1[1];
  299. if (sizeof(long) == 4)
  300. snprintf(buf, sizeof(buf), "%s", "0x%llx:i64");
  301. else
  302. snprintf(buf, sizeof(buf), "%s", "0x%lx:i64");
  303. wasm_printf(buf, u.val);
  304. break;
  305. }
  306. case VALUE_TYPE_F32:
  307. wasm_printf("%.7g:f32", *(float32*)argv1);
  308. break;
  309. case VALUE_TYPE_F64:
  310. {
  311. union { float64 val; uint32 parts[2]; } u;
  312. u.parts[0] = argv1[0];
  313. u.parts[1] = argv1[1];
  314. wasm_printf("%.7g:f64", u.val);
  315. break;
  316. }
  317. }
  318. wasm_printf("\n");
  319. wasm_free(argv1);
  320. return true;
  321. fail:
  322. wasm_free(argv1);
  323. return false;
  324. }
  325. static bool
  326. check_type(uint8 type, const char *p)
  327. {
  328. const char *str = "i32";
  329. if (strlen(p) < 3)
  330. return false;
  331. switch (type) {
  332. case VALUE_TYPE_I32:
  333. str = "i32";
  334. break;
  335. case VALUE_TYPE_I64:
  336. str = "i64";
  337. break;
  338. case VALUE_TYPE_F32:
  339. str = "f32";
  340. break;
  341. case VALUE_TYPE_F64:
  342. str = "f64";
  343. break;
  344. }
  345. if (strncmp(p, str, 3))
  346. return false;
  347. return true;
  348. }
  349. static bool
  350. check_function_type(const WASMType *type,
  351. const char *signature)
  352. {
  353. uint32 i;
  354. const char *p = signature;
  355. if (!p || *p++ != '(')
  356. return false;
  357. for (i = 0; i < type->param_count; i++) {
  358. if (!check_type(type->types[i], p))
  359. return false;
  360. p += 3;
  361. }
  362. if (*p++ != ')')
  363. return false;
  364. if (type->result_count) {
  365. if (!check_type(type->types[type->param_count], p))
  366. return false;
  367. p += 3;
  368. }
  369. if (*p != '\0')
  370. return false;
  371. return true;
  372. }
  373. WASMFunctionInstance*
  374. wasm_runtime_lookup_function(const WASMModuleInstance *module_inst,
  375. const char *name,
  376. const char *signature)
  377. {
  378. uint32 i;
  379. for (i = 0; i < module_inst->export_func_count; i++)
  380. if (!strcmp(module_inst->export_functions[i].name, name)
  381. && check_function_type(
  382. module_inst->export_functions[i].function->u.func->func_type,
  383. signature))
  384. return module_inst->export_functions[i].function;
  385. return NULL;
  386. }