shared_heap_test.cc 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. /*
  2. * Copyright (C) 2024 Xiaomi Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "platform_internal.h"
  6. #include "test_helper.h"
  7. #include "gtest/gtest.h"
  8. #include "bh_read_file.h"
  9. #include "wasm_runtime_common.h"
  10. #include "bh_platform.h"
  11. #include <gtest/gtest-spi.h>
  12. #include <vector>
  13. class shared_heap_test : public testing::Test
  14. {
  15. protected:
  16. virtual void SetUp() {}
  17. static void SetUpTestCase() {}
  18. virtual void TearDown() {}
  19. WAMRRuntimeRAII<512 * 1024> runtime;
  20. };
  21. struct ret_env {
  22. wasm_exec_env_t exec_env;
  23. wasm_module_t wasm_module;
  24. wasm_module_inst_t wasm_module_inst;
  25. unsigned char *wasm_file_buf;
  26. char error_buf[128];
  27. };
  28. static void
  29. destroy_module_env(struct ret_env module_env);
  30. static bool
  31. load_wasm(const char *wasm_file_tested, unsigned int app_heap_size,
  32. ret_env &ret_module_env)
  33. {
  34. char *wasm_file = strdup(wasm_file_tested);
  35. unsigned int wasm_file_size = 0;
  36. unsigned int stack_size = 16 * 1024, heap_size = app_heap_size;
  37. char error_buf[128] = {};
  38. ret_module_env.wasm_file_buf =
  39. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  40. if (!ret_module_env.wasm_file_buf) {
  41. goto fail;
  42. }
  43. ret_module_env.wasm_module =
  44. wasm_runtime_load(ret_module_env.wasm_file_buf, wasm_file_size,
  45. error_buf, sizeof(error_buf));
  46. if (!ret_module_env.wasm_module) {
  47. memcpy(ret_module_env.error_buf, error_buf, 128);
  48. goto fail;
  49. }
  50. ret_module_env.wasm_module_inst =
  51. wasm_runtime_instantiate(ret_module_env.wasm_module, stack_size,
  52. heap_size, error_buf, sizeof(error_buf));
  53. if (!ret_module_env.wasm_module_inst) {
  54. memcpy(ret_module_env.error_buf, error_buf, 128);
  55. goto fail;
  56. }
  57. ret_module_env.exec_env = wasm_runtime_create_exec_env(
  58. ret_module_env.wasm_module_inst, stack_size);
  59. if (!ret_module_env.exec_env) {
  60. goto fail;
  61. }
  62. free(wasm_file);
  63. return true;
  64. fail:
  65. free(wasm_file);
  66. destroy_module_env(ret_module_env);
  67. return false;
  68. }
  69. void
  70. destroy_module_env(struct ret_env module_env)
  71. {
  72. if (module_env.exec_env) {
  73. wasm_runtime_destroy_exec_env(module_env.exec_env);
  74. }
  75. if (module_env.wasm_module_inst) {
  76. wasm_runtime_deinstantiate(module_env.wasm_module_inst);
  77. }
  78. if (module_env.wasm_module) {
  79. wasm_runtime_unload(module_env.wasm_module);
  80. }
  81. if (module_env.wasm_file_buf) {
  82. wasm_runtime_free(module_env.wasm_file_buf);
  83. }
  84. }
  85. static void
  86. test_shared_heap(WASMSharedHeap *shared_heap, const char *file,
  87. const char *func_name, uint32 argc, uint32 argv[])
  88. {
  89. struct ret_env tmp_module_env;
  90. WASMFunctionInstanceCommon *func_test = nullptr;
  91. bool ret = false;
  92. if (!load_wasm((char *)file, 0, tmp_module_env)) {
  93. ADD_FAILURE() << "Failed to load wasm file\n";
  94. goto fail0;
  95. }
  96. if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst,
  97. shared_heap)) {
  98. ADD_FAILURE() << "Failed to attach shared heap\n";
  99. goto fail1;
  100. }
  101. func_test = wasm_runtime_lookup_function(tmp_module_env.wasm_module_inst,
  102. func_name);
  103. if (!func_test) {
  104. ADD_FAILURE() << "Failed to wasm_runtime_lookup_function!\n";
  105. goto fail2;
  106. }
  107. ret =
  108. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_test, argc, argv);
  109. if (!ret) {
  110. const char *s =
  111. wasm_runtime_get_exception(tmp_module_env.wasm_module_inst);
  112. ADD_FAILURE() << "Failed to wasm_runtime_call_wasm with "
  113. << "exception: " << s;
  114. }
  115. fail2:
  116. wasm_runtime_detach_shared_heap(tmp_module_env.wasm_module_inst);
  117. fail1:
  118. destroy_module_env(tmp_module_env);
  119. fail0:
  120. return;
  121. }
  122. TEST_F(shared_heap_test, test_shared_heap_basic)
  123. {
  124. SharedHeapInitArgs args = {};
  125. WASMSharedHeap *shared_heap = nullptr;
  126. uint32 argv[1] = {};
  127. args.size = os_getpagesize();
  128. shared_heap = wasm_runtime_create_shared_heap(&args);
  129. if (!shared_heap) {
  130. FAIL() << "Failed to create shared heap";
  131. }
  132. test_shared_heap(shared_heap, "test.wasm", "test", 0, argv);
  133. EXPECT_EQ(10, argv[0]);
  134. test_shared_heap(shared_heap, "test.aot", "test", 0, argv);
  135. EXPECT_EQ(10, argv[0]);
  136. test_shared_heap(shared_heap, "test_chain.aot", "test", 0, argv);
  137. EXPECT_EQ(10, argv[0]);
  138. }
  139. TEST_F(shared_heap_test, test_shared_heap_malloc_fail)
  140. {
  141. SharedHeapInitArgs args = {};
  142. WASMSharedHeap *shared_heap = nullptr;
  143. uint32 argv[1] = {};
  144. args.size = os_getpagesize();
  145. shared_heap = wasm_runtime_create_shared_heap(&args);
  146. if (!shared_heap) {
  147. FAIL() << "Failed to create shared heap";
  148. }
  149. argv[0] = os_getpagesize();
  150. test_shared_heap(shared_heap, "test.wasm", "test_malloc_fail", 1, argv);
  151. EXPECT_EQ(1, argv[0]);
  152. argv[0] = os_getpagesize();
  153. test_shared_heap(shared_heap, "test.aot", "test_malloc_fail", 1, argv);
  154. EXPECT_EQ(1, argv[0]);
  155. argv[0] = os_getpagesize();
  156. test_shared_heap(shared_heap, "test_chain.aot", "test_malloc_fail", 1,
  157. argv);
  158. EXPECT_EQ(1, argv[0]);
  159. }
  160. TEST_F(shared_heap_test, test_preallocated_shared_heap_malloc_fail)
  161. {
  162. SharedHeapInitArgs args = {};
  163. WASMSharedHeap *shared_heap = nullptr;
  164. uint32 argv[1] = {}, BUF_SIZE = os_getpagesize();
  165. uint8 *preallocated_buf_ptr = nullptr;
  166. ASSERT_GT(BUF_SIZE, 0u);
  167. std::vector<uint8> preallocated_buf(BUF_SIZE);
  168. preallocated_buf_ptr = preallocated_buf.data();
  169. ASSERT_NE(preallocated_buf_ptr, nullptr);
  170. /* create a preallocated shared heap */
  171. args.pre_allocated_addr = preallocated_buf_ptr;
  172. args.size = BUF_SIZE;
  173. shared_heap = wasm_runtime_create_shared_heap(&args);
  174. if (!shared_heap) {
  175. FAIL() << "Create preallocated shared heap failed.\n";
  176. }
  177. /* test wasm can't malloc with preallocated shared heap */
  178. argv[0] = 1024;
  179. test_shared_heap(shared_heap, "test.wasm", "my_shared_heap_malloc", 1,
  180. argv);
  181. EXPECT_EQ(0, argv[0]);
  182. argv[0] = 1024;
  183. test_shared_heap(shared_heap, "test.aot", "my_shared_heap_malloc", 1, argv);
  184. EXPECT_EQ(0, argv[0]);
  185. argv[0] = 1024;
  186. test_shared_heap(shared_heap, "test_chain.aot", "my_shared_heap_malloc", 1,
  187. argv);
  188. EXPECT_EQ(0, argv[0]);
  189. }
  190. TEST_F(shared_heap_test, test_preallocated_shared_runtime_api)
  191. {
  192. struct ret_env tmp_module_env;
  193. SharedHeapInitArgs args = {};
  194. WASMSharedHeap *shared_heap = nullptr;
  195. uint32 argv[1] = {};
  196. void *native_ptr;
  197. uint64 offset, size;
  198. bool ret;
  199. args.size = 0x4000;
  200. shared_heap = wasm_runtime_create_shared_heap(&args);
  201. if (!shared_heap) {
  202. FAIL() << "Failed to create shared heap";
  203. }
  204. if (!load_wasm("test.wasm", 0, tmp_module_env)) {
  205. FAIL() << "Failed to load wasm file\n";
  206. }
  207. if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst,
  208. shared_heap)) {
  209. ADD_FAILURE() << "Failed to attach shared heap\n";
  210. goto fail1;
  211. }
  212. offset = wasm_runtime_shared_heap_malloc(tmp_module_env.wasm_module_inst,
  213. 32, &native_ptr);
  214. if (!offset) {
  215. ADD_FAILURE() << "Failed to attach shared heap\n";
  216. goto fail2;
  217. }
  218. size = (uint64_t)UINT32_MAX + 0x2000;
  219. printf("offset %llx size: %llx\n", offset, size);
  220. ASSERT_EQ(false, wasm_runtime_validate_app_addr(
  221. tmp_module_env.wasm_module_inst, offset, size));
  222. ASSERT_EQ(NULL, wasm_runtime_addr_app_to_native(
  223. tmp_module_env.wasm_module_inst, offset + size));
  224. size = (uint64_t)10;
  225. ASSERT_EQ(true, wasm_runtime_validate_app_addr(
  226. tmp_module_env.wasm_module_inst, offset, size));
  227. ASSERT_EQ((char *)native_ptr + size,
  228. wasm_runtime_addr_app_to_native(tmp_module_env.wasm_module_inst,
  229. offset + size));
  230. fail2:
  231. wasm_runtime_detach_shared_heap(tmp_module_env.wasm_module_inst);
  232. fail1:
  233. destroy_module_env(tmp_module_env);
  234. }
  235. static void
  236. create_test_shared_heap(uint8 *preallocated_buf, size_t size,
  237. WASMSharedHeap **shared_heap_res)
  238. {
  239. SharedHeapInitArgs args = {};
  240. WASMSharedHeap *shared_heap = nullptr;
  241. args.pre_allocated_addr = preallocated_buf;
  242. args.size = size;
  243. shared_heap = wasm_runtime_create_shared_heap(&args);
  244. if (!shared_heap) {
  245. FAIL() << "Create preallocated shared heap failed.\n";
  246. }
  247. *shared_heap_res = shared_heap;
  248. if (!*shared_heap_res) {
  249. FAIL() << "Create shared heap chain failed.\n";
  250. }
  251. }
  252. static void
  253. create_test_shared_heap_chain(uint8 *preallocated_buf, size_t size,
  254. uint8 *preallocated_buf2, size_t size2,
  255. WASMSharedHeap **shared_heap_chain)
  256. {
  257. SharedHeapInitArgs args = {};
  258. WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr;
  259. args.pre_allocated_addr = preallocated_buf;
  260. args.size = size;
  261. shared_heap = wasm_runtime_create_shared_heap(&args);
  262. if (!shared_heap) {
  263. FAIL() << "Create preallocated shared heap failed.\n";
  264. }
  265. memset(&args, 0, sizeof(args));
  266. args.pre_allocated_addr = preallocated_buf2;
  267. args.size = size2;
  268. shared_heap2 = wasm_runtime_create_shared_heap(&args);
  269. if (!shared_heap2) {
  270. FAIL() << "Create preallocated shared heap failed.\n";
  271. }
  272. *shared_heap_chain =
  273. wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
  274. if (!*shared_heap_chain) {
  275. FAIL() << "Create shared heap chain failed.\n";
  276. }
  277. }
  278. TEST_F(shared_heap_test, test_shared_heap_rmw)
  279. {
  280. WASMSharedHeap *shared_heap = nullptr;
  281. uint32 argv[2] = {}, BUF_SIZE = os_getpagesize();
  282. uint8 *preallocated_buf_ptr = nullptr;
  283. uint32 start1, end1;
  284. ASSERT_GT(BUF_SIZE, 0u);
  285. std::vector<uint8> preallocated_buf(BUF_SIZE);
  286. preallocated_buf_ptr = preallocated_buf.data();
  287. ASSERT_NE(preallocated_buf_ptr, nullptr);
  288. create_test_shared_heap(preallocated_buf_ptr, BUF_SIZE, &shared_heap);
  289. /* app addr for shared heap */
  290. start1 = UINT32_MAX - BUF_SIZE + 1;
  291. end1 = UINT32_MAX;
  292. argv[0] = end1;
  293. argv[1] = 101;
  294. test_shared_heap(shared_heap, "test.wasm", "read_modify_write_8", 2, argv);
  295. EXPECT_EQ(0, argv[0]);
  296. EXPECT_EQ(preallocated_buf[BUF_SIZE - 1], 101);
  297. argv[0] = start1;
  298. argv[1] = 37;
  299. test_shared_heap(shared_heap, "test.wasm", "read_modify_write_8", 2, argv);
  300. EXPECT_EQ(0, argv[0]);
  301. EXPECT_EQ(preallocated_buf[0], 37);
  302. argv[0] = end1;
  303. argv[1] = 81;
  304. test_shared_heap(shared_heap, "test.aot", "read_modify_write_8", 2, argv);
  305. EXPECT_EQ(101, argv[0]);
  306. EXPECT_EQ(preallocated_buf[BUF_SIZE - 1], 81);
  307. argv[0] = start1;
  308. argv[1] = 98;
  309. test_shared_heap(shared_heap, "test.aot", "read_modify_write_8", 2, argv);
  310. EXPECT_EQ(37, argv[0]);
  311. EXPECT_EQ(preallocated_buf[0], 98);
  312. }
  313. TEST_F(shared_heap_test, test_shared_heap_chain_rmw)
  314. {
  315. SharedHeapInitArgs args = {};
  316. WASMSharedHeap *shared_heap_chain = nullptr;
  317. uint32 argv[2] = {}, BUF_SIZE = os_getpagesize();
  318. uint8 *preallocated_buf_ptr = nullptr;
  319. uint8 *preallocated_buf2_ptr = nullptr;
  320. uint32 start1, end1, start2, end2;
  321. ASSERT_GT(BUF_SIZE, 0u);
  322. std::vector<uint8> preallocated_buf(BUF_SIZE);
  323. std::vector<uint8> preallocated_buf2(BUF_SIZE);
  324. preallocated_buf_ptr = preallocated_buf.data();
  325. preallocated_buf2_ptr = preallocated_buf2.data();
  326. ASSERT_NE(preallocated_buf_ptr, nullptr);
  327. ASSERT_NE(preallocated_buf2_ptr, nullptr);
  328. create_test_shared_heap_chain(preallocated_buf_ptr, BUF_SIZE,
  329. preallocated_buf2_ptr, BUF_SIZE,
  330. &shared_heap_chain);
  331. /* app addr for shared heap */
  332. start1 = UINT32_MAX - 2 * BUF_SIZE + 1;
  333. end1 = UINT32_MAX - BUF_SIZE;
  334. start2 = UINT32_MAX - BUF_SIZE + 1;
  335. end2 = UINT32_MAX;
  336. /* shared heap 1 */
  337. argv[0] = end1;
  338. argv[1] = 101;
  339. test_shared_heap(shared_heap_chain, "test.wasm", "read_modify_write_8", 2,
  340. argv);
  341. EXPECT_EQ(0, argv[0]);
  342. EXPECT_EQ(preallocated_buf[BUF_SIZE - 1], 101);
  343. /* shared heap 2 */
  344. argv[0] = start2;
  345. argv[1] = 129;
  346. test_shared_heap(shared_heap_chain, "test.wasm", "read_modify_write_8", 2,
  347. argv);
  348. EXPECT_EQ(0, argv[0]);
  349. EXPECT_EQ(preallocated_buf2[0], 129);
  350. argv[0] = start1;
  351. argv[1] = 98;
  352. test_shared_heap(shared_heap_chain, "test_chain.aot", "read_modify_write_8",
  353. 2, argv);
  354. EXPECT_EQ(0, argv[0]);
  355. EXPECT_EQ(preallocated_buf[0], 98);
  356. argv[0] = end2;
  357. argv[1] = 81;
  358. test_shared_heap(shared_heap_chain, "test_chain.aot", "read_modify_write_8",
  359. 2, argv);
  360. EXPECT_EQ(0, argv[0]);
  361. EXPECT_EQ(preallocated_buf2[BUF_SIZE - 1], 81);
  362. }
  363. TEST_F(shared_heap_test, test_shared_heap_chain_rmw_bulk_memory)
  364. {
  365. SharedHeapInitArgs args = {};
  366. WASMSharedHeap *shared_heap_chain = nullptr;
  367. uint32 argv[3] = {}, BUF_SIZE = os_getpagesize();
  368. uint8 *preallocated_buf_ptr = nullptr;
  369. uint8 *preallocated_buf2_ptr = nullptr;
  370. uint32 start1, end1, start2, end2;
  371. ASSERT_GT(BUF_SIZE, 0u);
  372. std::vector<uint8> preallocated_buf(BUF_SIZE);
  373. std::vector<uint8> preallocated_buf2(BUF_SIZE);
  374. preallocated_buf_ptr = preallocated_buf.data();
  375. preallocated_buf2_ptr = preallocated_buf2.data();
  376. ASSERT_NE(preallocated_buf_ptr, nullptr);
  377. ASSERT_NE(preallocated_buf2_ptr, nullptr);
  378. create_test_shared_heap_chain(preallocated_buf_ptr, BUF_SIZE,
  379. preallocated_buf2_ptr, BUF_SIZE,
  380. &shared_heap_chain);
  381. /* app addr for shared heap */
  382. start1 = UINT32_MAX - 2 * BUF_SIZE + 1;
  383. end1 = UINT32_MAX - BUF_SIZE;
  384. start2 = UINT32_MAX - BUF_SIZE + 1;
  385. end2 = UINT32_MAX;
  386. argv[0] = end1;
  387. argv[1] = 101;
  388. argv[2] = 1;
  389. test_shared_heap(shared_heap_chain, "test_bulk_memory.wasm",
  390. "memory_fill_test", 3, argv);
  391. /* no modification since no return value */
  392. EXPECT_EQ(end1, argv[0]);
  393. EXPECT_EQ(preallocated_buf[BUF_SIZE - 1], 101);
  394. argv[0] = start1;
  395. argv[1] = 14;
  396. argv[2] = 1;
  397. test_shared_heap(shared_heap_chain, "test_bulk_memory_chain.aot",
  398. "memory_fill_test", 3, argv);
  399. /* no modification since no return value */
  400. EXPECT_EQ(start1, argv[0]);
  401. EXPECT_EQ(preallocated_buf[0], 14);
  402. /* nothing happen when memory fill 0 byte */
  403. argv[0] = start2;
  404. argv[1] = 68;
  405. argv[2] = 0;
  406. test_shared_heap(shared_heap_chain, "test_bulk_memory_chain.aot",
  407. "memory_fill_test", 3, argv);
  408. /* no modification since no return value */
  409. EXPECT_EQ(start2, argv[0]);
  410. EXPECT_EQ(preallocated_buf2[0], 0);
  411. argv[0] = end2;
  412. argv[1] = 98;
  413. argv[2] = 1;
  414. test_shared_heap(shared_heap_chain, "test_bulk_memory_chain.aot",
  415. "memory_fill_test", 3, argv);
  416. /* no modification since no return value */
  417. EXPECT_EQ(end2, argv[0]);
  418. EXPECT_EQ(preallocated_buf2[BUF_SIZE - 1], 98);
  419. }
  420. TEST_F(shared_heap_test, test_shared_heap_chain_rmw_bulk_memory_oob)
  421. {
  422. SharedHeapInitArgs args = {};
  423. WASMSharedHeap *shared_heap_chain = nullptr;
  424. uint32 argv[3] = {}, BUF_SIZE = os_getpagesize();
  425. uint8 *preallocated_buf_ptr = nullptr;
  426. uint8 *preallocated_buf2_ptr = nullptr;
  427. uint32 start1, end1, start2, end2;
  428. ASSERT_GT(BUF_SIZE, 0u);
  429. std::vector<uint8> preallocated_buf(BUF_SIZE);
  430. std::vector<uint8> preallocated_buf2(BUF_SIZE);
  431. preallocated_buf_ptr = preallocated_buf.data();
  432. preallocated_buf2_ptr = preallocated_buf2.data();
  433. ASSERT_NE(preallocated_buf_ptr, nullptr);
  434. ASSERT_NE(preallocated_buf2_ptr, nullptr);
  435. create_test_shared_heap_chain(preallocated_buf_ptr, BUF_SIZE,
  436. preallocated_buf2_ptr, BUF_SIZE,
  437. &shared_heap_chain);
  438. /* app addr for shared heap */
  439. start1 = UINT32_MAX - 2 * BUF_SIZE + 1;
  440. end1 = UINT32_MAX - BUF_SIZE;
  441. start2 = UINT32_MAX - BUF_SIZE + 1;
  442. end2 = UINT32_MAX;
  443. /* shared heap 1 */
  444. argv[0] = end1;
  445. argv[1] = 101;
  446. argv[2] = 2;
  447. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
  448. "test_bulk_memory.wasm",
  449. "memory_fill_test", 3, argv),
  450. "Exception: out of bounds memory access");
  451. argv[0] = end2;
  452. argv[1] = 98;
  453. argv[2] = 2;
  454. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
  455. "test_bulk_memory.wasm",
  456. "memory_fill_test", 3, argv),
  457. "Exception: out of bounds memory access");
  458. argv[0] = start1;
  459. argv[1] = 98;
  460. argv[2] = BUF_SIZE + 1;
  461. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
  462. "test_bulk_memory.wasm",
  463. "memory_fill_test", 3, argv),
  464. "Exception: out of bounds memory access");
  465. argv[0] = start2;
  466. argv[1] = 98;
  467. argv[2] = BUF_SIZE + 1;
  468. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
  469. "test_bulk_memory.wasm",
  470. "memory_fill_test", 3, argv),
  471. "Exception: out of bounds memory access");
  472. argv[0] = end1;
  473. argv[1] = 101;
  474. argv[2] = 2;
  475. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
  476. "test_bulk_memory_chain.aot",
  477. "memory_fill_test", 3, argv),
  478. "Exception: out of bounds memory access");
  479. argv[0] = end2;
  480. argv[1] = 98;
  481. argv[2] = 2;
  482. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
  483. "test_bulk_memory_chain.aot",
  484. "memory_fill_test", 3, argv),
  485. "Exception: out of bounds memory access");
  486. argv[0] = start1;
  487. argv[1] = 98;
  488. argv[2] = BUF_SIZE + 1;
  489. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
  490. "test_bulk_memory_chain.aot",
  491. "memory_fill_test", 3, argv),
  492. "Exception: out of bounds memory access");
  493. argv[0] = start2;
  494. argv[1] = 98;
  495. argv[2] = BUF_SIZE + 1;
  496. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
  497. "test_bulk_memory_chain.aot",
  498. "memory_fill_test", 3, argv),
  499. "Exception: out of bounds memory access");
  500. }
  501. TEST_F(shared_heap_test, test_shared_heap_rmw_oob)
  502. {
  503. WASMSharedHeap *shared_heap = nullptr;
  504. uint32 argv[2] = {}, BUF_SIZE = os_getpagesize();
  505. uint8 *preallocated_buf_ptr = nullptr;
  506. uint8 *preallocated_buf2_ptr = nullptr;
  507. uint32 start1, end1, start2, end2;
  508. ASSERT_GT(BUF_SIZE, 0u);
  509. std::vector<uint8> preallocated_buf(BUF_SIZE);
  510. std::vector<uint8> preallocated_buf2(BUF_SIZE);
  511. preallocated_buf_ptr = preallocated_buf.data();
  512. preallocated_buf2_ptr = preallocated_buf2.data();
  513. ASSERT_NE(preallocated_buf_ptr, nullptr);
  514. ASSERT_NE(preallocated_buf2_ptr, nullptr);
  515. create_test_shared_heap(preallocated_buf_ptr, BUF_SIZE, &shared_heap);
  516. /* app addr for shared heap */
  517. start1 = UINT32_MAX - BUF_SIZE + 1;
  518. end1 = UINT32_MAX;
  519. /* try to rmw an u16, first u8 is in the first shared heap and second u8 is
  520. * in the second shared heap, will be seen as oob */
  521. argv[0] = end1;
  522. argv[1] = 12025;
  523. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap, "test.wasm",
  524. "read_modify_write_16", 2, argv),
  525. "Exception: out of bounds memory access");
  526. argv[0] = start1 - 1;
  527. argv[1] = 12025;
  528. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap, "test.aot",
  529. "read_modify_write_16", 2, argv),
  530. "Exception: out of bounds memory access");
  531. argv[0] = end1;
  532. argv[1] = 12025;
  533. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap, "test.aot",
  534. "read_modify_write_16", 2, argv),
  535. "Exception: out of bounds memory access");
  536. }
  537. TEST_F(shared_heap_test, test_shared_heap_chain_rmw_oob)
  538. {
  539. WASMSharedHeap *shared_heap_chain = nullptr;
  540. uint32 argv[2] = {}, BUF_SIZE = os_getpagesize();
  541. uint8 *preallocated_buf_ptr = nullptr;
  542. uint8 *preallocated_buf2_ptr = nullptr;
  543. uint32 start1, end1, start2, end2;
  544. ASSERT_GT(BUF_SIZE, 0u);
  545. std::vector<uint8> preallocated_buf(BUF_SIZE);
  546. std::vector<uint8> preallocated_buf2(BUF_SIZE);
  547. preallocated_buf_ptr = preallocated_buf.data();
  548. preallocated_buf2_ptr = preallocated_buf2.data();
  549. ASSERT_NE(preallocated_buf_ptr, nullptr);
  550. ASSERT_NE(preallocated_buf2_ptr, nullptr);
  551. create_test_shared_heap_chain(preallocated_buf_ptr, BUF_SIZE,
  552. preallocated_buf2_ptr, BUF_SIZE,
  553. &shared_heap_chain);
  554. /* app addr for shared heap */
  555. start1 = UINT32_MAX - 2 * BUF_SIZE + 1;
  556. end1 = UINT32_MAX - BUF_SIZE;
  557. start2 = UINT32_MAX - BUF_SIZE + 1;
  558. end2 = UINT32_MAX;
  559. /* try to rmw an u16, first u8 is in the first shared heap and second u8 is
  560. * in the second shared heap, will be seen as oob */
  561. argv[0] = end2;
  562. argv[1] = 12025;
  563. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain, "test.wasm",
  564. "read_modify_write_16", 2, argv),
  565. "Exception: out of bounds memory access");
  566. argv[0] = end1;
  567. argv[1] = 12025;
  568. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
  569. "test_chain.aot",
  570. "read_modify_write_16", 2, argv),
  571. "Exception: out of bounds memory access");
  572. }
  573. #if WASM_ENABLE_MEMORY64 != 0
  574. TEST_F(shared_heap_test, test_shared_heap_chain_memory64_rmw)
  575. {
  576. WASMSharedHeap *shared_heap_chain = nullptr;
  577. uint32 argv[3] = {}, BUF_SIZE = os_getpagesize();
  578. uint8 *preallocated_buf_ptr = nullptr;
  579. uint8 *preallocated_buf2_ptr = nullptr;
  580. uint64 start1, end1, start2, end2;
  581. ASSERT_GT(BUF_SIZE, 0u);
  582. std::vector<uint8> preallocated_buf(BUF_SIZE);
  583. std::vector<uint8> preallocated_buf2(BUF_SIZE);
  584. preallocated_buf_ptr = preallocated_buf.data();
  585. preallocated_buf2_ptr = preallocated_buf2.data();
  586. ASSERT_NE(preallocated_buf_ptr, nullptr);
  587. ASSERT_NE(preallocated_buf2_ptr, nullptr);
  588. create_test_shared_heap_chain(preallocated_buf_ptr, BUF_SIZE,
  589. preallocated_buf2_ptr, BUF_SIZE,
  590. &shared_heap_chain);
  591. /* app addr for shared heap */
  592. start1 = UINT64_MAX - 2 * BUF_SIZE + 1;
  593. end1 = UINT64_MAX - BUF_SIZE;
  594. start2 = UINT64_MAX - BUF_SIZE + 1;
  595. end2 = UINT64_MAX;
  596. /* shared heap 1 */
  597. PUT_I64_TO_ADDR(argv, end1);
  598. argv[2] = 101;
  599. test_shared_heap(shared_heap_chain, "test64.wasm", "read_modify_write_8", 3,
  600. argv);
  601. EXPECT_EQ(0, argv[0]);
  602. EXPECT_EQ(preallocated_buf[BUF_SIZE - 1], 101);
  603. /* shared heap 2 */
  604. PUT_I64_TO_ADDR(argv, start2);
  605. argv[2] = 129;
  606. test_shared_heap(shared_heap_chain, "test64.wasm", "read_modify_write_8", 3,
  607. argv);
  608. EXPECT_EQ(0, argv[0]);
  609. EXPECT_EQ(preallocated_buf2[0], 129);
  610. PUT_I64_TO_ADDR(argv, start1);
  611. argv[2] = 98;
  612. test_shared_heap(shared_heap_chain, "test64_chain.aot",
  613. "read_modify_write_8", 3, argv);
  614. EXPECT_EQ(0, argv[0]);
  615. EXPECT_EQ(preallocated_buf[0], 98);
  616. PUT_I64_TO_ADDR(argv, end2);
  617. argv[2] = 81;
  618. test_shared_heap(shared_heap_chain, "test64_chain.aot",
  619. "read_modify_write_8", 3, argv);
  620. EXPECT_EQ(0, argv[0]);
  621. EXPECT_EQ(preallocated_buf2[BUF_SIZE - 1], 81);
  622. }
  623. TEST_F(shared_heap_test, test_shared_heap_chain_memory64_rmw_oob)
  624. {
  625. WASMSharedHeap *shared_heap_chain = nullptr;
  626. uint32 argv[3] = {}, BUF_SIZE = os_getpagesize();
  627. uint8 *preallocated_buf_ptr = nullptr;
  628. uint8 *preallocated_buf2_ptr = nullptr;
  629. uint64 start1, end1, start2, end2;
  630. ASSERT_GT(BUF_SIZE, 0u);
  631. std::vector<uint8> preallocated_buf(BUF_SIZE);
  632. std::vector<uint8> preallocated_buf2(BUF_SIZE);
  633. preallocated_buf_ptr = preallocated_buf.data();
  634. preallocated_buf2_ptr = preallocated_buf2.data();
  635. ASSERT_NE(preallocated_buf_ptr, nullptr);
  636. ASSERT_NE(preallocated_buf2_ptr, nullptr);
  637. create_test_shared_heap_chain(preallocated_buf_ptr, BUF_SIZE,
  638. preallocated_buf2_ptr, BUF_SIZE,
  639. &shared_heap_chain);
  640. /* app addr for shared heap */
  641. start1 = UINT64_MAX - 2 * BUF_SIZE + 1;
  642. end1 = UINT64_MAX - BUF_SIZE;
  643. start2 = UINT64_MAX - BUF_SIZE + 1;
  644. end2 = UINT64_MAX;
  645. /* try to rmw an u16, first u8 is in the first shared heap and second u8 is
  646. * in the second shared heap, will be seen as oob */
  647. PUT_I64_TO_ADDR(argv, end1);
  648. argv[2] = 12025;
  649. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain, "test64.wasm",
  650. "read_modify_write_16", 3, argv),
  651. "Exception: out of bounds memory access");
  652. PUT_I64_TO_ADDR(argv, end1);
  653. argv[2] = 12025;
  654. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
  655. "test64_chain.aot",
  656. "read_modify_write_16", 3, argv),
  657. "Exception: out of bounds memory access");
  658. }
  659. #endif
  660. #ifndef native_function
  661. /* clang-format off */
  662. #define native_function(func_name, signature) \
  663. { #func_name, (void *)glue_## func_name, signature, NULL }
  664. /* clang-format on */
  665. #endif
  666. #ifndef nitems
  667. #define nitems(_a) (sizeof(_a) / sizeof(0 [(_a)]))
  668. #endif /* nitems */
  669. uintptr_t
  670. glue_test_addr_conv(wasm_exec_env_t env, uintptr_t addr)
  671. {
  672. wasm_module_inst_t module_inst = get_module_inst(env);
  673. void *native_addr = (void *)addr;
  674. uintptr_t app_addr = addr_native_to_app(native_addr);
  675. native_addr = addr_app_to_native(app_addr);
  676. if (native_addr != (void *)addr) {
  677. ADD_FAILURE() << "address conversion incorrect";
  678. return 0;
  679. }
  680. return app_addr;
  681. }
  682. static NativeSymbol g_test_native_symbols[] = {
  683. native_function(test_addr_conv, "(*)i"),
  684. };
  685. TEST_F(shared_heap_test, test_addr_conv)
  686. {
  687. SharedHeapInitArgs args = {};
  688. WASMSharedHeap *shared_heap = nullptr;
  689. uint32 argv[1] = {};
  690. bool ret = false;
  691. ret = wasm_native_register_natives("env", g_test_native_symbols,
  692. nitems(g_test_native_symbols));
  693. if (!ret) {
  694. FAIL() << "Failed to register natives";
  695. }
  696. args.size = 1024;
  697. shared_heap = wasm_runtime_create_shared_heap(&args);
  698. if (!shared_heap) {
  699. FAIL() << "Failed to create shared heap";
  700. }
  701. test_shared_heap(shared_heap, "test_addr_conv.wasm", "test", 0, argv);
  702. EXPECT_EQ(1, argv[0]);
  703. test_shared_heap(shared_heap, "test_addr_conv.aot", "test", 0, argv);
  704. EXPECT_EQ(1, argv[0]);
  705. test_shared_heap(shared_heap, "test_addr_conv_chain.aot", "test", 0, argv);
  706. EXPECT_EQ(1, argv[0]);
  707. }
  708. TEST_F(shared_heap_test, test_addr_conv_pre_allocated_oob)
  709. {
  710. SharedHeapInitArgs args = {};
  711. WASMSharedHeap *shared_heap = nullptr;
  712. uint32 argv[1] = {}, BUF_SIZE = os_getpagesize(),
  713. app_addr = 0xFFFFFFFF - BUF_SIZE;
  714. uint8 *preallocated_buf_ptr = nullptr;
  715. ASSERT_GT(BUF_SIZE, 0u);
  716. std::vector<uint8> preallocated_buf(BUF_SIZE);
  717. preallocated_buf_ptr = preallocated_buf.data();
  718. ASSERT_NE(preallocated_buf_ptr, nullptr);
  719. bool ret = false;
  720. /* create a preallocated shared heap */
  721. ret = wasm_native_register_natives("env", g_test_native_symbols,
  722. nitems(g_test_native_symbols));
  723. if (!ret) {
  724. FAIL() << "Failed to register natives";
  725. }
  726. args.pre_allocated_addr = preallocated_buf_ptr;
  727. args.size = BUF_SIZE;
  728. shared_heap = wasm_runtime_create_shared_heap(&args);
  729. if (!shared_heap) {
  730. FAIL() << "Failed to create shared heap";
  731. }
  732. argv[0] = app_addr;
  733. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap, "test_addr_conv.wasm",
  734. "test_preallocated", 1, argv),
  735. "Exception: out of bounds memory access");
  736. argv[0] = app_addr;
  737. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap, "test_addr_conv.aot",
  738. "test_preallocated", 1, argv),
  739. "Exception: out of bounds memory access");
  740. argv[0] = app_addr;
  741. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap,
  742. "test_addr_conv_chain.aot",
  743. "test_preallocated", 1, argv),
  744. "Exception: out of bounds memory access");
  745. }
  746. TEST_F(shared_heap_test, test_shared_heap_chain)
  747. {
  748. SharedHeapInitArgs args = {};
  749. WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr,
  750. *shared_heap_chain = nullptr;
  751. uint32 argv[1] = {}, BUF_SIZE = os_getpagesize();
  752. uint8 *preallocated_buf_ptr = nullptr;
  753. ASSERT_GT(BUF_SIZE, 0u);
  754. std::vector<uint8> preallocated_buf(BUF_SIZE);
  755. preallocated_buf_ptr = preallocated_buf.data();
  756. ASSERT_NE(preallocated_buf_ptr, nullptr);
  757. bool ret = false;
  758. ret = wasm_native_register_natives("env", g_test_native_symbols,
  759. nitems(g_test_native_symbols));
  760. if (!ret) {
  761. FAIL() << "Failed to register natives";
  762. }
  763. args.size = 1024;
  764. shared_heap = wasm_runtime_create_shared_heap(&args);
  765. if (!shared_heap) {
  766. FAIL() << "Failed to create shared heap";
  767. }
  768. /* create a preallocated shared heap */
  769. memset(&args, 0, sizeof(args));
  770. args.pre_allocated_addr = preallocated_buf_ptr;
  771. args.size = BUF_SIZE;
  772. shared_heap2 = wasm_runtime_create_shared_heap(&args);
  773. if (!shared_heap2) {
  774. FAIL() << "Create preallocated shared heap failed.\n";
  775. }
  776. shared_heap_chain =
  777. wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
  778. if (!shared_heap_chain) {
  779. FAIL() << "Create shared heap chain failed.\n";
  780. }
  781. test_shared_heap(shared_heap_chain, "test_addr_conv.wasm", "test", 0, argv);
  782. EXPECT_EQ(1, argv[0]);
  783. test_shared_heap(shared_heap, "test_addr_conv.aot", "test", 0, argv);
  784. EXPECT_EQ(1, argv[0]);
  785. }
  786. TEST_F(shared_heap_test, test_shared_heap_chain_create_fail)
  787. {
  788. SharedHeapInitArgs args = {};
  789. WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr,
  790. *shared_heap_chain = nullptr;
  791. args.size = 1024;
  792. shared_heap = wasm_runtime_create_shared_heap(&args);
  793. if (!shared_heap) {
  794. FAIL() << "Failed to create shared heap";
  795. }
  796. args.size = 4096;
  797. shared_heap2 = wasm_runtime_create_shared_heap(&args);
  798. if (!shared_heap2) {
  799. FAIL() << "Create preallocated shared heap failed.\n";
  800. }
  801. shared_heap_chain =
  802. wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
  803. EXPECT_EQ(shared_heap_chain, nullptr);
  804. }
  805. TEST_F(shared_heap_test, test_shared_heap_chain_create_fail2)
  806. {
  807. SharedHeapInitArgs args = {};
  808. WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr,
  809. *shared_heap_chain = nullptr;
  810. uint32 argv[1] = {}, BUF_SIZE = os_getpagesize();
  811. uint8 *preallocated_buf_ptr = nullptr;
  812. ASSERT_GT(BUF_SIZE, 0u);
  813. std::vector<uint8> preallocated_buf(BUF_SIZE);
  814. preallocated_buf_ptr = preallocated_buf.data();
  815. ASSERT_NE(preallocated_buf_ptr, nullptr);
  816. struct ret_env tmp_module_env;
  817. args.size = 1024;
  818. shared_heap = wasm_runtime_create_shared_heap(&args);
  819. if (!shared_heap) {
  820. FAIL() << "Failed to create shared heap";
  821. }
  822. memset(&args, 0, sizeof(args));
  823. args.pre_allocated_addr = preallocated_buf_ptr;
  824. args.size = BUF_SIZE;
  825. shared_heap2 = wasm_runtime_create_shared_heap(&args);
  826. if (!shared_heap2) {
  827. FAIL() << "Create preallocated shared heap failed.\n";
  828. }
  829. if (!load_wasm((char *)"test.wasm", 0, tmp_module_env)) {
  830. FAIL() << "Failed to load wasm file\n";
  831. }
  832. if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst,
  833. shared_heap)) {
  834. FAIL() << "Failed to attach shared heap\n";
  835. }
  836. /* can't create shared heap chain when shared heap is attached to a wasm
  837. * app */
  838. shared_heap_chain =
  839. wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
  840. EXPECT_EQ(shared_heap_chain, nullptr);
  841. wasm_runtime_detach_shared_heap(tmp_module_env.wasm_module_inst);
  842. destroy_module_env(tmp_module_env);
  843. }
  844. TEST_F(shared_heap_test, test_shared_heap_chain_create_fail3)
  845. {
  846. SharedHeapInitArgs args = {};
  847. WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr,
  848. *shared_heap3 = nullptr, *shared_heap_chain = nullptr;
  849. uint32 argv[1] = {}, BUF_SIZE = os_getpagesize();
  850. uint8 *preallocated_buf_ptr = nullptr;
  851. uint8 *preallocated_buf2_ptr = nullptr;
  852. ASSERT_GT(BUF_SIZE, 0u);
  853. std::vector<uint8> preallocated_buf(BUF_SIZE);
  854. std::vector<uint8> preallocated_buf2(BUF_SIZE);
  855. preallocated_buf_ptr = preallocated_buf.data();
  856. preallocated_buf2_ptr = preallocated_buf2.data();
  857. ASSERT_NE(preallocated_buf_ptr, nullptr);
  858. ASSERT_NE(preallocated_buf2_ptr, nullptr);
  859. args.size = 1024;
  860. shared_heap = wasm_runtime_create_shared_heap(&args);
  861. if (!shared_heap) {
  862. FAIL() << "Failed to create shared heap";
  863. }
  864. memset(&args, 0, sizeof(args));
  865. args.pre_allocated_addr = preallocated_buf_ptr;
  866. args.size = BUF_SIZE;
  867. shared_heap2 = wasm_runtime_create_shared_heap(&args);
  868. if (!shared_heap2) {
  869. FAIL() << "Create preallocated shared heap failed.\n";
  870. }
  871. shared_heap_chain =
  872. wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
  873. if (!shared_heap_chain) {
  874. FAIL() << "Create shared heap chain failed.\n";
  875. }
  876. memset(&args, 0, sizeof(args));
  877. args.pre_allocated_addr = preallocated_buf2_ptr;
  878. args.size = BUF_SIZE;
  879. shared_heap3 = wasm_runtime_create_shared_heap(&args);
  880. if (!shared_heap3) {
  881. FAIL() << "Failed to create shared heap";
  882. }
  883. /* The head and body can't be already in other shared heap chain as body */
  884. shared_heap_chain =
  885. wasm_runtime_chain_shared_heaps(shared_heap3, shared_heap2);
  886. EXPECT_EQ(shared_heap_chain, nullptr);
  887. shared_heap_chain =
  888. wasm_runtime_chain_shared_heaps(shared_heap2, shared_heap);
  889. EXPECT_EQ(shared_heap_chain, nullptr);
  890. }
  891. TEST_F(shared_heap_test, test_shared_heap_chain_unchain)
  892. {
  893. SharedHeapInitArgs args = {};
  894. WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr,
  895. *shared_heap3 = nullptr, *shared_heap_chain = nullptr;
  896. uint32 argv[1] = {}, BUF_SIZE = os_getpagesize();
  897. uint8 *preallocated_buf_ptr = nullptr;
  898. uint8 *preallocated_buf2_ptr = nullptr;
  899. ASSERT_GT(BUF_SIZE, 0u);
  900. std::vector<uint8> preallocated_buf(BUF_SIZE);
  901. std::vector<uint8> preallocated_buf2(BUF_SIZE);
  902. preallocated_buf_ptr = preallocated_buf.data();
  903. preallocated_buf2_ptr = preallocated_buf2.data();
  904. ASSERT_NE(preallocated_buf_ptr, nullptr);
  905. ASSERT_NE(preallocated_buf2_ptr, nullptr);
  906. args.size = 1024;
  907. shared_heap = wasm_runtime_create_shared_heap(&args);
  908. if (!shared_heap) {
  909. FAIL() << "Failed to create shared heap";
  910. }
  911. memset(&args, 0, sizeof(args));
  912. args.pre_allocated_addr = preallocated_buf_ptr;
  913. args.size = BUF_SIZE;
  914. shared_heap2 = wasm_runtime_create_shared_heap(&args);
  915. if (!shared_heap2) {
  916. FAIL() << "Create preallocated shared heap failed.\n";
  917. }
  918. shared_heap_chain =
  919. wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
  920. if (!shared_heap_chain) {
  921. FAIL() << "Create shared heap chain failed.\n";
  922. }
  923. memset(&args, 0, sizeof(args));
  924. args.pre_allocated_addr = preallocated_buf2_ptr;
  925. args.size = BUF_SIZE;
  926. shared_heap3 = wasm_runtime_create_shared_heap(&args);
  927. if (!shared_heap3) {
  928. FAIL() << "Failed to create shared heap";
  929. }
  930. /* unchain shared heap so that the 'body' can be another chain 'body'
  931. * again(1->2 to 1->3->2) */
  932. EXPECT_EQ(shared_heap2,
  933. wasm_runtime_unchain_shared_heaps(shared_heap_chain, false));
  934. shared_heap_chain =
  935. wasm_runtime_chain_shared_heaps(shared_heap3, shared_heap2);
  936. EXPECT_EQ(shared_heap_chain, shared_heap3);
  937. shared_heap_chain =
  938. wasm_runtime_chain_shared_heaps(shared_heap, shared_heap3);
  939. EXPECT_EQ(shared_heap, shared_heap_chain);
  940. /* break down the entire shared heap chain */
  941. EXPECT_EQ(shared_heap2,
  942. wasm_runtime_unchain_shared_heaps(shared_heap_chain, true));
  943. }
  944. TEST_F(shared_heap_test, test_shared_heap_chain_reset_runtime_managed)
  945. {
  946. SharedHeapInitArgs args = {};
  947. WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr,
  948. *shared_heap_chain = nullptr;
  949. uint8 buf_size = 64;
  950. uint64 offset = 0, offset_after_reset = 0;
  951. void *native_ptr = nullptr, *native_ptr_after_reset = nullptr;
  952. uint32 argv[1] = {}, BUF_SIZE = os_getpagesize();
  953. uint8 *preallocated_buf_ptr = nullptr;
  954. ASSERT_GT(BUF_SIZE, 0u);
  955. std::vector<uint8> preallocated_buf(BUF_SIZE);
  956. preallocated_buf_ptr = preallocated_buf.data();
  957. ASSERT_NE(preallocated_buf_ptr, nullptr);
  958. struct ret_env tmp_module_env;
  959. args.size = 4096;
  960. shared_heap = wasm_runtime_create_shared_heap(&args);
  961. if (!shared_heap) {
  962. FAIL() << "Failed to create shared heap";
  963. }
  964. args.size = BUF_SIZE;
  965. args.pre_allocated_addr = preallocated_buf_ptr;
  966. shared_heap2 = wasm_runtime_create_shared_heap(&args);
  967. if (!shared_heap2) {
  968. FAIL() << "Failed to create second shared heap";
  969. }
  970. shared_heap_chain =
  971. wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
  972. if (!shared_heap_chain) {
  973. FAIL() << "Create shared heap chain failed.\n";
  974. }
  975. if (!load_wasm((char *)"test.wasm", 0, tmp_module_env)) {
  976. FAIL() << "Failed to load wasm file\n";
  977. }
  978. if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst,
  979. shared_heap_chain)) {
  980. destroy_module_env(tmp_module_env);
  981. FAIL() << "Failed to attach shared heap chain";
  982. }
  983. offset = wasm_runtime_shared_heap_malloc(tmp_module_env.wasm_module_inst,
  984. buf_size, &native_ptr);
  985. ASSERT_NE(0u, offset);
  986. ASSERT_NE(nullptr, native_ptr);
  987. memset(native_ptr, 0x5A, buf_size);
  988. for (uint8 i = 0; i < buf_size; i++) {
  989. EXPECT_EQ(0x5A, *((uint8 *)native_ptr + i));
  990. }
  991. wasm_runtime_detach_shared_heap(tmp_module_env.wasm_module_inst);
  992. EXPECT_TRUE(wasm_runtime_reset_shared_heap_chain(shared_heap_chain));
  993. if (!load_wasm((char *)"test.wasm", 0, tmp_module_env)) {
  994. FAIL() << "Failed to load wasm file after reset\n";
  995. }
  996. if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst,
  997. shared_heap_chain)) {
  998. destroy_module_env(tmp_module_env);
  999. FAIL() << "Failed to attach shared heap chain after reset";
  1000. }
  1001. offset_after_reset = wasm_runtime_shared_heap_malloc(
  1002. tmp_module_env.wasm_module_inst, buf_size, &native_ptr_after_reset);
  1003. ASSERT_NE(0u, offset_after_reset);
  1004. ASSERT_NE(nullptr, native_ptr_after_reset);
  1005. EXPECT_EQ(offset, offset_after_reset);
  1006. EXPECT_EQ(native_ptr, native_ptr_after_reset);
  1007. /* Only on some platform, the os_mmap will memset the memory to 0
  1008. for (uint8 i = 0; i < buf_size; i++) {
  1009. EXPECT_EQ(0, *((uint8 *)native_ptr_after_reset + i));
  1010. }
  1011. */
  1012. wasm_runtime_detach_shared_heap(tmp_module_env.wasm_module_inst);
  1013. destroy_module_env(tmp_module_env);
  1014. }
  1015. TEST_F(shared_heap_test, test_shared_heap_chain_reset_preallocated)
  1016. {
  1017. SharedHeapInitArgs args = {};
  1018. WASMSharedHeap *shared_heap = nullptr;
  1019. uint32 BUF_SIZE = os_getpagesize();
  1020. uint8 *preallocated_buf_ptr = nullptr;
  1021. ASSERT_GT(BUF_SIZE, 0u);
  1022. std::vector<uint8> preallocated_buf(BUF_SIZE);
  1023. preallocated_buf_ptr = preallocated_buf.data();
  1024. ASSERT_NE(preallocated_buf_ptr, nullptr);
  1025. uint8 set_val = 0xA5;
  1026. args.pre_allocated_addr = preallocated_buf_ptr;
  1027. args.size = BUF_SIZE;
  1028. shared_heap = wasm_runtime_create_shared_heap(&args);
  1029. if (!shared_heap) {
  1030. FAIL() << "Create preallocated shared heap failed.\n";
  1031. }
  1032. memset(preallocated_buf_ptr, set_val, BUF_SIZE);
  1033. for (uint32 i = 0; i < BUF_SIZE; i++) {
  1034. EXPECT_EQ(set_val, preallocated_buf[i]);
  1035. }
  1036. EXPECT_TRUE(wasm_runtime_reset_shared_heap_chain(shared_heap));
  1037. for (uint32 i = 0; i < BUF_SIZE; i++) {
  1038. EXPECT_EQ(0, preallocated_buf[i]);
  1039. }
  1040. }
  1041. TEST_F(shared_heap_test, test_shared_heap_chain_reset_attached_fail)
  1042. {
  1043. SharedHeapInitArgs args = {};
  1044. WASMSharedHeap *shared_heap = nullptr;
  1045. struct ret_env module_env = {};
  1046. bool ret;
  1047. args.size = 1024;
  1048. shared_heap = wasm_runtime_create_shared_heap(&args);
  1049. if (!shared_heap) {
  1050. FAIL() << "Failed to create shared heap";
  1051. }
  1052. ret = load_wasm((char *)"test.wasm", 0, module_env);
  1053. if (!ret) {
  1054. FAIL() << "Failed to load wasm";
  1055. }
  1056. ret = wasm_runtime_attach_shared_heap(module_env.wasm_module_inst,
  1057. shared_heap);
  1058. if (!ret) {
  1059. destroy_module_env(module_env);
  1060. FAIL() << "Failed to attach shared heap";
  1061. }
  1062. EXPECT_FALSE(wasm_runtime_reset_shared_heap_chain(shared_heap));
  1063. wasm_runtime_detach_shared_heap(module_env.wasm_module_inst);
  1064. destroy_module_env(module_env);
  1065. EXPECT_TRUE(wasm_runtime_reset_shared_heap_chain(shared_heap));
  1066. }
  1067. TEST_F(shared_heap_test, test_shared_heap_chain_addr_conv)
  1068. {
  1069. SharedHeapInitArgs args = {};
  1070. WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr,
  1071. *shared_heap_chain = nullptr;
  1072. uint32 argv[1] = {}, BUF_SIZE = os_getpagesize();
  1073. uint8 *preallocated_buf_ptr = nullptr;
  1074. ASSERT_GT(BUF_SIZE, 0u);
  1075. std::vector<uint8> preallocated_buf(BUF_SIZE);
  1076. preallocated_buf_ptr = preallocated_buf.data();
  1077. ASSERT_NE(preallocated_buf_ptr, nullptr);
  1078. bool ret = false;
  1079. ret = wasm_native_register_natives("env", g_test_native_symbols,
  1080. nitems(g_test_native_symbols));
  1081. if (!ret) {
  1082. FAIL() << "Failed to register natives";
  1083. }
  1084. args.size = 4096;
  1085. shared_heap = wasm_runtime_create_shared_heap(&args);
  1086. if (!shared_heap) {
  1087. FAIL() << "Failed to create shared heap";
  1088. }
  1089. /* create a preallocated shared heap */
  1090. memset(&args, 0, sizeof(args));
  1091. args.pre_allocated_addr = preallocated_buf_ptr;
  1092. args.size = BUF_SIZE;
  1093. shared_heap2 = wasm_runtime_create_shared_heap(&args);
  1094. if (!shared_heap2) {
  1095. FAIL() << "Create preallocated shared heap failed.\n";
  1096. }
  1097. shared_heap_chain =
  1098. wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
  1099. if (!shared_heap_chain) {
  1100. FAIL() << "Create shared heap chain failed.\n";
  1101. }
  1102. argv[0] = 0xFFFFFFFF;
  1103. test_shared_heap(shared_heap_chain, "test_addr_conv.wasm",
  1104. "test_preallocated", 1, argv);
  1105. EXPECT_EQ(1, argv[0]);
  1106. argv[0] = 0xFFFFF000;
  1107. test_shared_heap(shared_heap_chain, "test_addr_conv.wasm",
  1108. "test_preallocated", 1, argv);
  1109. EXPECT_EQ(1, argv[0]);
  1110. argv[0] = 0xFFFFFFFF;
  1111. test_shared_heap(shared_heap, "test_addr_conv_chain.aot",
  1112. "test_preallocated", 1, argv);
  1113. EXPECT_EQ(1, argv[0]);
  1114. argv[0] = 0xFFFFF000;
  1115. test_shared_heap(shared_heap, "test_addr_conv_chain.aot",
  1116. "test_preallocated", 1, argv);
  1117. EXPECT_EQ(1, argv[0]);
  1118. }
  1119. TEST_F(shared_heap_test, test_shared_heap_chain_addr_conv_oob)
  1120. {
  1121. SharedHeapInitArgs args = {};
  1122. WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr,
  1123. *shared_heap_chain = nullptr;
  1124. uint32 argv[1] = {}, BUF_SIZE = os_getpagesize();
  1125. uint8 *preallocated_buf_ptr = nullptr;
  1126. ASSERT_GT(BUF_SIZE, 0u);
  1127. std::vector<uint8> preallocated_buf(BUF_SIZE);
  1128. preallocated_buf_ptr = preallocated_buf.data();
  1129. ASSERT_NE(preallocated_buf_ptr, nullptr);
  1130. bool ret = false;
  1131. ret = wasm_native_register_natives("env", g_test_native_symbols,
  1132. nitems(g_test_native_symbols));
  1133. if (!ret) {
  1134. FAIL() << "Failed to register natives";
  1135. }
  1136. args.size = os_getpagesize();
  1137. shared_heap = wasm_runtime_create_shared_heap(&args);
  1138. if (!shared_heap) {
  1139. FAIL() << "Failed to create shared heap";
  1140. }
  1141. /* create a preallocated shared heap */
  1142. memset(&args, 0, sizeof(args));
  1143. args.pre_allocated_addr = preallocated_buf_ptr;
  1144. args.size = BUF_SIZE;
  1145. shared_heap2 = wasm_runtime_create_shared_heap(&args);
  1146. if (!shared_heap2) {
  1147. FAIL() << "Create preallocated shared heap failed.\n";
  1148. }
  1149. shared_heap_chain =
  1150. wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
  1151. if (!shared_heap_chain) {
  1152. FAIL() << "Create shared heap chain failed.\n";
  1153. }
  1154. /* test wasm */
  1155. argv[0] = 0xFFFFFFFF - BUF_SIZE - os_getpagesize();
  1156. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
  1157. "test_addr_conv.wasm",
  1158. "test_preallocated", 1, argv),
  1159. "Exception: out of bounds memory access");
  1160. /* test aot */
  1161. argv[0] = 0xFFFFFFFF - BUF_SIZE - os_getpagesize();
  1162. EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
  1163. "test_addr_conv_chain.aot",
  1164. "test_preallocated", 1, argv),
  1165. "Exception: out of bounds memory access");
  1166. }