Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[instantiation linking] create and import WASMMemoryInstance for interp #3845

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7d56289
Exclude fuzz test python and npm packages in scoreboard scan (#3871)
TianlongLiang Oct 23, 2024
6426fc4
Fix out of bounds issues after memory.grow on non-aot non-threads bui…
Zzzabiyaka Oct 23, 2024
e0e9ffa
Add APIs and test for instantiation linking
lum1n0us Sep 29, 2024
c7dd07b
No need to stick with the export memory rule of WASI apps
lum1n0us Sep 30, 2024
19c6646
Add create memory API and relative smoke test cases
lum1n0us Oct 7, 2024
e1ed8a0
Refactor memory deinstantiate function and fix memory destruction
lum1n0us Oct 9, 2024
9380299
Refactor memory instantiation and import handling
lum1n0us Oct 9, 2024
8397c6e
Refactor CMakeLists.txt and import_memory.c
lum1n0us Oct 9, 2024
c94b190
apply modification about memory instantiation in lib-pthread, lib-was…
lum1n0us Oct 9, 2024
6e56a01
Refactor InstantiationArgs struct in wasm_c_api.h
lum1n0us Oct 10, 2024
ec68f76
Create ExternalInstance for spec test in iwasm
lum1n0us Oct 11, 2024
5940cd5
Keep loading linking for thread features
lum1n0us Oct 12, 2024
ab2138e
Fix issues when enable MULTI_MODULE
lum1n0us Oct 12, 2024
e99be69
Refactor module instantiation in main.c in Windows
lum1n0us Oct 14, 2024
4551fdc
Rename WasmExternalInstance
lum1n0us Oct 16, 2024
0ac07e1
Refactor memory instantiation and import handling
lum1n0us Oct 18, 2024
6d78832
windows iwasm adapts to new instantiation linking
lum1n0us Oct 20, 2024
5684b7c
Refactor import handling
lum1n0us Oct 20, 2024
3a8b5a6
Rename variables and refactor few apis.
lum1n0us Oct 28, 2024
0c58cc1
fix enclave compilation errors
lum1n0us Oct 29, 2024
00fec83
fix adjust heap issues
lum1n0us Oct 29, 2024
19fc643
Revert "Exclude fuzz test python and npm packages in scoreboard scan …
lum1n0us Nov 1, 2024
74bd20f
Reorder import_count and imports parameters in instantiation functions
lum1n0us Nov 1, 2024
c3efbbb
fix a review comment
lum1n0us Nov 1, 2024
7f38415
Improve error handling for module imports and initialize error buffer…
lum1n0us Nov 1, 2024
93279c1
Refactor warning logs for import handling and update error return logic
lum1n0us Nov 1, 2024
cc4c78c
resolve comments
lum1n0us Nov 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ core/iwasm/libraries/lib-wasi-threads/test/*.wasm
core/iwasm/libraries/lib-socket/test/*.wasm

product-mini/app-samples/hello-world/test.wasm
product-mini/platforms/linux-sgx/enclave-sample/App/
product-mini/platforms/linux-sgx/enclave-sample/Enclave/
product-mini/platforms/linux-sgx/enclave-sample/iwasm
product-mini/platforms/linux-sgx/enclave-sample/App/*.o
product-mini/platforms/linux-sgx/enclave-sample/Enclave/*.o
product-mini/platforms/linux-sgx/enclave-sample/
!product-mini/platforms/linux-sgx/enclave-sample/Makefile*

build_out
tests/wamr-test-suites/workspace
Expand Down
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
42 changes: 42 additions & 0 deletions core/iwasm/common/wasm_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,48 @@ wasm_enlarge_memory_with_idx(WASMModuleInstance *module, uint32 inc_page_count,
return ret;
}

WASMMemoryInstance *
wasm_runtime_create_memory(WASMModuleCommon *module, WASMMemoryType *type)
{
if (!module)
return NULL;

#if WASM_ENABLE_INTERP != 0
if (module->module_type == Wasm_Module_Bytecode)
return wasm_create_memory((WASMModule *)module, type);
#endif

#if WASM_ENABLE_AOT != 0
if (module->module_type == Wasm_Module_AoT)
bh_assert(false && "Unsupported operation");
#endif

LOG_ERROR("create memory failed, invalid module type");
return NULL;
}

void
wasm_runtime_destroy_memory(WASMModuleCommon *const module,
WASMMemoryInstance *memory)
{
if (!module)
return;

#if WASM_ENABLE_INTERP != 0
if (module->module_type == Wasm_Module_Bytecode) {
wasm_destroy_memory(memory);
return;
}
#endif

#if WASM_ENABLE_AOT != 0
if (module->module_type == Wasm_Module_AoT)
bh_assert(false && "Unsupported operation");
#endif

LOG_ERROR("destroy memory failed, invalid module type");
}

WASMMemoryInstance *
wasm_runtime_lookup_memory(WASMModuleInstanceCommon *module_inst,
const char *name)
Expand Down
218 changes: 211 additions & 7 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 All @@ -1360,7 +1361,7 @@ wasm_runtime_is_built_in_module(const char *module_name)
|| !strcmp("spectest", module_name)
#endif
#if WASM_ENABLE_WASI_TEST != 0
|| !strcmp("foo", module_name)
|| !strcmp("foo", module_name) || !strncmp("wasi", module_name, 4)
#endif
|| !strcmp("", module_name));
}
Expand Down Expand Up @@ -1622,13 +1623,16 @@ wasm_runtime_instantiate_internal(WASMModuleCommon *module,
WASMModuleInstanceCommon *parent,
WASMExecEnv *exec_env_main, uint32 stack_size,
uint32 heap_size, uint32 max_memory_pages,
char *error_buf, uint32 error_buf_size)
const WASMExternInstance *imports,
uint32 import_count, 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, imports, import_count,
error_buf, error_buf_size);
#endif
#if WASM_ENABLE_AOT != 0
if (module->module_type == Wasm_Module_AoT)
Expand All @@ -1647,8 +1651,11 @@ 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,
error_buf_size);
heap_size,
0, // max_memory_pages
NULL, // imports
0, // import_count
error_buf, error_buf_size);
}

WASMModuleInstanceCommon *
Expand All @@ -1658,8 +1665,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->imports,
args->import_count, error_buf, error_buf_size);
}

void
Expand Down Expand Up @@ -7474,6 +7481,8 @@ wasm_runtime_sub_module_instantiate(WASMModuleCommon *module,
WASMModuleInstanceCommon *sub_module_inst = NULL;
sub_module_inst = wasm_runtime_instantiate_internal(
sub_module, NULL, NULL, stack_size, heap_size, max_memory_pages,
NULL, // imports
0, // import_count
error_buf, error_buf_size);
if (!sub_module_inst) {
LOG_DEBUG("instantiate %s failed",
Expand Down Expand Up @@ -7739,3 +7748,198 @@ wasm_runtime_is_underlying_binary_freeable(WASMModuleCommon *const module)

return true;
}

/*TODO: take us(below) out when have a linker */
bool
wasm_runtime_create_extern_inst(WASMModuleCommon *module,
wasm_import_t import_type,
WASMExternInstance *out)
{
if (!out)
return false;

LOG_DEBUG("create import(%s,%s) kind %d", import_type.module_name,
import_type.name, import_type.kind);

out->module_name = import_type.module_name;
out->field_name = import_type.name;
out->kind = import_type.kind;

if (import_type.kind == WASM_IMPORT_EXPORT_KIND_MEMORY) {
out->u.memory =
wasm_runtime_create_memory(module, import_type.u.memory_type);
if (!out->u.memory) {
LOG_ERROR("create memory failed\n");
return false;
}
}
else {
LOG_DEBUG("unimplemented import(%s,%s) kind %d",
import_type.module_name, import_type.name, import_type.kind);
}

return true;
}

void
wasm_runtime_destroy_extern_inst(WASMModuleCommon *module,
WASMExternInstance *extern_inst)
{
if (!extern_inst)
return;

if (extern_inst->kind == WASM_IMPORT_EXPORT_KIND_MEMORY) {
wasm_runtime_destroy_memory(module, extern_inst->u.memory);
extern_inst->u.memory = NULL;
}
else {
LOG_DEBUG("unimplemented import(%s,%s) kind %d",
extern_inst->module_name, extern_inst->field_name,
extern_inst->kind);
}

extern_inst->module_name = NULL;
extern_inst->field_name = NULL;
}

/*
* willn't take the ownership of new_extern_inst
* take the ownership of the content of new_extern_inst
*/
bool
wasm_runtime_overwirte_imports(WASMModuleCommon *module,
WASMExternInstance *orig_extern_inst_list,
WASMExternInstance *new_extern_inst, int32 index)
{
// argument check
if (!module || !orig_extern_inst_list || !new_extern_inst) {
return false;
}

// check index with the import count of the module
if (index < 0 || index >= wasm_runtime_get_import_count(module)) {
return false;
}

// get the targeted extern_inst. allow to be NULL
WASMExternInstance *old_extern_inst = orig_extern_inst_list + index;

// release the targeted extern_inst if not NULL
wasm_runtime_destroy_extern_inst(module, old_extern_inst);

// overwrite the targeted extern_inst
bh_memcpy_s(old_extern_inst, sizeof(WASMExternInstance), new_extern_inst,
sizeof(WASMExternInstance));
return true;
}

/*
* Be aware that it will remove all items in the list, regardless of whether
* they were created by the runtime (for built-ins) or by users.
*/
void
wasm_runtime_destroy_imports(WASMModuleCommon *module,
WASMExternInstance *extern_inst_list)
{
if (!module || !extern_inst_list)
return;

wenyongh marked this conversation as resolved.
Show resolved Hide resolved
for (int32 i = 0, import_count = wasm_runtime_get_import_count(module);
i < import_count; i++) {
wasm_runtime_destroy_extern_inst(module, extern_inst_list + i);
}

wasm_runtime_free(extern_inst_list);
}

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

if (import_count == 0)
return NULL;

imports = runtime_malloc(sizeof(WASMExternInstance) * import_count,
NULL, // module_inst
NULL, 0);
if (!imports) {
LOG_ERROR("allocate memory failed");
return NULL;
}

for (int32 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)", import_type.module_name,
import_type.name);
continue;
}

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

return imports;
}

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

#if WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0
/*
* The function is used to create a new WASMExternInstance list
* for a spawned thread.
*/
int32
wasm_runtime_inherit_imports(WASMModuleCommon *module,
WASMModuleInstanceCommon *inst,
WASMExternInstance *out, int32 out_len)
{
#if WASM_ENABLE_INTERP != 0
if (module->module_type == Wasm_Module_Bytecode) {
return wasm_inherit_imports((WASMModule *)module,
(WASMModuleInstance *)inst, out, out_len);
}
#endif
#if WASM_ENABLE_AOT != 0
if (module->module_type == Wasm_Module_AoT) {
bh_assert(false && "Unsupported operation");
}
#endif
LOG_ERROR("inherit imports failed, invalid module type");
return 0;
}

void
wasm_runtime_disinherit_imports(WASMModuleCommon *module,
wenyongh marked this conversation as resolved.
Show resolved Hide resolved
WASMExternInstance *imports, int32 import_count)
{
#if WASM_ENABLE_INTERP != 0
if (module->module_type == Wasm_Module_Bytecode) {
wasm_disinherit_imports((WASMModule *)module, imports, import_count);
return;
}
#endif
#if WASM_ENABLE_AOT != 0
if (module->module_type == Wasm_Module_AoT) {
bh_assert(false && "Unsupported operation");
}
#endif
LOG_ERROR("disinherit imports failed, invalid module type");
}
#endif /* WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0 */
20 changes: 19 additions & 1 deletion core/iwasm/common/wasm_runtime_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,9 @@ wasm_runtime_instantiate_internal(WASMModuleCommon *module,
WASMModuleInstanceCommon *parent,
WASMExecEnv *exec_env_main, uint32 stack_size,
uint32 heap_size, uint32 max_memory_pages,
char *error_buf, uint32 error_buf_size);
const WASMExternInstance *imports,
uint32 import_count, char *error_buf,
uint32 error_buf_size);

/* Internal API */
void
Expand Down Expand Up @@ -1215,6 +1217,22 @@ void
wasm_runtime_set_linux_perf(bool flag);
#endif

#if WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0
/*
* The function is used to create a new WASMExternInstance list
* for a spawned thread.
*/
int32
wasm_runtime_inherit_imports(WASMModuleCommon *module,
WASMModuleInstanceCommon *inst,
WASMExternInstance *out, int32 out_len);

void
wasm_runtime_disinherit_imports(WASMModuleCommon *module,
WASMExternInstance *imports,
int32 import_count);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions core/iwasm/include/wasm_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ typedef struct InstantiationArgs {
uint32_t default_stack_size;
uint32_t host_managed_heap_size;
uint32_t max_memory_pages;
const struct WasmExternInstance *imports;
uint32_t import_count;
} InstantiationArgs;
#endif /* INSTANTIATION_ARGS_OPTION_DEFINED */

Expand Down
Loading