memory64_test.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_deinstantiate(module_inst);
  51. if (module)
  52. wasm_runtime_unload(module);
  53. return false;
  54. }
  55. void destroy_exec_env()
  56. {
  57. wasm_runtime_destroy_exec_env(exec_env);
  58. wasm_runtime_deinstantiate(module_inst);
  59. wasm_runtime_unload(module);
  60. }
  61. public:
  62. // If your test fixture defines SetUpTestSuite() or TearDownTestSuite()
  63. // they must be declared public rather than protected in order to use
  64. // TEST_P.
  65. // virtual void SetUp() will be called before each test is run. You
  66. // should define it if you need to initialize the variables.
  67. // Otherwise, this can be skipped.
  68. virtual void SetUp()
  69. {
  70. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  71. init_args.mem_alloc_type = Alloc_With_Pool;
  72. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  73. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  74. ASSERT_EQ(wasm_runtime_full_init(&init_args), true);
  75. cleanup = true;
  76. }
  77. static void SetUpTestCase() {}
  78. // virtual void TearDown() will be called after each test is run.
  79. // You should define it if there is cleanup work to do. Otherwise,
  80. // you don't have to provide it.
  81. //
  82. virtual void TearDown()
  83. {
  84. if (cleanup) {
  85. wasm_runtime_destroy();
  86. cleanup = false;
  87. }
  88. }
  89. static void TearDownTestCase() {}
  90. RuntimeInitArgs init_args;
  91. wasm_module_t module = NULL;
  92. wasm_module_inst_t module_inst = NULL;
  93. wasm_exec_env_t exec_env = NULL;
  94. char error_buf[128];
  95. char global_heap_buf[512 * 1024];
  96. uint32_t stack_size = 8092, heap_size = 8092;
  97. bool cleanup = true;
  98. };
  99. TEST_F(memory64_test_suite, wasm_runtime_is_running_mode_supported)
  100. {
  101. // TODO: make sure the chosen running mode option is compiled, for memory64,
  102. // currently only support classic interp mode
  103. ASSERT_EQ(true, wasm_runtime_is_running_mode_supported(
  104. static_cast<RunningMode>(Mode_Default)));
  105. for (auto running_mode : running_mode_supported) {
  106. ASSERT_EQ(true, wasm_runtime_is_running_mode_supported(running_mode));
  107. }
  108. }
  109. TEST_F(memory64_test_suite, page_exceed_u32_1)
  110. {
  111. bool ret;
  112. ret = load_wasm_file("page_exceed_u32.wasm");
  113. ASSERT_FALSE(ret);
  114. ASSERT_TRUE(strcmp("WASM module load failed: integer too large", error_buf)
  115. == 0);
  116. }
  117. TEST_F(memory64_test_suite, page_exceed_u32_2)
  118. {
  119. bool ret;
  120. ret = load_wasm_file("page_exceed_u32_2.wasm");
  121. ASSERT_FALSE(ret);
  122. ASSERT_TRUE(strcmp("WASM module load failed: integer too large", error_buf)
  123. == 0);
  124. }
  125. TEST_F(memory64_test_suite, page_u32_max)
  126. {
  127. bool ret;
  128. ret = load_wasm_file("page_u32_max.wasm");
  129. ASSERT_TRUE(ret);
  130. }
  131. TEST_P(memory64_test_suite, memory_8GB)
  132. {
  133. RunningMode running_mode = GetParam();
  134. wasm_function_inst_t touch_every_page_func, i64_store_offset_4GB,
  135. i64_load_offset_4GB;
  136. uint32_t wasm_argv[6], i32;
  137. uint64_t i64;
  138. bool ret;
  139. ret = load_wasm_file("8GB_memory.wasm");
  140. ASSERT_TRUE(ret);
  141. ret = init_exec_env();
  142. ASSERT_TRUE(ret);
  143. ret = wasm_runtime_set_running_mode(module_inst, running_mode);
  144. ASSERT_TRUE(ret);
  145. ASSERT_EQ(running_mode, wasm_runtime_get_running_mode(module_inst));
  146. touch_every_page_func =
  147. wasm_runtime_lookup_function(module_inst, "touch_every_page");
  148. ASSERT_TRUE(touch_every_page_func != NULL);
  149. ret = wasm_runtime_call_wasm(exec_env, touch_every_page_func, 0, wasm_argv);
  150. ASSERT_TRUE(ret);
  151. // check return value: 0xfff8:i64,0x10000fff8:i64,0x1fff8:i32,0x1:i32
  152. i64 = 0xfff8;
  153. ASSERT_EQ(i64, GET_U64_FROM_ADDR(wasm_argv));
  154. i64 = 0x10000fff8;
  155. ASSERT_EQ(i64, GET_U64_FROM_ADDR(wasm_argv + 2));
  156. i32 = 0x1fff8;
  157. ASSERT_EQ(i32, wasm_argv[4]);
  158. i32 = 0x1;
  159. ASSERT_EQ(i32, wasm_argv[5]);
  160. // store at 0x100001000, with value 0xbeefdead
  161. PUT_I64_TO_ADDR(wasm_argv, 0x1000);
  162. PUT_I64_TO_ADDR(wasm_argv + 2, 0xbeefdead);
  163. i64_store_offset_4GB =
  164. wasm_runtime_lookup_function(module_inst, "i64_store_offset_4GB");
  165. ASSERT_TRUE(i64_store_offset_4GB != NULL);
  166. ret = wasm_runtime_call_wasm(exec_env, i64_store_offset_4GB, 4, wasm_argv);
  167. ASSERT_TRUE(ret);
  168. i64_load_offset_4GB =
  169. wasm_runtime_lookup_function(module_inst, "i64_load_offset_4GB");
  170. ASSERT_TRUE(i64_load_offset_4GB != NULL);
  171. ret = wasm_runtime_call_wasm(exec_env, i64_load_offset_4GB, 2, wasm_argv);
  172. ASSERT_TRUE(ret);
  173. // check return value: 0xbeefdead:i64
  174. i64 = 0xbeefdead;
  175. ASSERT_EQ(i64, GET_U64_FROM_ADDR(wasm_argv));
  176. destroy_exec_env();
  177. }
  178. TEST_P(memory64_test_suite, mem64_from_clang)
  179. {
  180. RunningMode running_mode = GetParam();
  181. wasm_function_inst_t test_func;
  182. uint32_t wasm_argv[1], i32;
  183. bool ret;
  184. ret = load_wasm_file("mem64.wasm");
  185. ASSERT_TRUE(ret);
  186. ret = init_exec_env();
  187. ASSERT_TRUE(ret);
  188. ret = wasm_runtime_set_running_mode(module_inst, running_mode);
  189. ASSERT_TRUE(ret);
  190. ASSERT_EQ(running_mode, wasm_runtime_get_running_mode(module_inst));
  191. test_func =
  192. wasm_runtime_lookup_function(module_inst, "test");
  193. ASSERT_TRUE(test_func != NULL);
  194. ret = wasm_runtime_call_wasm(exec_env, test_func, 0, wasm_argv);
  195. ASSERT_TRUE(ret);
  196. i32 = 0x109;
  197. ASSERT_EQ(i32, wasm_argv[0]);
  198. destroy_exec_env();
  199. }
  200. INSTANTIATE_TEST_CASE_P(RunningMode, memory64_test_suite,
  201. testing::ValuesIn(running_mode_supported));