|
|
|
|
@ -398,7 +398,7 @@ wasm_engine_new_internal(mem_alloc_type_t type, const MemAllocOption *opts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* global engine instance */
|
|
|
|
|
static wasm_engine_t *singleton_engine = NULL;
|
|
|
|
|
static wasm_engine_t *singleton_engine;
|
|
|
|
|
#ifdef os_thread_local_attribute
|
|
|
|
|
/* categorize wasm_store_t as threads*/
|
|
|
|
|
static os_thread_local_attribute unsigned thread_local_stores_num = 0;
|
|
|
|
|
@ -1458,6 +1458,30 @@ wasm_importtype_type(const wasm_importtype_t *import_type)
|
|
|
|
|
return import_type->extern_type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
wasm_importtype_is_linked(const wasm_importtype_t *import_type)
|
|
|
|
|
{
|
|
|
|
|
if (!import_type)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const wasm_name_t *module_name = wasm_importtype_module(import_type);
|
|
|
|
|
const wasm_name_t *field_name = wasm_importtype_name(import_type);
|
|
|
|
|
|
|
|
|
|
switch (wasm_externtype_kind(wasm_importtype_type(import_type))) {
|
|
|
|
|
case WASM_EXTERN_FUNC:
|
|
|
|
|
return wasm_runtime_is_import_func_linked(module_name->data,
|
|
|
|
|
field_name->data);
|
|
|
|
|
case WASM_EXTERN_GLOBAL:
|
|
|
|
|
return wasm_runtime_is_import_global_linked(module_name->data,
|
|
|
|
|
field_name->data);
|
|
|
|
|
case WASM_EXTERN_MEMORY:
|
|
|
|
|
case WASM_EXTERN_TABLE:
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
own wasm_exporttype_t *
|
|
|
|
|
wasm_exporttype_new(own wasm_byte_vec_t *name,
|
|
|
|
|
own wasm_externtype_t *extern_type)
|
|
|
|
|
@ -2537,12 +2561,12 @@ wasm_module_imports(const wasm_module_t *module, own wasm_importtype_vec_t *out)
|
|
|
|
|
|
|
|
|
|
bh_assert(extern_type);
|
|
|
|
|
|
|
|
|
|
wasm_name_new_from_string(&module_name, module_name_rt);
|
|
|
|
|
wasm_name_new_from_string_nt(&module_name, module_name_rt);
|
|
|
|
|
if (strlen(module_name_rt) && !module_name.data) {
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wasm_name_new_from_string(&name, field_name_rt);
|
|
|
|
|
wasm_name_new_from_string_nt(&name, field_name_rt);
|
|
|
|
|
if (strlen(field_name_rt) && !name.data) {
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
@ -2622,7 +2646,7 @@ wasm_module_exports(const wasm_module_t *module, wasm_exporttype_vec_t *out)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* byte* -> wasm_byte_vec_t */
|
|
|
|
|
wasm_name_new_from_string(&name, export->name);
|
|
|
|
|
wasm_name_new_from_string_nt(&name, export->name);
|
|
|
|
|
if (strlen(export->name) && !name.data) {
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
@ -3008,6 +3032,20 @@ failed:
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static wasm_func_t *
|
|
|
|
|
wasm_func_new_empty(wasm_store_t *store)
|
|
|
|
|
{
|
|
|
|
|
wasm_func_t *func = NULL;
|
|
|
|
|
|
|
|
|
|
if (!(func = malloc_internal(sizeof(wasm_func_t))))
|
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
|
|
func->store = store;
|
|
|
|
|
func->kind = WASM_EXTERN_FUNC;
|
|
|
|
|
|
|
|
|
|
RETURN_OBJ(func, wasm_func_delete)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
wasm_func_delete(wasm_func_t *func)
|
|
|
|
|
{
|
|
|
|
|
@ -3211,7 +3249,8 @@ wasm_func_call(const wasm_func_t *func, const wasm_val_vec_t *params,
|
|
|
|
|
wasm_name_t message = { 0 };
|
|
|
|
|
wasm_trap_t *trap;
|
|
|
|
|
|
|
|
|
|
wasm_name_new_from_string(&message, "failed to call unlinked function");
|
|
|
|
|
wasm_name_new_from_string_nt(&message,
|
|
|
|
|
"failed to call unlinked function");
|
|
|
|
|
trap = wasm_trap_new(func->store, &message);
|
|
|
|
|
wasm_byte_vec_delete(&message);
|
|
|
|
|
|
|
|
|
|
@ -3371,6 +3410,25 @@ failed:
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static wasm_global_t *
|
|
|
|
|
wasm_global_new_empty(wasm_store_t *store)
|
|
|
|
|
{
|
|
|
|
|
wasm_global_t *global = NULL;
|
|
|
|
|
|
|
|
|
|
global = malloc_internal(sizeof(wasm_global_t));
|
|
|
|
|
if (!global)
|
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
|
|
global->store = store;
|
|
|
|
|
global->kind = WASM_EXTERN_GLOBAL;
|
|
|
|
|
|
|
|
|
|
return global;
|
|
|
|
|
failed:
|
|
|
|
|
LOG_DEBUG("%s failed", __FUNCTION__);
|
|
|
|
|
wasm_global_delete(global);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* almost same with wasm_global_new */
|
|
|
|
|
wasm_global_t *
|
|
|
|
|
wasm_global_copy(const wasm_global_t *src)
|
|
|
|
|
@ -4288,6 +4346,11 @@ interp_link_func(const wasm_instance_t *inst, const WASMModule *module_interp,
|
|
|
|
|
|
|
|
|
|
imported_func_interp = module_interp->import_functions + func_idx_rt;
|
|
|
|
|
bh_assert(imported_func_interp);
|
|
|
|
|
bh_assert(imported_func_interp->kind == IMPORT_KIND_FUNC);
|
|
|
|
|
|
|
|
|
|
/* it is a placeholder and let's skip it*/
|
|
|
|
|
if (!import->type)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
/* type comparison */
|
|
|
|
|
if (!wasm_functype_same_internal(
|
|
|
|
|
@ -4302,6 +4365,8 @@ interp_link_func(const wasm_instance_t *inst, const WASMModule *module_interp,
|
|
|
|
|
imported_func_interp->u.function.func_ptr_linked = import->u.cb_env.cb;
|
|
|
|
|
else
|
|
|
|
|
imported_func_interp->u.function.func_ptr_linked = import->u.cb;
|
|
|
|
|
bh_assert(imported_func_interp->u.function.func_ptr_linked);
|
|
|
|
|
|
|
|
|
|
import->func_idx_rt = func_idx_rt;
|
|
|
|
|
|
|
|
|
|
(void)inst;
|
|
|
|
|
@ -4320,12 +4385,19 @@ interp_link_global(const WASMModule *module_interp, uint16 global_idx_rt,
|
|
|
|
|
|
|
|
|
|
imported_global_interp = module_interp->import_globals + global_idx_rt;
|
|
|
|
|
bh_assert(imported_global_interp);
|
|
|
|
|
bh_assert(imported_global_interp->kind == IMPORT_KIND_GLOBAL);
|
|
|
|
|
|
|
|
|
|
/* it is a placeholder and let's skip it*/
|
|
|
|
|
if (!import->type)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
/* type comparison */
|
|
|
|
|
if (!cmp_val_kind_with_val_type(wasm_valtype_kind(import->type->val_type),
|
|
|
|
|
imported_global_interp->u.global.type))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
/* set init value */
|
|
|
|
|
bh_assert(import->init);
|
|
|
|
|
switch (wasm_valtype_kind(import->type->val_type)) {
|
|
|
|
|
case WASM_I32:
|
|
|
|
|
imported_global_interp->u.global.global_data_linked.i32 =
|
|
|
|
|
@ -4352,58 +4424,6 @@ interp_link_global(const WASMModule *module_interp, uint16 global_idx_rt,
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
interp_link(const wasm_instance_t *inst, const WASMModule *module_interp,
|
|
|
|
|
wasm_extern_t *imports[])
|
|
|
|
|
{
|
|
|
|
|
uint32 i = 0;
|
|
|
|
|
uint32 import_func_i = 0;
|
|
|
|
|
uint32 import_global_i = 0;
|
|
|
|
|
|
|
|
|
|
bh_assert(inst && module_interp && imports);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < module_interp->import_count; ++i) {
|
|
|
|
|
wasm_extern_t *import = imports[i];
|
|
|
|
|
WASMImport *import_rt = module_interp->imports + i;
|
|
|
|
|
|
|
|
|
|
switch (import_rt->kind) {
|
|
|
|
|
case IMPORT_KIND_FUNC:
|
|
|
|
|
{
|
|
|
|
|
if (!interp_link_func(inst, module_interp, import_func_i,
|
|
|
|
|
wasm_extern_as_func(import))) {
|
|
|
|
|
LOG_WARNING("link #%d function failed", import_func_i);
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
import_func_i++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case IMPORT_KIND_GLOBAL:
|
|
|
|
|
{
|
|
|
|
|
if (!interp_link_global(module_interp, import_global_i,
|
|
|
|
|
wasm_extern_as_global(import))) {
|
|
|
|
|
LOG_WARNING("link #%d global failed", import_global_i);
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
import_global_i++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case IMPORT_KIND_MEMORY:
|
|
|
|
|
case IMPORT_KIND_TABLE:
|
|
|
|
|
default:
|
|
|
|
|
ASSERT_NOT_IMPLEMENTED();
|
|
|
|
|
LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
|
|
|
|
|
import_rt->kind);
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
failed:
|
|
|
|
|
LOG_DEBUG("%s failed", __FUNCTION__);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
interp_process_export(wasm_store_t *store,
|
|
|
|
|
const WASMModuleInstance *inst_interp,
|
|
|
|
|
@ -4503,6 +4523,10 @@ aot_link_func(const wasm_instance_t *inst, const AOTModule *module_aot,
|
|
|
|
|
import_aot_func = module_aot->import_funcs + import_func_idx_rt;
|
|
|
|
|
bh_assert(import_aot_func);
|
|
|
|
|
|
|
|
|
|
/* it is a placeholder and let's skip it*/
|
|
|
|
|
if (!import->type)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
/* type comparison */
|
|
|
|
|
if (!wasm_functype_same_internal(import->type, import_aot_func->func_type))
|
|
|
|
|
return false;
|
|
|
|
|
@ -4515,6 +4539,8 @@ aot_link_func(const wasm_instance_t *inst, const AOTModule *module_aot,
|
|
|
|
|
import_aot_func->func_ptr_linked = import->u.cb_env.cb;
|
|
|
|
|
else
|
|
|
|
|
import_aot_func->func_ptr_linked = import->u.cb;
|
|
|
|
|
bh_assert(import_aot_func->func_ptr_linked);
|
|
|
|
|
|
|
|
|
|
import->func_idx_rt = import_func_idx_rt;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
@ -4532,6 +4558,10 @@ aot_link_global(const AOTModule *module_aot, uint16 global_idx_rt,
|
|
|
|
|
import_aot_global = module_aot->import_globals + global_idx_rt;
|
|
|
|
|
bh_assert(import_aot_global);
|
|
|
|
|
|
|
|
|
|
/* it is a placeholder and let's skip it*/
|
|
|
|
|
if (!import->type)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
val_type = wasm_globaltype_content(import->type);
|
|
|
|
|
bh_assert(val_type);
|
|
|
|
|
|
|
|
|
|
@ -4539,6 +4569,7 @@ aot_link_global(const AOTModule *module_aot, uint16 global_idx_rt,
|
|
|
|
|
import_aot_global->type))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
bh_assert(import->init);
|
|
|
|
|
switch (wasm_valtype_kind(val_type)) {
|
|
|
|
|
case WASM_I32:
|
|
|
|
|
import_aot_global->global_data_linked.i32 = import->init->of.i32;
|
|
|
|
|
@ -4559,62 +4590,6 @@ aot_link_global(const AOTModule *module_aot, uint16 global_idx_rt,
|
|
|
|
|
import->global_idx_rt = global_idx_rt;
|
|
|
|
|
import_aot_global->is_linked = true;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
failed:
|
|
|
|
|
LOG_DEBUG("%s failed", __FUNCTION__);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
aot_link(const wasm_instance_t *inst, const AOTModule *module_aot,
|
|
|
|
|
wasm_extern_t *imports[])
|
|
|
|
|
{
|
|
|
|
|
uint32 i = 0;
|
|
|
|
|
uint32 import_func_i = 0;
|
|
|
|
|
uint32 import_global_i = 0;
|
|
|
|
|
wasm_extern_t *import = NULL;
|
|
|
|
|
wasm_func_t *func = NULL;
|
|
|
|
|
wasm_global_t *global = NULL;
|
|
|
|
|
|
|
|
|
|
bh_assert(inst && module_aot && imports);
|
|
|
|
|
|
|
|
|
|
while (import_func_i < module_aot->import_func_count
|
|
|
|
|
|| import_global_i < module_aot->import_global_count) {
|
|
|
|
|
import = imports[i++];
|
|
|
|
|
|
|
|
|
|
bh_assert(import);
|
|
|
|
|
|
|
|
|
|
switch (wasm_extern_kind(import)) {
|
|
|
|
|
case WASM_EXTERN_FUNC:
|
|
|
|
|
bh_assert(import_func_i < module_aot->import_func_count);
|
|
|
|
|
func = wasm_extern_as_func((wasm_extern_t *)import);
|
|
|
|
|
if (!aot_link_func(inst, module_aot, import_func_i, func)) {
|
|
|
|
|
LOG_WARNING("link #%d function failed", import_func_i);
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
import_func_i++;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case WASM_EXTERN_GLOBAL:
|
|
|
|
|
bh_assert(import_global_i < module_aot->import_global_count);
|
|
|
|
|
global = wasm_extern_as_global((wasm_extern_t *)import);
|
|
|
|
|
if (!aot_link_global(module_aot, import_global_i, global)) {
|
|
|
|
|
LOG_WARNING("link #%d global failed", import_global_i);
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
import_global_i++;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case WASM_EXTERN_MEMORY:
|
|
|
|
|
case WASM_EXTERN_TABLE:
|
|
|
|
|
default:
|
|
|
|
|
ASSERT_NOT_IMPLEMENTED();
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
failed:
|
|
|
|
|
LOG_DEBUG("%s failed", __FUNCTION__);
|
|
|
|
|
return false;
|
|
|
|
|
@ -4695,7 +4670,7 @@ aot_process_export(wasm_store_t *store, const AOTModuleInstance *inst_aot,
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wasm_name_new_from_string(external->name, export->name);
|
|
|
|
|
wasm_name_new_from_string_nt(external->name, export->name);
|
|
|
|
|
if (strlen(export->name) && !external->name->data) {
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
@ -4713,6 +4688,95 @@ failed:
|
|
|
|
|
}
|
|
|
|
|
#endif /* WASM_ENABLE_AOT */
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
do_link(const wasm_instance_t *inst, const wasm_module_t *module,
|
|
|
|
|
const wasm_extern_vec_t *imports)
|
|
|
|
|
{
|
|
|
|
|
uint32 i, import_func_i, import_global_i;
|
|
|
|
|
|
|
|
|
|
bh_assert(inst && module);
|
|
|
|
|
|
|
|
|
|
/* we have run a module_type check before. */
|
|
|
|
|
|
|
|
|
|
for (i = 0, import_func_i = 0, import_global_i = 0; i < imports->num_elems;
|
|
|
|
|
i++) {
|
|
|
|
|
wasm_extern_t *import = imports->data[i];
|
|
|
|
|
|
|
|
|
|
if (!import) {
|
|
|
|
|
LOG_ERROR("imports[%d] is NULL and it is fatal\n", i);
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (wasm_extern_kind(import)) {
|
|
|
|
|
case WASM_EXTERN_FUNC:
|
|
|
|
|
{
|
|
|
|
|
bool ret = false;
|
|
|
|
|
#if WASM_ENABLE_INTERP != 0
|
|
|
|
|
if ((*module)->module_type == Wasm_Module_Bytecode) {
|
|
|
|
|
ret = interp_link_func(inst, MODULE_INTERP(module),
|
|
|
|
|
import_func_i,
|
|
|
|
|
wasm_extern_as_func(import));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#if WASM_ENABLE_AOT != 0
|
|
|
|
|
if ((*module)->module_type == Wasm_Module_AoT) {
|
|
|
|
|
ret = aot_link_func(inst, MODULE_AOT(module), import_func_i,
|
|
|
|
|
wasm_extern_as_func(import));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if (!ret) {
|
|
|
|
|
LOG_WARNING("link function #%d failed", import_func_i);
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
import_func_i++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case WASM_EXTERN_GLOBAL:
|
|
|
|
|
{
|
|
|
|
|
bool ret = false;
|
|
|
|
|
#if WASM_ENABLE_INTERP != 0
|
|
|
|
|
if ((*module)->module_type == Wasm_Module_Bytecode) {
|
|
|
|
|
ret = interp_link_global(MODULE_INTERP(module),
|
|
|
|
|
import_global_i,
|
|
|
|
|
wasm_extern_as_global(import));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#if WASM_ENABLE_AOT != 0
|
|
|
|
|
if ((*module)->module_type == Wasm_Module_AoT) {
|
|
|
|
|
ret = aot_link_global(MODULE_AOT(module), import_global_i,
|
|
|
|
|
wasm_extern_as_global(import));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if (!ret) {
|
|
|
|
|
LOG_WARNING("link global #%d failed", import_global_i);
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
import_global_i++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case WASM_EXTERN_MEMORY:
|
|
|
|
|
case WASM_EXTERN_TABLE:
|
|
|
|
|
{
|
|
|
|
|
LOG_WARNING("doesn't support import memories and tables for "
|
|
|
|
|
"now, ignore them");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
UNREACHABLE();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
failed:
|
|
|
|
|
LOG_DEBUG("%s failed", __FUNCTION__);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wasm_instance_t *
|
|
|
|
|
wasm_instance_new(wasm_store_t *store, const wasm_module_t *module,
|
|
|
|
|
const wasm_extern_vec_t *imports, own wasm_trap_t **trap)
|
|
|
|
|
@ -4721,57 +4785,6 @@ wasm_instance_new(wasm_store_t *store, const wasm_module_t *module,
|
|
|
|
|
KILOBYTE(32), KILOBYTE(32));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
compare_imports(const wasm_module_t *module, const wasm_extern_vec_t *imports)
|
|
|
|
|
{
|
|
|
|
|
unsigned import_func_count = 0;
|
|
|
|
|
unsigned import_global_count = 0;
|
|
|
|
|
unsigned import_memory_count = 0;
|
|
|
|
|
unsigned import_table_count = 0;
|
|
|
|
|
unsigned i = 0;
|
|
|
|
|
|
|
|
|
|
for (i = 0; imports && i < imports->num_elems; i++) {
|
|
|
|
|
wasm_extern_t *import = imports->data[i];
|
|
|
|
|
switch (wasm_extern_kind(import)) {
|
|
|
|
|
case WASM_EXTERN_FUNC:
|
|
|
|
|
import_func_count++;
|
|
|
|
|
break;
|
|
|
|
|
case WASM_EXTERN_GLOBAL:
|
|
|
|
|
import_global_count++;
|
|
|
|
|
break;
|
|
|
|
|
case WASM_EXTERN_MEMORY:
|
|
|
|
|
import_memory_count++;
|
|
|
|
|
break;
|
|
|
|
|
case WASM_EXTERN_TABLE:
|
|
|
|
|
import_table_count++;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
UNREACHABLE();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if WASM_ENABLE_INTERP != 0
|
|
|
|
|
if ((*module)->module_type == Wasm_Module_Bytecode)
|
|
|
|
|
return import_func_count == MODULE_INTERP(module)->import_function_count
|
|
|
|
|
&& import_global_count
|
|
|
|
|
== MODULE_INTERP(module)->import_global_count
|
|
|
|
|
&& import_memory_count
|
|
|
|
|
== MODULE_INTERP(module)->import_memory_count
|
|
|
|
|
&& import_table_count
|
|
|
|
|
== MODULE_INTERP(module)->import_table_count;
|
|
|
|
|
#endif
|
|
|
|
|
#if WASM_ENABLE_AOT != 0
|
|
|
|
|
if ((*module)->module_type == Wasm_Module_AoT)
|
|
|
|
|
return import_func_count == MODULE_AOT(module)->import_func_count
|
|
|
|
|
&& import_global_count == MODULE_AOT(module)->import_global_count
|
|
|
|
|
&& import_memory_count == MODULE_AOT(module)->import_memory_count
|
|
|
|
|
&& import_table_count == MODULE_AOT(module)->import_table_count;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wasm_instance_t *
|
|
|
|
|
wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
|
|
|
|
const wasm_extern_vec_t *imports,
|
|
|
|
|
@ -4781,7 +4794,6 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
|
|
|
|
char sub_error_buf[128] = { 0 };
|
|
|
|
|
char error_buf[256] = { 0 };
|
|
|
|
|
wasm_instance_t *instance = NULL;
|
|
|
|
|
WASMModuleInstance *inst_rt;
|
|
|
|
|
CApiFuncImport *func_import = NULL, **p_func_imports = NULL;
|
|
|
|
|
uint32 i = 0, import_func_count = 0;
|
|
|
|
|
uint64 total_size;
|
|
|
|
|
@ -4792,11 +4804,9 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
|
|
|
|
if (!module)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if (!compare_imports(module, imports)) {
|
|
|
|
|
snprintf(sub_error_buf, sizeof(sub_error_buf),
|
|
|
|
|
"Failed to match imports");
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* will do the check at the end of wasm_runtime_instantiate
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
WASM_C_DUMP_PROC_MEM();
|
|
|
|
|
|
|
|
|
|
@ -4807,43 +4817,17 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* link module and imports */
|
|
|
|
|
if (imports && imports->num_elems) {
|
|
|
|
|
bool link = false;
|
|
|
|
|
#if WASM_ENABLE_INTERP != 0
|
|
|
|
|
if ((*module)->module_type == Wasm_Module_Bytecode) {
|
|
|
|
|
if (!interp_link(instance, MODULE_INTERP(module),
|
|
|
|
|
(wasm_extern_t **)imports->data)) {
|
|
|
|
|
snprintf(sub_error_buf, sizeof(sub_error_buf),
|
|
|
|
|
"Failed to validate imports");
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
link = true;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if WASM_ENABLE_AOT != 0
|
|
|
|
|
if ((*module)->module_type == Wasm_Module_AoT) {
|
|
|
|
|
if (!aot_link(instance, MODULE_AOT(module),
|
|
|
|
|
(wasm_extern_t **)imports->data)) {
|
|
|
|
|
snprintf(sub_error_buf, sizeof(sub_error_buf),
|
|
|
|
|
"Failed to validate imports");
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
link = true;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* a wrong combination of module filetype and compilation flags
|
|
|
|
|
* also leads to below branch
|
|
|
|
|
*/
|
|
|
|
|
if (!link) {
|
|
|
|
|
/* executes the instantiate-time linking if provided */
|
|
|
|
|
if (imports) {
|
|
|
|
|
if (!do_link(instance, module, imports)) {
|
|
|
|
|
snprintf(sub_error_buf, sizeof(sub_error_buf),
|
|
|
|
|
"Failed to verify import count");
|
|
|
|
|
"Failed to validate imports");
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* will do the linking result check at the end of wasm_runtime_instantiate
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
instance->inst_comm_rt = wasm_runtime_instantiate(
|
|
|
|
|
*module, stack_size, heap_size, sub_error_buf, sizeof(sub_error_buf));
|
|
|
|
|
@ -4858,18 +4842,22 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* create the c-api func import list */
|
|
|
|
|
inst_rt = (WASMModuleInstance *)instance->inst_comm_rt;
|
|
|
|
|
#if WASM_ENABLE_INTERP != 0
|
|
|
|
|
if (instance->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
|
|
|
|
|
p_func_imports = &inst_rt->e->c_api_func_imports;
|
|
|
|
|
import_func_count = inst_rt->module->import_function_count;
|
|
|
|
|
WASMModuleInstanceExtra *e =
|
|
|
|
|
((WASMModuleInstance *)instance->inst_comm_rt)->e;
|
|
|
|
|
p_func_imports = &(e->c_api_func_imports);
|
|
|
|
|
import_func_count = MODULE_INTERP(module)->import_function_count;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#if WASM_ENABLE_AOT != 0
|
|
|
|
|
if (instance->inst_comm_rt->module_type == Wasm_Module_AoT) {
|
|
|
|
|
p_func_imports =
|
|
|
|
|
&((AOTModuleInstanceExtra *)inst_rt->e)->c_api_func_imports;
|
|
|
|
|
import_func_count = ((AOTModule *)inst_rt->module)->import_func_count;
|
|
|
|
|
AOTModuleInstanceExtra *e =
|
|
|
|
|
(AOTModuleInstanceExtra *)((AOTModuleInstance *)
|
|
|
|
|
instance->inst_comm_rt)
|
|
|
|
|
->e;
|
|
|
|
|
p_func_imports = &(e->c_api_func_imports);
|
|
|
|
|
import_func_count = MODULE_AOT(module)->import_func_count;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
bh_assert(p_func_imports);
|
|
|
|
|
@ -4882,16 +4870,21 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* fill in c-api func import list */
|
|
|
|
|
/* fill in module_inst->e->c_api_func_imports */
|
|
|
|
|
for (i = 0; imports && i < imports->num_elems; i++) {
|
|
|
|
|
wasm_func_t *func_host;
|
|
|
|
|
wasm_extern_t *in;
|
|
|
|
|
wasm_func_t *func_host = NULL;
|
|
|
|
|
wasm_extern_t *in = imports->data[i];
|
|
|
|
|
bh_assert(in);
|
|
|
|
|
|
|
|
|
|
in = imports->data[i];
|
|
|
|
|
if (wasm_extern_kind(in) != WASM_EXTERN_FUNC)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
func_host = wasm_extern_as_func(in);
|
|
|
|
|
/* it is a placeholder and let's skip it*/
|
|
|
|
|
if (!func_host->type) {
|
|
|
|
|
func_import++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func_import->with_env_arg = func_host->with_env;
|
|
|
|
|
if (func_host->with_env) {
|
|
|
|
|
@ -4902,6 +4895,7 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
|
|
|
|
func_import->func_ptr_linked = func_host->u.cb;
|
|
|
|
|
func_import->env_arg = NULL;
|
|
|
|
|
}
|
|
|
|
|
bh_assert(func_import->func_ptr_linked);
|
|
|
|
|
|
|
|
|
|
func_import++;
|
|
|
|
|
}
|
|
|
|
|
@ -4909,6 +4903,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
|
|
|
|
/* fill with inst */
|
|
|
|
|
for (i = 0; imports && imports->data && i < imports->num_elems; ++i) {
|
|
|
|
|
wasm_extern_t *import = imports->data[i];
|
|
|
|
|
bh_assert(import);
|
|
|
|
|
|
|
|
|
|
switch (import->kind) {
|
|
|
|
|
case WASM_EXTERN_FUNC:
|
|
|
|
|
wasm_extern_as_func(import)->inst_comm_rt =
|
|
|
|
|
@ -5004,7 +5000,7 @@ failed:
|
|
|
|
|
sub_error_buf);
|
|
|
|
|
if (trap != NULL) {
|
|
|
|
|
wasm_message_t message = { 0 };
|
|
|
|
|
wasm_name_new_from_string(&message, error_buf);
|
|
|
|
|
wasm_name_new_from_string_nt(&message, error_buf);
|
|
|
|
|
*trap = wasm_trap_new(store, &message);
|
|
|
|
|
wasm_byte_vec_delete(&message);
|
|
|
|
|
}
|
|
|
|
|
@ -5204,3 +5200,16 @@ BASIC_FOUR_LIST(WASM_EXTERN_AS_OTHER_CONST)
|
|
|
|
|
|
|
|
|
|
BASIC_FOUR_LIST(WASM_OTHER_AS_EXTERN_CONST)
|
|
|
|
|
#undef WASM_OTHER_AS_EXTERN_CONST
|
|
|
|
|
|
|
|
|
|
wasm_extern_t *
|
|
|
|
|
wasm_extern_new_empty(wasm_store_t *store, wasm_externkind_t extern_kind)
|
|
|
|
|
{
|
|
|
|
|
if (extern_kind == WASM_EXTERN_FUNC)
|
|
|
|
|
return wasm_func_as_extern(wasm_func_new_empty(store));
|
|
|
|
|
|
|
|
|
|
if (extern_kind == WASM_EXTERN_GLOBAL)
|
|
|
|
|
return wasm_global_as_extern(wasm_global_new_empty(store));
|
|
|
|
|
|
|
|
|
|
LOG_ERROR("Don't support linking table and memory for now");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|