Procházet zdrojové kódy

fix bug and add unit test case for runtime api when shared heap is enabled (#4695)

TianlongLiang před 2 měsíci
rodič
revize
7898af9f21

+ 4 - 4
core/iwasm/common/wasm_memory.c

@@ -616,12 +616,12 @@ wasm_runtime_get_shared_heap(WASMModuleInstanceCommon *module_inst_comm)
 
 
 bool
 bool
 is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
 is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
-                           bool is_memory64, uint64 app_offset, uint32 bytes)
+                           bool is_memory64, uint64 app_offset, uint64 bytes)
 {
 {
     WASMSharedHeap *heap = get_shared_heap(module_inst), *cur;
     WASMSharedHeap *heap = get_shared_heap(module_inst), *cur;
     uint64 shared_heap_start, shared_heap_end;
     uint64 shared_heap_start, shared_heap_end;
 
 
-    if (!heap) {
+    if (!heap || bytes > APP_HEAP_SIZE_MAX) {
         goto fail;
         goto fail;
     }
     }
 
 
@@ -665,12 +665,12 @@ fail:
 
 
 static bool
 static bool
 is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
 is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
-                              bool is_memory64, uint8 *addr, uint32 bytes)
+                              bool is_memory64, uint8 *addr, uint64 bytes)
 {
 {
     WASMSharedHeap *cur, *heap = get_shared_heap(module_inst);
     WASMSharedHeap *cur, *heap = get_shared_heap(module_inst);
     uintptr_t base_addr, addr_int, end_addr;
     uintptr_t base_addr, addr_int, end_addr;
 
 
-    if (!heap) {
+    if (!heap || bytes > APP_HEAP_SIZE_MAX) {
         goto fail;
         goto fail;
     }
     }
 
 

+ 1 - 1
core/iwasm/common/wasm_memory.h

@@ -84,7 +84,7 @@ SET_LINEAR_MEMORY_SIZE(WASMMemoryInstance *memory, uint64 size)
 #if WASM_ENABLE_SHARED_HEAP != 0
 #if WASM_ENABLE_SHARED_HEAP != 0
 bool
 bool
 is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
 is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
-                           bool is_memory64, uint64 app_offset, uint32 bytes);
+                           bool is_memory64, uint64 app_offset, uint64 bytes);
 
 
 WASMSharedHeap *
 WASMSharedHeap *
 wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args);
 wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args);

+ 5 - 1
core/iwasm/common/wasm_shared_memory.c

@@ -249,10 +249,14 @@ map_try_release_wait_info(HashMap *wait_hash_map, AtomicWaitInfo *wait_info,
 #if WASM_ENABLE_SHARED_HEAP != 0
 #if WASM_ENABLE_SHARED_HEAP != 0
 static bool
 static bool
 is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
 is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
-                              uint8 *addr, uint32 bytes)
+                              uint8 *addr, uint64 bytes)
 {
 {
     WASMSharedHeap *shared_heap = NULL;
     WASMSharedHeap *shared_heap = NULL;
 
 
+    if (bytes > APP_HEAP_SIZE_MAX) {
+        return false;
+    }
+
 #if WASM_ENABLE_INTERP != 0
 #if WASM_ENABLE_INTERP != 0
     if (module_inst->module_type == Wasm_Module_Bytecode) {
     if (module_inst->module_type == Wasm_Module_Bytecode) {
         shared_heap = ((WASMModuleInstance *)module_inst)->e->shared_heap;
         shared_heap = ((WASMModuleInstance *)module_inst)->e->shared_heap;

+ 56 - 0
tests/unit/shared-heap/shared_heap_test.cc

@@ -217,6 +217,62 @@ TEST_F(shared_heap_test, test_preallocated_shared_heap_malloc_fail)
     EXPECT_EQ(0, argv[0]);
     EXPECT_EQ(0, argv[0]);
 }
 }
 
 
+TEST_F(shared_heap_test, test_preallocated_shared_runtime_api)
+{
+    struct ret_env tmp_module_env;
+    SharedHeapInitArgs args = { 0 };
+    WASMSharedHeap *shared_heap = nullptr;
+    uint32 argv[1] = { 0 };
+    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 %lx size: %lx\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(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
 static void
 create_test_shared_heap(uint8 *preallocated_buf, size_t size,
 create_test_shared_heap(uint8 *preallocated_buf, size_t size,
                         WASMSharedHeap **shared_heap_res)
                         WASMSharedHeap **shared_heap_res)