aot_emit_memory_test.cc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. NULL);
  88. }
  89. }
  90. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_i32_load)
  91. {
  92. uint32 align = 0;
  93. uint32 offset = 1024;
  94. uint32 bytes = 0;
  95. bool sign = false;
  96. bool atomic = false;
  97. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  98. align = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  99. offset = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  100. bytes = (1 + (rand() % (4 - 1 + 1)));
  101. printf("---%d", aot_compile_op_i32_load(comp_ctx, func_ctx, align,
  102. offset, bytes, sign, atomic));
  103. }
  104. }
  105. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_i64_load)
  106. {
  107. uint32 align = 0;
  108. uint32 offset = 1024;
  109. uint32 bytes = 0;
  110. bool sign = false;
  111. bool atomic = false;
  112. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  113. align = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  114. offset = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  115. bytes = (1 + (rand() % (4 - 1 + 1)));
  116. sign = !sign;
  117. atomic = !atomic;
  118. aot_compile_op_i64_load(comp_ctx, func_ctx, align, offset, bytes, sign,
  119. atomic);
  120. }
  121. }
  122. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_f32_load)
  123. {
  124. uint32 align = 10;
  125. uint32 offset = 10;
  126. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  127. align = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  128. offset = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  129. aot_compile_op_f32_load(comp_ctx, func_ctx, align, offset);
  130. }
  131. }
  132. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_f64_load)
  133. {
  134. uint32 align = 10;
  135. uint32 offset = 10;
  136. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  137. align = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  138. offset = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
  139. aot_compile_op_f64_load(comp_ctx, func_ctx, align, offset);
  140. }
  141. }
  142. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_i32_store)
  143. {
  144. uint32 align = 0;
  145. uint32 offset = 0;
  146. uint32 bytes = 0;
  147. bool atomic = false;
  148. EXPECT_FALSE(aot_compile_op_i32_store(comp_ctx, func_ctx, align, offset,
  149. bytes, atomic));
  150. /* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
  151. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  152. bytes = (1 + (rand() % (4 - 1 + 1)));
  153. offset = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  154. align = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  155. atomic = !atomic;
  156. EXPECT_FALSE(aot_compile_op_i32_store(comp_ctx, func_ctx, align, offset,
  157. bytes, atomic));
  158. }
  159. }
  160. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_i64_store)
  161. {
  162. uint32 align = 0;
  163. uint32 offset = 0;
  164. uint32 bytes = 0;
  165. bool atomic = false;
  166. EXPECT_FALSE(aot_compile_op_i64_store(comp_ctx, func_ctx, align, offset,
  167. bytes, atomic));
  168. /* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
  169. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  170. bytes = (1 + (rand() % (8 - 1 + 1)));
  171. offset = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  172. align = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  173. atomic = !atomic;
  174. EXPECT_FALSE(aot_compile_op_i64_store(comp_ctx, func_ctx, align, offset,
  175. bytes, atomic));
  176. }
  177. }
  178. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_f32_store)
  179. {
  180. uint32 align = 0;
  181. uint32 offset = 0;
  182. EXPECT_FALSE(aot_compile_op_f32_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_f32_store(comp_ctx, func_ctx, align, offset));
  189. }
  190. }
  191. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_f64_store)
  192. {
  193. uint32 align = 0;
  194. uint32 offset = 0;
  195. EXPECT_FALSE(aot_compile_op_f64_store(comp_ctx, func_ctx, align, offset));
  196. /* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
  197. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  198. offset = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  199. align = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  200. EXPECT_FALSE(
  201. aot_compile_op_f64_store(comp_ctx, func_ctx, align, offset));
  202. }
  203. }
  204. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_memory_size)
  205. {
  206. aot_compile_op_memory_size(comp_ctx, func_ctx);
  207. }
  208. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_memory_grow)
  209. {
  210. aot_compile_op_memory_grow(comp_ctx, func_ctx);
  211. }
  212. #if WASM_ENABLE_BULK_MEMORY != 0
  213. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_memory_init)
  214. {
  215. uint32 seg_index = 0;
  216. /* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
  217. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  218. seg_index = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  219. aot_compile_op_memory_init(comp_ctx, func_ctx, seg_index);
  220. }
  221. }
  222. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_data_drop)
  223. {
  224. uint32 seg_index = 0;
  225. /* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
  226. for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
  227. seg_index = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
  228. aot_compile_op_data_drop(comp_ctx, func_ctx, seg_index);
  229. }
  230. }
  231. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_memory_copy)
  232. {
  233. aot_compile_op_memory_copy(comp_ctx, func_ctx);
  234. }
  235. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_memory_fill)
  236. {
  237. aot_compile_op_memory_fill(comp_ctx, func_ctx);
  238. }
  239. #endif
  240. #if WASM_ENABLE_SHARED_MEMORY != 0
  241. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_atomic_rmw)
  242. {
  243. uint8 atomic_op = LLVMAtomicRMWBinOpAdd;
  244. uint8 op_type = VALUE_TYPE_I32;
  245. uint32 align = 4;
  246. uint32 offset = 64;
  247. uint32 bytes = 4;
  248. aot_compile_op_atomic_rmw(comp_ctx, func_ctx, atomic_op, op_type, align,
  249. offset, bytes);
  250. }
  251. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_atomic_cmpxchg)
  252. {
  253. uint8 op_type = VALUE_TYPE_I32;
  254. uint32 align = 4;
  255. uint32 offset = 64;
  256. uint32 bytes = 4;
  257. aot_compile_op_atomic_cmpxchg(comp_ctx, func_ctx, op_type, align, offset,
  258. bytes);
  259. }
  260. TEST_F(compilation_aot_emit_memory_test, aot_compile_op_atomic_wait)
  261. {
  262. uint8 op_type = VALUE_TYPE_I32;
  263. uint32 align = 4;
  264. uint32 offset = 64;
  265. uint32 bytes = 4;
  266. aot_compile_op_atomic_wait(comp_ctx, func_ctx, op_type, align, offset,
  267. bytes);
  268. }
  269. TEST_F(compilation_aot_emit_memory_test, aot_compiler_op_atomic_notify)
  270. {
  271. uint32 align = 4;
  272. uint32 offset = 64;
  273. uint32 bytes = 4;
  274. aot_compiler_op_atomic_notify(comp_ctx, func_ctx, align, offset, bytes);
  275. }
  276. #endif