main.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_export.h"
  6. #include "bh_read_file.h"
  7. #include "bh_getopt.h"
  8. void
  9. print_usage(void)
  10. {
  11. fprintf(stdout, "Options:\r\n");
  12. fprintf(stdout, " -f [path of wasm file] \n");
  13. }
  14. int
  15. main(int argc, char *argv_main[])
  16. {
  17. int exit_code = 1;
  18. static char global_heap_buf[512 * 1024];
  19. char *buffer;
  20. char error_buf[128];
  21. int opt;
  22. char *wasm_path = NULL;
  23. const unsigned int N = 4;
  24. wasm_module_t module = NULL;
  25. wasm_module_inst_t module_inst[N];
  26. wasm_exec_env_t exec_env[N];
  27. const char *name_test_data_drop = "test_data_drop";
  28. const char *name_test_elem_drop = "test_elem_drop";
  29. wasm_function_inst_t func_test_data_drop[N];
  30. wasm_function_inst_t func_test_elem_drop[N];
  31. unsigned int i;
  32. unsigned int iter;
  33. uint32 buf_size, stack_size = 8092, heap_size = 8092;
  34. for (i = 0; i < N; i++) {
  35. module_inst[i] = NULL;
  36. exec_env[i] = NULL;
  37. func_test_data_drop[i] = NULL;
  38. func_test_elem_drop[i] = NULL;
  39. }
  40. RuntimeInitArgs init_args;
  41. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  42. while ((opt = getopt(argc, argv_main, "hf:")) != -1) {
  43. switch (opt) {
  44. case 'f':
  45. wasm_path = optarg;
  46. break;
  47. case 'h':
  48. print_usage();
  49. return 0;
  50. case '?':
  51. print_usage();
  52. return 0;
  53. }
  54. }
  55. if (optind == 1) {
  56. print_usage();
  57. return 0;
  58. }
  59. memset(&init_args, 0, sizeof(init_args));
  60. init_args.mem_alloc_type = Alloc_With_Pool;
  61. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  62. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  63. if (!wasm_runtime_full_init(&init_args)) {
  64. printf("Init runtime environment failed.\n");
  65. return -1;
  66. }
  67. buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
  68. if (!buffer) {
  69. printf("Open wasm app file [%s] failed.\n", wasm_path);
  70. goto fail;
  71. }
  72. module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf,
  73. sizeof(error_buf));
  74. if (!module) {
  75. printf("Load wasm module failed. error: %s\n", error_buf);
  76. goto fail;
  77. }
  78. for (i = 0; i < N; i++) {
  79. module_inst[i] = wasm_runtime_instantiate(module, stack_size, heap_size,
  80. error_buf, sizeof(error_buf));
  81. if (!module_inst[i]) {
  82. printf("Instantiate wasm module failed. error: %s\n", error_buf);
  83. goto fail;
  84. }
  85. exec_env[i] = wasm_runtime_create_exec_env(module_inst[i], stack_size);
  86. if (!exec_env[i]) {
  87. printf("Create wasm execution environment failed.\n");
  88. goto fail;
  89. }
  90. func_test_data_drop[i] = wasm_runtime_lookup_function(
  91. module_inst[i], name_test_data_drop, NULL);
  92. if (!func_test_data_drop[i]) {
  93. printf("The wasm function %s is not found.\n", name_test_data_drop);
  94. goto fail;
  95. }
  96. func_test_elem_drop[i] = wasm_runtime_lookup_function(
  97. module_inst[i], name_test_elem_drop, NULL);
  98. if (!func_test_elem_drop[i]) {
  99. printf("The wasm function %s is not found.\n", name_test_elem_drop);
  100. goto fail;
  101. }
  102. }
  103. for (iter = 0; iter < 2; iter++) {
  104. /*
  105. * as we drop data/table in the first iteration,
  106. * the later iterations should trap.
  107. */
  108. const bool should_trap = iter > 0;
  109. for (i = 0; i < N; i++) {
  110. uint32_t argv[1] = {};
  111. if (wasm_runtime_call_wasm(exec_env[i], func_test_data_drop[i], 0,
  112. argv)) {
  113. uint32_t result = argv[0];
  114. printf(
  115. "Native finished calling wasm function: %s, return: %x\n",
  116. name_test_data_drop, result);
  117. if (result != 0x64636261) { /* "abcd" */
  118. printf("unexpected return value\n");
  119. goto fail;
  120. }
  121. if (should_trap) {
  122. printf("a trap is expected\n");
  123. goto fail;
  124. }
  125. }
  126. else if (should_trap) {
  127. printf("call wasm function %s failed as expected. error: %s\n",
  128. name_test_data_drop,
  129. wasm_runtime_get_exception(module_inst[i]));
  130. }
  131. else {
  132. printf("call wasm function %s failed. error: %s\n",
  133. name_test_data_drop,
  134. wasm_runtime_get_exception(module_inst[i]));
  135. goto fail;
  136. }
  137. }
  138. for (i = 0; i < N; i++) {
  139. wasm_runtime_clear_exception(module_inst[i]);
  140. uint32_t argv[1] = {};
  141. if (wasm_runtime_call_wasm(exec_env[i], func_test_elem_drop[i], 0,
  142. argv)) {
  143. uint32_t result = argv[0];
  144. printf(
  145. "Native finished calling wasm function: %s, return: %x\n",
  146. name_test_elem_drop, result);
  147. if (result != 0) {
  148. printf("unexpected return value\n");
  149. goto fail;
  150. }
  151. if (should_trap) {
  152. printf("a trap is expected\n");
  153. goto fail;
  154. }
  155. }
  156. else if (should_trap) {
  157. printf("call wasm function %s failed as expected. error: %s\n",
  158. name_test_elem_drop,
  159. wasm_runtime_get_exception(module_inst[i]));
  160. }
  161. else {
  162. printf("call wasm function %s failed. error: %s\n",
  163. name_test_elem_drop,
  164. wasm_runtime_get_exception(module_inst[i]));
  165. goto fail;
  166. }
  167. }
  168. }
  169. exit_code = 0;
  170. fail:
  171. for (i = 0; i < N; i++) {
  172. if (exec_env[i])
  173. wasm_runtime_destroy_exec_env(exec_env[i]);
  174. if (module_inst[i])
  175. wasm_runtime_deinstantiate(module_inst[i]);
  176. }
  177. if (module)
  178. wasm_runtime_unload(module);
  179. if (buffer)
  180. BH_FREE(buffer);
  181. wasm_runtime_destroy();
  182. return exit_code;
  183. }