Skip to content

Commit

Permalink
Merge pull request #933 from bytecodealliance/main
Browse files Browse the repository at this point in the history
Merge bytecodealliance:main into wenyongh:main
  • Loading branch information
wenyongh authored Jun 26, 2024
2 parents d25cecc + 65bf04e commit c867cf8
Show file tree
Hide file tree
Showing 25 changed files with 626 additions and 274 deletions.
16 changes: 16 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# for now, it is used to speed up wasi-nn tests only.
# you shall adapt below rules to incoming requirements

build
*/build
*/*/build
*/*/*/build
*/*/*/*/build
*/*/*/*/*/build
*/*/*/*/*/*/build
.*

core/deps
!core/deps/tensorflow-src
samples
tests
4 changes: 4 additions & 0 deletions build-scripts/config_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ if (WAMR_BUILD_SPEC_TEST EQUAL 1)
add_definitions (-DWASM_ENABLE_SPEC_TEST=1)
message (" spec test compatible mode is on")
endif ()
if (WAMR_BUILD_WASI_TEST EQUAL 1)
add_definitions (-DWASM_ENABLE_WASI_TEST=1)
message (" wasi test compatible mode is on")
endif ()
if (NOT DEFINED WAMR_BUILD_BULK_MEMORY)
# Enable bulk memory by default
set (WAMR_BUILD_BULK_MEMORY 1)
Expand Down
4 changes: 4 additions & 0 deletions core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@
#define WASM_ENABLE_SPEC_TEST 0
#endif

#ifndef WASM_ENABLE_WASI_TEST
#define WASM_ENABLE_WASI_TEST 0
#endif

/* Global heap pool size in bytes */
#ifndef WASM_GLOBAL_HEAP_SIZE
#define WASM_GLOBAL_HEAP_SIZE (10 * 1024 * 1024)
Expand Down
64 changes: 48 additions & 16 deletions core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,15 +591,17 @@ str2uint64(const char *buf, uint64 *p_res);

#if WASM_ENABLE_MULTI_MODULE != 0
static void *
aot_loader_resolve_function(const char *module_name, const char *function_name,
aot_loader_resolve_function(const AOTModule *module, const char *function_name,
const AOTFuncType *expected_function_type,
char *error_buf, uint32 error_buf_size)
char *error_buf, uint32 error_buf_size);

static void *
aot_loader_resolve_function_ex(const char *module_name,
const char *function_name,
const AOTFuncType *expected_function_type,
char *error_buf, uint32 error_buf_size)
{
WASMModuleCommon *module_reg;
void *function = NULL;
AOTExport *export = NULL;
AOTModule *module = NULL;
AOTFuncType *target_function_type = NULL;

module_reg = wasm_runtime_find_module_registered(module_name);
if (!module_reg || module_reg->module_type != Wasm_Module_AoT) {
Expand All @@ -608,10 +610,23 @@ aot_loader_resolve_function(const char *module_name, const char *function_name,
set_error_buf(error_buf, error_buf_size, "unknown import");
return NULL;
}
return aot_loader_resolve_function((AOTModule *)module_reg, function_name,
expected_function_type, error_buf,
error_buf_size);
}

module = (AOTModule *)module_reg;
export = loader_find_export(module_reg, module_name, function_name,
EXPORT_KIND_FUNC, error_buf, error_buf_size);
static void *
aot_loader_resolve_function(const AOTModule *module, const char *function_name,
const AOTFuncType *expected_function_type,
char *error_buf, uint32 error_buf_size)
{
void *function = NULL;
AOTExport *export = NULL;
AOTFuncType *target_function_type = NULL;

export = loader_find_export((WASMModuleCommon *)module, module->name,
function_name, EXPORT_KIND_FUNC, error_buf,
error_buf_size);
if (!export) {
return NULL;
}
Expand All @@ -633,7 +648,7 @@ aot_loader_resolve_function(const char *module_name, const char *function_name,
if (!wasm_type_equal((WASMType *)expected_function_type,
(WASMType *)target_function_type, module->types,
module->type_count)) {
LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
LOG_DEBUG("%s.%s failed the type check", module->name, function_name);
set_error_buf(error_buf, error_buf_size, "incompatible import type");
return NULL;
}
Expand All @@ -660,15 +675,18 @@ load_native_symbol_section(const uint8 *buf, const uint8 *buf_end,
read_uint32(p, p_end, cnt);

if (cnt > 0) {
module->native_symbol_list = wasm_runtime_malloc(cnt * sizeof(void *));
uint64 list_size = cnt * (uint64)sizeof(void *);
module->native_symbol_list =
loader_malloc(list_size, error_buf, error_buf_size);
if (module->native_symbol_list == NULL) {
set_error_buf(error_buf, error_buf_size,
"malloc native symbol list failed");
goto fail;
}

for (i = cnt - 1; i >= 0; i--) {
read_string(p, p_end, symbol);
if (!strlen(symbol))
continue;

if (!strncmp(symbol, "f32#", 4) || !strncmp(symbol, "i32#", 4)) {
uint32 u32;
/* Resolve the raw int bits of f32 const */
Expand Down Expand Up @@ -2079,6 +2097,10 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
read_string(buf, buf_end, import_globals[i].module_name);
read_string(buf, buf_end, import_globals[i].global_name);

if (!is_valid_value_type(import_globals[i].type.val_type)) {
return false;
}

#if WASM_ENABLE_LIBC_BUILTIN != 0
if (wasm_native_lookup_libc_builtin_global(
import_globals[i].module_name, import_globals[i].global_name,
Expand Down Expand Up @@ -2167,6 +2189,9 @@ load_globals(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
read_uint8(buf, buf_end, globals[i].type.val_type);
read_uint8(buf, buf_end, globals[i].type.is_mutable);

if (!is_valid_value_type(globals[i].type.val_type))
return false;

buf = align_ptr(buf, 4);

if (!load_init_expr(&buf, buf_end, module, &globals[i].init_expr,
Expand Down Expand Up @@ -2254,17 +2279,24 @@ load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
&import_funcs[i].signature, &import_funcs[i].attachment,
&import_funcs[i].call_conv_raw);
if (!linked_func) {
sub_module = NULL;
if (!wasm_runtime_is_built_in_module(module_name)) {
sub_module = (AOTModule *)wasm_runtime_load_depended_module(
(WASMModuleCommon *)module, module_name, error_buf,
error_buf_size);
if (!sub_module) {
LOG_ERROR("failed to load sub module: %s", error_buf);
return false;
}
}
linked_func = aot_loader_resolve_function(
module_name, field_name, declare_func_type, error_buf,
error_buf_size);
if (!sub_module)
linked_func = aot_loader_resolve_function_ex(
module_name, field_name, declare_func_type, error_buf,
error_buf_size);
else
linked_func = aot_loader_resolve_function(
sub_module, field_name, declare_func_type, error_buf,
error_buf_size);
}
import_funcs[i].func_ptr_linked = linked_func;
import_funcs[i].func_type = declare_func_type;
Expand Down
48 changes: 30 additions & 18 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,16 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,

#if WASM_ENABLE_MULTI_MODULE != 0
extra->sub_module_inst_list = &extra->sub_module_inst_list_head;

/* Allocate memory for import_func_module_insts*/
if (module->import_func_count > 0
&& !(extra->import_func_module_insts =
runtime_malloc((uint64)module->import_func_count
* sizeof(WASMModuleInstanceCommon *),
error_buf, error_buf_size))) {
goto fail;
}

ret = wasm_runtime_sub_module_instantiate(
(WASMModuleCommon *)module, (WASMModuleInstanceCommon *)module_inst,
stack_size, heap_size, max_memory_pages, error_buf, error_buf_size);
Expand Down Expand Up @@ -1980,6 +1990,8 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
#if WASM_ENABLE_MULTI_MODULE != 0
wasm_runtime_sub_module_deinstantiate(
(WASMModuleInstanceCommon *)module_inst);
if (extra->import_func_module_insts)
wasm_runtime_free(extra->import_func_module_insts);
#endif

if (module_inst->tables)
Expand Down Expand Up @@ -2835,10 +2847,6 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
void *attachment;
char buf[96];
bool ret = false;
#if WASM_ENABLE_MULTI_MODULE != 0
bh_list *sub_module_list_node = NULL;
const char *sub_inst_name = NULL;
#endif
bh_assert(func_idx < aot_module->import_func_count);

import_func = aot_module->import_funcs + func_idx;
Expand All @@ -2863,30 +2871,34 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
else if (!import_func->call_conv_raw) {
signature = import_func->signature;
#if WASM_ENABLE_MULTI_MODULE != 0
sub_module_list_node =
((AOTModuleInstanceExtra *)module_inst->e)->sub_module_inst_list;
sub_module_list_node = bh_list_first_elem(sub_module_list_node);
while (sub_module_list_node) {
sub_inst_name =
((AOTSubModInstNode *)sub_module_list_node)->module_name;
if (strcmp(sub_inst_name, import_func->module_name) == 0) {
exec_env = wasm_runtime_get_exec_env_singleton(
(WASMModuleInstanceCommon *)((AOTSubModInstNode *)
sub_module_list_node)
->module_inst);
break;
}
sub_module_list_node = bh_list_elem_next(sub_module_list_node);
WASMModuleInstanceCommon *sub_inst = NULL;
if ((sub_inst = ((AOTModuleInstanceExtra *)module_inst->e)
->import_func_module_insts[func_idx])) {
exec_env = wasm_runtime_get_exec_env_singleton(sub_inst);
}
if (exec_env == NULL) {
wasm_runtime_set_exception((WASMModuleInstanceCommon *)module_inst,
"create singleton exec_env failed");
goto fail;
}
#if WASM_ENABLE_AOT_STACK_FRAME != 0
struct WASMInterpFrame *prev_frame = exec_env->cur_frame;

if (!aot_alloc_frame(exec_env, func_idx)) {
goto fail;
}
#endif
#endif /* WASM_ENABLE_MULTI_MODULE != 0 */
ret =
wasm_runtime_invoke_native(exec_env, func_ptr, func_type, signature,
attachment, argv, argc, argv);
#if WASM_ENABLE_MULTI_MODULE != 0 && WASM_ENABLE_AOT_STACK_FRAME != 0
/* Free all frames allocated, note that some frames
may be allocated in AOT code and haven't been
freed if exception occurred */
while (exec_env->cur_frame != prev_frame)
aot_free_frame(exec_env);
#endif
}
else {
signature = import_func->signature;
Expand Down
1 change: 1 addition & 0 deletions core/iwasm/aot/aot_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ typedef struct AOTModuleInstanceExtra {
#if WASM_ENABLE_MULTI_MODULE != 0
bh_list sub_module_inst_list_head;
bh_list *sub_module_inst_list;
WASMModuleInstanceCommon **import_func_module_insts;
#endif
} AOTModuleInstanceExtra;

Expand Down
20 changes: 20 additions & 0 deletions core/iwasm/common/wasm_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,26 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
return ret;
}

bool
wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module_inst,
uint32_t inc_page_count)
{
#if WASM_ENABLE_AOT != 0
if (module_inst->module_type == Wasm_Module_AoT) {
return aot_enlarge_memory((AOTModuleInstance *)module_inst,
inc_page_count);
}
#endif
#if WASM_ENABLE_INTERP != 0
if (module_inst->module_type == Wasm_Module_Bytecode) {
return wasm_enlarge_memory((WASMModuleInstance *)module_inst,
inc_page_count);
}
#endif

return false;
}

void
wasm_runtime_set_enlarge_mem_error_callback(
const enlarge_memory_error_callback_t callback, void *user_data)
Expand Down
29 changes: 29 additions & 0 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,9 @@ wasm_runtime_is_built_in_module(const char *module_name)
|| !strcmp("wasi_snapshot_preview1", module_name)
#if WASM_ENABLE_SPEC_TEST != 0
|| !strcmp("spectest", module_name)
#endif
#if WASM_ENABLE_WASI_TEST != 0
|| !strcmp("foo", module_name)
#endif
|| !strcmp("", module_name));
}
Expand Down Expand Up @@ -7322,6 +7325,32 @@ wasm_runtime_sub_module_instantiate(WASMModuleCommon *module,
(WASMModuleInstance *)sub_module_inst;
sub_module_inst_list_node->module_name =
sub_module_list_node->module_name;

#if WASM_ENABLE_AOT != 0
if (module_inst->module_type == Wasm_Module_AoT) {
AOTModuleInstance *aot_module_inst =
(AOTModuleInstance *)module_inst;
AOTModule *aot_module = (AOTModule *)module;
AOTModuleInstanceExtra *aot_extra =
(AOTModuleInstanceExtra *)aot_module_inst->e;
uint32 i;
AOTImportFunc *import_func;
for (i = 0; i < aot_module->import_func_count; i++) {
if (aot_extra->import_func_module_insts[i])
continue;

import_func = &aot_module->import_funcs[i];
if (strcmp(sub_module_inst_list_node->module_name,
import_func->module_name)
== 0) {
aot_extra->import_func_module_insts[i] =
(WASMModuleInstanceCommon *)
sub_module_inst_list_node->module_inst;
}
}
}
#endif

bh_list_status ret =
bh_list_insert(sub_module_inst_list, sub_module_inst_list_node);
bh_assert(BH_LIST_SUCCESS == ret);
Expand Down
8 changes: 8 additions & 0 deletions core/iwasm/compilation/aot_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,14 @@ set_local_gc_ref(AOTCompFrame *frame, int n, LLVMValueRef value, uint8 ref_type)
#define PUSH_PAGE_COUNT(v) \
PUSH(v, MEMORY64_COND_VALUE(VALUE_TYPE_I64, VALUE_TYPE_I32))

#define SET_CONST(v) \
do { \
AOTValue *aot_value = \
func_ctx->block_stack.block_list_end->value_stack.value_list_end; \
aot_value->is_const = true; \
aot_value->const_value = (v); \
} while (0)

#define TO_LLVM_TYPE(wasm_type) \
wasm_type_to_llvm_type(comp_ctx, &comp_ctx->basic_types, wasm_type)

Expand Down
Loading

0 comments on commit c867cf8

Please sign in to comment.