Skip to content

Commit

Permalink
Fix memory deinstantiation loop to correctly reference import_memory_…
Browse files Browse the repository at this point in the history
…count
  • Loading branch information
lum1n0us committed Nov 1, 2024
1 parent 7f50b14 commit 730cd55
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
32 changes: 29 additions & 3 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ static void
memories_deinstantiate(AOTModuleInstance *module_inst)
{
/* import memories created by host or linker. released by them too */
for (uint32 i = module_inst->module->import_memory_count;
for (uint32 i = ((AOTModule *)module_inst->module)->import_memory_count;
i < module_inst->memory_count; i++) {
memory_deinstantiate(module_inst->memories[i]);
}
Expand Down Expand Up @@ -1117,6 +1117,7 @@ aot_lookup_memory(AOTModuleInstance *module_inst, char const *name)
return module_inst->export_memories[i].memory;
return NULL;
#else
(void)name;
(void)module_inst->export_memories;
return aot_get_default_memory(module_inst);
#endif
Expand Down Expand Up @@ -1179,6 +1180,7 @@ memories_instantiate(const AOTModule *module, AOTModuleInstance *module_inst,
mem_index++, memory++) {
AOTImportMemory *memory_type = module->import_memories + mem_index;

#if WASM_ENABLE_MULTI_MODULE == 0
const WASMExternInstance *extern_inst =
wasm_runtime_get_extern_instance(imports, import_count,
WASM_IMPORT_EXPORT_KIND_MEMORY,
Expand Down Expand Up @@ -1209,6 +1211,22 @@ memories_instantiate(const AOTModule *module, AOTModuleInstance *module_inst,
module_inst->memories[mem_index] = extern_inst->u.memory;
bh_memcpy_s(memory, sizeof(AOTMemoryInstance), extern_inst->u.memory,
sizeof(AOTMemoryInstance));
#else
uint32 max_page_count = wasm_runtime_get_max_mem(
max_memory_pages, memory_type->mem_type.init_page_count,
memory_type->mem_type.max_page_count);

module_inst->memories[mem_index] = memory_instantiate(
module, parent, memory, parent ? parent->memories[mem_index] : NULL,
memory_type->mem_type.num_bytes_per_page,
memory_type->mem_type.init_page_count, max_page_count,
/* only inst->memories[0] will have a app heap */
mem_index == 0 ? heap_size : 0, memory_type->mem_type.flags,
aux_heap_base_global_data, error_buf, error_buf_size);
if (!module_inst->memories[mem_index]) {
return false;
}
#endif
}

/* process internal memories */
Expand Down Expand Up @@ -1887,7 +1905,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
*/
set_error_buf(error_buf, error_buf_size,
"imports is NULL while module has imports");
return NULL;
// return NULL;
}
#endif

Expand Down Expand Up @@ -3609,6 +3627,9 @@ static void
const_string_node_size_cb(void *key, void *value, void *p_const_string_size)
{
uint32 const_string_size = 0;

(void)key;

const_string_size += bh_hash_map_get_elem_struct_size();
const_string_size += strlen((const char *)value) + 1;
*(uint32 *)p_const_string_size += const_string_size;
Expand Down Expand Up @@ -3646,7 +3667,7 @@ aot_get_module_mem_consumption(const AOTModule *module,

mem_conspn->memories_size =
sizeof(AOTMemory)
* (module->memory_count + moudle->import_memory_count);
* (module->memory_count + module->import_memory_count);
mem_conspn->globals_size = sizeof(AOTGlobal) * module->global_count;
mem_conspn->exports_size = sizeof(AOTExport) * module->export_count;

Expand Down Expand Up @@ -5428,10 +5449,12 @@ aot_resolve_import_func(AOTModule *module, AOTImportFunc *import_func)
char error_buf[128];
AOTModule *sub_module = NULL;
#endif

import_func->func_ptr_linked = wasm_native_resolve_symbol(
import_func->module_name, import_func->func_name,
import_func->func_type, &import_func->signature,
&import_func->attachment, &import_func->call_conv_raw);

#if WASM_ENABLE_MULTI_MODULE != 0
if (!import_func->func_ptr_linked) {
if (!wasm_runtime_is_built_in_module(import_func->module_name)) {
Expand All @@ -5454,7 +5477,10 @@ aot_resolve_import_func(AOTModule *module, AOTImportFunc *import_func)
}
}
}
#else
(void)module;
#endif

return import_func->func_ptr_linked != NULL;
}

Expand Down
8 changes: 4 additions & 4 deletions core/iwasm/interpreter/wasm_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
return NULL;
}

if (!(memories[mem_index++] = wasm_lookup_memory(
if (!(memories[mem_index] = wasm_lookup_memory(
module_inst_linked, memory_type->field_name))) {
set_error_buf(error_buf, error_buf_size, "unknown memory");
memories_deinstantiate(module_inst, memories, memory_count);
Expand Down Expand Up @@ -598,7 +598,6 @@ memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
memories_deinstantiate(module_inst, memories, memory_count);
return NULL;
}
mem_index++;
}
#else

Expand All @@ -609,7 +608,7 @@ memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
if (!extern_inst) {
LOG_ERROR("missing a import memory(%s, %s)",
memory_type->module_name, memory_type->field_name);
return false;
return NULL;
}

/* just in case */
Expand All @@ -618,7 +617,7 @@ memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
LOG_ERROR(
"mismatched import memory name: expect \"%s\", got \"%s\"",
memory_type->field_name, extern_inst->field_name);
return false;
return NULL;
}
#endif
/*
Expand Down Expand Up @@ -2068,6 +2067,7 @@ check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
for (i = 0; i < module->import_memory_count; i++) {
WASMMemoryImport *type = &((module->import_memories + i)->u.memory);
WASMMemoryInstance *memory = module_inst->memories[i];
(void)memory;
if (
#if WASM_ENABLE_MULTI_MODULE != 0
!wasm_runtime_is_built_in_module(type->module_name)
Expand Down
5 changes: 5 additions & 0 deletions core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ wasi_ctx_get_curfds(wasi_ctx_t wasi_ctx)
static inline struct argv_environ_values *
wasi_ctx_get_argv_environ(wasm_module_inst_t module_inst, wasi_ctx_t wasi_ctx)
{
(void)module_inst;
if (!wasi_ctx)
return NULL;
return wasi_ctx->argv_environ;
Expand Down Expand Up @@ -2171,6 +2172,8 @@ wasi_sock_send(wasm_exec_env_t exec_env, wasi_fd_t sock,
wasi_errno_t err;
size_t send_bytes = 0;

(void)si_flags;

if (!wasi_ctx) {
return __WASI_EINVAL;
}
Expand Down Expand Up @@ -2241,6 +2244,8 @@ wasi_sock_shutdown(wasm_exec_env_t exec_env, wasi_fd_t sock, wasi_sdflags_t how)
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);

(void)how;

if (!wasi_ctx)
return __WASI_EINVAL;

Expand Down

0 comments on commit 730cd55

Please sign in to comment.