main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <assert.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <pthread.h>
  9. #include "wasm_export.h"
  10. #include "bh_read_file.h"
  11. #include "bh_getopt.h"
  12. void
  13. print_usage(void)
  14. {
  15. fprintf(stdout, "Options:\r\n");
  16. fprintf(stdout, " -f [path of wasm file] \n");
  17. }
  18. static void *
  19. runner_with_sigleton_exec_env(void *vp)
  20. {
  21. wasm_module_inst_t inst = vp;
  22. bool ok = wasm_runtime_init_thread_env();
  23. assert(ok);
  24. wasm_application_execute_main(inst, 0, NULL);
  25. wasm_runtime_destroy_thread_env();
  26. return inst;
  27. }
  28. static void *
  29. runner_with_spawn_exec_env(void *vp)
  30. {
  31. wasm_exec_env_t env = vp;
  32. wasm_module_inst_t inst = wasm_runtime_get_module_inst(env);
  33. wasm_function_inst_t func;
  34. bool ok = wasm_runtime_init_thread_env();
  35. assert(ok);
  36. func = wasm_runtime_lookup_function(inst, "block_forever");
  37. assert(func != NULL);
  38. wasm_runtime_call_wasm(env, func, 0, NULL);
  39. wasm_runtime_destroy_spawned_exec_env(env);
  40. wasm_runtime_destroy_thread_env();
  41. return inst;
  42. }
  43. int
  44. main(int argc, char *argv_main[])
  45. {
  46. int exit_code = 1;
  47. static char global_heap_buf[512 * 1024];
  48. char *buffer;
  49. char error_buf[128];
  50. int opt;
  51. char *wasm_path = NULL;
  52. int ret;
  53. int pipe_fds[2];
  54. const unsigned int N = 4;
  55. wasm_module_t module = NULL;
  56. wasm_module_inst_t module_inst[N];
  57. pthread_t th[N];
  58. unsigned int i;
  59. uint32 buf_size, stack_size = 8092, heap_size = 8092;
  60. for (i = 0; i < N; i++) {
  61. module_inst[i] = NULL;
  62. }
  63. RuntimeInitArgs init_args;
  64. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  65. while ((opt = getopt(argc, argv_main, "hf:")) != -1) {
  66. switch (opt) {
  67. case 'f':
  68. wasm_path = optarg;
  69. break;
  70. case 'h':
  71. print_usage();
  72. return 0;
  73. case '?':
  74. print_usage();
  75. return 0;
  76. }
  77. }
  78. if (optind == 1) {
  79. print_usage();
  80. return 0;
  81. }
  82. memset(&init_args, 0, sizeof(init_args));
  83. init_args.mem_alloc_type = Alloc_With_Pool;
  84. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  85. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  86. if (!wasm_runtime_full_init(&init_args)) {
  87. printf("Init runtime environment failed.\n");
  88. return -1;
  89. }
  90. buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
  91. if (!buffer) {
  92. printf("Open wasm app file [%s] failed.\n", wasm_path);
  93. goto fail;
  94. }
  95. module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf,
  96. sizeof(error_buf));
  97. if (!module) {
  98. printf("Load wasm module failed. error: %s\n", error_buf);
  99. goto fail;
  100. }
  101. /* Ensure that fd_read on FD 0 blocks. */
  102. ret = pipe(pipe_fds);
  103. if (ret != 0) {
  104. goto fail;
  105. }
  106. wasm_runtime_set_wasi_args_ex(module, NULL, 0, NULL, 0, NULL, 0, NULL, 0,
  107. pipe_fds[0], -1, -1);
  108. for (i = 0; i < N; i++) {
  109. bool use_wasm_runtime_spawn_exec_env = i / 2 == 0;
  110. wasm_exec_env_t env;
  111. module_inst[i] = wasm_runtime_instantiate(module, stack_size, heap_size,
  112. error_buf, sizeof(error_buf));
  113. if (!module_inst[i]) {
  114. printf("Instantiate wasm module failed. error: %s\n", error_buf);
  115. goto fail;
  116. }
  117. /* Note: ensure that module inst has an exec env so that
  118. * it can receive the termination request.
  119. */
  120. env = wasm_runtime_get_exec_env_singleton(module_inst[i]);
  121. assert(env != NULL);
  122. if (use_wasm_runtime_spawn_exec_env) {
  123. env = wasm_runtime_spawn_exec_env(env);
  124. assert(env != NULL);
  125. }
  126. if ((i % 2) == 0) {
  127. printf("terminating thread %u before starting\n", i);
  128. wasm_runtime_terminate(module_inst[i]);
  129. }
  130. if (use_wasm_runtime_spawn_exec_env) {
  131. printf("starting thread %u (spawn_exec_env)\n", i);
  132. ret = pthread_create(&th[i], NULL, runner_with_spawn_exec_env, env);
  133. if (ret != 0) {
  134. wasm_runtime_destroy_spawned_exec_env(env);
  135. goto fail;
  136. }
  137. }
  138. else {
  139. printf("starting thread %u (singleton exec_env)\n", i);
  140. ret = pthread_create(&th[i], NULL, runner_with_sigleton_exec_env,
  141. module_inst[i]);
  142. if (ret != 0) {
  143. goto fail;
  144. }
  145. }
  146. }
  147. printf("sleeping a bit to ensure that the threads actually started\n");
  148. sleep(1);
  149. for (i = 0; i < N; i++) {
  150. if ((i % 2) != 0) {
  151. printf("terminating thread %u\n", i);
  152. wasm_runtime_terminate(module_inst[i]);
  153. }
  154. }
  155. for (i = 0; i < N; i++) {
  156. printf("joining thread %u\n", i);
  157. void *status;
  158. ret = pthread_join(th[i], &status);
  159. if (ret != 0) {
  160. printf("pthread_join failed for thread %u\n", i);
  161. goto fail;
  162. }
  163. }
  164. for (i = 0; i < N; i++) {
  165. const char *exception = wasm_runtime_get_exception(module_inst[i]);
  166. if (exception != NULL) {
  167. if (!strstr(exception, "terminated by user")) {
  168. printf("thread %u got an exception: %s (unexpected)\n", i,
  169. exception);
  170. goto fail;
  171. }
  172. printf("thread %u got an exception: %s (expected)\n", i, exception);
  173. }
  174. else {
  175. printf("thread %u got no exception (unexpected)\n", i);
  176. goto fail;
  177. }
  178. }
  179. exit_code = 0;
  180. fail:
  181. for (i = 0; i < N; i++) {
  182. if (module_inst[i])
  183. wasm_runtime_deinstantiate(module_inst[i]);
  184. }
  185. if (module)
  186. wasm_runtime_unload(module);
  187. if (buffer)
  188. BH_FREE(buffer);
  189. wasm_runtime_destroy();
  190. return exit_code;
  191. }