Implement a first version of shared heap feature (#3789)

ps. https://github.com/bytecodealliance/wasm-micro-runtime/issues/3546
This commit is contained in:
WenLY1
2024-09-14 10:51:42 +08:00
committed by GitHub
parent 27b69176c1
commit 92852f3719
15 changed files with 826 additions and 22 deletions

View File

@ -48,6 +48,31 @@ typedef float64 CellType_F64;
#if WASM_ENABLE_MEMORY64 == 0
#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
|| WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
|| WASM_ENABLE_BULK_MEMORY != 0
#define GOTO_OUT_OF_BOUNDS goto out_of_bounds;
#else
#define GOTO_OUT_OF_BOUNDS
#endif
#if WASM_ENABLE_SHARED_HEAP != 0
#define CHECK_MEMORY_SHARED_HEAP_OVERFLOW(bytes) \
do { \
if (offset1 + bytes >= UINT32_MAX - module->shared_heap->size \
&& offset1 + bytes <= UINT32_MAX) { \
uint64 heap_start = UINT32_MAX - module->shared_heap->size; \
uint64 heap_offset = (uint64)offset1 - heap_start; \
maddr = module->shared_heap->data + heap_offset; \
} \
else { \
GOTO_OUT_OF_BOUNDS; \
} \
} while (0)
#else
#define CHECK_MEMORY_SHARED_HEAP_OVERFLOW(bytes) GOTO_OUT_OF_BOUNDS
#endif
#if (!defined(OS_ENABLE_HW_BOUND_CHECK) \
|| WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0)
#define CHECK_MEMORY_OVERFLOW(bytes) \
@ -58,7 +83,7 @@ typedef float64 CellType_F64;
be in valid range, no need to check it again. */ \
maddr = memory->memory_data + offset1; \
else \
goto out_of_bounds; \
CHECK_MEMORY_SHARED_HEAP_OVERFLOW(bytes); \
} while (0)
#define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
@ -69,7 +94,7 @@ typedef float64 CellType_F64;
bulk memory operation */ \
maddr = memory->memory_data + offset1; \
else \
goto out_of_bounds; \
CHECK_MEMORY_SHARED_HEAP_OVERFLOW(bytes); \
} while (0)
#else /* else of !defined(OS_ENABLE_HW_BOUND_CHECK) || \
WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 */
@ -82,22 +107,40 @@ typedef float64 CellType_F64;
#define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
do { \
maddr = memory->memory_data + (uint32)(start); \
uint64 offset1 = start; \
} while (0)
#endif /* end of !defined(OS_ENABLE_HW_BOUND_CHECK) || \
WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 */
#else /* else of WASM_ENABLE_MEMORY64 == 0 */
#define CHECK_MEMORY_OVERFLOW(bytes) \
do { \
uint64 offset1 = (uint64)offset + (uint64)addr; \
/* If memory64 is enabled, offset1, offset1 + bytes can overflow */ \
if (disable_bounds_checks \
|| (offset1 >= offset && offset1 + bytes >= offset1 \
&& offset1 + bytes <= get_linear_mem_size())) \
maddr = memory->memory_data + offset1; \
else \
goto out_of_bounds; \
#if WASM_ENABLE_SHARED_HEAP != 0
#define CHECK_MEMORY_SHARED_HEAP_OVERFLOW(bytes) \
do { \
if (offset1 + bytes >= UINT64_MAX - module->shared_heap->size \
&& offset1 + bytes <= UINT64_MAX) { \
uint64 heap_start = UINT64_MAX - module->shared_heap->size; \
uint64 heap_offset = (uint64)offset1 - heap_start; \
maddr = module->shared_heap->data + heap_offset; \
} \
else \
goto out_of_bounds; \
} while (0)
#else
#define CHECK_MEMORY_SHARED_HEAP_OVERFLOW(bytes) goto out_of_bounds;
#endif
#define CHECK_MEMORY_OVERFLOW(bytes) \
do { \
uint64 offset1 = (uint64)offset + (uint64)addr; \
/* If memory64 is enabled, offset1, offset1 + bytes can \
* overflow */ \
if (disable_bounds_checks \
|| (offset1 >= offset && offset1 + bytes >= offset1 \
&& offset1 + bytes <= get_linear_mem_size())) \
maddr = memory->memory_data + offset1; \
else \
CHECK_MEMORY_SHARED_HEAP_OVERFLOW(bytes); \
} while (0)
#define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
do { \
@ -110,7 +153,7 @@ typedef float64 CellType_F64;
bulk memory operation */ \
maddr = memory->memory_data + offset1; \
else \
goto out_of_bounds; \
CHECK_MEMORY_SHARED_HEAP_OVERFLOW(bytes); \
} while (0)
#endif /* end of WASM_ENABLE_MEMORY64 == 0 */

View File

@ -37,6 +37,31 @@ typedef float64 CellType_F64;
#define get_linear_mem_size() GET_LINEAR_MEMORY_SIZE(memory)
#endif
#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
|| WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
|| WASM_ENABLE_BULK_MEMORY != 0
#define GOTO_OUT_OF_BOUNDS goto out_of_bounds;
#else
#define GOTO_OUT_OF_BOUNDS
#endif
#if WASM_ENABLE_SHARED_HEAP != 0
#define CHECK_MEMORY_SHARED_HEAP_OVERFLOW(bytes) \
do { \
if (offset1 + bytes >= UINT32_MAX - module->e->shared_heap->size \
&& offset1 + bytes <= UINT32_MAX) { \
uint64 heap_start = UINT32_MAX - module->e->shared_heap->size; \
uint64 heap_offset = (uint64)offset1 - heap_start; \
maddr = module->e->shared_heap->base_addr + heap_offset; \
} \
else { \
GOTO_OUT_OF_BOUNDS; \
} \
} while (0)
#else
#define CHECK_MEMORY_SHARED_HEAP_OVERFLOW(bytes) GOTO_OUT_OF_BOUNDS
#endif
#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
|| WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
#define CHECK_MEMORY_OVERFLOW(bytes) \
@ -47,7 +72,7 @@ typedef float64 CellType_F64;
be in valid range, no need to check it again. */ \
maddr = memory->memory_data + offset1; \
else \
goto out_of_bounds; \
CHECK_MEMORY_SHARED_HEAP_OVERFLOW(byets); \
} while (0)
#define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
@ -58,7 +83,7 @@ typedef float64 CellType_F64;
bulk memory operation */ \
maddr = memory->memory_data + offset1; \
else \
goto out_of_bounds; \
CHECK_MEMORY_SHARED_HEAP_OVERFLOW(byets); \
} while (0)
#else
#define CHECK_MEMORY_OVERFLOW(bytes) \
@ -70,6 +95,7 @@ typedef float64 CellType_F64;
#define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
do { \
maddr = memory->memory_data + (uint32)(start); \
uint64 offset1 = start; \
} while (0)
#endif /* !defined(OS_ENABLE_HW_BOUND_CHECK) \
|| WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 */

View File

@ -92,6 +92,17 @@ typedef union {
uint32 u32[2];
} MemBound;
#if WASM_ENABLE_SHARED_HEAP != 0
typedef struct WASMSharedHeap {
struct WASMSharedHeap *next;
void *heap_handle;
uint8_t *base_addr;
uint32_t size;
uint64 start_off_mem64;
uint64 start_off_mem32;
} WASMSharedHeap;
#endif
struct WASMMemoryInstance {
/* Module type */
uint32 module_type;
@ -353,6 +364,10 @@ typedef struct WASMModuleInstanceExtra {
uint32 max_aux_stack_used;
#endif
#if WASM_ENABLE_SHARED_HEAP != 0
WASMSharedHeap *shared_heap;
#endif
#if WASM_ENABLE_DEBUG_INTERP != 0 \
|| (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
&& WASM_ENABLE_LAZY_JIT != 0)