aot_emit_memory_test.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "gtest/gtest.h"
  6. #include "bh_platform.h"
  7. #include "bh_read_file.h"
  8. #include "aot_emit_memory.h"
  9. #include "test_helper.h"
  10. #define DEFAULT_CYCLE_TIMES 0xFFFF
  11. #define DEFAULT_MAX_RAND_NUM 0xFFFFFFFF
  12. static std::string CWD;
  13. static std::string MAIN_WASM = "/main.wasm";
  14. static char *WASM_FILE;
  15. class compilation_aot_emit_memory_test : public testing::Test
  16. {
  17. protected:
  18. void SetUp() override
  19. {
  20. CWD = get_test_binary_dir();
  21. WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
  22. AOTCompOption option = { 0 };
  23. option.opt_level = 3;
  24. option.size_level = 3;
  25. option.output_format = AOT_FORMAT_FILE;
  26. /* default value, enable or disable depends on the platform */
  27. option.bounds_checks = 2;
  28. /* default value, enable or disable depends on the platform */
  29. option.stack_bounds_checks = 2;
  30. option.enable_simd = true;
  31. option.enable_aux_stack_check = true;
  32. option.enable_bulk_memory = true;
  33. option.enable_ref_types = true;
  34. const char *wasm_file = WASM_FILE;
  35. unsigned int wasm_file_size = 0;
  36. unsigned char *wasm_file_buf = nullptr;
  37. char error_buf[128] = { 0 };
  38. wasm_file_buf =
  39. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  40. EXPECT_NE(wasm_file_buf, nullptr);
  41. wasm_module = reinterpret_cast<WASMModule *>(wasm_runtime_load(
  42. wasm_file_buf, wasm_file_size, error_buf, sizeof(error_buf)));
  43. EXPECT_NE(wasm_module, nullptr);
  44. comp_data = aot_create_comp_data(wasm_module, NULL, false);
  45. EXPECT_NE(comp_data, nullptr);
  46. // properly init compilation and function context, to do that,
  47. // use as a dummy module(instead of compile the function in it, simply
  48. // test the APIs)
  49. comp_ctx = aot_create_comp_context(comp_data, &option);
  50. EXPECT_NE(comp_ctx, nullptr);
  51. func_ctx = comp_ctx->func_ctxes[0];
  52. EXPECT_NE(func_ctx, nullptr);
  53. }
  54. void TearDown() override
  55. {
  56. aot_destroy_comp_context(comp_ctx);
  57. aot_destroy_comp_data(comp_data);
  58. wasm_runtime_unload(reinterpret_cast<WASMModuleCommon *>(wasm_module));
  59. }
  60. public:
  61. WASMModule *wasm_module = nullptr;
  62. AOTCompData *comp_data = nullptr;
  63. AOTCompContext *comp_ctx = nullptr;
  64. AOTFuncContext *func_ctx = nullptr;
  65. WAMRRuntimeRAII<512 * 1024> runtime;
  66. };
  67. TEST_F(compilation_aot_emit_memory_test, aot_check_memory_overflow)
  68. {
  69. uint32 offset = 64;
  70. uint32 bytes = 4;
  71. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  72. offset = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  73. aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes, false,
  74. NULL);
  75. }
  76. }
  77. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_i32_load)
  78. {
  79. uint32 align = 0;
  80. uint32 offset = 1024;
  81. uint32 bytes = 0;
  82. bool sign = false;
  83. bool atomic = false;
  84. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  85. align = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  86. offset = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  87. bytes = (1 + (rand() % (4 - 1 + 1)));
  88. printf("---%d", aot_compile_op_i32_load(comp_ctx, func_ctx, align,
  89. offset, bytes, sign, atomic));
  90. }
  91. }
  92. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_i64_load)
  93. {
  94. uint32 align = 0;
  95. uint32 offset = 1024;
  96. uint32 bytes = 0;
  97. bool sign = false;
  98. bool atomic = false;
  99. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  100. align = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  101. offset = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  102. bytes = (1 + (rand() % (4 - 1 + 1)));
  103. sign = !sign;
  104. atomic = !atomic;
  105. aot_compile_op_i64_load(comp_ctx, func_ctx, align, offset, bytes, sign,
  106. atomic);
  107. }
  108. }
  109. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_f32_load)
  110. {
  111. uint32 align = 10;
  112. uint32 offset = 10;
  113. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  114. align = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  115. offset = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  116. aot_compile_op_f32_load(comp_ctx, func_ctx, align, offset);
  117. }
  118. }
  119. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_f64_load)
  120. {
  121. uint32 align = 10;
  122. uint32 offset = 10;
  123. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  124. align = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  125. offset = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  126. aot_compile_op_f64_load(comp_ctx, func_ctx, align, offset);
  127. }
  128. }
  129. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_i32_store)
  130. {
  131. uint32 align = 0;
  132. uint32 offset = 0;
  133. uint32 bytes = 0;
  134. bool atomic = false;
  135. EXPECT_FALSE(aot_compile_op_i32_store(comp_ctx, func_ctx, align, offset,
  136. bytes, atomic));
  137. /* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
  138. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  139. bytes = (1 + (rand() % (4 - 1 + 1)));
  140. offset = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  141. align = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  142. atomic = !atomic;
  143. EXPECT_FALSE(aot_compile_op_i32_store(comp_ctx, func_ctx, align, offset,
  144. bytes, atomic));
  145. }
  146. }
  147. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_i64_store)
  148. {
  149. uint32 align = 0;
  150. uint32 offset = 0;
  151. uint32 bytes = 0;
  152. bool atomic = false;
  153. EXPECT_FALSE(aot_compile_op_i64_store(comp_ctx, func_ctx, align, offset,
  154. bytes, atomic));
  155. /* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
  156. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  157. bytes = (1 + (rand() % (8 - 1 + 1)));
  158. offset = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  159. align = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  160. atomic = !atomic;
  161. EXPECT_FALSE(aot_compile_op_i64_store(comp_ctx, func_ctx, align, offset,
  162. bytes, atomic));
  163. }
  164. }
  165. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_f32_store)
  166. {
  167. uint32 align = 0;
  168. uint32 offset = 0;
  169. EXPECT_FALSE(aot_compile_op_f32_store(comp_ctx, func_ctx, align, offset));
  170. /* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
  171. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  172. offset = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  173. align = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  174. EXPECT_FALSE(
  175. aot_compile_op_f32_store(comp_ctx, func_ctx, align, offset));
  176. }
  177. }
  178. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_f64_store)
  179. {
  180. uint32 align = 0;
  181. uint32 offset = 0;
  182. EXPECT_FALSE(aot_compile_op_f64_store(comp_ctx, func_ctx, align, offset));
  183. /* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
  184. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  185. offset = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  186. align = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  187. EXPECT_FALSE(
  188. aot_compile_op_f64_store(comp_ctx, func_ctx, align, offset));
  189. }
  190. }
  191. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_memory_size)
  192. {
  193. aot_compile_op_memory_size(comp_ctx, func_ctx);
  194. }
  195. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_memory_grow)
  196. {
  197. aot_compile_op_memory_grow(comp_ctx, func_ctx);
  198. }
  199. #if WASM_ENABLE_BULK_MEMORY != 0
  200. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_memory_init)
  201. {
  202. uint32 seg_index = 0;
  203. /* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
  204. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  205. seg_index = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  206. aot_compile_op_memory_init(comp_ctx, func_ctx, seg_index);
  207. }
  208. }
  209. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_data_drop)
  210. {
  211. uint32 seg_index = 0;
  212. /* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
  213. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  214. seg_index = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  215. aot_compile_op_data_drop(comp_ctx, func_ctx, seg_index);
  216. }
  217. }
  218. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_memory_copy)
  219. {
  220. aot_compile_op_memory_copy(comp_ctx, func_ctx);
  221. }
  222. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_memory_fill)
  223. {
  224. aot_compile_op_memory_fill(comp_ctx, func_ctx);
  225. }
  226. #endif
  227. #if WASM_ENABLE_SHARED_MEMORY != 0
  228. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_atomic_rmw)
  229. {
  230. uint8 atomic_op = LLVMAtomicRMWBinOpAdd;
  231. uint8 op_type = VALUE_TYPE_I32;
  232. uint32 align = 4;
  233. uint32 offset = 64;
  234. uint32 bytes = 4;
  235. aot_compile_op_atomic_rmw(comp_ctx, func_ctx, atomic_op, op_type, align,
  236. offset, bytes);
  237. }
  238. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_atomic_cmpxchg)
  239. {
  240. uint8 op_type = VALUE_TYPE_I32;
  241. uint32 align = 4;
  242. uint32 offset = 64;
  243. uint32 bytes = 4;
  244. aot_compile_op_atomic_cmpxchg(comp_ctx, func_ctx, op_type, align, offset,
  245. bytes);
  246. }
  247. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_atomic_wait)
  248. {
  249. uint8 op_type = VALUE_TYPE_I32;
  250. uint32 align = 4;
  251. uint32 offset = 64;
  252. uint32 bytes = 4;
  253. aot_compile_op_atomic_wait(comp_ctx, func_ctx, op_type, align, offset,
  254. bytes);
  255. }
  256. TEST_F(compilation_aot_emit_memory_test, aot_compiler_op_atomic_notify)
  257. {
  258. uint32 align = 4;
  259. uint32 offset = 64;
  260. uint32 bytes = 4;
  261. aot_compiler_op_atomic_notify(comp_ctx, func_ctx, align, offset, bytes);
  262. }
  263. #endif