linear_memory_wasm_test.cc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_wasm_no_hw_bound
  25. #else
  26. #define TEST_SUITE_NAME linear_memory_test_suite_wasm
  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 wasm_module;
  48. wasm_module_inst_t wasm_module_inst;
  49. unsigned char *wasm_file_buf;
  50. char error_buf[128];
  51. };
  52. struct ret_env
  53. load_wasm(char *wasm_file_tested, unsigned int app_heap_size)
  54. {
  55. std::string wasm_mem_page = wasm_file_tested;
  56. const char *wasm_file = strdup((CWD + wasm_mem_page).c_str());
  57. wasm_module_inst_t wasm_module_inst = nullptr;
  58. wasm_module_t wasm_module = nullptr;
  59. wasm_exec_env_t exec_env = nullptr;
  60. unsigned char *wasm_file_buf = nullptr;
  61. unsigned int wasm_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. wasm_file_buf =
  67. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  68. if (!wasm_file_buf) {
  69. goto fail;
  70. }
  71. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  72. sizeof(error_buf));
  73. if (!wasm_module) {
  74. memcpy(ret_module_env.error_buf, error_buf, 128);
  75. goto fail;
  76. }
  77. wasm_module_inst = wasm_runtime_instantiate(
  78. wasm_module, stack_size, heap_size, error_buf, sizeof(error_buf));
  79. if (!wasm_module_inst) {
  80. memcpy(ret_module_env.error_buf, error_buf, 128);
  81. goto fail;
  82. }
  83. exec_env = wasm_runtime_create_exec_env(wasm_module_inst, stack_size);
  84. fail:
  85. ret_module_env.exec_env = exec_env;
  86. ret_module_env.wasm_module = wasm_module;
  87. ret_module_env.wasm_module_inst = wasm_module_inst;
  88. ret_module_env.wasm_file_buf = wasm_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.wasm_module_inst) {
  98. wasm_runtime_deinstantiate(module_env.wasm_module_inst);
  99. }
  100. if (module_env.wasm_module) {
  101. wasm_runtime_unload(module_env.wasm_module);
  102. }
  103. if (module_env.wasm_file_buf) {
  104. wasm_runtime_free(module_env.wasm_file_buf);
  105. }
  106. }
  107. TEST_F(TEST_SUITE_NAME, test_wasm_mem_page_count)
  108. {
  109. struct ret_env tmp_module_env;
  110. unsigned int num_normal_wasm = 9;
  111. unsigned int num_error_wasm = 10;
  112. const char *wasm_file_normal[num_normal_wasm] = {
  113. "/wasm_mem_page_01.wasm", "/wasm_mem_page_02.wasm",
  114. "/wasm_mem_page_05.wasm", "/wasm_mem_page_07.wasm",
  115. "/wasm_mem_page_08.wasm", "/wasm_mem_page_09.wasm",
  116. "/wasm_mem_page_10.wasm", "/wasm_mem_page_12.wasm",
  117. "/wasm_mem_page_14.wasm"
  118. };
  119. const char *wasm_file_error[num_error_wasm] = {
  120. "/wasm_mem_page_03.wasm", "/wasm_mem_page_04.wasm",
  121. "/wasm_mem_page_06.wasm", "/wasm_mem_page_11.wasm",
  122. "/wasm_mem_page_13.wasm", "/wasm_mem_page_15.wasm",
  123. "/wasm_mem_page_16.wasm", "/wasm_mem_page_17.wasm",
  124. "/wasm_mem_page_18.wasm", "/wasm_mem_page_19.wasm"
  125. };
  126. // Test normal wasm file.
  127. for (int i = 0; i < num_normal_wasm; i++) {
  128. #if UINTPTR_MAX != UINT64_MAX
  129. // 32 bit do not load this wasm.
  130. if ((0 == strcmp("/wasm_mem_page_12.wasm", wasm_file_normal[i]))
  131. || (0 == strcmp("/wasm_mem_page_14.wasm", wasm_file_normal[i]))) {
  132. continue;
  133. }
  134. #endif
  135. tmp_module_env = load_wasm((char *)wasm_file_normal[i], 16 * 1024);
  136. EXPECT_NE(nullptr, tmp_module_env.wasm_module);
  137. EXPECT_NE(nullptr, tmp_module_env.wasm_file_buf);
  138. #if WASM_DISABLE_HW_BOUND_CHECK == 0
  139. EXPECT_NE(nullptr, tmp_module_env.exec_env);
  140. EXPECT_NE(nullptr, tmp_module_env.wasm_module_inst);
  141. #endif
  142. destroy_module_env(tmp_module_env);
  143. }
  144. // Test error wasm file.
  145. for (int i = 0; i < num_error_wasm; i++) {
  146. tmp_module_env = load_wasm((char *)wasm_file_error[i], 16 * 1024);
  147. if (0 != strlen(tmp_module_env.error_buf)) {
  148. EXPECT_EQ(0, strncmp("WASM module",
  149. (const char *)tmp_module_env.error_buf, 11));
  150. }
  151. destroy_module_env(tmp_module_env);
  152. }
  153. }
  154. TEST_F(TEST_SUITE_NAME, test_wasm_about_app_heap)
  155. {
  156. struct ret_env tmp_module_env;
  157. // Test case: init_page_count = 65536, app heap size = 1.
  158. tmp_module_env = load_wasm((char *)"/wasm_mem_page_03.wasm", 1);
  159. EXPECT_EQ(0, strncmp("WASM module instantiate failed",
  160. (const char *)tmp_module_env.error_buf, 30));
  161. destroy_module_env(tmp_module_env);
  162. // Test case: init_page_count = 65535, app heap size = 65537.
  163. tmp_module_env = load_wasm((char *)"/wasm_mem_page_20.wasm", 65537);
  164. EXPECT_EQ(0, strncmp("WASM module instantiate failed",
  165. (const char *)tmp_module_env.error_buf, 30));
  166. destroy_module_env(tmp_module_env);
  167. }
  168. TEST_F(TEST_SUITE_NAME, test_throw_exception_out_of_bounds)
  169. {
  170. struct ret_env tmp_module_env;
  171. WASMFunctionInstanceCommon *func = nullptr;
  172. bool ret = false;
  173. uint32 argv[1] = { 9999 * 64 * 1024 };
  174. const char *exception = nullptr;
  175. tmp_module_env = load_wasm((char *)"/out_of_bounds.wasm", 16 * 1024);
  176. func =
  177. wasm_runtime_lookup_function(tmp_module_env.wasm_module_inst, "load");
  178. if (!func) {
  179. printf("\nFailed to wasm_runtime_lookup_function!\n");
  180. goto failed_out_of_bounds;
  181. }
  182. ret = wasm_runtime_call_wasm(tmp_module_env.exec_env, func, 1, argv);
  183. if (!ret) {
  184. printf("\nFailed to wasm_runtime_call_wasm!\n");
  185. }
  186. exception = wasm_runtime_get_exception(tmp_module_env.wasm_module_inst);
  187. EXPECT_EQ(0,
  188. strncmp("Exception: out of bounds memory access", exception, 38));
  189. failed_out_of_bounds:
  190. destroy_module_env(tmp_module_env);
  191. }
  192. TEST_F(TEST_SUITE_NAME, test_mem_grow_out_of_bounds)
  193. {
  194. struct ret_env tmp_module_env;
  195. WASMFunctionInstanceCommon *func_mem_grow = nullptr;
  196. WASMFunctionInstanceCommon *func_mem_size = nullptr;
  197. bool ret = false;
  198. // after refactor, the 65536 pages to one 4G page optimization is removed
  199. // the size can be 65536 now, so use 2 + 65535 to test OOB
  200. uint32 argv[1] = { 65535 };
  201. const char *exception = nullptr;
  202. // Test case: module((memory 2)), memory.grow 65535, then memory.size.
  203. tmp_module_env = load_wasm((char *)"/mem_grow_out_of_bounds_01.wasm", 0);
  204. func_mem_grow = wasm_runtime_lookup_function(
  205. tmp_module_env.wasm_module_inst, "mem_grow");
  206. if (!func_mem_grow) {
  207. printf("\nFailed to wasm_runtime_lookup_function!\n");
  208. goto failed_out_of_bounds;
  209. }
  210. func_mem_size = wasm_runtime_lookup_function(
  211. tmp_module_env.wasm_module_inst, "mem_size");
  212. if (!func_mem_size) {
  213. printf("\nFailed to wasm_runtime_lookup_function!\n");
  214. goto failed_out_of_bounds;
  215. }
  216. ret =
  217. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_mem_grow, 1, argv);
  218. if (!ret) {
  219. printf("\nFailed to wasm_runtime_call_wasm!\n");
  220. goto failed_out_of_bounds;
  221. }
  222. EXPECT_EQ(-1, argv[0]);
  223. ret =
  224. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_mem_size, 0, argv);
  225. if (!ret) {
  226. printf("\nFailed to wasm_runtime_call_wasm!\n");
  227. goto failed_out_of_bounds;
  228. }
  229. EXPECT_EQ(2, argv[0]);
  230. // Test case: wasm_runtime_instantiate(heap_size=32768), memory.grow 65535,
  231. // memory.grow 1.
  232. destroy_module_env(tmp_module_env);
  233. tmp_module_env =
  234. load_wasm((char *)"/mem_grow_out_of_bounds_02.wasm", 32768);
  235. func_mem_grow = wasm_runtime_lookup_function(
  236. tmp_module_env.wasm_module_inst, "mem_grow");
  237. if (!func_mem_grow) {
  238. printf("\nFailed to wasm_runtime_lookup_function!\n");
  239. goto failed_out_of_bounds;
  240. }
  241. func_mem_size = wasm_runtime_lookup_function(
  242. tmp_module_env.wasm_module_inst, "mem_size");
  243. if (!func_mem_size) {
  244. printf("\nFailed to wasm_runtime_lookup_function!\n");
  245. goto failed_out_of_bounds;
  246. }
  247. ret =
  248. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_mem_size, 0, argv);
  249. if (!ret) {
  250. printf("\nFailed to wasm_runtime_call_wasm!\n");
  251. goto failed_out_of_bounds;
  252. }
  253. EXPECT_EQ(2, argv[0]);
  254. argv[0] = 65535;
  255. ret =
  256. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_mem_grow, 1, argv);
  257. if (!ret) {
  258. printf("\nFailed to wasm_runtime_call_wasm!\n");
  259. goto failed_out_of_bounds;
  260. }
  261. EXPECT_NE(2, argv[0]);
  262. argv[0] = 1;
  263. ret =
  264. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_mem_grow, 1, argv);
  265. if (!ret) {
  266. printf("\nFailed to wasm_runtime_call_wasm!\n");
  267. goto failed_out_of_bounds;
  268. }
  269. EXPECT_EQ(2, argv[0]);
  270. failed_out_of_bounds:
  271. destroy_module_env(tmp_module_env);
  272. }