Merge main into dev/wasi_threads
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "bh_log.h"
|
||||
#include "wasm_c_api_internal.h"
|
||||
|
||||
#include "bh_assert.h"
|
||||
@ -275,10 +276,14 @@ WASM_DEFINE_VEC_OWN(store, wasm_store_delete)
|
||||
WASM_DEFINE_VEC_OWN(valtype, wasm_valtype_delete)
|
||||
|
||||
#ifndef NDEBUG
|
||||
#if WAMR_BUILD_MEMORY_PROFILING != 0
|
||||
#define WASM_C_DUMP_PROC_MEM() LOG_PROC_MEM()
|
||||
#else
|
||||
#define WASM_C_DUMP_PROC_MEM() (void)0
|
||||
#endif
|
||||
#else
|
||||
#define WASM_C_DUMP_PROC_MEM() (void)0
|
||||
#endif
|
||||
|
||||
/* Runtime Environment */
|
||||
own wasm_config_t *
|
||||
@ -1628,6 +1633,8 @@ wasm_val_to_rt_val(WASMModuleInstanceCommon *inst_comm_rt, uint8 val_type_rt,
|
||||
ret =
|
||||
wasm_externref_obj2ref(inst_comm_rt, v->of.ref, (uint32 *)data);
|
||||
break;
|
||||
#else
|
||||
(void)inst_comm_rt;
|
||||
#endif
|
||||
default:
|
||||
LOG_WARNING("unexpected value type %d", val_type_rt);
|
||||
@ -1907,6 +1914,9 @@ wasm_trap_new_internal(wasm_store_t *store,
|
||||
frame_instance;
|
||||
}
|
||||
}
|
||||
#else
|
||||
(void)store;
|
||||
(void)inst_comm_rt;
|
||||
#endif /* WASM_ENABLE_DUMP_CALL_STACK != 0 */
|
||||
|
||||
return trap;
|
||||
@ -2034,6 +2044,7 @@ wasm_foreign_new_internal(wasm_store_t *store, uint32 foreign_idx_rt,
|
||||
}
|
||||
|
||||
foreign->ref_cnt++;
|
||||
(void)inst_comm_rt;
|
||||
return foreign;
|
||||
}
|
||||
|
||||
@ -4291,6 +4302,7 @@ interp_link_func(const wasm_instance_t *inst, const WASMModule *module_interp,
|
||||
imported_func_interp->u.function.func_ptr_linked = import->u.cb;
|
||||
import->func_idx_rt = func_idx_rt;
|
||||
|
||||
(void)inst;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -4338,7 +4350,7 @@ interp_link_global(const WASMModule *module_interp, uint16 global_idx_rt,
|
||||
return true;
|
||||
}
|
||||
|
||||
static uint32
|
||||
static bool
|
||||
interp_link(const wasm_instance_t *inst, const WASMModule *module_interp,
|
||||
wasm_extern_t *imports[])
|
||||
{
|
||||
@ -4383,11 +4395,11 @@ interp_link(const wasm_instance_t *inst, const WASMModule *module_interp,
|
||||
}
|
||||
}
|
||||
|
||||
return i;
|
||||
return true;
|
||||
|
||||
failed:
|
||||
LOG_DEBUG("%s failed", __FUNCTION__);
|
||||
return (uint32)-1;
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool
|
||||
@ -4550,7 +4562,7 @@ failed:
|
||||
return false;
|
||||
}
|
||||
|
||||
static uint32
|
||||
static bool
|
||||
aot_link(const wasm_instance_t *inst, const AOTModule *module_aot,
|
||||
wasm_extern_t *imports[])
|
||||
{
|
||||
@ -4598,11 +4610,11 @@ aot_link(const wasm_instance_t *inst, const AOTModule *module_aot,
|
||||
}
|
||||
}
|
||||
|
||||
return i;
|
||||
return true;
|
||||
|
||||
failed:
|
||||
LOG_DEBUG("%s failed", __FUNCTION__);
|
||||
return (uint32)-1;
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool
|
||||
@ -4706,6 +4718,57 @@ 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,
|
||||
@ -4714,18 +4777,22 @@ 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 };
|
||||
bool import_count_verified = false;
|
||||
wasm_instance_t *instance = NULL;
|
||||
WASMModuleInstance *inst_rt;
|
||||
CApiFuncImport *func_import = NULL, **p_func_imports = NULL;
|
||||
uint32 i = 0, import_count = 0, import_func_count = 0;
|
||||
uint32 i = 0, import_func_count = 0;
|
||||
uint64 total_size;
|
||||
bool processed = false;
|
||||
bool build_exported = false;
|
||||
|
||||
bh_assert(singleton_engine);
|
||||
|
||||
if (!module) {
|
||||
if (!module)
|
||||
return NULL;
|
||||
|
||||
if (!compare_imports(module, imports)) {
|
||||
snprintf(sub_error_buf, sizeof(sub_error_buf),
|
||||
"Failed to match imports");
|
||||
goto failed;
|
||||
}
|
||||
|
||||
WASM_C_DUMP_PROC_MEM();
|
||||
@ -4739,43 +4806,28 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
||||
|
||||
/* link module and imports */
|
||||
if (imports && imports->num_elems) {
|
||||
bool link = false;
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if ((*module)->module_type == Wasm_Module_Bytecode) {
|
||||
import_count = MODULE_INTERP(module)->import_count;
|
||||
|
||||
if (import_count) {
|
||||
uint32 actual_link_import_count =
|
||||
interp_link(instance, MODULE_INTERP(module),
|
||||
(wasm_extern_t **)imports->data);
|
||||
/* make sure a complete import list */
|
||||
if ((int32)import_count < 0
|
||||
|| import_count != actual_link_import_count) {
|
||||
snprintf(sub_error_buf, sizeof(sub_error_buf),
|
||||
"Failed to validate imports");
|
||||
goto failed;
|
||||
}
|
||||
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;
|
||||
}
|
||||
import_count_verified = true;
|
||||
link = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if ((*module)->module_type == Wasm_Module_AoT) {
|
||||
import_count = MODULE_AOT(module)->import_func_count
|
||||
+ MODULE_AOT(module)->import_global_count
|
||||
+ MODULE_AOT(module)->import_memory_count
|
||||
+ MODULE_AOT(module)->import_table_count;
|
||||
|
||||
if (import_count) {
|
||||
import_count = aot_link(instance, MODULE_AOT(module),
|
||||
(wasm_extern_t **)imports->data);
|
||||
if ((int32)import_count < 0) {
|
||||
snprintf(sub_error_buf, sizeof(sub_error_buf),
|
||||
"Failed to validate imports");
|
||||
goto failed;
|
||||
}
|
||||
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;
|
||||
}
|
||||
import_count_verified = true;
|
||||
link = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -4783,7 +4835,7 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
||||
* a wrong combination of module filetype and compilation flags
|
||||
* also leads to below branch
|
||||
*/
|
||||
if (!import_count_verified) {
|
||||
if (!link) {
|
||||
snprintf(sub_error_buf, sizeof(sub_error_buf),
|
||||
"Failed to verify import count");
|
||||
goto failed;
|
||||
@ -4802,6 +4854,7 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
||||
goto failed;
|
||||
}
|
||||
|
||||
/* 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) {
|
||||
@ -4818,7 +4871,6 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
||||
#endif
|
||||
bh_assert(p_func_imports);
|
||||
|
||||
/* create the c-api func import list */
|
||||
total_size = (uint64)sizeof(CApiFuncImport) * import_func_count;
|
||||
if (total_size > 0
|
||||
&& !(*p_func_imports = func_import = malloc_internal(total_size))) {
|
||||
@ -4828,7 +4880,7 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
||||
}
|
||||
|
||||
/* fill in c-api func import list */
|
||||
for (i = 0; i < import_count; i++) {
|
||||
for (i = 0; imports && i < imports->num_elems; i++) {
|
||||
wasm_func_t *func_host;
|
||||
wasm_extern_t *in;
|
||||
|
||||
@ -4850,10 +4902,9 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
||||
|
||||
func_import++;
|
||||
}
|
||||
bh_assert((uint32)(func_import - *p_func_imports) == import_func_count);
|
||||
|
||||
/* fill with inst */
|
||||
for (i = 0; imports && imports->data && i < (uint32)import_count; ++i) {
|
||||
for (i = 0; imports && imports->data && i < imports->num_elems; ++i) {
|
||||
wasm_extern_t *import = imports->data[i];
|
||||
switch (import->kind) {
|
||||
case WASM_EXTERN_FUNC:
|
||||
@ -4896,7 +4947,7 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
||||
goto failed;
|
||||
}
|
||||
|
||||
processed = true;
|
||||
build_exported = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -4920,7 +4971,7 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
||||
goto failed;
|
||||
}
|
||||
|
||||
processed = true;
|
||||
build_exported = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -4928,7 +4979,7 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
|
||||
* a wrong combination of module filetype and compilation flags
|
||||
* leads to below branch
|
||||
*/
|
||||
if (!processed) {
|
||||
if (!build_exported) {
|
||||
snprintf(sub_error_buf, sizeof(sub_error_buf),
|
||||
"Incorrect filetype and compilation flags");
|
||||
goto failed;
|
||||
|
||||
@ -2359,19 +2359,27 @@ wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
|
||||
{
|
||||
WASIArguments *wasi_args = get_wasi_args_from_module(module);
|
||||
|
||||
if (wasi_args) {
|
||||
wasi_args->dir_list = dir_list;
|
||||
wasi_args->dir_count = dir_count;
|
||||
wasi_args->map_dir_list = map_dir_list;
|
||||
wasi_args->map_dir_count = map_dir_count;
|
||||
wasi_args->env = env_list;
|
||||
wasi_args->env_count = env_count;
|
||||
wasi_args->argv = argv;
|
||||
wasi_args->argc = (uint32)argc;
|
||||
wasi_args->stdio[0] = stdinfd;
|
||||
wasi_args->stdio[1] = stdoutfd;
|
||||
wasi_args->stdio[2] = stderrfd;
|
||||
bh_assert(wasi_args);
|
||||
|
||||
wasi_args->dir_list = dir_list;
|
||||
wasi_args->dir_count = dir_count;
|
||||
wasi_args->map_dir_list = map_dir_list;
|
||||
wasi_args->map_dir_count = map_dir_count;
|
||||
wasi_args->env = env_list;
|
||||
wasi_args->env_count = env_count;
|
||||
wasi_args->argv = argv;
|
||||
wasi_args->argc = (uint32)argc;
|
||||
wasi_args->stdio[0] = stdinfd;
|
||||
wasi_args->stdio[1] = stdoutfd;
|
||||
wasi_args->stdio[2] = stderrfd;
|
||||
|
||||
#if WASM_ENABLE_MULTI_MODULE != 0
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module->module_type == Wasm_Module_Bytecode) {
|
||||
wasm_propagate_wasi_args((WASMModule *)module);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
@ -2488,7 +2496,8 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
|
||||
|
||||
wasm_runtime_set_wasi_ctx(module_inst, wasi_ctx);
|
||||
|
||||
/* process argv[0], trip the path and suffix, only keep the program name */
|
||||
/* process argv[0], trip the path and suffix, only keep the program name
|
||||
*/
|
||||
if (!copy_string_array((const char **)argv, argc, &argv_buf, &argv_list,
|
||||
&argv_buf_size)) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
@ -3188,7 +3197,8 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
|
||||
uint32 *argv_ret)
|
||||
{
|
||||
WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
|
||||
/* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args */
|
||||
/* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args
|
||||
*/
|
||||
uint32 argv_buf[32], *argv1 = argv_buf, *ints, *stacks, size;
|
||||
uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
|
||||
uint32 arg_i32, ptr_len;
|
||||
|
||||
Reference in New Issue
Block a user