linear_memory_wasm_test.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 variables.
  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. const char *wasm_file_normal[9] = {
  111. "/wasm_mem_page_01.wasm", "/wasm_mem_page_02.wasm",
  112. "/wasm_mem_page_05.wasm", "/wasm_mem_page_07.wasm",
  113. "/wasm_mem_page_08.wasm", "/wasm_mem_page_09.wasm",
  114. "/wasm_mem_page_10.wasm", "/wasm_mem_page_12.wasm",
  115. "/wasm_mem_page_14.wasm"
  116. };
  117. unsigned int num_normal_wasm =
  118. sizeof(wasm_file_normal) / sizeof(wasm_file_normal[0]);
  119. const char *wasm_file_error[10] = {
  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. unsigned int num_error_wasm =
  127. sizeof(wasm_file_error) / sizeof(wasm_file_error[0]);
  128. // Test normal wasm file.
  129. for (int i = 0; i < num_normal_wasm; i++) {
  130. #if UINTPTR_MAX != UINT64_MAX
  131. // 32 bit do not load this wasm.
  132. if ((0 == strcmp("/wasm_mem_page_12.wasm", wasm_file_normal[i]))
  133. || (0 == strcmp("/wasm_mem_page_14.wasm", wasm_file_normal[i]))) {
  134. continue;
  135. }
  136. #endif
  137. tmp_module_env = load_wasm((char *)wasm_file_normal[i], 16 * 1024);
  138. EXPECT_NE(nullptr, tmp_module_env.wasm_module);
  139. EXPECT_NE(nullptr, tmp_module_env.wasm_file_buf);
  140. #if WASM_DISABLE_HW_BOUND_CHECK == 0
  141. EXPECT_NE(nullptr, tmp_module_env.exec_env);
  142. EXPECT_NE(nullptr, tmp_module_env.wasm_module_inst);
  143. #endif
  144. destroy_module_env(tmp_module_env);
  145. }
  146. // Test error wasm file.
  147. for (int i = 0; i < num_error_wasm; i++) {
  148. tmp_module_env = load_wasm((char *)wasm_file_error[i], 16 * 1024);
  149. if (0 != strlen(tmp_module_env.error_buf)) {
  150. EXPECT_EQ(0, strncmp("WASM module",
  151. (const char *)tmp_module_env.error_buf, 11));
  152. }
  153. destroy_module_env(tmp_module_env);
  154. }
  155. }
  156. TEST_F(TEST_SUITE_NAME, test_wasm_about_app_heap)
  157. {
  158. struct ret_env tmp_module_env;
  159. // Test case: init_page_count = 65536, app heap size = 1.
  160. tmp_module_env = load_wasm((char *)"/wasm_mem_page_03.wasm", 1);
  161. EXPECT_EQ(0, strncmp("WASM module instantiate failed",
  162. (const char *)tmp_module_env.error_buf, 30));
  163. destroy_module_env(tmp_module_env);
  164. // Test case: init_page_count = 65535, app heap size = 65537.
  165. tmp_module_env = load_wasm((char *)"/wasm_mem_page_20.wasm", 65537);
  166. EXPECT_EQ(0, strncmp("WASM module instantiate failed",
  167. (const char *)tmp_module_env.error_buf, 30));
  168. destroy_module_env(tmp_module_env);
  169. }
  170. TEST_F(TEST_SUITE_NAME, test_throw_exception_out_of_bounds)
  171. {
  172. struct ret_env tmp_module_env;
  173. WASMFunctionInstanceCommon *func = nullptr;
  174. bool ret = false;
  175. uint32 argv[1] = { 9999 * 64 * 1024 };
  176. const char *exception = nullptr;
  177. tmp_module_env = load_wasm((char *)"/out_of_bounds.wasm", 16 * 1024);
  178. func =
  179. wasm_runtime_lookup_function(tmp_module_env.wasm_module_inst, "load");
  180. if (!func) {
  181. printf("\nFailed to wasm_runtime_lookup_function!\n");
  182. goto failed_out_of_bounds;
  183. }
  184. ret = wasm_runtime_call_wasm(tmp_module_env.exec_env, func, 1, argv);
  185. if (!ret) {
  186. printf("\nFailed to wasm_runtime_call_wasm!\n");
  187. }
  188. exception = wasm_runtime_get_exception(tmp_module_env.wasm_module_inst);
  189. EXPECT_EQ(0,
  190. strncmp("Exception: out of bounds memory access", exception, 38));
  191. failed_out_of_bounds:
  192. destroy_module_env(tmp_module_env);
  193. }
  194. TEST_F(TEST_SUITE_NAME, test_mem_grow_out_of_bounds)
  195. {
  196. struct ret_env tmp_module_env;
  197. WASMFunctionInstanceCommon *func_mem_grow = nullptr;
  198. WASMFunctionInstanceCommon *func_mem_size = nullptr;
  199. bool ret = false;
  200. // after refactor, the 65536 pages to one 4G page optimization is removed
  201. // the size can be 65536 now, so use 2 + 65535 to test OOB
  202. uint32 argv[1] = { 65535 };
  203. const char *exception = nullptr;
  204. // Test case: module((memory 2)), memory.grow 65535, then memory.size.
  205. tmp_module_env = load_wasm((char *)"/mem_grow_out_of_bounds_01.wasm", 0);
  206. func_mem_grow = wasm_runtime_lookup_function(
  207. tmp_module_env.wasm_module_inst, "mem_grow");
  208. if (!func_mem_grow) {
  209. printf("\nFailed to wasm_runtime_lookup_function!\n");
  210. goto failed_out_of_bounds;
  211. }
  212. func_mem_size = wasm_runtime_lookup_function(
  213. tmp_module_env.wasm_module_inst, "mem_size");
  214. if (!func_mem_size) {
  215. printf("\nFailed to wasm_runtime_lookup_function!\n");
  216. goto failed_out_of_bounds;
  217. }
  218. ret =
  219. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_mem_grow, 1, argv);
  220. if (!ret) {
  221. printf("\nFailed to wasm_runtime_call_wasm!\n");
  222. goto failed_out_of_bounds;
  223. }
  224. EXPECT_EQ(-1, argv[0]);
  225. ret =
  226. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_mem_size, 0, argv);
  227. if (!ret) {
  228. printf("\nFailed to wasm_runtime_call_wasm!\n");
  229. goto failed_out_of_bounds;
  230. }
  231. EXPECT_EQ(2, argv[0]);
  232. // Test case: wasm_runtime_instantiate(heap_size=32768), memory.grow 65535,
  233. // memory.grow 1.
  234. destroy_module_env(tmp_module_env);
  235. tmp_module_env =
  236. load_wasm((char *)"/mem_grow_out_of_bounds_02.wasm", 32768);
  237. func_mem_grow = wasm_runtime_lookup_function(
  238. tmp_module_env.wasm_module_inst, "mem_grow");
  239. if (!func_mem_grow) {
  240. printf("\nFailed to wasm_runtime_lookup_function!\n");
  241. goto failed_out_of_bounds;
  242. }
  243. func_mem_size = wasm_runtime_lookup_function(
  244. tmp_module_env.wasm_module_inst, "mem_size");
  245. if (!func_mem_size) {
  246. printf("\nFailed to wasm_runtime_lookup_function!\n");
  247. goto failed_out_of_bounds;
  248. }
  249. ret =
  250. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_mem_size, 0, argv);
  251. if (!ret) {
  252. printf("\nFailed to wasm_runtime_call_wasm!\n");
  253. goto failed_out_of_bounds;
  254. }
  255. EXPECT_EQ(2, argv[0]);
  256. argv[0] = 65535;
  257. ret =
  258. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_mem_grow, 1, argv);
  259. if (!ret) {
  260. printf("\nFailed to wasm_runtime_call_wasm!\n");
  261. goto failed_out_of_bounds;
  262. }
  263. EXPECT_NE(2, argv[0]);
  264. argv[0] = 1;
  265. ret =
  266. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_mem_grow, 1, argv);
  267. if (!ret) {
  268. printf("\nFailed to wasm_runtime_call_wasm!\n");
  269. goto failed_out_of_bounds;
  270. }
  271. EXPECT_EQ(2, argv[0]);
  272. failed_out_of_bounds:
  273. destroy_module_env(tmp_module_env);
  274. }