Przeglądaj źródła

cr suggestions and some refactor

TL 11 miesięcy temu
rodzic
commit
a9d776eda3

+ 7 - 6
core/iwasm/common/wasm_memory.c

@@ -185,7 +185,7 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
 
 
     if (size > APP_HEAP_SIZE_MAX || size < APP_HEAP_SIZE_MIN) {
     if (size > APP_HEAP_SIZE_MAX || size < APP_HEAP_SIZE_MIN) {
         LOG_WARNING("Invalid size of shared heap");
         LOG_WARNING("Invalid size of shared heap");
-        goto fail1;
+        goto fail2;
     }
     }
 
 
     if (init_args->pre_allocated_addr != NULL) {
     if (init_args->pre_allocated_addr != NULL) {
@@ -194,7 +194,7 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
         if (size != init_args->size) {
         if (size != init_args->size) {
             LOG_WARNING("Pre allocated size need to be aligned with system "
             LOG_WARNING("Pre allocated size need to be aligned with system "
                         "page size to create shared heap");
                         "page size to create shared heap");
-            goto fail1;
+            goto fail2;
         }
         }
 
 
         heap->heap_handle = NULL;
         heap->heap_handle = NULL;
@@ -252,12 +252,13 @@ WASMSharedHeap *
 wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body)
 wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body)
 {
 {
     WASMSharedHeap *cur;
     WASMSharedHeap *cur;
-    bool heap_handle_exist = head->heap_handle != NULL;
+    bool heap_handle_exist = false;
 
 
     if (!head || !body) {
     if (!head || !body) {
         LOG_WARNING("Invalid shared heap to chain.");
         LOG_WARNING("Invalid shared heap to chain.");
         return NULL;
         return NULL;
     }
     }
+    heap_handle_exist = head->heap_handle != NULL;
 
 
     os_mutex_lock(&shared_heap_list_lock);
     os_mutex_lock(&shared_heap_list_lock);
     if (head->attached_count != 0 || body->attached_count != 0) {
     if (head->attached_count != 0 || body->attached_count != 0) {
@@ -543,16 +544,16 @@ is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
                               uint8 *addr, uint32 bytes,
                               uint8 *addr, uint32 bytes,
                               WASMSharedHeap **target_heap)
                               WASMSharedHeap **target_heap)
 {
 {
-    WASMSharedHeap *cur, *heap_head = 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_head) {
+    if (!heap) {
         goto fail;
         goto fail;
     }
     }
 
 
     /* Iterate through shared heap chain to find whether native addr in one of
     /* Iterate through shared heap chain to find whether native addr in one of
      * shared heap */
      * shared heap */
-    for (cur = heap_head; cur != NULL; cur = cur->chain_next) {
+    for (cur = heap; cur != NULL; cur = cur->chain_next) {
         base_addr = (uintptr_t)cur->base_addr;
         base_addr = (uintptr_t)cur->base_addr;
         addr_int = (uintptr_t)addr;
         addr_int = (uintptr_t)addr;
         if (addr_int < base_addr)
         if (addr_int < base_addr)

+ 48 - 0
core/iwasm/common/wasm_shared_memory.h

@@ -56,6 +56,54 @@ uint32
 wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module, void *address,
 wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module, void *address,
                            uint32 count);
                            uint32 count);
 
 
+#if WASM_ENABLE_MULTI_MEMORY != 0
+/* Only enable shared heap for the default memory */
+#define is_default_memory (memidx == 0)
+#else
+#define is_default_memory true
+#endif
+#if WASM_ENABLE_MEMORY64 != 0
+#define get_shared_heap_start_off(shared_heap) \
+    (is_memory64 ? shared_heap->start_off_mem64 : shared_heap->start_off_mem32)
+#else
+#define get_shared_heap_start_off(shared_heap) (shared_heap->start_off_mem32)
+#endif
+/* Check whether the app addr in the last visited shared heap, if not, check the
+ * shared heap chain to find which(if any) shared heap the app addr in, and
+ * update the last visited shared heap info if found. */
+#define app_addr_in_shared_heap(app_addr, bytes)                               \
+    (shared_heap && is_default_memory && (app_addr) >= shared_heap_start_off   \
+     && (app_addr) <= shared_heap_end_off - bytes + 1)                         \
+        || ({                                                                  \
+               bool in_chain = false;                                          \
+               WASMSharedHeap *cur;                                            \
+               uint64 cur_shared_heap_start_off, cur_shared_heap_end_off;      \
+               for (cur = shared_heap; cur; cur = cur->chain_next) {           \
+                   cur_shared_heap_start_off = get_shared_heap_start_off(cur); \
+                   cur_shared_heap_end_off =                                   \
+                       cur_shared_heap_start_off - 1 + cur->size;              \
+                   if ((app_addr) >= cur_shared_heap_start_off                 \
+                       && (app_addr) <= cur_shared_heap_end_off - bytes + 1) { \
+                       shared_heap_start_off = cur_shared_heap_start_off;      \
+                       shared_heap_end_off = cur_shared_heap_end_off;          \
+                       shared_heap_base_addr = cur->base_addr;                 \
+                       in_chain = true;                                        \
+                       break;                                                  \
+                   }                                                           \
+               }                                                               \
+               in_chain;                                                       \
+           })
+
+#define shared_heap_addr_app_to_native(app_addr, native_addr) \
+    native_addr = shared_heap_base_addr + ((app_addr)-shared_heap_start_off)
+
+#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr) \
+    if (app_addr_in_shared_heap(app_addr, bytes))                \
+        shared_heap_addr_app_to_native(app_addr, native_addr);   \
+    else
+#else
+#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr)
+
 #ifdef __cplusplus
 #ifdef __cplusplus
 }
 }
 #endif
 #endif

+ 5 - 5
core/iwasm/include/wasm_export.h

@@ -2259,11 +2259,11 @@ WASM_RUNTIME_API_EXTERN wasm_shared_heap_t
 wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args);
 wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args);
 
 
 /**
 /**
- * This function links two shared heaps, `head` and `body`, where `head` is a
- * shared heap and `body` can be shared heap chain head, into a single chain.
- * The `head` heap will be the head of the chain, and the `body` heap will be
- * appended to it. At most one shared heap in shared heap chain can be
- * dynamically allocated, the rest have to be the pre-allocated shared heap *
+ * This function links two shared heap(lists), `head` and `body` in to a single
+ * shared heap list, where `head` becomes the new shared heap list head. The
+ * shared heap list remains one continuous shared heap in wasm app's point of
+ * view.  At most one shared heap in shared heap list can be dynamically
+ * allocated, the rest have to be the pre-allocated shared heap. *
  *
  *
  * @param head The head of the shared heap chain.
  * @param head The head of the shared heap chain.
  * @param body The body of the shared heap chain to be appended.
  * @param body The body of the shared heap chain to be appended.

+ 0 - 50
core/iwasm/interpreter/wasm_interp_classic.c

@@ -46,56 +46,6 @@ typedef float64 CellType_F64;
 #define get_linear_mem_size() GET_LINEAR_MEMORY_SIZE(memory)
 #define get_linear_mem_size() GET_LINEAR_MEMORY_SIZE(memory)
 #endif
 #endif
 
 
-#if WASM_ENABLE_SHARED_HEAP != 0
-#if WASM_ENABLE_MULTI_MEMORY != 0
-/* Only enable shared heap for the default memory */
-#define is_default_memory (memidx == 0)
-#else
-#define is_default_memory true
-#endif
-#if WASM_ENABLE_MEMORY64 != 0
-#define get_shared_heap_start_off(shared_heap) \
-    (is_memory64 ? shared_heap->start_off_mem64 : shared_heap->start_off_mem32)
-#else
-#define get_shared_heap_start_off(shared_heap) (shared_heap->start_off_mem32)
-#endif
-/* Check whether the app addr in the last visited shared heap, if not, check the
- * shared heap chain to find which(if any) shared heap the app addr in, and
- * update the last visited shared heap info if found. */
-#define app_addr_in_shared_heap(app_addr, bytes)                               \
-    (shared_heap && is_default_memory && (app_addr) >= shared_heap_start_off   \
-     && (app_addr) <= shared_heap_end_off - bytes + 1)                         \
-        || ({                                                                  \
-               bool in_chain = false;                                          \
-               WASMSharedHeap *cur;                                            \
-               uint64 cur_shared_heap_start_off, cur_shared_heap_end_off;      \
-               for (cur = shared_heap; cur; cur = cur->chain_next) {           \
-                   cur_shared_heap_start_off = get_shared_heap_start_off(cur); \
-                   cur_shared_heap_end_off =                                   \
-                       cur_shared_heap_start_off - 1 + cur->size;              \
-                   if ((app_addr) >= cur_shared_heap_start_off                 \
-                       && (app_addr) <= cur_shared_heap_end_off - bytes + 1) { \
-                       shared_heap_start_off = cur_shared_heap_start_off;      \
-                       shared_heap_end_off = cur_shared_heap_end_off;          \
-                       shared_heap_base_addr = cur->base_addr;                 \
-                       in_chain = true;                                        \
-                       break;                                                  \
-                   }                                                           \
-               }                                                               \
-               in_chain;                                                       \
-           })
-
-#define shared_heap_addr_app_to_native(app_addr, native_addr) \
-    native_addr = shared_heap_base_addr + ((app_addr)-shared_heap_start_off)
-
-#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr) \
-    if (app_addr_in_shared_heap(app_addr, bytes))                \
-        shared_heap_addr_app_to_native(app_addr, native_addr);   \
-    else
-#else
-#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr)
-#endif
-
 #if WASM_ENABLE_MEMORY64 == 0
 #if WASM_ENABLE_MEMORY64 == 0
 
 
 #if (!defined(OS_ENABLE_HW_BOUND_CHECK) \
 #if (!defined(OS_ENABLE_HW_BOUND_CHECK) \

+ 6 - 40
core/iwasm/interpreter/wasm_interp_fast.c

@@ -37,46 +37,6 @@ typedef float64 CellType_F64;
 #define get_linear_mem_size() GET_LINEAR_MEMORY_SIZE(memory)
 #define get_linear_mem_size() GET_LINEAR_MEMORY_SIZE(memory)
 #endif
 #endif
 
 
-#if WASM_ENABLE_SHARED_HEAP != 0
-/* TODO: add code for 64 bit version when memory64 is enabled for fast-interp */
-#define get_shared_heap_start_off(shared_heap) (shared_heap->start_off_mem32)
-/* Check whether the app addr in the last visited shared heap, if not, check the
- * shared heap chain to find which(if any) shared heap the app addr in, and
- * update the last visited shared heap info if found. */
-#define app_addr_in_shared_heap(app_addr, bytes)                               \
-    (shared_heap && (app_addr) >= shared_heap_start_off                        \
-     && (app_addr) <= shared_heap_end_off - bytes + 1)                         \
-        || ({                                                                  \
-               bool in_chain = false;                                          \
-               WASMSharedHeap *cur;                                            \
-               uint64 cur_shared_heap_start_off, cur_shared_heap_end_off;      \
-               for (cur = shared_heap; cur; cur = cur->chain_next) {           \
-                   cur_shared_heap_start_off = get_shared_heap_start_off(cur); \
-                   cur_shared_heap_end_off =                                   \
-                       cur_shared_heap_start_off - 1 + cur->size;              \
-                   if ((app_addr) >= cur_shared_heap_start_off                 \
-                       && (app_addr) <= cur_shared_heap_end_off - bytes + 1) { \
-                       shared_heap_start_off = cur_shared_heap_start_off;      \
-                       shared_heap_end_off = cur_shared_heap_end_off;          \
-                       shared_heap_base_addr = cur->base_addr;                 \
-                       in_chain = true;                                        \
-                       break;                                                  \
-                   }                                                           \
-               }                                                               \
-               in_chain;                                                       \
-           })
-
-#define shared_heap_addr_app_to_native(app_addr, native_addr) \
-    native_addr = shared_heap_base_addr + ((app_addr)-shared_heap_start_off)
-
-#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr) \
-    if (app_addr_in_shared_heap(app_addr, bytes))                \
-        shared_heap_addr_app_to_native(app_addr, native_addr);   \
-    else
-#else
-#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr)
-#endif
-
 #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
 #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
     || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
     || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
 #define CHECK_MEMORY_OVERFLOW(bytes)                                           \
 #define CHECK_MEMORY_OVERFLOW(bytes)                                           \
@@ -1562,6 +1522,12 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
     bool is_return_call = false;
     bool is_return_call = false;
 #endif
 #endif
 #if WASM_ENABLE_SHARED_HEAP != 0
 #if WASM_ENABLE_SHARED_HEAP != 0
+    /* TODO: currently flowing two variables are only dummy for shared heap
+     * boundary check, need to be updated when multi-memory or memory64
+     * proposals are to be implemented */
+    bool is_memory64 = false;
+    uint32 memidx = 0;
+
     WASMSharedHeap *shared_heap = module->e ? module->e->shared_heap : NULL;
     WASMSharedHeap *shared_heap = module->e ? module->e->shared_heap : NULL;
     uint8 *shared_heap_base_addr = shared_heap ? shared_heap->base_addr : NULL;
     uint8 *shared_heap_base_addr = shared_heap ? shared_heap->base_addr : NULL;
     uint64 shared_heap_start_off =
     uint64 shared_heap_start_off =