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);
```
This commit is contained in:
Benbuck Nason
2024-06-05 18:20:24 -07:00
committed by GitHub
parent 3fbb7fca25
commit 8239dd4aa7
18 changed files with 184 additions and 138 deletions

View File

@ -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;

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -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