Skip to content

Commit

Permalink
Add memory import support
Browse files Browse the repository at this point in the history
  • Loading branch information
bnason-nf committed Sep 17, 2024
1 parent d64a3ab commit 3698e7c
Show file tree
Hide file tree
Showing 16 changed files with 247 additions and 102 deletions.
26 changes: 14 additions & 12 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ memories_deinstantiate(AOTModuleInstance *module_inst)
AOTMemoryInstance *memory_inst;

for (i = 0; i < module_inst->memory_count; i++) {
memory_inst = module_inst->memories[i];
memory_inst = module_inst->memories[i].memory;
if (memory_inst) {
#if WASM_ENABLE_SHARED_MEMORY != 0
if (shared_memory_is_shared(memory_inst)) {
Expand Down Expand Up @@ -872,7 +872,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
AOTMemoryInstance *shared_memory_instance;
bh_assert(memory_idx == 0);
bh_assert(parent->memory_count > memory_idx);
shared_memory_instance = parent->memories[memory_idx];
shared_memory_instance = parent->memories[memory_idx].memory;
shared_memory_inc_reference(shared_memory_instance);
return shared_memory_instance;
}
Expand Down Expand Up @@ -1071,15 +1071,15 @@ aot_lookup_memory(AOTModuleInstance *module_inst, char const *name)
(void)module_inst->export_memories;
if (!module_inst->memories)
return NULL;
return module_inst->memories[0];
return module_inst->memories[0].memory;
#endif
}

AOTMemoryInstance *
aot_get_default_memory(AOTModuleInstance *module_inst)
{
if (module_inst->memories)
return module_inst->memories[0];
return module_inst->memories[0].memory;
else
return NULL;
}
Expand All @@ -1089,7 +1089,7 @@ aot_get_memory_with_index(AOTModuleInstance *module_inst, uint32 index)
{
if ((index >= module_inst->memory_count) || !module_inst->memories)
return NULL;
return module_inst->memories[index];
return module_inst->memories[index].memory;
}

static bool
Expand All @@ -1106,7 +1106,7 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
mem_offset_t base_offset;

module_inst->memory_count = memory_count;
total_size = sizeof(AOTMemoryInstance *) * (uint64)memory_count;
total_size = sizeof(AOTMemoryWrapper *) * (uint64)memory_count;
if (!(module_inst->memories =
runtime_malloc(total_size, error_buf, error_buf_size))) {
return false;
Expand All @@ -1121,7 +1121,7 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
return false;
}

module_inst->memories[i] = memory_inst;
module_inst->memories[i].memory = memory_inst;
}

/* Get default memory instance */
Expand Down Expand Up @@ -1434,7 +1434,7 @@ export_memories_instantiate(const AOTModule *module,
for (i = 0; i < module->export_count; i++, export ++)
if (export->kind == EXPORT_KIND_MEMORY) {
export_memory->name = export->name;
export_memory->memory = module_inst->memories[export->index];
export_memory->memory = module_inst->memories[export->index].memory;
export_memory++;
}

Expand Down Expand Up @@ -1684,7 +1684,9 @@ check_linked_symbol(AOTModule *module, char *error_buf, uint32 error_buf_size)
AOTModuleInstance *
aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
WASMExecEnv *exec_env_main, uint32 stack_size, uint32 heap_size,
uint32 max_memory_pages, char *error_buf, uint32 error_buf_size)
uint32 max_memory_pages, uint32 import_count,
const WASMImportInst *imports, char *error_buf,
uint32 error_buf_size)
{
AOTModuleInstance *module_inst;
#if WASM_ENABLE_BULK_MEMORY != 0 || WASM_ENABLE_REF_TYPES != 0
Expand Down Expand Up @@ -2591,7 +2593,7 @@ execute_malloc_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
bool ret;

#if WASM_ENABLE_MEMORY64 != 0
bool is_memory64 = module_inst->memories[0]->is_memory64;
bool is_memory64 = module_inst->memories[0].memory->is_memory64;
if (is_memory64) {
argc = 2;
PUT_I64_TO_ADDR(&argv.u64, size);
Expand Down Expand Up @@ -2694,7 +2696,7 @@ execute_free_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
bool ret;

#if WASM_ENABLE_MEMORY64 != 0
if (module_inst->memories[0]->is_memory64) {
if (module_inst->memories[0].memory->is_memory64) {
PUT_I64_TO_ADDR(&argv.u64, offset);
argc = 2;
}
Expand Down Expand Up @@ -3490,7 +3492,7 @@ aot_get_module_inst_mem_consumption(const AOTModuleInstance *module_inst,
sizeof(void *) * module_inst->memory_count
+ sizeof(AOTMemoryInstance) * module_inst->memory_count;
for (i = 0; i < module_inst->memory_count; i++) {
AOTMemoryInstance *mem_inst = module_inst->memories[i];
AOTMemoryInstance *mem_inst = module_inst->memories[i].memory;
mem_conspn->memories_size +=
mem_inst->num_bytes_per_page * mem_inst->cur_page_count;
mem_conspn->app_heap_size =
Expand Down
4 changes: 3 additions & 1 deletion core/iwasm/aot/aot_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ typedef struct AOTModule {
#endif
} AOTModule;

#define AOTMemoryWrapper WASMMemoryWrapper
#define AOTMemoryInstance WASMMemoryInstance
#define AOTTableInstance WASMTableInstance
#define AOTModuleInstance WASMModuleInstance
Expand Down Expand Up @@ -509,7 +510,8 @@ aot_unload(AOTModule *module);
AOTModuleInstance *
aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
WASMExecEnv *exec_env_main, uint32 stack_size, uint32 heap_size,
uint32 max_memory_pages, char *error_buf,
uint32 max_memory_pages, uint32 import_count,
const WASMImportInst *imports, char *error_buf,
uint32 error_buf_size);

/**
Expand Down
2 changes: 1 addition & 1 deletion core/iwasm/common/wasm_application.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
bool ret, is_import_func = true, is_memory64 = false;
#if WASM_ENABLE_MEMORY64 != 0
WASMModuleInstance *wasm_module_inst = (WASMModuleInstance *)module_inst;
is_memory64 = wasm_module_inst->memories[0]->is_memory64;
is_memory64 = wasm_module_inst->memories[0].memory->is_memory64;
#endif

exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
Expand Down
11 changes: 6 additions & 5 deletions core/iwasm/common/wasm_c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -4294,8 +4294,9 @@ wasm_memory_new_internal(wasm_store_t *store, uint16 memory_idx_rt,

#if WASM_ENABLE_INTERP != 0
if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
WASMMemoryInstance *memory_interp =
((WASMModuleInstance *)inst_comm_rt)->memories[memory_idx_rt];
WASMMemoryInstance *memory_interp = ((WASMModuleInstance *)inst_comm_rt)
->memories[memory_idx_rt]
.memory;
min_pages = memory_interp->cur_page_count;
max_pages = memory_interp->max_page_count;
init_flag = true;
Expand Down Expand Up @@ -4379,7 +4380,7 @@ wasm_memory_data(wasm_memory_t *memory)
WASMModuleInstance *module_inst =
(WASMModuleInstance *)module_inst_comm;
WASMMemoryInstance *memory_inst =
module_inst->memories[memory->memory_idx_rt];
module_inst->memories[memory->memory_idx_rt].memory;
return (byte_t *)memory_inst->memory_data;
}
#endif
Expand Down Expand Up @@ -4416,7 +4417,7 @@ wasm_memory_data_size(const wasm_memory_t *memory)
WASMModuleInstance *module_inst =
(WASMModuleInstance *)module_inst_comm;
WASMMemoryInstance *memory_inst =
module_inst->memories[memory->memory_idx_rt];
module_inst->memories[memory->memory_idx_rt].memory;
return (size_t)memory_inst->cur_page_count
* memory_inst->num_bytes_per_page;
}
Expand Down Expand Up @@ -4455,7 +4456,7 @@ wasm_memory_size(const wasm_memory_t *memory)
WASMModuleInstance *module_inst =
(WASMModuleInstance *)module_inst_comm;
WASMMemoryInstance *memory_inst =
module_inst->memories[memory->memory_idx_rt];
module_inst->memories[memory->memory_idx_rt].memory;
return memory_inst->cur_page_count;
}
#endif
Expand Down
75 changes: 66 additions & 9 deletions core/iwasm/common/wasm_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst_comm,
goto fail;

#if WASM_ENABLE_MEMORY64 != 0
if (module_inst->memories[0]->is_memory64)
if (module_inst->memories[0].memory->is_memory64)
max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
#endif
/* boundary overflow check, max start offset can only be size - 1, while end
Expand Down Expand Up @@ -665,7 +665,7 @@ WASMMemoryInstance *
wasm_get_default_memory(WASMModuleInstance *module_inst)
{
if (module_inst->memories)
return module_inst->memories[0];
return module_inst->memories[0].memory;
else
return NULL;
}
Expand All @@ -675,7 +675,7 @@ wasm_get_memory_with_idx(WASMModuleInstance *module_inst, uint32 index)
{
if ((index >= module_inst->memory_count) || !module_inst->memories)
return NULL;
return module_inst->memories[index];
return module_inst->memories[index].memory;
}

void
Expand Down Expand Up @@ -965,12 +965,13 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)

if (module->memory_count > 0) {
#if WASM_ENABLE_SHARED_MEMORY != 0
shared_memory_lock(module->memories[0]);
shared_memory_lock(module->memories[0].memory);
#endif
ret = wasm_enlarge_memory_internal((WASMModuleInstanceCommon *)module,
module->memories[0], inc_page_count);
module->memories[0].memory,
inc_page_count);
#if WASM_ENABLE_SHARED_MEMORY != 0
shared_memory_unlock(module->memories[0]);
shared_memory_unlock(module->memories[0].memory);
#endif
}

Expand All @@ -985,19 +986,75 @@ wasm_enlarge_memory_with_idx(WASMModuleInstance *module, uint32 inc_page_count,

if (memidx < module->memory_count) {
#if WASM_ENABLE_SHARED_MEMORY != 0
shared_memory_lock(module->memories[memidx]);
shared_memory_lock(module->memories[memidx].memory);
#endif
ret = wasm_enlarge_memory_internal((WASMModuleInstanceCommon *)module,
module->memories[memidx],
module->memories[memidx].memory,
inc_page_count);
#if WASM_ENABLE_SHARED_MEMORY != 0
shared_memory_unlock(module->memories[memidx]);
shared_memory_unlock(module->memories[memidx].memory);
#endif
}

return ret;
}

WASMMemoryInstance *
wasm_runtime_memory_create(uint64 initial_pages, uint64 max_pages, bool shared)
{
WASMMemoryInstance *memory;

if (!initial_pages || (max_pages && (max_pages < initial_pages))) {
return NULL;
}

if (!(memory = wasm_runtime_malloc(sizeof(WASMMemoryInstance)))) {
return NULL;
}

memset(memory, 0, sizeof(WASMMemoryInstance));

memory->module_type = Package_Type_Unknown;
memory->num_bytes_per_page = 64 * 1024;
memory->cur_page_count = (uint32)initial_pages;
memory->max_page_count = (uint32)max_pages;
memory->memory_data_size =
(uint64)memory->num_bytes_per_page * memory->max_page_count;
bh_assert(memory->memory_data_size
<= GET_MAX_LINEAR_MEMORY_SIZE(memory->is_memory64));

#if WASM_ENABLE_SHARED_MEMORY != 0
if (shared) {
memory->is_shared_memory = 1;
memory->ref_count = 1;
}
#endif

if (wasm_allocate_linear_memory(
&memory->memory_data, memory->is_shared_memory, memory->is_memory64,
memory->num_bytes_per_page, memory->cur_page_count,
memory->max_page_count, &memory->memory_data_size)
!= BHT_OK) {
wasm_runtime_free(memory);
return NULL;
}

if (memory->memory_data_size > 0) {
wasm_runtime_set_mem_bound_check_bytes(memory,
memory->memory_data_size);
}

return memory;
}

void
wasm_memory_destroy(WASMMemoryInstance *memory_inst)
{
if (memory_inst) {
wasm_runtime_free(memory_inst);
}
}

WASMMemoryInstance *
wasm_runtime_lookup_memory(WASMModuleInstanceCommon *module_inst,
const char *name)
Expand Down
24 changes: 14 additions & 10 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1589,19 +1589,23 @@ wasm_runtime_instantiate_internal(WASMModuleCommon *module,
WASMModuleInstanceCommon *parent,
WASMExecEnv *exec_env_main, uint32 stack_size,
uint32 heap_size, uint32 max_memory_pages,
uint32 import_count,
const WASMImportInst *imports,
char *error_buf, uint32 error_buf_size)
{
#if WASM_ENABLE_INTERP != 0
if (module->module_type == Wasm_Module_Bytecode)
return (WASMModuleInstanceCommon *)wasm_instantiate(
(WASMModule *)module, (WASMModuleInstance *)parent, exec_env_main,
stack_size, heap_size, max_memory_pages, error_buf, error_buf_size);
stack_size, heap_size, max_memory_pages, import_count, imports,
error_buf, error_buf_size);
#endif
#if WASM_ENABLE_AOT != 0
if (module->module_type == Wasm_Module_AoT)
return (WASMModuleInstanceCommon *)aot_instantiate(
(AOTModule *)module, (AOTModuleInstance *)parent, exec_env_main,
stack_size, heap_size, max_memory_pages, error_buf, error_buf_size);
stack_size, heap_size, max_memory_pages, import_count, imports,
error_buf, error_buf_size);
#endif
set_error_buf(error_buf, error_buf_size,
"Instantiate module failed, invalid module type");
Expand All @@ -1614,7 +1618,7 @@ wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size,
uint32 error_buf_size)
{
return wasm_runtime_instantiate_internal(module, NULL, NULL, stack_size,
heap_size, 0, error_buf,
heap_size, 0, 0, NULL, error_buf,
error_buf_size);
}

Expand All @@ -1625,8 +1629,8 @@ wasm_runtime_instantiate_ex(WASMModuleCommon *module,
{
return wasm_runtime_instantiate_internal(
module, NULL, NULL, args->default_stack_size,
args->host_managed_heap_size, args->max_memory_pages, error_buf,
error_buf_size);
args->host_managed_heap_size, args->max_memory_pages,
args->import_count, args->imports, error_buf, error_buf_size);
}

void
Expand Down Expand Up @@ -1865,7 +1869,7 @@ wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env)
WASMModule *wasm_module = wasm_module_inst->module;
module_common = (WASMModuleCommon *)wasm_module;
if (wasm_module_inst->memories) {
heap_handle = wasm_module_inst->memories[0]->heap_handle;
heap_handle = wasm_module_inst->memories[0].memory->heap_handle;
}
wasm_get_module_inst_mem_consumption(wasm_module_inst,
&module_inst_mem_consps);
Expand All @@ -1881,8 +1885,8 @@ wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env)
AOTModule *aot_module = (AOTModule *)aot_module_inst->module;
module_common = (WASMModuleCommon *)aot_module;
if (aot_module_inst->memories) {
AOTMemoryInstance **memories = aot_module_inst->memories;
heap_handle = memories[0]->heap_handle;
AOTMemoryWrapper *memories = aot_module_inst->memories;
heap_handle = memories[0].memory->heap_handle;
}
aot_get_module_inst_mem_consumption(aot_module_inst,
&module_inst_mem_consps);
Expand Down Expand Up @@ -7434,8 +7438,8 @@ wasm_runtime_sub_module_instantiate(WASMModuleCommon *module,
WASMModuleCommon *sub_module = sub_module_list_node->module;
WASMModuleInstanceCommon *sub_module_inst = NULL;
sub_module_inst = wasm_runtime_instantiate_internal(
sub_module, NULL, NULL, stack_size, heap_size, max_memory_pages,
error_buf, error_buf_size);
sub_module, NULL, NULL, stack_size, heap_size, max_memory_pages, 0,
NULL, error_buf, error_buf_size);
if (!sub_module_inst) {
LOG_DEBUG("instantiate %s failed",
sub_module_list_node->module_name);
Expand Down
2 changes: 2 additions & 0 deletions core/iwasm/common/wasm_runtime_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,8 @@ wasm_runtime_instantiate_internal(WASMModuleCommon *module,
WASMModuleInstanceCommon *parent,
WASMExecEnv *exec_env_main, uint32 stack_size,
uint32 heap_size, uint32 max_memory_pages,
uint32 import_count,
const WASMImportInst *imports,
char *error_buf, uint32 error_buf_size);

/* Internal API */
Expand Down
Loading

0 comments on commit 3698e7c

Please sign in to comment.