linear_memory_aot_test.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "test_helper.h"
  6. #include "gtest/gtest.h"
  7. #include "bh_read_file.h"
  8. #include "wasm_runtime_common.h"
  9. static std::string CWD;
  10. static std::string
  11. get_binary_path()
  12. {
  13. char cwd[1024];
  14. memset(cwd, 0, 1024);
  15. if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
  16. }
  17. char *path_end = strrchr(cwd, '/');
  18. if (path_end != NULL) {
  19. *path_end = '\0';
  20. }
  21. return std::string(cwd);
  22. }
  23. #if WASM_DISABLE_HW_BOUND_CHECK != 0
  24. #define TEST_SUITE_NAME linear_memory_test_suite_aot_no_hw_bound
  25. #else
  26. #define TEST_SUITE_NAME linear_memory_test_suite_aot
  27. #endif
  28. class TEST_SUITE_NAME : public testing::Test
  29. {
  30. protected:
  31. // You should make the members protected s.t. they can be
  32. // accessed from sub-classes.
  33. // virtual void SetUp() will be called before each test is run. You
  34. // should define it if you need to initialize the varaibles.
  35. // Otherwise, this can be skipped.
  36. virtual void SetUp() {}
  37. static void SetUpTestCase() { CWD = get_binary_path(); }
  38. // virtual void TearDown() will be called after each test is run.
  39. // You should define it if there is cleanup work to do. Otherwise,
  40. // you don't have to provide it.
  41. //
  42. virtual void TearDown() {}
  43. WAMRRuntimeRAII<512 * 1024> runtime;
  44. };
  45. struct ret_env {
  46. wasm_exec_env_t exec_env;
  47. wasm_module_t aot_module;
  48. wasm_module_inst_t aot_module_inst;
  49. unsigned char *aot_file_buf;
  50. char error_buf[128];
  51. };
  52. struct ret_env
  53. load_aot(char *aot_file_tested, unsigned int app_heap_size)
  54. {
  55. std::string aot_mem_page = aot_file_tested;
  56. const char *aot_file = strdup((CWD + aot_mem_page).c_str());
  57. wasm_module_inst_t aot_module_inst = nullptr;
  58. wasm_module_t aot_module = nullptr;
  59. wasm_exec_env_t exec_env = nullptr;
  60. unsigned char *aot_file_buf = nullptr;
  61. unsigned int aot_file_size = 0;
  62. unsigned int stack_size = 16 * 1024, heap_size = app_heap_size;
  63. char error_buf[128] = { 0 };
  64. struct ret_env ret_module_env;
  65. memset(ret_module_env.error_buf, 0, 128);
  66. aot_file_buf =
  67. (unsigned char *)bh_read_file_to_buffer(aot_file, &aot_file_size);
  68. if (!aot_file_buf) {
  69. goto fail;
  70. }
  71. aot_module = wasm_runtime_load(aot_file_buf, aot_file_size, error_buf,
  72. sizeof(error_buf));
  73. if (!aot_module) {
  74. memcpy(ret_module_env.error_buf, error_buf, 128);
  75. goto fail;
  76. }
  77. aot_module_inst = wasm_runtime_instantiate(
  78. aot_module, stack_size, heap_size, error_buf, sizeof(error_buf));
  79. if (!aot_module_inst) {
  80. memcpy(ret_module_env.error_buf, error_buf, 128);
  81. goto fail;
  82. }
  83. exec_env = wasm_runtime_create_exec_env(aot_module_inst, stack_size);
  84. fail:
  85. ret_module_env.exec_env = exec_env;
  86. ret_module_env.aot_module = aot_module;
  87. ret_module_env.aot_module_inst = aot_module_inst;
  88. ret_module_env.aot_file_buf = aot_file_buf;
  89. return ret_module_env;
  90. }
  91. void
  92. destroy_module_env(struct ret_env module_env)
  93. {
  94. if (module_env.exec_env) {
  95. wasm_runtime_destroy_exec_env(module_env.exec_env);
  96. }
  97. if (module_env.aot_module_inst) {
  98. wasm_runtime_deinstantiate(module_env.aot_module_inst);
  99. }
  100. if (module_env.aot_module) {
  101. wasm_runtime_unload(module_env.aot_module);
  102. }
  103. if (module_env.aot_file_buf) {
  104. wasm_runtime_free(module_env.aot_file_buf);
  105. }
  106. }
  107. TEST_F(TEST_SUITE_NAME, test_aot_mem_page_count)
  108. {
  109. struct ret_env tmp_module_env;
  110. const unsigned int num_normal_aot = 9;
  111. const unsigned int num_error_aot = 2;
  112. #if UINTPTR_MAX == UINT64_MAX
  113. const char *aot_file_normal[num_normal_aot] = {
  114. "/mem_page_01.aot", "/mem_page_02.aot", "/mem_page_05.aot",
  115. "/mem_page_07.aot", "/mem_page_08.aot", "/mem_page_09.aot",
  116. "/mem_page_10.aot", "/mem_page_12.aot", "/mem_page_14.aot"
  117. };
  118. const char *aot_file_error[num_error_aot] = { "/mem_page_03.aot",
  119. "/mem_page_16.aot" };
  120. #else
  121. const char *aot_file_normal[num_normal_aot] = {
  122. "/mem_page_01_32.aot", "/mem_page_02_32.aot", "/mem_page_05_32.aot",
  123. "/mem_page_07_32.aot", "/mem_page_08_32.aot", "/mem_page_09_32.aot",
  124. "/mem_page_10_32.aot", "/mem_page_12_32.aot", "/mem_page_14_32.aot"
  125. };
  126. const char *aot_file_error[num_error_aot] = { "/mem_page_03_32.aot",
  127. "/mem_page_16_32.aot" };
  128. #endif
  129. // Test normal wasm file.
  130. for (int i = 0; i < num_normal_aot; i++) {
  131. #if UINTPTR_MAX != UINT64_MAX
  132. // 32 bit do not load this wasm.
  133. if ((0 == strcmp("/mem_page_14_32.aot", aot_file_normal[i]))) {
  134. continue;
  135. }
  136. #endif
  137. tmp_module_env = load_aot((char *)aot_file_normal[i], 16 * 1024);
  138. EXPECT_NE(nullptr, tmp_module_env.aot_module);
  139. EXPECT_NE(nullptr, tmp_module_env.aot_file_buf);
  140. destroy_module_env(tmp_module_env);
  141. }
  142. // Test error wasm file.
  143. for (int i = 0; i < num_error_aot; i++) {
  144. tmp_module_env = load_aot((char *)aot_file_error[i], 16 * 1024);
  145. if (0 != strlen(tmp_module_env.error_buf)) {
  146. /* 3 and 16 are for legit for loader, the init and max page count
  147. * can be 65536, but they can't allocate any host managed heap, so
  148. * instantiating errors */
  149. EXPECT_EQ(0, strncmp("AOT module instantiate failed",
  150. (const char *)tmp_module_env.error_buf, 29));
  151. printf("%s\n", tmp_module_env.error_buf);
  152. }
  153. destroy_module_env(tmp_module_env);
  154. }
  155. }
  156. TEST_F(TEST_SUITE_NAME, test_aot_about_app_heap)
  157. {
  158. struct ret_env tmp_module_env;
  159. // Test case: init_page_count = 65536, app heap size = 1.
  160. #if UINTPTR_MAX == UINT64_MAX
  161. tmp_module_env = load_aot((char *)"/mem_page_03.aot", 1);
  162. #else
  163. tmp_module_env = load_aot((char *)"/mem_page_03_32.aot", 1);
  164. #endif
  165. EXPECT_EQ(
  166. 0, strncmp("AOT module", (const char *)tmp_module_env.error_buf, 10));
  167. destroy_module_env(tmp_module_env);
  168. // Test case: init_page_count = 65535, app heap size = 65537.
  169. #if UINTPTR_MAX == UINT64_MAX
  170. tmp_module_env = load_aot((char *)"/mem_page_20.aot", 65537);
  171. #else
  172. tmp_module_env = load_aot((char *)"/mem_page_20_32.aot", 65537);
  173. #endif
  174. EXPECT_EQ(
  175. 0, strncmp("AOT module", (const char *)tmp_module_env.error_buf, 10));
  176. destroy_module_env(tmp_module_env);
  177. }
  178. TEST_F(TEST_SUITE_NAME, test_throw_exception_out_of_bounds)
  179. {
  180. struct ret_env tmp_module_env;
  181. WASMFunctionInstanceCommon *func = nullptr;
  182. bool ret = false;
  183. uint32 argv[1] = { 9999 * 64 * 1024 };
  184. const char *exception = nullptr;
  185. /* TODO: use no_hw_bounds version when disable */
  186. #if UINTPTR_MAX == UINT64_MAX
  187. tmp_module_env = load_aot((char *)"/out_of_bounds.aot", 16 * 1024);
  188. #else
  189. tmp_module_env = load_aot((char *)"/out_of_bounds_32.aot", 16 * 1024);
  190. #endif
  191. func = wasm_runtime_lookup_function(tmp_module_env.aot_module_inst, "load");
  192. if (!func) {
  193. printf("\nFailed to wasm_runtime_lookup_function!\n");
  194. goto failed_out_of_bounds;
  195. }
  196. ret = wasm_runtime_call_wasm(tmp_module_env.exec_env, func, 1, argv);
  197. if (!ret) {
  198. printf("\nFailed to wasm_runtime_call_wasm!\n");
  199. }
  200. exception = wasm_runtime_get_exception(tmp_module_env.aot_module_inst);
  201. EXPECT_EQ(0,
  202. strncmp("Exception: out of bounds memory access", exception, 38));
  203. failed_out_of_bounds:
  204. destroy_module_env(tmp_module_env);
  205. }
  206. TEST_F(TEST_SUITE_NAME, test_mem_grow_out_of_bounds)
  207. {
  208. struct ret_env tmp_module_env;
  209. WASMFunctionInstanceCommon *func_mem_grow = nullptr;
  210. WASMFunctionInstanceCommon *func_mem_size = nullptr;
  211. bool ret = false;
  212. uint32 argv[1] = { 65535 };
  213. const char *exception = nullptr;
  214. /* TODO: use no_hw_bounds version when disable */
  215. // Test case: module((memory 2)), memory.grow 65535, then memory.size.
  216. #if UINTPTR_MAX == UINT64_MAX
  217. tmp_module_env = load_aot((char *)"/mem_grow_out_of_bounds_01.aot", 0);
  218. #else
  219. tmp_module_env = load_aot((char *)"/mem_grow_out_of_bounds_01_32.aot", 0);
  220. #endif
  221. func_mem_grow = wasm_runtime_lookup_function(tmp_module_env.aot_module_inst,
  222. "mem_grow");
  223. if (!func_mem_grow) {
  224. printf("\nFailed to wasm_runtime_lookup_function!\n");
  225. goto failed_out_of_bounds;
  226. }
  227. func_mem_size = wasm_runtime_lookup_function(tmp_module_env.aot_module_inst,
  228. "mem_size");
  229. if (!func_mem_size) {
  230. printf("\nFailed to wasm_runtime_lookup_function!\n");
  231. goto failed_out_of_bounds;
  232. }
  233. ret =
  234. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_mem_grow, 1, argv);
  235. if (!ret) {
  236. printf("\nFailed to wasm_runtime_call_wasm!\n");
  237. goto failed_out_of_bounds;
  238. }
  239. EXPECT_EQ(-1, argv[0]);
  240. ret =
  241. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_mem_size, 0, argv);
  242. if (!ret) {
  243. printf("\nFailed to wasm_runtime_call_wasm!\n");
  244. goto failed_out_of_bounds;
  245. }
  246. EXPECT_EQ(2, argv[0]);
  247. // Test case: wasm_runtime_instantiate(heap_size=32768), memory.grow 65534,
  248. // memory.grow 1.
  249. destroy_module_env(tmp_module_env);
  250. #if UINTPTR_MAX == UINT64_MAX
  251. tmp_module_env = load_aot((char *)"/mem_grow_out_of_bounds_02.aot", 32768);
  252. #else
  253. tmp_module_env =
  254. load_aot((char *)"/mem_grow_out_of_bounds_02_32.aot", 32768);
  255. #endif
  256. func_mem_grow = wasm_runtime_lookup_function(tmp_module_env.aot_module_inst,
  257. "mem_grow");
  258. if (!func_mem_grow) {
  259. printf("\nFailed to wasm_runtime_lookup_function!\n");
  260. goto failed_out_of_bounds;
  261. }
  262. func_mem_size = wasm_runtime_lookup_function(tmp_module_env.aot_module_inst,
  263. "mem_size");
  264. if (!func_mem_size) {
  265. printf("\nFailed to wasm_runtime_lookup_function!\n");
  266. goto failed_out_of_bounds;
  267. }
  268. ret =
  269. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_mem_size, 0, argv);
  270. if (!ret) {
  271. printf("\nFailed to wasm_runtime_call_wasm!\n");
  272. goto failed_out_of_bounds;
  273. }
  274. EXPECT_EQ(2, argv[0]);
  275. argv[0] = 65534;
  276. ret =
  277. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_mem_grow, 1, argv);
  278. if (!ret) {
  279. printf("\nFailed to wasm_runtime_call_wasm!\n");
  280. goto failed_out_of_bounds;
  281. }
  282. #if UINTPTR_MAX == UINT64_MAX
  283. EXPECT_EQ(2, argv[0]);
  284. #else
  285. EXPECT_EQ(-1, argv[0]);
  286. #endif
  287. argv[0] = 1;
  288. ret =
  289. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_mem_grow, 1, argv);
  290. if (!ret) {
  291. printf("\nFailed to wasm_runtime_call_wasm!\n");
  292. goto failed_out_of_bounds;
  293. }
  294. #if UINTPTR_MAX == UINT64_MAX
  295. EXPECT_EQ(-1, argv[0]);
  296. #else
  297. EXPECT_EQ(2, argv[0]);
  298. #endif
  299. failed_out_of_bounds:
  300. destroy_module_env(tmp_module_env);
  301. }