From 8239dd4aa7bdcf4ca5047d70bd1a6a4635bef5ce Mon Sep 17 00:00:00 2001 From: Benbuck Nason Date: Wed, 5 Jun 2024 18:20:24 -0700 Subject: [PATCH] Add wasm_export.h APIs to expose memory type (#3496) Support to get `wasm_memory_type_t memory_type` from API `wasm_runtime_get_import_type` and `wasm_runtime_get_export_type`, and then get shared flag, initial page cout, maximum page count from the memory_type: ```C bool wasm_memory_type_get_shared(const wasm_memory_type_t memory_type); uint32_t wasm_memory_type_get_init_page_count(const wasm_memory_type_t memory_type); uint32_t wasm_memory_type_get_max_page_count(const wasm_memory_type_t memory_type); ``` --- core/iwasm/aot/aot_loader.c | 14 ++--- core/iwasm/aot/aot_runtime.c | 11 ++-- core/iwasm/common/wasm_c_api.c | 16 +++--- core/iwasm/common/wasm_runtime_common.c | 67 +++++++++++++++------- core/iwasm/compilation/aot.c | 24 ++++---- core/iwasm/compilation/aot.h | 18 +----- core/iwasm/compilation/aot_compiler.h | 3 +- core/iwasm/compilation/aot_emit_aot_file.c | 8 +-- core/iwasm/compilation/aot_emit_control.c | 2 +- core/iwasm/compilation/aot_emit_memory.c | 11 ++-- core/iwasm/compilation/aot_llvm.c | 2 +- core/iwasm/fast-jit/jit_frontend.c | 2 +- core/iwasm/include/wasm_export.h | 36 ++++++++++++ core/iwasm/interpreter/wasm.h | 7 +-- core/iwasm/interpreter/wasm_loader.c | 40 +++++++------ core/iwasm/interpreter/wasm_mini_loader.c | 38 ++++++------ core/iwasm/interpreter/wasm_runtime.c | 11 ++-- test-tools/aot-analyzer/src/main.cc | 12 ++-- 18 files changed, 184 insertions(+), 138 deletions(-) diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index 33e74acc8..8d705248c 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -1043,16 +1043,16 @@ load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, } for (i = 0; i < module->memory_count; i++) { - read_uint32(buf, buf_end, module->memories[i].memory_flags); + read_uint32(buf, buf_end, module->memories[i].flags); - if (!wasm_memory_check_flags(module->memories[i].memory_flags, - error_buf, error_buf_size, true)) { + if (!wasm_memory_check_flags(module->memories[i].flags, error_buf, + error_buf_size, true)) { return false; } read_uint32(buf, buf_end, module->memories[i].num_bytes_per_page); - read_uint32(buf, buf_end, module->memories[i].mem_init_page_count); - read_uint32(buf, buf_end, module->memories[i].mem_max_page_count); + read_uint32(buf, buf_end, module->memories[i].init_page_count); + read_uint32(buf, buf_end, module->memories[i].max_page_count); } read_uint32(buf, buf_end, module->mem_init_data_count); @@ -3637,9 +3637,9 @@ has_module_memory64(AOTModule *module) /* TODO: multi-memories for now assuming the memory idx type is consistent * across multi-memories */ if (module->import_memory_count > 0) - return !!(module->import_memories[0].memory_flags & MEMORY64_FLAG); + return !!(module->import_memories[0].mem_type.flags & MEMORY64_FLAG); else if (module->memory_count > 0) - return !!(module->memories[0].memory_flags & MEMORY64_FLAG); + return !!(module->memories[0].flags & MEMORY64_FLAG); return false; } diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index e01b8d207..ea068378a 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -789,10 +789,9 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent, { void *heap_handle; uint32 num_bytes_per_page = memory->num_bytes_per_page; - uint32 init_page_count = memory->mem_init_page_count; - uint32 max_page_count = - wasm_runtime_get_max_mem(max_memory_pages, memory->mem_init_page_count, - memory->mem_max_page_count); + uint32 init_page_count = memory->init_page_count; + uint32 max_page_count = wasm_runtime_get_max_mem( + max_memory_pages, memory->init_page_count, memory->max_page_count); uint32 default_max_pages; uint32 inc_page_count, global_idx; uint32 bytes_of_last_page, bytes_to_page_end; @@ -800,11 +799,11 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent, heap_offset = (uint64)num_bytes_per_page * init_page_count; uint64 memory_data_size, max_memory_data_size; uint8 *p = NULL, *global_addr; - bool is_memory64 = memory->memory_flags & MEMORY64_FLAG; + bool is_memory64 = memory->flags & MEMORY64_FLAG; bool is_shared_memory = false; #if WASM_ENABLE_SHARED_MEMORY != 0 - is_shared_memory = memory->memory_flags & SHARED_MEMORY_FLAG ? true : false; + is_shared_memory = memory->flags & SHARED_MEMORY_FLAG ? true : false; /* Shared memory */ if (is_shared_memory && parent != NULL) { AOTMemoryInstance *shared_memory_instance; diff --git a/core/iwasm/common/wasm_c_api.c b/core/iwasm/common/wasm_c_api.c index 190a4beff..2a8f26681 100644 --- a/core/iwasm/common/wasm_c_api.c +++ b/core/iwasm/common/wasm_c_api.c @@ -2580,8 +2580,8 @@ wasm_module_imports(const wasm_module_t *module, own wasm_importtype_vec_t *out) + (i - import_func_count - import_global_count); module_name_rt = import->u.names.module_name; field_name_rt = import->u.names.field_name; - min_page = import->u.memory.init_page_count; - max_page = import->u.memory.max_page_count; + min_page = import->u.memory.mem_type.init_page_count; + max_page = import->u.memory.mem_type.max_page_count; } #endif @@ -2592,8 +2592,8 @@ wasm_module_imports(const wasm_module_t *module, own wasm_importtype_vec_t *out) + (i - import_func_count - import_global_count); module_name_rt = import->module_name; field_name_rt = import->memory_name; - min_page = import->mem_init_page_count; - max_page = import->mem_max_page_count; + min_page = import->mem_type.init_page_count; + max_page = import->mem_type.max_page_count; } #endif @@ -4308,12 +4308,12 @@ wasm_memory_new_internal(wasm_store_t *store, uint16 memory_idx_rt, AOTModule *module_aot = (AOTModule *)inst_aot->module; if (memory_idx_rt < module_aot->import_memory_count) { - min_pages = module_aot->import_memories->mem_init_page_count; - max_pages = module_aot->import_memories->mem_max_page_count; + min_pages = module_aot->import_memories->mem_type.init_page_count; + max_pages = module_aot->import_memories->mem_type.max_page_count; } else { - min_pages = module_aot->memories->mem_init_page_count; - max_pages = module_aot->memories->mem_max_page_count; + min_pages = module_aot->memories->init_page_count; + max_pages = module_aot->memories->max_page_count; } init_flag = true; } diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 794897cbc..f18cd784a 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -3873,7 +3873,8 @@ wasm_runtime_get_import_type(WASMModuleCommon *const module, int32 import_index, import_type->kind = WASM_IMPORT_EXPORT_KIND_FUNC; import_type->linked = aot_import_func->func_ptr_linked ? true : false; - import_type->u.func_type = aot_import_func->func_type; + import_type->u.func_type = + (WASMFuncType *)aot_import_func->func_type; return; } @@ -3909,6 +3910,8 @@ wasm_runtime_get_import_type(WASMModuleCommon *const module, int32 import_index, import_type->name = aot_import_memory->memory_name; import_type->kind = WASM_IMPORT_EXPORT_KIND_MEMORY; import_type->linked = false; + import_type->u.memory_type = + (WASMMemoryType *)&aot_import_memory->mem_type; return; } @@ -3933,7 +3936,8 @@ wasm_runtime_get_import_type(WASMModuleCommon *const module, int32 import_index, switch (import_type->kind) { case WASM_IMPORT_EXPORT_KIND_FUNC: import_type->linked = wasm_import->u.function.func_ptr_linked; - import_type->u.func_type = wasm_import->u.function.func_type; + import_type->u.func_type = + (WASMFuncType *)wasm_import->u.function.func_type; break; case WASM_IMPORT_EXPORT_KIND_GLOBAL: import_type->linked = wasm_import->u.global.is_linked; @@ -3941,12 +3945,12 @@ wasm_runtime_get_import_type(WASMModuleCommon *const module, int32 import_index, (WASMGlobalType *)&wasm_import->u.global.type; break; case WASM_IMPORT_EXPORT_KIND_TABLE: - /* not supported */ - import_type->linked = false; + import_type->linked = false; /* not supported */ break; case WASM_IMPORT_EXPORT_KIND_MEMORY: - /* not supported */ - import_type->linked = false; + import_type->linked = false; /* not supported */ + import_type->u.memory_type = + (WASMMemoryType *)&wasm_import->u.memory.mem_type; break; default: bh_assert(0); @@ -4026,12 +4030,11 @@ wasm_runtime_get_export_type(WASMModuleCommon *const module, int32 export_index, .type; break; case WASM_IMPORT_EXPORT_KIND_TABLE: - /* not supported */ - // export_type->linked = false; break; case WASM_IMPORT_EXPORT_KIND_MEMORY: - /* not supported */ - // export_type->linked = false; + export_type->u.memory_type = + &aot_module->memories[aot_export->index + - aot_module->import_memory_count]; break; default: bh_assert(0); @@ -4068,13 +4071,13 @@ wasm_runtime_get_export_type(WASMModuleCommon *const module, int32 export_index, .type; break; case WASM_IMPORT_EXPORT_KIND_TABLE: - /* not supported */ - // export_type->linked = false; break; case WASM_IMPORT_EXPORT_KIND_MEMORY: - /* not supported */ - // export_type->linked = false; + export_type->u.memory_type = + &wasm_module->memories[wasm_export->index + - wasm_module->import_memory_count]; break; + default: bh_assert(0); break; } @@ -4185,6 +4188,30 @@ wasm_global_type_get_mutable(WASMGlobalType *const global_type) return global_type->is_mutable; } +bool +wasm_memory_type_get_shared(WASMMemoryType *const memory_type) +{ + bh_assert(memory_type); + + return (memory_type->flags & SHARED_MEMORY_FLAG) ? true : false; +} + +uint32 +wasm_memory_type_get_init_page_count(WASMMemoryType *const memory_type) +{ + bh_assert(memory_type); + + return memory_type->init_page_count; +} + +uint32 +wasm_memory_type_get_max_page_count(WASMMemoryType *const memory_type) +{ + bh_assert(memory_type); + + return memory_type->max_page_count; +} + bool wasm_runtime_register_natives(const char *module_name, NativeSymbol *native_symbols, @@ -6519,8 +6546,8 @@ wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm, if (export->index < module->import_memory_count) { WASMMemoryImport *import_memory = &((module->import_memories + export->index)->u.memory); - *out_min_page = import_memory->init_page_count; - *out_max_page = import_memory->max_page_count; + *out_min_page = import_memory->mem_type.init_page_count; + *out_max_page = import_memory->mem_type.max_page_count; } else { WASMMemory *memory = @@ -6540,14 +6567,14 @@ wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm, if (export->index < module->import_memory_count) { AOTImportMemory *import_memory = module->import_memories + export->index; - *out_min_page = import_memory->mem_init_page_count; - *out_max_page = import_memory->mem_max_page_count; + *out_min_page = import_memory->mem_type.init_page_count; + *out_max_page = import_memory->mem_type.max_page_count; } else { AOTMemory *memory = module->memories + (export->index - module->import_memory_count); - *out_min_page = memory->mem_init_page_count; - *out_max_page = memory->mem_max_page_count; + *out_min_page = memory->init_page_count; + *out_max_page = memory->max_page_count; } return true; } diff --git a/core/iwasm/compilation/aot.c b/core/iwasm/compilation/aot.c index 0d1b9028c..33b8c0f34 100644 --- a/core/iwasm/compilation/aot.c +++ b/core/iwasm/compilation/aot.c @@ -558,28 +558,24 @@ aot_create_comp_data(WASMModule *module, const char *target_arch, /* Set memory page count */ for (i = 0; i < module->import_memory_count + module->memory_count; i++) { if (i < module->import_memory_count) { - comp_data->memories[i].memory_flags = - module->import_memories[i].u.memory.flags; + comp_data->memories[i].flags = + module->import_memories[i].u.memory.mem_type.flags; comp_data->memories[i].num_bytes_per_page = - module->import_memories[i].u.memory.num_bytes_per_page; - comp_data->memories[i].mem_init_page_count = - module->import_memories[i].u.memory.init_page_count; - comp_data->memories[i].mem_max_page_count = - module->import_memories[i].u.memory.max_page_count; - comp_data->memories[i].num_bytes_per_page = - module->import_memories[i].u.memory.num_bytes_per_page; + module->import_memories[i].u.memory.mem_type.num_bytes_per_page; + comp_data->memories[i].init_page_count = + module->import_memories[i].u.memory.mem_type.init_page_count; + comp_data->memories[i].max_page_count = + module->import_memories[i].u.memory.mem_type.max_page_count; } else { j = i - module->import_memory_count; - comp_data->memories[i].memory_flags = module->memories[j].flags; + comp_data->memories[i].flags = module->memories[j].flags; comp_data->memories[i].num_bytes_per_page = module->memories[j].num_bytes_per_page; - comp_data->memories[i].mem_init_page_count = + comp_data->memories[i].init_page_count = module->memories[j].init_page_count; - comp_data->memories[i].mem_max_page_count = + comp_data->memories[i].max_page_count = module->memories[j].max_page_count; - comp_data->memories[i].num_bytes_per_page = - module->memories[j].num_bytes_per_page; } } diff --git a/core/iwasm/compilation/aot.h b/core/iwasm/compilation/aot.h index ae04dfbc7..94e34113b 100644 --- a/core/iwasm/compilation/aot.h +++ b/core/iwasm/compilation/aot.h @@ -46,6 +46,8 @@ typedef WASMStructType AOTStructType; typedef WASMArrayType AOTArrayType; #endif typedef WASMExport AOTExport; +typedef WASMMemory AOTMemory; +typedef WASMMemoryType AOTMemoryType; #if WASM_ENABLE_DEBUG_AOT != 0 typedef void *dwarf_extractor_handle_t; @@ -81,23 +83,9 @@ typedef enum AOTFloatCond { typedef struct AOTImportMemory { char *module_name; char *memory_name; - uint32 memory_flags; - uint32 num_bytes_per_page; - uint32 mem_init_page_count; - uint32 mem_max_page_count; + AOTMemoryType mem_type; } AOTImportMemory; -/** - * Memory information - */ -typedef struct AOTMemory { - /* memory info */ - uint32 memory_flags; - uint32 num_bytes_per_page; - uint32 mem_init_page_count; - uint32 mem_max_page_count; -} AOTMemory; - /** * A segment of memory init data */ diff --git a/core/iwasm/compilation/aot_compiler.h b/core/iwasm/compilation/aot_compiler.h index f9016ac6b..06dc77212 100644 --- a/core/iwasm/compilation/aot_compiler.h +++ b/core/iwasm/compilation/aot_compiler.h @@ -520,8 +520,7 @@ set_local_gc_ref(AOTCompFrame *frame, int n, LLVMValueRef value, uint8 ref_type) } while (0) #if WASM_ENABLE_MEMORY64 != 0 -#define IS_MEMORY64 \ - (comp_ctx->comp_data->memories[0].memory_flags & MEMORY64_FLAG) +#define IS_MEMORY64 (comp_ctx->comp_data->memories[0].flags & MEMORY64_FLAG) #define MEMORY64_COND_VALUE(VAL_IF_ENABLED, VAL_IF_DISABLED) \ (IS_MEMORY64 ? VAL_IF_ENABLED : VAL_IF_DISABLED) #else diff --git a/core/iwasm/compilation/aot_emit_aot_file.c b/core/iwasm/compilation/aot_emit_aot_file.c index 70fd2efac..1c5906a8e 100644 --- a/core/iwasm/compilation/aot_emit_aot_file.c +++ b/core/iwasm/compilation/aot_emit_aot_file.c @@ -189,7 +189,7 @@ get_import_memory_size(AOTCompData *comp_data) static uint32 get_memory_size(AOTCompData *comp_data) { - /* memory_count + count * (memory_flags + num_bytes_per_page + + /* memory_count + count * (flags + num_bytes_per_page + init_page_count + max_page_count) */ return (uint32)(sizeof(uint32) + comp_data->memory_count * sizeof(uint32) * 4); @@ -1762,10 +1762,10 @@ aot_emit_mem_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset, EMIT_U32(comp_data->memory_count); /* Emit memory items */ for (i = 0; i < comp_data->memory_count; i++) { - EMIT_U32(comp_data->memories[i].memory_flags); + EMIT_U32(comp_data->memories[i].flags); EMIT_U32(comp_data->memories[i].num_bytes_per_page); - EMIT_U32(comp_data->memories[i].mem_init_page_count); - EMIT_U32(comp_data->memories[i].mem_max_page_count); + EMIT_U32(comp_data->memories[i].init_page_count); + EMIT_U32(comp_data->memories[i].max_page_count); } /* Emit mem init data count */ diff --git a/core/iwasm/compilation/aot_emit_control.c b/core/iwasm/compilation/aot_emit_control.c index 24511ffd0..7d73d8d90 100644 --- a/core/iwasm/compilation/aot_emit_control.c +++ b/core/iwasm/compilation/aot_emit_control.c @@ -853,7 +853,7 @@ check_suspend_flags(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, LLVMBasicBlockRef terminate_block, non_terminate_block; AOTFuncType *aot_func_type = func_ctx->aot_func->func_type; bool is_shared_memory = - comp_ctx->comp_data->memories[0].memory_flags & 0x02 ? true : false; + comp_ctx->comp_data->memories[0].flags & 0x02 ? true : false; /* Only need to check the suspend flags when memory is shared since shared memory must be enabled for multi-threading */ diff --git a/core/iwasm/compilation/aot_emit_memory.c b/core/iwasm/compilation/aot_emit_memory.c index a506f2a78..506467449 100644 --- a/core/iwasm/compilation/aot_emit_memory.c +++ b/core/iwasm/compilation/aot_emit_memory.c @@ -109,7 +109,7 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, bool is_target_64bit, is_local_of_aot_value = false; #if WASM_ENABLE_SHARED_MEMORY != 0 bool is_shared_memory = - comp_ctx->comp_data->memories[0].memory_flags & SHARED_MEMORY_FLAG; + comp_ctx->comp_data->memories[0].flags & SHARED_MEMORY_FLAG; #endif is_target_64bit = (comp_ctx->pointer_size == sizeof(uint64)) ? true : false; @@ -177,7 +177,7 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, uint32 num_bytes_per_page = comp_ctx->comp_data->memories[0].num_bytes_per_page; uint32 init_page_count = - comp_ctx->comp_data->memories[0].mem_init_page_count; + comp_ctx->comp_data->memories[0].init_page_count; uint64 mem_data_size = (uint64)num_bytes_per_page * init_page_count; if (mem_offset + bytes <= mem_data_size) { @@ -224,7 +224,7 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, && aot_checked_addr_list_find(func_ctx, local_idx_of_aot_value, offset, bytes))) { uint32 init_page_count = - comp_ctx->comp_data->memories[0].mem_init_page_count; + comp_ctx->comp_data->memories[0].init_page_count; if (init_page_count == 0) { LLVMValueRef mem_size; @@ -932,8 +932,7 @@ check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, /* Get memory base address and memory data size */ #if WASM_ENABLE_SHARED_MEMORY != 0 - bool is_shared_memory = - comp_ctx->comp_data->memories[0].memory_flags & 0x02; + bool is_shared_memory = comp_ctx->comp_data->memories[0].flags & 0x02; if (func_ctx->mem_space_unchanged || is_shared_memory) { #else @@ -961,7 +960,7 @@ check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, uint32 num_bytes_per_page = comp_ctx->comp_data->memories[0].num_bytes_per_page; uint32 init_page_count = - comp_ctx->comp_data->memories[0].mem_init_page_count; + comp_ctx->comp_data->memories[0].init_page_count; uint64 mem_data_size = (uint64)num_bytes_per_page * init_page_count; if (mem_data_size > 0 && mem_offset + mem_len <= mem_data_size) { /* inside memory space */ diff --git a/core/iwasm/compilation/aot_llvm.c b/core/iwasm/compilation/aot_llvm.c index bda8ee3b7..df07c3ca6 100644 --- a/core/iwasm/compilation/aot_llvm.c +++ b/core/iwasm/compilation/aot_llvm.c @@ -1219,7 +1219,7 @@ create_memory_info(const AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, /* Load memory base address */ #if WASM_ENABLE_SHARED_MEMORY != 0 is_shared_memory = - comp_ctx->comp_data->memories[0].memory_flags & 0x02 ? true : false; + comp_ctx->comp_data->memories[0].flags & 0x02 ? true : false; if (is_shared_memory) { LLVMValueRef shared_mem_addr; offset = I32_CONST(offsetof(AOTModuleInstance, memories)); diff --git a/core/iwasm/fast-jit/jit_frontend.c b/core/iwasm/fast-jit/jit_frontend.c index 17c33230c..a8e87c92b 100644 --- a/core/iwasm/fast-jit/jit_frontend.c +++ b/core/iwasm/fast-jit/jit_frontend.c @@ -234,7 +234,7 @@ is_shared_memory(WASMModule *module, uint32 mem_idx) if (mem_idx < module->import_memory_count) { memory_import = &(module->import_memories[mem_idx].u.memory); - is_shared = memory_import->flags & 0x02 ? true : false; + is_shared = memory_import->mem_type.flags & 0x02 ? true : false; } else { memory = &module->memories[mem_idx - module->import_memory_count]; diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 2dd1eb905..85fd522d7 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -78,6 +78,10 @@ typedef struct WASMFuncType *wasm_func_type_t; struct WASMGlobalType; typedef struct WASMGlobalType *wasm_global_type_t; +struct WASMMemory; +typedef struct WASMMemory WASMMemoryType; +typedef WASMMemoryType *wasm_memory_type_t; + typedef struct wasm_import_t { const char *module_name; const char *name; @@ -86,6 +90,7 @@ typedef struct wasm_import_t { union { wasm_func_type_t func_type; wasm_global_type_t global_type; + wasm_memory_type_t memory_type; } u; } wasm_import_t; @@ -95,6 +100,7 @@ typedef struct wasm_export_t { union { wasm_func_type_t func_type; wasm_global_type_t global_type; + wasm_memory_type_t memory_type; } u; } wasm_export_t; @@ -1350,6 +1356,36 @@ wasm_global_type_get_valkind(const wasm_global_type_t global_type); WASM_RUNTIME_API_EXTERN bool wasm_global_type_get_mutable(const wasm_global_type_t global_type); +/** + * Get the shared setting for a memory type + * + * @param memory_type the memory type + * + * @return true if shared, false otherwise + */ +WASM_RUNTIME_API_EXTERN bool +wasm_memory_type_get_shared(const wasm_memory_type_t memory_type); + +/** + * Get the initial page count for a memory type + * + * @param memory_type the memory type + * + * @return the initial memory page count + */ +WASM_RUNTIME_API_EXTERN uint32_t +wasm_memory_type_get_init_page_count(const wasm_memory_type_t memory_type); + +/** + * Get the maximum page count for a memory type + * + * @param memory_type the memory type + * + * @return the maximum memory page count + */ +WASM_RUNTIME_API_EXTERN uint32_t +wasm_memory_type_get_max_page_count(const wasm_memory_type_t memory_type); + /** * Register native functions with same module name * diff --git a/core/iwasm/interpreter/wasm.h b/core/iwasm/interpreter/wasm.h index 1f6ca6deb..4bd650529 100644 --- a/core/iwasm/interpreter/wasm.h +++ b/core/iwasm/interpreter/wasm.h @@ -512,7 +512,7 @@ typedef struct WASMMemory { uint32 num_bytes_per_page; uint32 init_page_count; uint32 max_page_count; -} WASMMemory; +} WASMMemory, WASMMemoryType; typedef struct WASMTableImport { char *module_name; @@ -536,10 +536,7 @@ typedef struct WASMTableImport { typedef struct WASMMemoryImport { char *module_name; char *field_name; - uint32 flags; - uint32 num_bytes_per_page; - uint32 init_page_count; - uint32 max_page_count; + WASMMemoryType mem_type; #if WASM_ENABLE_MULTI_MODULE != 0 WASMModule *import_module; WASMMemory *import_memory_linked; diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index d61907e43..bf117dd82 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -44,7 +44,8 @@ has_module_memory64(WASMModule *module) /* TODO: multi-memories for now assuming the memory idx type is consistent * across multi-memories */ if (module->import_memory_count > 0) - return !!(module->import_memories[0].u.memory.flags & MEMORY64_FLAG); + return !!(module->import_memories[0].u.memory.mem_type.flags + & MEMORY64_FLAG); else if (module->memory_count > 0) return !!(module->memories[0].flags & MEMORY64_FLAG); @@ -2935,10 +2936,10 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end, } /* now we believe all declaration are ok */ - memory->flags = mem_flag; - memory->init_page_count = declare_init_page_count; - memory->max_page_count = declare_max_page_count; - memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE; + memory->mem_type.flags = mem_flag; + memory->mem_type.init_page_count = declare_init_page_count; + memory->mem_type.max_page_count = declare_max_page_count; + memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE; *p_buf = p; @@ -4805,8 +4806,8 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end, /* This memory_flag is from memory instead of data segment */ uint8 memory_flag; if (module->import_memory_count > 0) { - memory_flag = - module->import_memories[mem_index].u.memory.flags; + memory_flag = module->import_memories[mem_index] + .u.memory.mem_type.flags; } else { memory_flag = @@ -6153,13 +6154,14 @@ load_from_sections(WASMModule *module, WASMSection *sections, if (shrunk_memory_size <= UINT32_MAX) { if (module->import_memory_count) { memory_import = &module->import_memories[0].u.memory; - init_memory_size = (uint64)memory_import->num_bytes_per_page - * memory_import->init_page_count; + init_memory_size = + (uint64)memory_import->mem_type.num_bytes_per_page + * memory_import->mem_type.init_page_count; if (shrunk_memory_size <= init_memory_size) { /* Reset memory info to decrease memory usage */ - memory_import->num_bytes_per_page = + memory_import->mem_type.num_bytes_per_page = (uint32)shrunk_memory_size; - memory_import->init_page_count = 1; + memory_import->mem_type.init_page_count = 1; LOG_VERBOSE("Shrink import memory size to %" PRIu64, shrunk_memory_size); } @@ -6185,16 +6187,16 @@ load_from_sections(WASMModule *module, WASMSection *sections, memory_import = &module->import_memories[0].u.memory; /* Only resize the memory to one big page if num_bytes_per_page is * in valid range of uint32 */ - if (memory_import->init_page_count < DEFAULT_MAX_PAGES) { - memory_import->num_bytes_per_page *= - memory_import->init_page_count; + if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) { + memory_import->mem_type.num_bytes_per_page *= + memory_import->mem_type.init_page_count; - if (memory_import->init_page_count > 0) - memory_import->init_page_count = - memory_import->max_page_count = 1; + if (memory_import->mem_type.init_page_count > 0) + memory_import->mem_type.init_page_count = + memory_import->mem_type.max_page_count = 1; else - memory_import->init_page_count = - memory_import->max_page_count = 0; + memory_import->mem_type.init_page_count = + memory_import->mem_type.max_page_count = 0; } } if (module->memory_count) { diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index 96d7b5ed0..75ac876cf 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -33,7 +33,7 @@ has_module_memory64(WASMModule *module) /* TODO: multi-memories for now assuming the memory idx type is consistent * across multi-memories */ if (module->import_memory_count > 0) - return !!(module->import_memories[0].u.memory.flags & MEMORY64_FLAG); + return !!(module->import_memories[0].u.mem_type.flags & MEMORY64_FLAG); else if (module->memory_count > 0) return !!(module->memories[0].flags & MEMORY64_FLAG); @@ -761,10 +761,10 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end, } /* now we believe all declaration are ok */ - memory->flags = mem_flag; - memory->init_page_count = declare_init_page_count; - memory->max_page_count = declare_max_page_count; - memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE; + memory->mem_type.flags = mem_flag; + memory->mem_type.init_page_count = declare_init_page_count; + memory->mem_type.max_page_count = declare_max_page_count; + memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE; *p_buf = p; return true; @@ -1812,7 +1812,7 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end, uint8 memory_flag; if (module->import_memory_count > 0) { memory_flag = - module->import_memories[mem_index].u.memory.flags; + module->import_memories[mem_index].u.mem_type.flags; } else { memory_flag = @@ -2922,12 +2922,14 @@ load_from_sections(WASMModule *module, WASMSection *sections, if (shrunk_memory_size <= UINT32_MAX) { if (module->import_memory_count) { memory_import = &module->import_memories[0].u.memory; - init_memory_size = (uint64)memory_import->num_bytes_per_page - * memory_import->init_page_count; + init_memory_size = + (uint64)memory_import->mem_type.num_bytes_per_page + * memory_import->mem_type.init_page_count; if (shrunk_memory_size <= init_memory_size) { /* Reset memory info to decrease memory usage */ - memory_import->num_bytes_per_page = shrunk_memory_size; - memory_import->init_page_count = 1; + memory_import->mem_type.num_bytes_per_page = + shrunk_memory_size; + memory_import->mem_type.init_page_count = 1; LOG_VERBOSE("Shrink import memory size to %" PRIu64, shrunk_memory_size); } @@ -2950,15 +2952,15 @@ load_from_sections(WASMModule *module, WASMSection *sections, if (module->import_memory_count) { memory_import = &module->import_memories[0].u.memory; - if (memory_import->init_page_count < DEFAULT_MAX_PAGES) { - memory_import->num_bytes_per_page *= - memory_import->init_page_count; - if (memory_import->init_page_count > 0) - memory_import->init_page_count = - memory_import->max_page_count = 1; + if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) { + memory_import->mem_type.num_bytes_per_page *= + memory_import->mem_type.init_page_count; + if (memory_import->mem_type.init_page_count > 0) + memory_import->mem_type.init_page_count = + memory_import->mem_type.max_page_count = 1; else - memory_import->init_page_count = - memory_import->max_page_count = 0; + memory_import->mem_type.init_page_count = + memory_import->mem_type.max_page_count = 0; } } diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 635fdea6d..19455983e 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -396,12 +396,13 @@ memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst, /* instantiate memories from import section */ import = module->import_memories; for (i = 0; i < module->import_memory_count; i++, import++, memory++) { - uint32 num_bytes_per_page = import->u.memory.num_bytes_per_page; - uint32 init_page_count = import->u.memory.init_page_count; + uint32 num_bytes_per_page = + import->u.memory.mem_type.num_bytes_per_page; + uint32 init_page_count = import->u.memory.mem_type.init_page_count; uint32 max_page_count = wasm_runtime_get_max_mem( - max_memory_pages, import->u.memory.init_page_count, - import->u.memory.max_page_count); - uint32 flags = import->u.memory.flags; + max_memory_pages, import->u.memory.mem_type.init_page_count, + import->u.memory.mem_type.max_page_count); + uint32 flags = import->u.memory.mem_type.flags; uint32 actual_heap_size = heap_size; #if WASM_ENABLE_MULTI_MODULE != 0 diff --git a/test-tools/aot-analyzer/src/main.cc b/test-tools/aot-analyzer/src/main.cc index fa9582cd7..7b71e3005 100644 --- a/test-tools/aot-analyzer/src/main.cc +++ b/test-tools/aot-analyzer/src/main.cc @@ -272,9 +272,9 @@ DumpDetails(AoTFile *aot) AOTImportMemory memory = import_memories[index]; printf(" -[%u] num_bytes_per_page:%5u init_page_count:%5u " "max_page_count:%5u module_name: %s memory_name: %s\n", - index, memory.num_bytes_per_page, memory.mem_init_page_count, - memory.mem_max_page_count, memory.module_name, - memory.memory_name); + index, memory.memory.num_bytes_per_page, + memory.memory.init_page_count, memory.memory.max_page_count, + memory.module_name, memory.memory_name); } printf("\n"); @@ -365,10 +365,10 @@ DumpDetails(AoTFile *aot) for (uint32_t index = 0; index < memory_count; index++) { AOTMemory memory = memories[index]; - printf(" -[%u] memory_flags:%5u bytes_per_page:%5u " + printf(" -[%u] flags:%5u bytes_per_page:%5u " "init_page_count:%5u max_page_count:%5u\n", - index, memory.memory_flags, memory.num_bytes_per_page, - memory.mem_init_page_count, memory.mem_max_page_count); + index, memory.flags, memory.num_bytes_per_page, + memory.init_page_count, memory.max_page_count); } printf("\n\n");