main.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_read_file.h"
  6. #include "wasm_export.h"
  7. RunningMode
  8. str_to_running_mode(char const *str)
  9. {
  10. RunningMode running_mode = 0;
  11. #if WASM_ENABLE_INTERP != 0
  12. if (!strcmp(str, "interp")) {
  13. running_mode = Mode_Interp;
  14. }
  15. #endif
  16. #if WASM_ENABLE_FAST_JIT != 0
  17. else if (!strcmp(str, "fast-jit")) {
  18. running_mode = Mode_Fast_JIT;
  19. }
  20. #endif
  21. #if WASM_ENABLE_JIT != 0
  22. else if (!strcmp(str, "llvm-jit")) {
  23. running_mode = Mode_LLVM_JIT;
  24. }
  25. #endif
  26. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 \
  27. && WASM_ENABLE_LAZY_JIT != 0
  28. else if (!strcmp(str, "multi-tier-jit")) {
  29. running_mode = Mode_Multi_Tier_JIT;
  30. }
  31. #endif
  32. return running_mode;
  33. }
  34. int
  35. one_time_run_wasm(RunningMode default_running_mode,
  36. RunningMode module_running_mode)
  37. {
  38. char *buffer, *another_buffer, error_buf[128];
  39. wasm_module_t module = NULL, another_module = NULL;
  40. wasm_module_inst_t module_inst = NULL, another_module_inst = NULL;
  41. wasm_function_inst_t main_func = NULL, echo_func = NULL;
  42. wasm_exec_env_t exec_env = NULL, another_exec_env = NULL;
  43. uint32_t size, stack_size = 8092, heap_size = 8092;
  44. if (wasm_runtime_is_running_mode_supported(default_running_mode)) {
  45. printf("Support running mode: %d\n", default_running_mode);
  46. }
  47. else {
  48. printf("This runtime Doesn't support running mode: %d\n",
  49. default_running_mode);
  50. goto fail;
  51. }
  52. if (wasm_runtime_set_default_running_mode(default_running_mode)) {
  53. printf("Successfully set default running mode: %d\n",
  54. default_running_mode);
  55. }
  56. else {
  57. printf("Set default running mode: %d failed\n", default_running_mode);
  58. goto fail;
  59. }
  60. /* module 1 */
  61. if (!(buffer = bh_read_file_to_buffer("mytest.wasm", &size))) {
  62. printf("Open wasm app file %s failed.\n", "mytest.wasm");
  63. goto fail;
  64. }
  65. if (!(module = wasm_runtime_load((uint8_t *)buffer, size, error_buf,
  66. sizeof(error_buf)))) {
  67. printf("Load wasm module failed. error: %s\n", error_buf);
  68. goto fail;
  69. }
  70. if (!(module_inst = wasm_runtime_instantiate(
  71. module, stack_size, heap_size, error_buf, sizeof(error_buf)))) {
  72. printf("Instantiate wasm module failed. error: %s\n", error_buf);
  73. goto fail;
  74. }
  75. if (!(exec_env = wasm_runtime_create_exec_env(module_inst, stack_size))) {
  76. printf("Create wasm execution environment failed.\n");
  77. goto fail;
  78. }
  79. /* module 2 */
  80. if (!(another_buffer = bh_read_file_to_buffer("hello.wasm", &size))) {
  81. printf("Open wasm app file %s failed.\n", "hello.wasm");
  82. goto fail;
  83. }
  84. if (!(another_module = wasm_runtime_load((uint8_t *)another_buffer, size,
  85. error_buf, sizeof(error_buf)))) {
  86. printf("Load wasm module failed. error: %s\n", error_buf);
  87. goto fail;
  88. }
  89. if (!(another_module_inst =
  90. wasm_runtime_instantiate(another_module, stack_size, heap_size,
  91. error_buf, sizeof(error_buf)))) {
  92. printf("Instantiate wasm module failed. error: %s\n", error_buf);
  93. goto fail;
  94. }
  95. if (!(another_exec_env =
  96. wasm_runtime_create_exec_env(another_module_inst, stack_size))) {
  97. printf("Create wasm execution environment failed.\n");
  98. goto fail;
  99. }
  100. /* run main function in module 1 */
  101. uint32 wasm_argv[2];
  102. if (!(main_func =
  103. wasm_runtime_lookup_function(module_inst, "__main_argc_argv"))) {
  104. printf("The main wasm function from module 1 is not found.\n");
  105. goto fail;
  106. }
  107. wasm_argv[0] = 3;
  108. if (wasm_runtime_call_wasm(exec_env, main_func, 2, wasm_argv)) {
  109. printf("Run module 1 in running mode: %d\n",
  110. wasm_runtime_get_running_mode(module_inst));
  111. assert(default_running_mode
  112. == wasm_runtime_get_running_mode(module_inst));
  113. printf("Wasm main function return: %d\n", wasm_argv[0]);
  114. }
  115. else {
  116. printf("%s\n", wasm_runtime_get_exception(module_inst));
  117. goto fail;
  118. }
  119. /* run echo function in module 2 */
  120. if (!(wasm_runtime_set_running_mode(another_module_inst,
  121. module_running_mode))) {
  122. printf("Set running mode for module instance failed\n");
  123. goto fail;
  124. }
  125. if (!(echo_func =
  126. wasm_runtime_lookup_function(another_module_inst, "echo"))) {
  127. printf("The echo wasm function from module 2 is not found.\n");
  128. goto fail;
  129. }
  130. wasm_argv[0] = 5;
  131. if (wasm_runtime_call_wasm(another_exec_env, echo_func, 1, wasm_argv)) {
  132. printf("Run module 2 in running mode: %d\n",
  133. wasm_runtime_get_running_mode(another_module_inst));
  134. assert(module_running_mode
  135. == wasm_runtime_get_running_mode(another_module_inst));
  136. printf("Wasm echo function return: %d\n\n", wasm_argv[0]);
  137. }
  138. else {
  139. printf("%s\n", wasm_runtime_get_exception(another_module_inst));
  140. goto fail;
  141. }
  142. fail:
  143. if (exec_env)
  144. wasm_runtime_destroy_exec_env(exec_env);
  145. if (another_exec_env)
  146. wasm_runtime_destroy_exec_env(another_exec_env);
  147. if (module_inst)
  148. wasm_runtime_deinstantiate(module_inst);
  149. if (another_module_inst)
  150. wasm_runtime_deinstantiate(another_module_inst);
  151. if (module)
  152. wasm_runtime_unload(module);
  153. if (another_module)
  154. wasm_runtime_unload(another_module);
  155. return 0;
  156. }
  157. int
  158. main(int argc, char const *argv[])
  159. {
  160. RunningMode default_running_mode = 0, module_running_mode = 0;
  161. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  162. if (!strncmp(argv[0], "--default-running-mode=", 23)) {
  163. default_running_mode = str_to_running_mode(argv[0] + 23);
  164. }
  165. else if (!strncmp(argv[0], "--module-running-mode=", 22)) {
  166. module_running_mode = str_to_running_mode(argv[0] + 22);
  167. }
  168. }
  169. /* all the runtime memory allocations are restricted in the global_heap_buf
  170. * array */
  171. static char global_heap_buf[512 * 1024];
  172. RuntimeInitArgs init_args;
  173. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  174. /* configure the memory allocator for the runtime */
  175. init_args.mem_alloc_type = Alloc_With_Pool;
  176. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  177. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  178. /* initialize runtime environment with user configurations*/
  179. if (!wasm_runtime_full_init(&init_args)) {
  180. printf("Init runtime environment failed.\n");
  181. return -1;
  182. }
  183. for (int i = 0; i < 1; ++i) {
  184. one_time_run_wasm(default_running_mode, module_running_mode);
  185. }
  186. wasm_runtime_destroy();
  187. return 0;
  188. }