memory64_test.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "memory64_common.h"
  6. // To use a test fixture and Value Parameterized Tests,
  7. // derive a class from testing::TestWithParam.
  8. class memory64_test_suite : public testing::TestWithParam<RunningMode>
  9. {
  10. protected:
  11. bool load_wasm_file(const char *wasm_file)
  12. {
  13. const char *file;
  14. unsigned char *wasm_file_buf;
  15. uint32 wasm_file_size;
  16. file = wasm_file;
  17. wasm_file_buf =
  18. (unsigned char *)bh_read_file_to_buffer(file, &wasm_file_size);
  19. if (!wasm_file_buf)
  20. goto fail;
  21. if (!(module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
  22. error_buf, sizeof(error_buf)))) {
  23. printf("Load wasm module failed. error: %s\n", error_buf);
  24. goto fail;
  25. }
  26. return true;
  27. fail:
  28. if (!module)
  29. wasm_runtime_unload(module);
  30. return false;
  31. }
  32. bool init_exec_env()
  33. {
  34. if (!(module_inst =
  35. wasm_runtime_instantiate(module, stack_size, heap_size,
  36. error_buf, sizeof(error_buf)))) {
  37. printf("Instantiate wasm module failed. error: %s\n", error_buf);
  38. goto fail;
  39. }
  40. if (!(exec_env =
  41. wasm_runtime_create_exec_env(module_inst, stack_size))) {
  42. printf("Create wasm execution environment failed.\n");
  43. goto fail;
  44. }
  45. return true;
  46. fail:
  47. if (exec_env)
  48. wasm_runtime_destroy_exec_env(exec_env);
  49. if (module_inst)
  50. wasm_runtime_unload(module);
  51. return false;
  52. }
  53. void destory_exec_env()
  54. {
  55. wasm_runtime_destroy_exec_env(exec_env);
  56. wasm_runtime_deinstantiate(module_inst);
  57. wasm_runtime_unload(module);
  58. }
  59. public:
  60. // If your test fixture defines SetUpTestSuite() or TearDownTestSuite()
  61. // they must be declared public rather than protected in order to use
  62. // TEST_P.
  63. // virtual void SetUp() will be called before each test is run. You
  64. // should define it if you need to initialize the variables.
  65. // Otherwise, this can be skipped.
  66. virtual void SetUp()
  67. {
  68. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  69. init_args.mem_alloc_type = Alloc_With_Pool;
  70. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  71. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  72. ASSERT_EQ(wasm_runtime_full_init(&init_args), true);
  73. cleanup = true;
  74. }
  75. static void SetUpTestCase() {}
  76. // virtual void TearDown() will be called after each test is run.
  77. // You should define it if there is cleanup work to do. Otherwise,
  78. // you don't have to provide it.
  79. //
  80. virtual void TearDown()
  81. {
  82. if (cleanup) {
  83. wasm_runtime_destroy();
  84. cleanup = false;
  85. }
  86. }
  87. static void TearDownTestCase() {}
  88. RuntimeInitArgs init_args;
  89. wasm_module_t module = NULL;
  90. wasm_module_inst_t module_inst = NULL;
  91. wasm_exec_env_t exec_env = NULL;
  92. char error_buf[128];
  93. char global_heap_buf[512 * 1024];
  94. uint32_t stack_size = 8092, heap_size = 8092;
  95. bool cleanup = true;
  96. };
  97. TEST_F(memory64_test_suite, wasm_runtime_is_running_mode_supported)
  98. {
  99. // TODO: make sure the chosen running mode option is compiled, for memory64,
  100. // currently only support classic interp mode
  101. ASSERT_EQ(true, wasm_runtime_is_running_mode_supported(
  102. static_cast<RunningMode>(Mode_Default)));
  103. for (auto running_mode : running_mode_supported) {
  104. ASSERT_EQ(true, wasm_runtime_is_running_mode_supported(running_mode));
  105. }
  106. }
  107. TEST_F(memory64_test_suite, page_exceed_u32_1)
  108. {
  109. bool ret;
  110. ret = load_wasm_file("page_exceed_u32.wasm");
  111. ASSERT_FALSE(ret);
  112. ASSERT_TRUE(strcmp("WASM module load failed: integer too large", error_buf)
  113. == 0);
  114. }
  115. TEST_F(memory64_test_suite, page_exceed_u32_2)
  116. {
  117. bool ret;
  118. ret = load_wasm_file("page_exceed_u32_2.wasm");
  119. ASSERT_FALSE(ret);
  120. ASSERT_TRUE(strcmp("WASM module load failed: integer too large", error_buf)
  121. == 0);
  122. }
  123. TEST_F(memory64_test_suite, page_u32_max)
  124. {
  125. bool ret;
  126. ret = load_wasm_file("page_u32_max.wasm");
  127. ASSERT_TRUE(ret);
  128. }
  129. TEST_P(memory64_test_suite, memory_8GB)
  130. {
  131. RunningMode running_mode = GetParam();
  132. wasm_function_inst_t touch_every_page_func, i64_store_offset_4GB,
  133. i64_load_offset_4GB;
  134. uint32_t wasm_argv[6], i32;
  135. uint64_t i64;
  136. bool ret;
  137. ret = load_wasm_file("8GB_memory.wasm");
  138. ASSERT_TRUE(ret);
  139. ret = init_exec_env();
  140. ASSERT_TRUE(ret);
  141. ret = wasm_runtime_set_running_mode(module_inst, running_mode);
  142. ASSERT_TRUE(ret);
  143. ASSERT_EQ(running_mode, wasm_runtime_get_running_mode(module_inst));
  144. touch_every_page_func =
  145. wasm_runtime_lookup_function(module_inst, "touch_every_page");
  146. ASSERT_TRUE(touch_every_page_func != NULL);
  147. ret = wasm_runtime_call_wasm(exec_env, touch_every_page_func, 0, wasm_argv);
  148. ASSERT_TRUE(ret);
  149. // check return value: 0xfff8:i64,0x10000fff8:i64,0x1fff8:i32,0x1:i32
  150. i64 = 0xfff8;
  151. ASSERT_EQ(i64, GET_U64_FROM_ADDR(wasm_argv));
  152. i64 = 0x10000fff8;
  153. ASSERT_EQ(i64, GET_U64_FROM_ADDR(wasm_argv + 2));
  154. i32 = 0x1fff8;
  155. ASSERT_EQ(i32, wasm_argv[4]);
  156. i32 = 0x1;
  157. ASSERT_EQ(i32, wasm_argv[5]);
  158. // store at 0x100001000, with value 0xbeefdead
  159. PUT_I64_TO_ADDR(wasm_argv, 0x1000);
  160. PUT_I64_TO_ADDR(wasm_argv + 2, 0xbeefdead);
  161. i64_store_offset_4GB =
  162. wasm_runtime_lookup_function(module_inst, "i64_store_offset_4GB");
  163. ASSERT_TRUE(i64_store_offset_4GB != NULL);
  164. ret = wasm_runtime_call_wasm(exec_env, i64_store_offset_4GB, 4, wasm_argv);
  165. ASSERT_TRUE(ret);
  166. i64_load_offset_4GB =
  167. wasm_runtime_lookup_function(module_inst, "i64_load_offset_4GB");
  168. ASSERT_TRUE(i64_load_offset_4GB != NULL);
  169. ret = wasm_runtime_call_wasm(exec_env, i64_load_offset_4GB, 2, wasm_argv);
  170. ASSERT_TRUE(ret);
  171. // check return value: 0xbeefdead:i64
  172. i64 = 0xbeefdead;
  173. ASSERT_EQ(i64, GET_U64_FROM_ADDR(wasm_argv));
  174. destory_exec_env();
  175. }
  176. TEST_P(memory64_test_suite, mem64_from_clang)
  177. {
  178. RunningMode running_mode = GetParam();
  179. wasm_function_inst_t test_func;
  180. uint32_t wasm_argv[1], i32;
  181. bool ret;
  182. ret = load_wasm_file("mem64.wasm");
  183. ASSERT_TRUE(ret);
  184. ret = init_exec_env();
  185. ASSERT_TRUE(ret);
  186. ret = wasm_runtime_set_running_mode(module_inst, running_mode);
  187. ASSERT_TRUE(ret);
  188. ASSERT_EQ(running_mode, wasm_runtime_get_running_mode(module_inst));
  189. test_func =
  190. wasm_runtime_lookup_function(module_inst, "test");
  191. ASSERT_TRUE(test_func != NULL);
  192. ret = wasm_runtime_call_wasm(exec_env, test_func, 0, wasm_argv);
  193. ASSERT_TRUE(ret);
  194. i32 = 0x109;
  195. ASSERT_EQ(i32, wasm_argv[0]);
  196. destory_exec_env();
  197. }
  198. INSTANTIATE_TEST_CASE_P(RunningMode, memory64_test_suite,
  199. testing::ValuesIn(running_mode_supported));