Skip to content

Commit

Permalink
Refactor import handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lum1n0us committed Oct 21, 2024
1 parent 18398a0 commit 7678f4d
Show file tree
Hide file tree
Showing 12 changed files with 319 additions and 102 deletions.
3 changes: 1 addition & 2 deletions core/iwasm/common/wasm_c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -3384,7 +3384,7 @@ wasm_func_call(const wasm_func_t *func, const wasm_val_vec_t *params,
AOTModuleInstance *inst_aot =
(AOTModuleInstance *)func->inst_comm_rt;
AOTModule *module_aot = (AOTModule *)inst_aot->module;
uint32 export_i = 0, export_func_j = 0;
uint32 export_i = 0;

for (; export_i < module_aot->export_count; ++export_i) {
AOTExport *export = module_aot->exports + export_i;
Expand All @@ -3395,7 +3395,6 @@ wasm_func_call(const wasm_func_t *func, const wasm_val_vec_t *params,
((wasm_func_t *)func)->func_comm_rt = func_comm_rt;
break;
}
export_func_j++;
}
}
}
Expand Down
68 changes: 68 additions & 0 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,7 @@ wasm_runtime_destroy_loading_module_list()
}
#endif /* WASM_ENABLE_MULTI_MODULE */

/*TODO: may need to merge with wasm_native. Even do more classification work */
bool
wasm_runtime_is_built_in_module(const char *module_name)
{
Expand Down Expand Up @@ -7742,3 +7743,70 @@ wasm_runtime_is_underlying_binary_freeable(WASMModuleCommon *const module)

return true;
}

/*TODO: take us(below) out when have a linker */
void
wasm_runtime_release_imports(struct WasmExternInstance *extern_inst)
{
if (extern_inst == NULL)
return;

wasm_runtime_free(extern_inst);
}

struct WasmExternInstance *
wasm_runtime_create_imports(WASMModuleCommon *module,
bool (*module_name_filter)(const char *))
{
int32_t import_count = wasm_runtime_get_import_count(module);
struct WasmExternInstance *imports = NULL;

if (import_count == 0)
return NULL;

imports =
wasm_runtime_malloc(sizeof(struct WasmExternInstance) * import_count);
if (!imports) {
LOG_ERROR("allocate memory failed: %s", strerror(errno));
return NULL;
}

memset(imports, 0, sizeof(struct WasmExternInstance) * import_count);
for (int32_t i = 0; i < import_count; i++) {
wasm_import_t import_type = { 0 };
wasm_runtime_get_import_type(module, i, &import_type);

if (module_name_filter
&& !module_name_filter(import_type.module_name)) {
LOG_DEBUG("skip import(%s,%s)\n", import_type.module_name,
import_type.name);
continue;
}

LOG_DEBUG("create import(%s,%s) kind %d\n", import_type.module_name,
import_type.name, import_type.kind);
struct WasmExternInstance *extern_instance = imports + i;
extern_instance->module_name = import_type.module_name;
extern_instance->field_name = import_type.name;
extern_instance->kind = import_type.kind;

if (import_type.kind == WASM_IMPORT_EXPORT_KIND_MEMORY) {
extern_instance->u.memory =
wasm_runtime_create_memory(NULL, import_type.u.memory_type, 0);
}
else {
LOG_DEBUG("unimplemented import(%s,%s) kind %d\n",
import_type.module_name, import_type.name,
import_type.kind);
}
}

return imports;
}

struct WasmExternInstance *
wasm_runtime_create_imports_with_builtin(WASMModuleCommon *module)
{
LOG_DEBUG("create imports with builtin\n");
return wasm_runtime_create_imports(module, wasm_runtime_is_built_in_module);
}
18 changes: 16 additions & 2 deletions core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ struct WasmExternInstance {
wasm_memory_inst_t memory;
} u;
};
typedef struct WasmExternInstance wasm_extern_inst_t;
typedef struct WasmExternInstance *wasm_extern_inst_t;

#ifndef INSTANTIATION_ARGS_OPTION_DEFINED
#define INSTANTIATION_ARGS_OPTION_DEFINED
Expand All @@ -282,7 +282,7 @@ typedef struct InstantiationArgs {
uint32_t host_managed_heap_size;
uint32_t max_memory_pages;
uint32_t import_count;
const wasm_extern_inst_t *imports;
const wasm_extern_inst_t imports;
} InstantiationArgs;
#endif /* INSTANTIATION_ARGS_OPTION_DEFINED */

Expand Down Expand Up @@ -2333,6 +2333,20 @@ wasm_runtime_shared_heap_malloc(wasm_module_inst_t module_inst, uint64_t size,
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 */
/**
* @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 void
wasm_runtime_release_imports(wasm_extern_inst_t imports);

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

#ifdef __cplusplus
}
#endif
Expand Down
60 changes: 47 additions & 13 deletions core/iwasm/interpreter/wasm_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,6 @@ memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
uint32 max_memory_pages, uint8 *aux_heap_base_global_data,
char *error_buf, uint32 error_buf_size)
{
WASMImport *import;
uint32 mem_index = 0, i,
memory_count = module->import_memory_count + module->memory_count;
uint64 total_size;
Expand All @@ -536,9 +535,9 @@ memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
memory = module_inst->global_table_data.memory_instances;

/* instantiate memories from import section */
import = module->import_memories;
for (i = 0; i < module->import_memory_count; i++, import++, memory++) {
#if WASM_ENABLE_MULTI_MODULE != 0
WASMImport *import = module->import_memories;
for (i = 0; i < module->import_memory_count; i++, import++, memory++) {
// TODO: ? make sure import->u.memory.import_module is set properly
if (import->u.memory.import_module != NULL) {
WASMModuleInstance *module_inst_linked;
Expand All @@ -557,10 +556,9 @@ memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
return NULL;
}
}
else
#endif
{
#if WASM_ENABLE_MULTI_MODULE != 0
else {
// TODO: Although it is for inherited memory, it misses a situation
// where the memory is imported from host or other modules
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;
Expand All @@ -578,12 +576,20 @@ memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
return NULL;
}
mem_index++;
#endif
}
}

/* instantiate memories from memory section */
bh_assert(mem_index == module->import_memory_count);
bh_assert(memory
== module_inst->global_table_data.memory_instances
+ module->import_memory_count);
#else
mem_index = module->import_memory_count;
memory = module_inst->global_table_data.memory_instances
+ module->import_memory_count;
#endif /* WASM_ENABLE_MULTI_MODULE != 0 */

/* instantiate memories from memory section */
for (i = 0; i < module->memory_count; i++, memory++) {
uint32 max_page_count = wasm_runtime_get_max_mem(
max_memory_pages, module->memories[i].init_page_count,
Expand Down Expand Up @@ -1930,6 +1936,12 @@ execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
return ret;
}

/*
* all imported WASMXXXInstance shall be linked and NOT NULL
*
* TODO: not sure if MULTI_MODULE can be used under the same conditions
* for checking.
*/
static bool
check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
uint32 error_buf_size)
Expand Down Expand Up @@ -2410,6 +2422,17 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
if (!module)
return NULL;

/* TODO: turn me on if the entire instantiation linking feature is complete
*/
// #if WASM_ENABLE_MULTI_MODULE == 0
// if (module->import_count > 0 && !imports) {
// set_error_buf(error_buf, error_buf_size,
// "argument imports is NULL while module has
// imports");
// return NULL;
// }
// #endif

/* Check the heap size */
heap_size = align_uint(heap_size, 8);
if (heap_size > APP_HEAP_SIZE_MAX)
Expand Down Expand Up @@ -2657,6 +2680,11 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
uint32 import_memory_index = 0;
uint32 import_index = 0;
for (; import_index < import_count; import_index++) {
/* TODO: remove me if the entire instantiation linking feature is
* complete */
if (imports == NULL)
break;

const struct WasmExternInstance *import = imports + import_index;
if (import->kind == WASM_IMPORT_EXPORT_KIND_MEMORY) {
if (import_memory_index >= module->import_memory_count) {
Expand Down Expand Up @@ -5112,15 +5140,19 @@ wasm_get_module_name(WASMModule *module)
}

#if WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0
bool
/*
* The function is used to create a new struct WasmExternInstance list
* for a spawned thread.
*/
int32
wasm_runtime_inherit_imports(WASMModule *module, WASMModuleInstance *inst,
struct WasmExternInstance *out, int32_t out_len)
{
int32_t spawned_import_count = module->import_count;
if (spawned_import_count > out_len) {
LOG_WARNING("The number of imported functions is more than the "
"length of provided buffer ");
return false;
return -1;
}

for (int32_t i = 0, import_memory_index = 0; i < spawned_import_count;
Expand All @@ -5136,7 +5168,9 @@ wasm_runtime_inherit_imports(WASMModule *module, WASMModuleInstance *inst,

if (import_type.kind == WASM_IMPORT_EXPORT_KIND_MEMORY) {
out[i].u.memory = inst->memories[import_memory_index];
#if WASM_ENABLE_SHARED_MEMORY != 0
shared_memory_inc_reference(inst->memories[import_memory_index]);
#endif
import_memory_index++;
}
else {
Expand All @@ -5146,6 +5180,6 @@ wasm_runtime_inherit_imports(WASMModule *module, WASMModuleInstance *inst,
}
}

return true;
return 0;
}
#endif
#endif
2 changes: 1 addition & 1 deletion core/iwasm/interpreter/wasm_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ const char *
wasm_get_module_name(WASMModule *module);

#if WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0
bool
int32
wasm_runtime_inherit_imports(WASMModule *module, WASMModuleInstance *inst,
struct WasmExternInstance *out, int32_t out_len);
#endif
Expand Down
38 changes: 34 additions & 4 deletions core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ pthread_create_wrapper(wasm_exec_env_t exec_env,
uint32 aux_stack_size;
uint64 aux_stack_start = 0;
int32 ret = -1;
int32_t spawned_import_count = ((WASMModule *)module)->import_count;
struct WasmExternInstance *spawned_imports = NULL;

bh_assert(module);
bh_assert(module_inst);
Expand All @@ -579,12 +581,37 @@ pthread_create_wrapper(wasm_exec_env_t exec_env,
}
#endif

/*
* build a imports list(struct WASMExternInstance[]) from parent's imports
*/
spawned_imports = wasm_runtime_malloc(sizeof(struct WasmExternInstance)
* spawned_import_count);
if (spawned_imports == NULL) {
LOG_ERROR("Failed to allocate memory for imports");
goto fail;
}

#if WASM_ENABLE_INTERP != 0
ret = wasm_runtime_inherit_imports((WASMModule *)module,
(WASMModuleInstance *)module_inst,
spawned_imports, spawned_import_count);
if (ret != 0) {
LOG_ERROR("Failed to inherit imports");
goto fail;
}
#endif

#if WASM_ENABLE_AOT != 0
// TODO:
// bh_assert(false && "Unsupported operation");
#endif

if (!(new_module_inst = wasm_runtime_instantiate_internal(
module, module_inst, exec_env, stack_size,
0, // heap_size
0, // max_memory_pages
0, // import_count
NULL, // imports
0, // heap_size
0, // max_memory_pages
spawned_import_count, // import_count
spawned_imports, // imports
NULL, 0)))
return -1;

Expand Down Expand Up @@ -646,9 +673,12 @@ pthread_create_wrapper(wasm_exec_env_t exec_env,
if (thread)
*thread = thread_handle;

wasm_runtime_free(spawned_imports);
return 0;

fail:
if (spawned_imports)
wasm_runtime_free(spawned_imports);
if (new_module_inst)
wasm_runtime_deinstantiate_internal(new_module_inst, true);
if (info_node)
Expand Down
Loading

0 comments on commit 7678f4d

Please sign in to comment.