Skip to content

Commit

Permalink
Refactor wasm_runtime_instantiate()
Browse files Browse the repository at this point in the history
The main goal is to let existed code run without modification
  • Loading branch information
lum1n0us committed Nov 1, 2024
1 parent 730cd55 commit 4016457
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 236 deletions.
6 changes: 1 addition & 5 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1899,13 +1899,9 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
module->import_func_count + module->import_global_count
+ module->import_memory_count + module->import_table_count;
if (total_import_count > 0 && !imports) {
/*
* TODO: might be too strict
* might wasm_runtime_create_imports_with_builtin() here by default
*/
set_error_buf(error_buf, error_buf_size,
"imports is NULL while module has imports");
// return NULL;
return NULL;
}
#endif

Expand Down
81 changes: 64 additions & 17 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1651,12 +1651,21 @@ wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size,
uint32 heap_size, char *error_buf,
uint32 error_buf_size)
{
#if WASM_ENABLE_MULTI_MODULE == 0
/*
* TODO: if wasm_runtime_instantiate_with_builtin_linker is not in this
* library what should we do? _weak?
*/
return wasm_runtime_instantiate_with_builtin_linker(
module, stack_size, heap_size, error_buf, error_buf_size);
#else
return wasm_runtime_instantiate_internal(module, NULL, NULL, stack_size,
heap_size,
0, // max_memory_pages
NULL, // imports
0, // import_count
error_buf, error_buf_size);
#endif
}

WASMModuleInstanceCommon *
Expand Down Expand Up @@ -7751,6 +7760,45 @@ wasm_runtime_is_underlying_binary_freeable(WASMModuleCommon *const module)
}

/*TODO: take us(below) out when have a linker */
WASMModuleInstanceCommon *
wasm_runtime_instantiate_with_builtin_linker(WASMModuleCommon *module,
uint32 stack_size,
uint32 heap_size, char *error_buf,
uint32 error_buf_size)
{
int32_t import_count = 0;
WASMExternInstance *imports = NULL;

import_count = wasm_runtime_get_import_count(module);
if (import_count) {
imports = runtime_malloc(sizeof(WASMExternInstance) * import_count,
NULL, error_buf, error_buf_size);
if (!imports) {
set_error_buf(error_buf, error_buf_size, "allocate imports failed");
return NULL;
}

if (!wasm_runtime_create_imports_with_builtin(module, imports,
import_count)) {
set_error_buf(error_buf, error_buf_size,
"initialize imports failed");
wasm_runtime_free(imports);
return NULL;
}
}

WASMModuleInstanceCommon *inst = wasm_runtime_instantiate_internal(
module, NULL, NULL, stack_size, heap_size,
0, // max_memory_pages
imports, import_count, error_buf, error_buf_size);

if (imports) {
wasm_runtime_free(imports);
}

return inst;
}

bool
wasm_runtime_create_extern_inst(WASMModuleCommon *module,
wasm_import_t import_type,
Expand Down Expand Up @@ -7853,22 +7901,19 @@ wasm_runtime_destroy_imports(WASMModuleCommon *module,
wasm_runtime_free(extern_inst_list);
}

WASMExternInstance *
bool
wasm_runtime_create_imports(WASMModuleCommon *module,
bool (*module_name_filter)(const char *))
bool (*module_name_filter)(const char *),
WASMExternInstance *out, int32 out_len)
{
int32 import_count = wasm_runtime_get_import_count(module);
WASMExternInstance *imports = NULL;

if (import_count == 0)
return NULL;
return true;

imports = runtime_malloc(sizeof(WASMExternInstance) * import_count,
NULL, // module_inst
NULL, 0);
if (!imports) {
LOG_ERROR("allocate memory failed");
return NULL;
if (!out || out_len < import_count) {
LOG_ERROR("invalid arguments");
return false;
}

for (int32 i = 0; i < import_count; i++) {
Expand All @@ -7882,23 +7927,25 @@ wasm_runtime_create_imports(WASMModuleCommon *module,
continue;
}

WASMExternInstance *extern_instance = imports + i;
WASMExternInstance *extern_instance = out + i;
if (!wasm_runtime_create_extern_inst(module, import_type,
extern_instance)) {
wasm_runtime_destroy_imports(module, imports);
wasm_runtime_destroy_imports(module, out);
LOG_ERROR("create import failed");
return NULL;
return false;
}
}

return imports;
return true;
}

WASMExternInstance *
wasm_runtime_create_imports_with_builtin(WASMModuleCommon *module)
bool
wasm_runtime_create_imports_with_builtin(WASMModuleCommon *module,
WASMExternInstance *out, int32 out_len)
{
LOG_DEBUG("create imports with builtin");
return wasm_runtime_create_imports(module, wasm_runtime_is_built_in_module);
return wasm_runtime_create_imports(module, wasm_runtime_is_built_in_module,
out, out_len);
}

#if WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0
Expand Down
21 changes: 16 additions & 5 deletions core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -2331,18 +2331,29 @@ WASM_RUNTIME_API_EXTERN void
wasm_runtime_shared_heap_free(wasm_module_inst_t module_inst, uint64_t ptr);

/*TODO: take me out when have a linker */
WASM_RUNTIME_API_EXTERN wasm_module_inst_t
wasm_runtime_instantiate_with_builtin_linker(wasm_module_t module,
uint32_t stack_size,
uint32_t heap_size,
char *error_buf,
uint32_t error_buf_size);

/**
* @return NULL if failed and if there is no import
*/
WASM_RUNTIME_API_EXTERN wasm_extern_inst_t
wasm_runtime_create_imports_with_builtin(wasm_module_t module);
WASM_RUNTIME_API_EXTERN bool
wasm_runtime_create_imports_with_builtin(wasm_module_t module,
wasm_extern_inst_t out,
int32_t out_len);

WASM_RUNTIME_API_EXTERN void
WASM_RUNTIME_API_EXTERN
void
wasm_runtime_destroy_imports(wasm_module_t module, wasm_extern_inst_t imports);

WASM_RUNTIME_API_EXTERN wasm_extern_inst_t
WASM_RUNTIME_API_EXTERN bool
wasm_runtime_create_imports(wasm_module_t module,
bool (*module_name_filter)(const char *));
bool (*module_name_filter)(const char *),
wasm_extern_inst_t out, int32_t out_len);

#ifdef __cplusplus
}
Expand Down
6 changes: 1 addition & 5 deletions core/iwasm/interpreter/wasm_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -2502,13 +2502,9 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,

#if WASM_ENABLE_MULTI_MODULE == 0
if (module->import_count > 0 && !imports) {
/*
* TODO: might be too strict
* might wasm_runtime_create_imports_with_builtin() here by default
*/
set_error_buf(error_buf, error_buf_size,
"argument imports is NULL while module has imports");
// return NULL;
return NULL;
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,56 +377,17 @@ handle_cmd_instantiate_module(uint64 *args, uint32 argc)

bh_assert(argc == 5);

*(void **)args_org = NULL;

if (!runtime_inited) {
*(void **)args_org = NULL;
return;
}

#if WASM_ENABLE_MULTI_MODULE == 0
{
int32_t import_count =
wasm_runtime_get_import_count(enclave_module->module);
WASMExternInstance *imports = NULL;

#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_WASI_TEST != 0 \
|| WASM_ENABLE_LIBC_BUILTIN != 0 || WASM_ENABLE_LIBC_WASI != 0
imports =
wasm_runtime_create_imports_with_builtin(enclave_module->module);
#endif
if (import_count > 0 && imports == NULL) {
LOG_WARNING("Need to provide necessary imported objects");
return;
}

InstantiationArgs inst_args = {
.default_stack_size = stack_size,
.host_managed_heap_size = heap_size,
.max_memory_pages = 0,
.imports = imports,
.import_count = (uint32_t)import_count,
};

module_inst = wasm_runtime_instantiate_ex(
enclave_module->module, &inst_args, error_buf, error_buf_size);
if (!module_inst) {
wasm_runtime_destroy_imports(enclave_module->module, imports);
return;
}

/*
* FIXME: how to relese imports.
* if there will be spawned thread, need to release when the thread is
* done.
*/
}
#else /* WASM_ENABLE_MULTI_MODULE == 0 */
if (!(module_inst =
wasm_runtime_instantiate(enclave_module->module, stack_size,
heap_size, error_buf, error_buf_size))) {
*(void **)args_org = NULL;
return;
}
#endif /* WASM_ENABLE_MULTI_MODULE == 0 */

*(wasm_module_inst_t *)args_org = module_inst;

Expand Down Expand Up @@ -802,11 +763,6 @@ ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size)
RuntimeInitArgs init_args;
char error_buf[128];
const char *exception;
uint32 stack_size = 16 * 1024;
uint32 heap_size = 16 * 1024;
#if WASM_ENABLE_MULTI_MODULE == 0
WASMExternInstance *imports = NULL;
#endif

/* avoid duplicated init */
if (runtime_inited) {
Expand Down Expand Up @@ -841,45 +797,13 @@ ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size)
}

/* instantiate the module */
#if WASM_ENABLE_MULTI_MODULE == 0
{
int32_t import_count = wasm_runtime_get_import_count(wasm_module);

#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_WASI_TEST != 0 \
|| WASM_ENABLE_LIBC_BUILTIN != 0 || WASM_ENABLE_LIBC_WASI != 0
imports = wasm_runtime_create_imports_with_builtin(wasm_module);
#endif
if (import_count > 0 && imports == NULL) {
enclave_print("Need to provide necessary imported objects");
enclave_print("\n");
goto fail2;
}

InstantiationArgs inst_args = {
.default_stack_size = stack_size,
.host_managed_heap_size = heap_size,
.max_memory_pages = 0,
.imports = imports,
.import_count = (uint32_t)import_count,
};

wasm_module_inst = wasm_runtime_instantiate_ex(
wasm_module, &inst_args, error_buf, sizeof(error_buf));
if (!wasm_module_inst) {
enclave_print(error_buf);
enclave_print("\n");
goto fail3;
}
}
#else /* WASM_ENABLE_MULTI_MODULE == 0 */
if (!(wasm_module_inst =
wasm_runtime_instantiate(wasm_module, stack_size, heap_size,
wasm_runtime_instantiate(wasm_module, 16 * 1024, 16 * 1024,
error_buf, sizeof(error_buf)))) {
enclave_print(error_buf);
enclave_print("\n");
goto fail2;
}
#endif /* WASM_ENABLE_MULTI_MODULE == 0 */

/* execute the main function of wasm app */
wasm_application_execute_main(wasm_module_inst, 0, NULL);
Expand All @@ -891,12 +815,6 @@ ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size)
/* destroy the module instance */
wasm_runtime_deinstantiate(wasm_module_inst);

#if WASM_ENABLE_MULTI_MODULE == 0
fail3:
/* destory imports */
wasm_runtime_destroy_imports(wasm_module, imports);
#endif /* WASM_ENABLE_MULTI_MODULE == 0*/

fail2:
/* unload the module */
wasm_runtime_unload(wasm_module);
Expand Down
Loading

0 comments on commit 4016457

Please sign in to comment.