| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373 |
- /*
- * Copyright (C) 2024 Xiaomi Corporation. All rights reserved.
- * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- */
- #include "test_helper.h"
- #include "gtest/gtest.h"
- #include "bh_read_file.h"
- #include "wasm_runtime_common.h"
- #include "bh_platform.h"
- #include <gtest/gtest-spi.h>
- #include <vector>
- class shared_heap_test : public testing::Test
- {
- protected:
- virtual void SetUp() {}
- static void SetUpTestCase() {}
- virtual void TearDown() {}
- WAMRRuntimeRAII<512 * 1024> runtime;
- };
- struct ret_env {
- wasm_exec_env_t exec_env;
- wasm_module_t wasm_module;
- wasm_module_inst_t wasm_module_inst;
- unsigned char *wasm_file_buf;
- char error_buf[128];
- };
- static void
- destroy_module_env(struct ret_env module_env);
- static bool
- load_wasm(const char *wasm_file_tested, unsigned int app_heap_size,
- ret_env &ret_module_env)
- {
- char *wasm_file = strdup(wasm_file_tested);
- unsigned int wasm_file_size = 0;
- unsigned int stack_size = 16 * 1024, heap_size = app_heap_size;
- char error_buf[128] = {};
- ret_module_env.wasm_file_buf =
- (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
- if (!ret_module_env.wasm_file_buf) {
- goto fail;
- }
- ret_module_env.wasm_module =
- wasm_runtime_load(ret_module_env.wasm_file_buf, wasm_file_size,
- error_buf, sizeof(error_buf));
- if (!ret_module_env.wasm_module) {
- memcpy(ret_module_env.error_buf, error_buf, 128);
- goto fail;
- }
- ret_module_env.wasm_module_inst =
- wasm_runtime_instantiate(ret_module_env.wasm_module, stack_size,
- heap_size, error_buf, sizeof(error_buf));
- if (!ret_module_env.wasm_module_inst) {
- memcpy(ret_module_env.error_buf, error_buf, 128);
- goto fail;
- }
- ret_module_env.exec_env = wasm_runtime_create_exec_env(
- ret_module_env.wasm_module_inst, stack_size);
- if (!ret_module_env.exec_env) {
- goto fail;
- }
- free(wasm_file);
- return true;
- fail:
- free(wasm_file);
- destroy_module_env(ret_module_env);
- return false;
- }
- void
- destroy_module_env(struct ret_env module_env)
- {
- if (module_env.exec_env) {
- wasm_runtime_destroy_exec_env(module_env.exec_env);
- }
- if (module_env.wasm_module_inst) {
- wasm_runtime_deinstantiate(module_env.wasm_module_inst);
- }
- if (module_env.wasm_module) {
- wasm_runtime_unload(module_env.wasm_module);
- }
- if (module_env.wasm_file_buf) {
- wasm_runtime_free(module_env.wasm_file_buf);
- }
- }
- static void
- test_shared_heap(WASMSharedHeap *shared_heap, const char *file,
- const char *func_name, uint32 argc, uint32 argv[])
- {
- struct ret_env tmp_module_env;
- WASMFunctionInstanceCommon *func_test = nullptr;
- bool ret = false;
- if (!load_wasm((char *)file, 0, tmp_module_env)) {
- ADD_FAILURE() << "Failed to load wasm file\n";
- goto fail0;
- }
- if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst,
- shared_heap)) {
- ADD_FAILURE() << "Failed to attach shared heap\n";
- goto fail1;
- }
- func_test = wasm_runtime_lookup_function(tmp_module_env.wasm_module_inst,
- func_name);
- if (!func_test) {
- ADD_FAILURE() << "Failed to wasm_runtime_lookup_function!\n";
- goto fail2;
- }
- ret =
- wasm_runtime_call_wasm(tmp_module_env.exec_env, func_test, argc, argv);
- if (!ret) {
- const char *s =
- wasm_runtime_get_exception(tmp_module_env.wasm_module_inst);
- ADD_FAILURE() << "Failed to wasm_runtime_call_wasm with "
- << "exception: " << s;
- }
- fail2:
- wasm_runtime_detach_shared_heap(tmp_module_env.wasm_module_inst);
- fail1:
- destroy_module_env(tmp_module_env);
- fail0:
- return;
- }
- TEST_F(shared_heap_test, test_shared_heap_basic)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr;
- uint32 argv[1] = {};
- args.size = os_getpagesize();
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Failed to create shared heap";
- }
- test_shared_heap(shared_heap, "test.wasm", "test", 0, argv);
- EXPECT_EQ(10, argv[0]);
- test_shared_heap(shared_heap, "test.aot", "test", 0, argv);
- EXPECT_EQ(10, argv[0]);
- test_shared_heap(shared_heap, "test_chain.aot", "test", 0, argv);
- EXPECT_EQ(10, argv[0]);
- }
- TEST_F(shared_heap_test, test_shared_heap_malloc_fail)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr;
- uint32 argv[1] = {};
- args.size = os_getpagesize();
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Failed to create shared heap";
- }
- argv[0] = os_getpagesize();
- test_shared_heap(shared_heap, "test.wasm", "test_malloc_fail", 1, argv);
- EXPECT_EQ(1, argv[0]);
- argv[0] = os_getpagesize();
- test_shared_heap(shared_heap, "test.aot", "test_malloc_fail", 1, argv);
- EXPECT_EQ(1, argv[0]);
- argv[0] = os_getpagesize();
- test_shared_heap(shared_heap, "test_chain.aot", "test_malloc_fail", 1,
- argv);
- EXPECT_EQ(1, argv[0]);
- }
- TEST_F(shared_heap_test, test_preallocated_shared_heap_malloc_fail)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr;
- uint32 argv[1] = {}, BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- /* create a preallocated shared heap */
- args.pre_allocated_addr = preallocated_buf_ptr;
- args.size = BUF_SIZE;
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Create preallocated shared heap failed.\n";
- }
- /* test wasm can't malloc with preallocated shared heap */
- argv[0] = 1024;
- test_shared_heap(shared_heap, "test.wasm", "my_shared_heap_malloc", 1,
- argv);
- EXPECT_EQ(0, argv[0]);
- argv[0] = 1024;
- test_shared_heap(shared_heap, "test.aot", "my_shared_heap_malloc", 1, argv);
- EXPECT_EQ(0, argv[0]);
- argv[0] = 1024;
- test_shared_heap(shared_heap, "test_chain.aot", "my_shared_heap_malloc", 1,
- argv);
- EXPECT_EQ(0, argv[0]);
- }
- TEST_F(shared_heap_test, test_preallocated_shared_runtime_api)
- {
- struct ret_env tmp_module_env;
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr;
- uint32 argv[1] = {};
- void *native_ptr;
- uint64 offset, size;
- bool ret;
- args.size = 0x4000;
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Failed to create shared heap";
- }
- if (!load_wasm("test.wasm", 0, tmp_module_env)) {
- FAIL() << "Failed to load wasm file\n";
- }
- if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst,
- shared_heap)) {
- ADD_FAILURE() << "Failed to attach shared heap\n";
- goto fail1;
- }
- offset = wasm_runtime_shared_heap_malloc(tmp_module_env.wasm_module_inst,
- 32, &native_ptr);
- if (!offset) {
- ADD_FAILURE() << "Failed to attach shared heap\n";
- goto fail2;
- }
- size = (uint64_t)UINT32_MAX + 0x2000;
- printf("offset %llx size: %llx\n", offset, size);
- ASSERT_EQ(false, wasm_runtime_validate_app_addr(
- tmp_module_env.wasm_module_inst, offset, size));
- ASSERT_EQ(NULL, wasm_runtime_addr_app_to_native(
- tmp_module_env.wasm_module_inst, offset + size));
- size = (uint64_t)10;
- ASSERT_EQ(true, wasm_runtime_validate_app_addr(
- tmp_module_env.wasm_module_inst, offset, size));
- ASSERT_EQ((char *)native_ptr + size,
- wasm_runtime_addr_app_to_native(tmp_module_env.wasm_module_inst,
- offset + size));
- fail2:
- wasm_runtime_detach_shared_heap(tmp_module_env.wasm_module_inst);
- fail1:
- destroy_module_env(tmp_module_env);
- }
- static void
- create_test_shared_heap(uint8 *preallocated_buf, size_t size,
- WASMSharedHeap **shared_heap_res)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr;
- args.pre_allocated_addr = preallocated_buf;
- args.size = size;
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Create preallocated shared heap failed.\n";
- }
- *shared_heap_res = shared_heap;
- if (!*shared_heap_res) {
- FAIL() << "Create shared heap chain failed.\n";
- }
- }
- static void
- create_test_shared_heap_chain(uint8 *preallocated_buf, size_t size,
- uint8 *preallocated_buf2, size_t size2,
- WASMSharedHeap **shared_heap_chain)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr;
- args.pre_allocated_addr = preallocated_buf;
- args.size = size;
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Create preallocated shared heap failed.\n";
- }
- memset(&args, 0, sizeof(args));
- args.pre_allocated_addr = preallocated_buf2;
- args.size = size2;
- shared_heap2 = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap2) {
- FAIL() << "Create preallocated shared heap failed.\n";
- }
- *shared_heap_chain =
- wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
- if (!*shared_heap_chain) {
- FAIL() << "Create shared heap chain failed.\n";
- }
- }
- TEST_F(shared_heap_test, test_shared_heap_rmw)
- {
- WASMSharedHeap *shared_heap = nullptr;
- uint32 argv[2] = {}, BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- uint32 start1, end1;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- create_test_shared_heap(preallocated_buf_ptr, BUF_SIZE, &shared_heap);
- /* app addr for shared heap */
- start1 = UINT32_MAX - BUF_SIZE + 1;
- end1 = UINT32_MAX;
- argv[0] = end1;
- argv[1] = 101;
- test_shared_heap(shared_heap, "test.wasm", "read_modify_write_8", 2, argv);
- EXPECT_EQ(0, argv[0]);
- EXPECT_EQ(preallocated_buf[BUF_SIZE - 1], 101);
- argv[0] = start1;
- argv[1] = 37;
- test_shared_heap(shared_heap, "test.wasm", "read_modify_write_8", 2, argv);
- EXPECT_EQ(0, argv[0]);
- EXPECT_EQ(preallocated_buf[0], 37);
- argv[0] = end1;
- argv[1] = 81;
- test_shared_heap(shared_heap, "test.aot", "read_modify_write_8", 2, argv);
- EXPECT_EQ(101, argv[0]);
- EXPECT_EQ(preallocated_buf[BUF_SIZE - 1], 81);
- argv[0] = start1;
- argv[1] = 98;
- test_shared_heap(shared_heap, "test.aot", "read_modify_write_8", 2, argv);
- EXPECT_EQ(37, argv[0]);
- EXPECT_EQ(preallocated_buf[0], 98);
- }
- TEST_F(shared_heap_test, test_shared_heap_chain_rmw)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap_chain = nullptr;
- uint32 argv[2] = {}, BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- uint8 *preallocated_buf2_ptr = nullptr;
- uint32 start1, end1, start2, end2;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- std::vector<uint8> preallocated_buf2(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- preallocated_buf2_ptr = preallocated_buf2.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- ASSERT_NE(preallocated_buf2_ptr, nullptr);
- create_test_shared_heap_chain(preallocated_buf_ptr, BUF_SIZE,
- preallocated_buf2_ptr, BUF_SIZE,
- &shared_heap_chain);
- /* app addr for shared heap */
- start1 = UINT32_MAX - 2 * BUF_SIZE + 1;
- end1 = UINT32_MAX - BUF_SIZE;
- start2 = UINT32_MAX - BUF_SIZE + 1;
- end2 = UINT32_MAX;
- /* shared heap 1 */
- argv[0] = end1;
- argv[1] = 101;
- test_shared_heap(shared_heap_chain, "test.wasm", "read_modify_write_8", 2,
- argv);
- EXPECT_EQ(0, argv[0]);
- EXPECT_EQ(preallocated_buf[BUF_SIZE - 1], 101);
- /* shared heap 2 */
- argv[0] = start2;
- argv[1] = 129;
- test_shared_heap(shared_heap_chain, "test.wasm", "read_modify_write_8", 2,
- argv);
- EXPECT_EQ(0, argv[0]);
- EXPECT_EQ(preallocated_buf2[0], 129);
- argv[0] = start1;
- argv[1] = 98;
- test_shared_heap(shared_heap_chain, "test_chain.aot", "read_modify_write_8",
- 2, argv);
- EXPECT_EQ(0, argv[0]);
- EXPECT_EQ(preallocated_buf[0], 98);
- argv[0] = end2;
- argv[1] = 81;
- test_shared_heap(shared_heap_chain, "test_chain.aot", "read_modify_write_8",
- 2, argv);
- EXPECT_EQ(0, argv[0]);
- EXPECT_EQ(preallocated_buf2[BUF_SIZE - 1], 81);
- }
- TEST_F(shared_heap_test, test_shared_heap_chain_rmw_bulk_memory)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap_chain = nullptr;
- uint32 argv[3] = {}, BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- uint8 *preallocated_buf2_ptr = nullptr;
- uint32 start1, end1, start2, end2;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- std::vector<uint8> preallocated_buf2(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- preallocated_buf2_ptr = preallocated_buf2.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- ASSERT_NE(preallocated_buf2_ptr, nullptr);
- create_test_shared_heap_chain(preallocated_buf_ptr, BUF_SIZE,
- preallocated_buf2_ptr, BUF_SIZE,
- &shared_heap_chain);
- /* app addr for shared heap */
- start1 = UINT32_MAX - 2 * BUF_SIZE + 1;
- end1 = UINT32_MAX - BUF_SIZE;
- start2 = UINT32_MAX - BUF_SIZE + 1;
- end2 = UINT32_MAX;
- argv[0] = end1;
- argv[1] = 101;
- argv[2] = 1;
- test_shared_heap(shared_heap_chain, "test_bulk_memory.wasm",
- "memory_fill_test", 3, argv);
- /* no modification since no return value */
- EXPECT_EQ(end1, argv[0]);
- EXPECT_EQ(preallocated_buf[BUF_SIZE - 1], 101);
- argv[0] = start1;
- argv[1] = 14;
- argv[2] = 1;
- test_shared_heap(shared_heap_chain, "test_bulk_memory_chain.aot",
- "memory_fill_test", 3, argv);
- /* no modification since no return value */
- EXPECT_EQ(start1, argv[0]);
- EXPECT_EQ(preallocated_buf[0], 14);
- /* nothing happen when memory fill 0 byte */
- argv[0] = start2;
- argv[1] = 68;
- argv[2] = 0;
- test_shared_heap(shared_heap_chain, "test_bulk_memory_chain.aot",
- "memory_fill_test", 3, argv);
- /* no modification since no return value */
- EXPECT_EQ(start2, argv[0]);
- EXPECT_EQ(preallocated_buf2[0], 0);
- argv[0] = end2;
- argv[1] = 98;
- argv[2] = 1;
- test_shared_heap(shared_heap_chain, "test_bulk_memory_chain.aot",
- "memory_fill_test", 3, argv);
- /* no modification since no return value */
- EXPECT_EQ(end2, argv[0]);
- EXPECT_EQ(preallocated_buf2[BUF_SIZE - 1], 98);
- }
- TEST_F(shared_heap_test, test_shared_heap_chain_rmw_bulk_memory_oob)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap_chain = nullptr;
- uint32 argv[3] = {}, BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- uint8 *preallocated_buf2_ptr = nullptr;
- uint32 start1, end1, start2, end2;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- std::vector<uint8> preallocated_buf2(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- preallocated_buf2_ptr = preallocated_buf2.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- ASSERT_NE(preallocated_buf2_ptr, nullptr);
- create_test_shared_heap_chain(preallocated_buf_ptr, BUF_SIZE,
- preallocated_buf2_ptr, BUF_SIZE,
- &shared_heap_chain);
- /* app addr for shared heap */
- start1 = UINT32_MAX - 2 * BUF_SIZE + 1;
- end1 = UINT32_MAX - BUF_SIZE;
- start2 = UINT32_MAX - BUF_SIZE + 1;
- end2 = UINT32_MAX;
- /* shared heap 1 */
- argv[0] = end1;
- argv[1] = 101;
- argv[2] = 2;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
- "test_bulk_memory.wasm",
- "memory_fill_test", 3, argv),
- "Exception: out of bounds memory access");
- argv[0] = end2;
- argv[1] = 98;
- argv[2] = 2;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
- "test_bulk_memory.wasm",
- "memory_fill_test", 3, argv),
- "Exception: out of bounds memory access");
- argv[0] = start1;
- argv[1] = 98;
- argv[2] = BUF_SIZE + 1;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
- "test_bulk_memory.wasm",
- "memory_fill_test", 3, argv),
- "Exception: out of bounds memory access");
- argv[0] = start2;
- argv[1] = 98;
- argv[2] = BUF_SIZE + 1;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
- "test_bulk_memory.wasm",
- "memory_fill_test", 3, argv),
- "Exception: out of bounds memory access");
- argv[0] = end1;
- argv[1] = 101;
- argv[2] = 2;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
- "test_bulk_memory_chain.aot",
- "memory_fill_test", 3, argv),
- "Exception: out of bounds memory access");
- argv[0] = end2;
- argv[1] = 98;
- argv[2] = 2;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
- "test_bulk_memory_chain.aot",
- "memory_fill_test", 3, argv),
- "Exception: out of bounds memory access");
- argv[0] = start1;
- argv[1] = 98;
- argv[2] = BUF_SIZE + 1;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
- "test_bulk_memory_chain.aot",
- "memory_fill_test", 3, argv),
- "Exception: out of bounds memory access");
- argv[0] = start2;
- argv[1] = 98;
- argv[2] = BUF_SIZE + 1;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
- "test_bulk_memory_chain.aot",
- "memory_fill_test", 3, argv),
- "Exception: out of bounds memory access");
- }
- TEST_F(shared_heap_test, test_shared_heap_rmw_oob)
- {
- WASMSharedHeap *shared_heap = nullptr;
- uint32 argv[2] = {}, BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- uint8 *preallocated_buf2_ptr = nullptr;
- uint32 start1, end1, start2, end2;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- std::vector<uint8> preallocated_buf2(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- preallocated_buf2_ptr = preallocated_buf2.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- ASSERT_NE(preallocated_buf2_ptr, nullptr);
- create_test_shared_heap(preallocated_buf_ptr, BUF_SIZE, &shared_heap);
- /* app addr for shared heap */
- start1 = UINT32_MAX - BUF_SIZE + 1;
- end1 = UINT32_MAX;
- /* try to rmw an u16, first u8 is in the first shared heap and second u8 is
- * in the second shared heap, will be seen as oob */
- argv[0] = end1;
- argv[1] = 12025;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap, "test.wasm",
- "read_modify_write_16", 2, argv),
- "Exception: out of bounds memory access");
- argv[0] = start1 - 1;
- argv[1] = 12025;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap, "test.aot",
- "read_modify_write_16", 2, argv),
- "Exception: out of bounds memory access");
- argv[0] = end1;
- argv[1] = 12025;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap, "test.aot",
- "read_modify_write_16", 2, argv),
- "Exception: out of bounds memory access");
- }
- TEST_F(shared_heap_test, test_shared_heap_chain_rmw_oob)
- {
- WASMSharedHeap *shared_heap_chain = nullptr;
- uint32 argv[2] = {}, BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- uint8 *preallocated_buf2_ptr = nullptr;
- uint32 start1, end1, start2, end2;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- std::vector<uint8> preallocated_buf2(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- preallocated_buf2_ptr = preallocated_buf2.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- ASSERT_NE(preallocated_buf2_ptr, nullptr);
- create_test_shared_heap_chain(preallocated_buf_ptr, BUF_SIZE,
- preallocated_buf2_ptr, BUF_SIZE,
- &shared_heap_chain);
- /* app addr for shared heap */
- start1 = UINT32_MAX - 2 * BUF_SIZE + 1;
- end1 = UINT32_MAX - BUF_SIZE;
- start2 = UINT32_MAX - BUF_SIZE + 1;
- end2 = UINT32_MAX;
- /* try to rmw an u16, first u8 is in the first shared heap and second u8 is
- * in the second shared heap, will be seen as oob */
- argv[0] = end2;
- argv[1] = 12025;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain, "test.wasm",
- "read_modify_write_16", 2, argv),
- "Exception: out of bounds memory access");
- argv[0] = end1;
- argv[1] = 12025;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
- "test_chain.aot",
- "read_modify_write_16", 2, argv),
- "Exception: out of bounds memory access");
- }
- #if WASM_ENABLE_MEMORY64 != 0
- TEST_F(shared_heap_test, test_shared_heap_chain_memory64_rmw)
- {
- WASMSharedHeap *shared_heap_chain = nullptr;
- uint32 argv[3] = {}, BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- uint8 *preallocated_buf2_ptr = nullptr;
- uint64 start1, end1, start2, end2;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- std::vector<uint8> preallocated_buf2(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- preallocated_buf2_ptr = preallocated_buf2.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- ASSERT_NE(preallocated_buf2_ptr, nullptr);
- create_test_shared_heap_chain(preallocated_buf_ptr, BUF_SIZE,
- preallocated_buf2_ptr, BUF_SIZE,
- &shared_heap_chain);
- /* app addr for shared heap */
- start1 = UINT64_MAX - 2 * BUF_SIZE + 1;
- end1 = UINT64_MAX - BUF_SIZE;
- start2 = UINT64_MAX - BUF_SIZE + 1;
- end2 = UINT64_MAX;
- /* shared heap 1 */
- PUT_I64_TO_ADDR(argv, end1);
- argv[2] = 101;
- test_shared_heap(shared_heap_chain, "test64.wasm", "read_modify_write_8", 3,
- argv);
- EXPECT_EQ(0, argv[0]);
- EXPECT_EQ(preallocated_buf[BUF_SIZE - 1], 101);
- /* shared heap 2 */
- PUT_I64_TO_ADDR(argv, start2);
- argv[2] = 129;
- test_shared_heap(shared_heap_chain, "test64.wasm", "read_modify_write_8", 3,
- argv);
- EXPECT_EQ(0, argv[0]);
- EXPECT_EQ(preallocated_buf2[0], 129);
- PUT_I64_TO_ADDR(argv, start1);
- argv[2] = 98;
- test_shared_heap(shared_heap_chain, "test64_chain.aot",
- "read_modify_write_8", 3, argv);
- EXPECT_EQ(0, argv[0]);
- EXPECT_EQ(preallocated_buf[0], 98);
- PUT_I64_TO_ADDR(argv, end2);
- argv[2] = 81;
- test_shared_heap(shared_heap_chain, "test64_chain.aot",
- "read_modify_write_8", 3, argv);
- EXPECT_EQ(0, argv[0]);
- EXPECT_EQ(preallocated_buf2[BUF_SIZE - 1], 81);
- }
- TEST_F(shared_heap_test, test_shared_heap_chain_memory64_rmw_oob)
- {
- WASMSharedHeap *shared_heap_chain = nullptr;
- uint32 argv[3] = {}, BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- uint8 *preallocated_buf2_ptr = nullptr;
- uint64 start1, end1, start2, end2;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- std::vector<uint8> preallocated_buf2(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- preallocated_buf2_ptr = preallocated_buf2.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- ASSERT_NE(preallocated_buf2_ptr, nullptr);
- create_test_shared_heap_chain(preallocated_buf_ptr, BUF_SIZE,
- preallocated_buf2_ptr, BUF_SIZE,
- &shared_heap_chain);
- /* app addr for shared heap */
- start1 = UINT64_MAX - 2 * BUF_SIZE + 1;
- end1 = UINT64_MAX - BUF_SIZE;
- start2 = UINT64_MAX - BUF_SIZE + 1;
- end2 = UINT64_MAX;
- /* try to rmw an u16, first u8 is in the first shared heap and second u8 is
- * in the second shared heap, will be seen as oob */
- PUT_I64_TO_ADDR(argv, end1);
- argv[2] = 12025;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain, "test64.wasm",
- "read_modify_write_16", 3, argv),
- "Exception: out of bounds memory access");
- PUT_I64_TO_ADDR(argv, end1);
- argv[2] = 12025;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
- "test64_chain.aot",
- "read_modify_write_16", 3, argv),
- "Exception: out of bounds memory access");
- }
- #endif
- #ifndef native_function
- /* clang-format off */
- #define native_function(func_name, signature) \
- { #func_name, (void *)glue_## func_name, signature, NULL }
- /* clang-format on */
- #endif
- #ifndef nitems
- #define nitems(_a) (sizeof(_a) / sizeof(0 [(_a)]))
- #endif /* nitems */
- uintptr_t
- glue_test_addr_conv(wasm_exec_env_t env, uintptr_t addr)
- {
- wasm_module_inst_t module_inst = get_module_inst(env);
- void *native_addr = (void *)addr;
- uintptr_t app_addr = addr_native_to_app(native_addr);
- native_addr = addr_app_to_native(app_addr);
- if (native_addr != (void *)addr) {
- ADD_FAILURE() << "address conversion incorrect";
- return 0;
- }
- return app_addr;
- }
- static NativeSymbol g_test_native_symbols[] = {
- native_function(test_addr_conv, "(*)i"),
- };
- TEST_F(shared_heap_test, test_addr_conv)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr;
- uint32 argv[1] = {};
- bool ret = false;
- ret = wasm_native_register_natives("env", g_test_native_symbols,
- nitems(g_test_native_symbols));
- if (!ret) {
- FAIL() << "Failed to register natives";
- }
- args.size = 1024;
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Failed to create shared heap";
- }
- test_shared_heap(shared_heap, "test_addr_conv.wasm", "test", 0, argv);
- EXPECT_EQ(1, argv[0]);
- test_shared_heap(shared_heap, "test_addr_conv.aot", "test", 0, argv);
- EXPECT_EQ(1, argv[0]);
- test_shared_heap(shared_heap, "test_addr_conv_chain.aot", "test", 0, argv);
- EXPECT_EQ(1, argv[0]);
- }
- TEST_F(shared_heap_test, test_addr_conv_pre_allocated_oob)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr;
- uint32 argv[1] = {}, BUF_SIZE = os_getpagesize(),
- app_addr = 0xFFFFFFFF - BUF_SIZE;
- uint8 *preallocated_buf_ptr = nullptr;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- bool ret = false;
- /* create a preallocated shared heap */
- ret = wasm_native_register_natives("env", g_test_native_symbols,
- nitems(g_test_native_symbols));
- if (!ret) {
- FAIL() << "Failed to register natives";
- }
- args.pre_allocated_addr = preallocated_buf_ptr;
- args.size = BUF_SIZE;
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Failed to create shared heap";
- }
- argv[0] = app_addr;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap, "test_addr_conv.wasm",
- "test_preallocated", 1, argv),
- "Exception: out of bounds memory access");
- argv[0] = app_addr;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap, "test_addr_conv.aot",
- "test_preallocated", 1, argv),
- "Exception: out of bounds memory access");
- argv[0] = app_addr;
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap,
- "test_addr_conv_chain.aot",
- "test_preallocated", 1, argv),
- "Exception: out of bounds memory access");
- }
- TEST_F(shared_heap_test, test_shared_heap_chain)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr,
- *shared_heap_chain = nullptr;
- uint32 argv[1] = {}, BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- bool ret = false;
- ret = wasm_native_register_natives("env", g_test_native_symbols,
- nitems(g_test_native_symbols));
- if (!ret) {
- FAIL() << "Failed to register natives";
- }
- args.size = 1024;
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Failed to create shared heap";
- }
- /* create a preallocated shared heap */
- memset(&args, 0, sizeof(args));
- args.pre_allocated_addr = preallocated_buf_ptr;
- args.size = BUF_SIZE;
- shared_heap2 = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap2) {
- FAIL() << "Create preallocated shared heap failed.\n";
- }
- shared_heap_chain =
- wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
- if (!shared_heap_chain) {
- FAIL() << "Create shared heap chain failed.\n";
- }
- test_shared_heap(shared_heap_chain, "test_addr_conv.wasm", "test", 0, argv);
- EXPECT_EQ(1, argv[0]);
- test_shared_heap(shared_heap, "test_addr_conv.aot", "test", 0, argv);
- EXPECT_EQ(1, argv[0]);
- }
- TEST_F(shared_heap_test, test_shared_heap_chain_create_fail)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr,
- *shared_heap_chain = nullptr;
- args.size = 1024;
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Failed to create shared heap";
- }
- args.size = 4096;
- shared_heap2 = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap2) {
- FAIL() << "Create preallocated shared heap failed.\n";
- }
- shared_heap_chain =
- wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
- EXPECT_EQ(shared_heap_chain, nullptr);
- }
- TEST_F(shared_heap_test, test_shared_heap_chain_create_fail2)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr,
- *shared_heap_chain = nullptr;
- uint32 argv[1] = {}, BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- struct ret_env tmp_module_env;
- args.size = 1024;
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Failed to create shared heap";
- }
- memset(&args, 0, sizeof(args));
- args.pre_allocated_addr = preallocated_buf_ptr;
- args.size = BUF_SIZE;
- shared_heap2 = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap2) {
- FAIL() << "Create preallocated shared heap failed.\n";
- }
- if (!load_wasm((char *)"test.wasm", 0, tmp_module_env)) {
- FAIL() << "Failed to load wasm file\n";
- }
- if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst,
- shared_heap)) {
- FAIL() << "Failed to attach shared heap\n";
- }
- /* can't create shared heap chain when shared heap is attached to a wasm
- * app */
- shared_heap_chain =
- wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
- EXPECT_EQ(shared_heap_chain, nullptr);
- wasm_runtime_detach_shared_heap(tmp_module_env.wasm_module_inst);
- destroy_module_env(tmp_module_env);
- }
- TEST_F(shared_heap_test, test_shared_heap_chain_create_fail3)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr,
- *shared_heap3 = nullptr, *shared_heap_chain = nullptr;
- uint32 argv[1] = {}, BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- uint8 *preallocated_buf2_ptr = nullptr;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- std::vector<uint8> preallocated_buf2(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- preallocated_buf2_ptr = preallocated_buf2.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- ASSERT_NE(preallocated_buf2_ptr, nullptr);
- args.size = 1024;
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Failed to create shared heap";
- }
- memset(&args, 0, sizeof(args));
- args.pre_allocated_addr = preallocated_buf_ptr;
- args.size = BUF_SIZE;
- shared_heap2 = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap2) {
- FAIL() << "Create preallocated shared heap failed.\n";
- }
- shared_heap_chain =
- wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
- if (!shared_heap_chain) {
- FAIL() << "Create shared heap chain failed.\n";
- }
- memset(&args, 0, sizeof(args));
- args.pre_allocated_addr = preallocated_buf2_ptr;
- args.size = BUF_SIZE;
- shared_heap3 = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap3) {
- FAIL() << "Failed to create shared heap";
- }
- /* The head and body can't be already in other shared heap chain as body */
- shared_heap_chain =
- wasm_runtime_chain_shared_heaps(shared_heap3, shared_heap2);
- EXPECT_EQ(shared_heap_chain, nullptr);
- shared_heap_chain =
- wasm_runtime_chain_shared_heaps(shared_heap2, shared_heap);
- EXPECT_EQ(shared_heap_chain, nullptr);
- }
- TEST_F(shared_heap_test, test_shared_heap_chain_unchain)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr,
- *shared_heap3 = nullptr, *shared_heap_chain = nullptr;
- uint32 argv[1] = {}, BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- uint8 *preallocated_buf2_ptr = nullptr;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- std::vector<uint8> preallocated_buf2(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- preallocated_buf2_ptr = preallocated_buf2.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- ASSERT_NE(preallocated_buf2_ptr, nullptr);
- args.size = 1024;
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Failed to create shared heap";
- }
- memset(&args, 0, sizeof(args));
- args.pre_allocated_addr = preallocated_buf_ptr;
- args.size = BUF_SIZE;
- shared_heap2 = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap2) {
- FAIL() << "Create preallocated shared heap failed.\n";
- }
- shared_heap_chain =
- wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
- if (!shared_heap_chain) {
- FAIL() << "Create shared heap chain failed.\n";
- }
- memset(&args, 0, sizeof(args));
- args.pre_allocated_addr = preallocated_buf2_ptr;
- args.size = BUF_SIZE;
- shared_heap3 = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap3) {
- FAIL() << "Failed to create shared heap";
- }
- /* unchain shared heap so that the 'body' can be another chain 'body'
- * again(1->2 to 1->3->2) */
- EXPECT_EQ(shared_heap2,
- wasm_runtime_unchain_shared_heaps(shared_heap_chain, false));
- shared_heap_chain =
- wasm_runtime_chain_shared_heaps(shared_heap3, shared_heap2);
- EXPECT_EQ(shared_heap_chain, shared_heap3);
- shared_heap_chain =
- wasm_runtime_chain_shared_heaps(shared_heap, shared_heap3);
- EXPECT_EQ(shared_heap, shared_heap_chain);
- /* break down the entire shared heap chain */
- EXPECT_EQ(shared_heap2,
- wasm_runtime_unchain_shared_heaps(shared_heap_chain, true));
- }
- TEST_F(shared_heap_test, test_shared_heap_chain_reset_runtime_managed)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr,
- *shared_heap_chain = nullptr;
- uint8 buf_size = 64;
- uint64 offset = 0, offset_after_reset = 0;
- void *native_ptr = nullptr, *native_ptr_after_reset = nullptr;
- uint32 argv[1] = {}, BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- struct ret_env tmp_module_env;
- args.size = 4096;
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Failed to create shared heap";
- }
- args.size = BUF_SIZE;
- args.pre_allocated_addr = preallocated_buf_ptr;
- shared_heap2 = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap2) {
- FAIL() << "Failed to create second shared heap";
- }
- shared_heap_chain =
- wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
- if (!shared_heap_chain) {
- FAIL() << "Create shared heap chain failed.\n";
- }
- if (!load_wasm((char *)"test.wasm", 0, tmp_module_env)) {
- FAIL() << "Failed to load wasm file\n";
- }
- if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst,
- shared_heap_chain)) {
- destroy_module_env(tmp_module_env);
- FAIL() << "Failed to attach shared heap chain";
- }
- offset = wasm_runtime_shared_heap_malloc(tmp_module_env.wasm_module_inst,
- buf_size, &native_ptr);
- ASSERT_NE(0u, offset);
- ASSERT_NE(nullptr, native_ptr);
- memset(native_ptr, 0x5A, buf_size);
- for (uint8 i = 0; i < buf_size; i++) {
- EXPECT_EQ(0x5A, *((uint8 *)native_ptr + i));
- }
- wasm_runtime_detach_shared_heap(tmp_module_env.wasm_module_inst);
- EXPECT_TRUE(wasm_runtime_reset_shared_heap_chain(shared_heap_chain));
- if (!load_wasm((char *)"test.wasm", 0, tmp_module_env)) {
- FAIL() << "Failed to load wasm file after reset\n";
- }
- if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst,
- shared_heap_chain)) {
- destroy_module_env(tmp_module_env);
- FAIL() << "Failed to attach shared heap chain after reset";
- }
- offset_after_reset = wasm_runtime_shared_heap_malloc(
- tmp_module_env.wasm_module_inst, buf_size, &native_ptr_after_reset);
- ASSERT_NE(0u, offset_after_reset);
- ASSERT_NE(nullptr, native_ptr_after_reset);
- EXPECT_EQ(offset, offset_after_reset);
- EXPECT_EQ(native_ptr, native_ptr_after_reset);
- /* Only on some platform, the os_mmap will memset the memory to 0
- for (uint8 i = 0; i < buf_size; i++) {
- EXPECT_EQ(0, *((uint8 *)native_ptr_after_reset + i));
- }
- */
- wasm_runtime_detach_shared_heap(tmp_module_env.wasm_module_inst);
- destroy_module_env(tmp_module_env);
- }
- TEST_F(shared_heap_test, test_shared_heap_chain_reset_preallocated)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr;
- uint32 BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- uint8 set_val = 0xA5;
- args.pre_allocated_addr = preallocated_buf_ptr;
- args.size = BUF_SIZE;
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Create preallocated shared heap failed.\n";
- }
- memset(preallocated_buf_ptr, set_val, BUF_SIZE);
- for (uint32 i = 0; i < BUF_SIZE; i++) {
- EXPECT_EQ(set_val, preallocated_buf[i]);
- }
- EXPECT_TRUE(wasm_runtime_reset_shared_heap_chain(shared_heap));
- for (uint32 i = 0; i < BUF_SIZE; i++) {
- EXPECT_EQ(0, preallocated_buf[i]);
- }
- }
- TEST_F(shared_heap_test, test_shared_heap_chain_reset_attached_fail)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr;
- struct ret_env module_env = {};
- bool ret;
- args.size = 1024;
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Failed to create shared heap";
- }
- ret = load_wasm((char *)"test.wasm", 0, module_env);
- if (!ret) {
- FAIL() << "Failed to load wasm";
- }
- ret = wasm_runtime_attach_shared_heap(module_env.wasm_module_inst,
- shared_heap);
- if (!ret) {
- destroy_module_env(module_env);
- FAIL() << "Failed to attach shared heap";
- }
- EXPECT_FALSE(wasm_runtime_reset_shared_heap_chain(shared_heap));
- wasm_runtime_detach_shared_heap(module_env.wasm_module_inst);
- destroy_module_env(module_env);
- EXPECT_TRUE(wasm_runtime_reset_shared_heap_chain(shared_heap));
- }
- TEST_F(shared_heap_test, test_shared_heap_chain_addr_conv)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr,
- *shared_heap_chain = nullptr;
- uint32 argv[1] = {}, BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- bool ret = false;
- ret = wasm_native_register_natives("env", g_test_native_symbols,
- nitems(g_test_native_symbols));
- if (!ret) {
- FAIL() << "Failed to register natives";
- }
- args.size = 4096;
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Failed to create shared heap";
- }
- /* create a preallocated shared heap */
- memset(&args, 0, sizeof(args));
- args.pre_allocated_addr = preallocated_buf_ptr;
- args.size = BUF_SIZE;
- shared_heap2 = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap2) {
- FAIL() << "Create preallocated shared heap failed.\n";
- }
- shared_heap_chain =
- wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
- if (!shared_heap_chain) {
- FAIL() << "Create shared heap chain failed.\n";
- }
- argv[0] = 0xFFFFFFFF;
- test_shared_heap(shared_heap_chain, "test_addr_conv.wasm",
- "test_preallocated", 1, argv);
- EXPECT_EQ(1, argv[0]);
- argv[0] = 0xFFFFF000;
- test_shared_heap(shared_heap_chain, "test_addr_conv.wasm",
- "test_preallocated", 1, argv);
- EXPECT_EQ(1, argv[0]);
- argv[0] = 0xFFFFFFFF;
- test_shared_heap(shared_heap, "test_addr_conv_chain.aot",
- "test_preallocated", 1, argv);
- EXPECT_EQ(1, argv[0]);
- argv[0] = 0xFFFFF000;
- test_shared_heap(shared_heap, "test_addr_conv_chain.aot",
- "test_preallocated", 1, argv);
- EXPECT_EQ(1, argv[0]);
- }
- TEST_F(shared_heap_test, test_shared_heap_chain_addr_conv_oob)
- {
- SharedHeapInitArgs args = {};
- WASMSharedHeap *shared_heap = nullptr, *shared_heap2 = nullptr,
- *shared_heap_chain = nullptr;
- uint32 argv[1] = {}, BUF_SIZE = os_getpagesize();
- uint8 *preallocated_buf_ptr = nullptr;
- ASSERT_GT(BUF_SIZE, 0u);
- std::vector<uint8> preallocated_buf(BUF_SIZE);
- preallocated_buf_ptr = preallocated_buf.data();
- ASSERT_NE(preallocated_buf_ptr, nullptr);
- bool ret = false;
- ret = wasm_native_register_natives("env", g_test_native_symbols,
- nitems(g_test_native_symbols));
- if (!ret) {
- FAIL() << "Failed to register natives";
- }
- args.size = os_getpagesize();
- shared_heap = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap) {
- FAIL() << "Failed to create shared heap";
- }
- /* create a preallocated shared heap */
- memset(&args, 0, sizeof(args));
- args.pre_allocated_addr = preallocated_buf_ptr;
- args.size = BUF_SIZE;
- shared_heap2 = wasm_runtime_create_shared_heap(&args);
- if (!shared_heap2) {
- FAIL() << "Create preallocated shared heap failed.\n";
- }
- shared_heap_chain =
- wasm_runtime_chain_shared_heaps(shared_heap, shared_heap2);
- if (!shared_heap_chain) {
- FAIL() << "Create shared heap chain failed.\n";
- }
- /* test wasm */
- argv[0] = 0xFFFFFFFF - BUF_SIZE - os_getpagesize();
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
- "test_addr_conv.wasm",
- "test_preallocated", 1, argv),
- "Exception: out of bounds memory access");
- /* test aot */
- argv[0] = 0xFFFFFFFF - BUF_SIZE - os_getpagesize();
- EXPECT_NONFATAL_FAILURE(test_shared_heap(shared_heap_chain,
- "test_addr_conv_chain.aot",
- "test_preallocated", 1, argv),
- "Exception: out of bounds memory access");
- }
|