aot_emit_memory_test.cc 9.9 KB

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