hello.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_platform.h"
  6. #include "bh_read_file.h"
  7. #include "wasm_export.h"
  8. #define USE_GLOBAL_HEAP_BUF 0
  9. #if USE_GLOBAL_HEAP_BUF != 0
  10. static char global_heap_buf[10 * 1024 * 1024] = { 0 };
  11. #endif
  12. static uintptr_t global_objects[10] = { 0 };
  13. int32
  14. local_cmp_externref(wasm_exec_env_t exec_env, uintptr_t externref_a,
  15. uintptr_t externref_b)
  16. {
  17. return externref_a == externref_b;
  18. }
  19. int32
  20. local_chk_externref(wasm_exec_env_t exec_env, int32 index, uintptr_t externref)
  21. {
  22. return externref == global_objects[index];
  23. }
  24. /* clang-format off */
  25. static NativeSymbol native_symbols[] = {
  26. { "native-cmp-externref", local_cmp_externref, "(rr)i", NULL },
  27. { "native-chk-externref", local_chk_externref, "(ir)i", NULL },
  28. };
  29. /* clang-format on */
  30. static inline void
  31. local_set_externref(int32 index, uintptr_t externref)
  32. {
  33. global_objects[index] = externref;
  34. }
  35. static WASMFunctionInstanceCommon *wasm_set_externref_ptr;
  36. static WASMFunctionInstanceCommon *wasm_get_externref_ptr;
  37. static WASMFunctionInstanceCommon *wasm_cmp_externref_ptr;
  38. static bool
  39. wasm_set_externref(wasm_exec_env_t exec_env, wasm_module_inst_t inst,
  40. int32 index, uintptr_t externref)
  41. {
  42. union {
  43. uintptr_t val;
  44. uint32 parts[2];
  45. } u;
  46. uint32 argv[3] = { 0 };
  47. if (!exec_env || !wasm_set_externref_ptr) {
  48. return false;
  49. }
  50. u.val = externref;
  51. argv[0] = index;
  52. argv[1] = u.parts[0];
  53. argv[2] = u.parts[1];
  54. if (!wasm_runtime_call_wasm(exec_env, wasm_set_externref_ptr, 2, argv)) {
  55. const char *exception;
  56. if ((exception = wasm_runtime_get_exception(inst))) {
  57. printf("Exception: %s\n", exception);
  58. }
  59. return false;
  60. }
  61. return true;
  62. }
  63. static bool
  64. wasm_get_externref(wasm_exec_env_t exec_env, wasm_module_inst_t inst,
  65. int32 index, uintptr_t *ret_externref)
  66. {
  67. wasm_val_t results[1] = { 0 };
  68. if (!exec_env || !wasm_get_externref_ptr || !ret_externref) {
  69. return false;
  70. }
  71. if (!wasm_runtime_call_wasm_v(exec_env, wasm_get_externref_ptr, 1, results,
  72. 1, index)) {
  73. const char *exception;
  74. if ((exception = wasm_runtime_get_exception(inst))) {
  75. printf("Exception: %s\n", exception);
  76. }
  77. return false;
  78. }
  79. if (WASM_ANYREF != results[0].kind) {
  80. return false;
  81. }
  82. *ret_externref = results[0].of.foreign;
  83. return true;
  84. }
  85. static bool
  86. wasm_cmp_externref(wasm_exec_env_t exec_env, wasm_module_inst_t inst,
  87. int32 index, uintptr_t externref, int32 *ret_result)
  88. {
  89. wasm_val_t results[1] = { 0 };
  90. wasm_val_t arguments[2] = {
  91. { .kind = WASM_I32, .of.i32 = index },
  92. { .kind = WASM_ANYREF, .of.foreign = externref },
  93. };
  94. if (!exec_env || !wasm_cmp_externref_ptr || !ret_result) {
  95. return false;
  96. }
  97. if (!wasm_runtime_call_wasm_a(exec_env, wasm_cmp_externref_ptr, 1, results,
  98. 2, arguments)) {
  99. const char *exception;
  100. if ((exception = wasm_runtime_get_exception(inst))) {
  101. printf("Exception: %s\n", exception);
  102. }
  103. return false;
  104. }
  105. if (results[0].kind != WASM_I32) {
  106. return false;
  107. }
  108. *ret_result = results[0].of.i32;
  109. return true;
  110. }
  111. static bool
  112. set_and_cmp(wasm_exec_env_t exec_env, wasm_module_inst_t inst, int32 i,
  113. uintptr_t externref)
  114. {
  115. int32 cmp_result = 0;
  116. uintptr_t wasm_externref = 0;
  117. wasm_set_externref(exec_env, inst, i, externref);
  118. local_set_externref(i, externref);
  119. wasm_get_externref(exec_env, inst, 0, &wasm_externref);
  120. if (!local_chk_externref(exec_env, 0, wasm_externref)) {
  121. printf("#%d, In host language world Wasm Externref 0x%lx Vs. Native "
  122. "Externref 0x%lx FAILED\n",
  123. i, wasm_externref, externref);
  124. return false;
  125. }
  126. if (!wasm_cmp_externref(exec_env, inst, i, global_objects[i], &cmp_result)
  127. || !cmp_result) {
  128. printf("#%d, In Wasm world Native Externref 0x%lx Vs, Wasm Externref "
  129. "FAILED\n",
  130. i, global_objects[i]);
  131. return false;
  132. }
  133. return true;
  134. }
  135. int
  136. main(int argc, char *argv[])
  137. {
  138. char *wasm_file = "hello.wasm";
  139. uint8 *wasm_file_buf = NULL;
  140. uint32 wasm_file_size;
  141. uint32 stack_size = 16 * 1024, heap_size = 16 * 1024;
  142. wasm_module_t wasm_module = NULL;
  143. wasm_module_inst_t wasm_module_inst = NULL;
  144. wasm_exec_env_t exec_env = NULL;
  145. RuntimeInitArgs init_args;
  146. char error_buf[128] = { 0 };
  147. #if WASM_ENABLE_LOG != 0
  148. int log_verbose_level = 2;
  149. #endif
  150. const uint64 big_number = 0x123456789abc;
  151. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  152. #if USE_GLOBAL_HEAP_BUF != 0
  153. init_args.mem_alloc_type = Alloc_With_Pool;
  154. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  155. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  156. #else
  157. init_args.mem_alloc_type = Alloc_With_Allocator;
  158. init_args.mem_alloc_option.allocator.malloc_func = malloc;
  159. init_args.mem_alloc_option.allocator.realloc_func = realloc;
  160. init_args.mem_alloc_option.allocator.free_func = free;
  161. #endif
  162. init_args.n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
  163. init_args.native_module_name = "env";
  164. init_args.native_symbols = native_symbols;
  165. /* initialize runtime environment */
  166. if (!wasm_runtime_full_init(&init_args)) {
  167. printf("Init runtime environment failed.\n");
  168. return -1;
  169. }
  170. #if WASM_ENABLE_LOG != 0
  171. bh_log_set_verbose_level(log_verbose_level);
  172. #endif
  173. /* load WASM byte buffer from WASM bin file */
  174. if (!(wasm_file_buf =
  175. (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size)))
  176. goto fail;
  177. /* load WASM module */
  178. if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
  179. error_buf, sizeof(error_buf)))) {
  180. printf("%s\n", error_buf);
  181. goto fail;
  182. }
  183. /* instantiate the module */
  184. if (!(wasm_module_inst =
  185. wasm_runtime_instantiate(wasm_module, stack_size, heap_size,
  186. error_buf, sizeof(error_buf)))) {
  187. printf("%s\n", error_buf);
  188. goto fail;
  189. }
  190. /* create an execution env */
  191. if (!(exec_env =
  192. wasm_runtime_create_exec_env(wasm_module_inst, stack_size))) {
  193. printf("%s\n", "create exec env failed");
  194. goto fail;
  195. }
  196. /* lookup function instance */
  197. if (!(wasm_cmp_externref_ptr = wasm_runtime_lookup_function(
  198. wasm_module_inst, "cmp-externref", NULL))) {
  199. printf("%s\n", "lookup function cmp-externref failed");
  200. goto fail;
  201. }
  202. if (!(wasm_get_externref_ptr = wasm_runtime_lookup_function(
  203. wasm_module_inst, "get-externref", NULL))) {
  204. printf("%s\n", "lookup function get-externref failed");
  205. goto fail;
  206. }
  207. if (!(wasm_set_externref_ptr = wasm_runtime_lookup_function(
  208. wasm_module_inst, "set-externref", NULL))) {
  209. printf("%s\n", "lookup function set-externref failed");
  210. goto fail;
  211. }
  212. /* test with NULL */
  213. if (!set_and_cmp(exec_env, wasm_module_inst, 0, 0)
  214. || !set_and_cmp(exec_env, wasm_module_inst, 1, big_number + 1)
  215. || !set_and_cmp(exec_env, wasm_module_inst, 2, big_number + 2)
  216. || !set_and_cmp(exec_env, wasm_module_inst, 3, big_number + 3)) {
  217. goto fail;
  218. }
  219. printf("GREAT! PASS ALL CHKs\n");
  220. fail:
  221. /* destroy exec env */
  222. if (exec_env) {
  223. wasm_runtime_destroy_exec_env(exec_env);
  224. }
  225. /* destroy the module instance */
  226. if (wasm_module_inst) {
  227. wasm_runtime_deinstantiate(wasm_module_inst);
  228. }
  229. /* unload the module */
  230. if (wasm_module) {
  231. wasm_runtime_unload(wasm_module);
  232. }
  233. /* free the file buffer */
  234. if (wasm_file_buf) {
  235. wasm_runtime_free(wasm_file_buf);
  236. }
  237. /* destroy runtime environment */
  238. wasm_runtime_destroy();
  239. return 0;
  240. }