Merge main into dev/wasi_threads

This commit is contained in:
Wenyong Huang
2023-01-03 18:15:23 +08:00
46 changed files with 1081 additions and 527 deletions

View File

@ -58,6 +58,8 @@ static const aot_intrinsic g_intrinsic_mapping[] = {
{ "i32_trunc_f64_u", "aot_intrinsic_f64_to_u32", AOT_INTRINSIC_FLAG_F64_TO_U32 },
{ "i32_trunc_f64_s", "aot_intrinsic_f64_to_i32", AOT_INTRINSIC_FLAG_F64_TO_I32 },
{ "i64_trunc_f64_u", "aot_intrinsic_f64_to_u64", AOT_INTRINSIC_FLAG_F64_TO_U64 },
{ "i64_trunc_f32_s", "aot_intrinsic_f32_to_i64", AOT_INTRINSIC_FLAG_F32_TO_I64 },
{ "i64_trunc_f32_u", "aot_intrinsic_f32_to_u64", AOT_INTRINSIC_FLAG_F32_TO_U64 },
{ "i64_trunc_f64_s", "aot_intrinsic_f64_to_i64", AOT_INTRINSIC_FLAG_F64_TO_I64 },
{ "f32_demote_f64", "aot_intrinsic_f64_to_f32", AOT_INTRINSIC_FLAG_F64_TO_F32 },
{ "f64_promote_f32", "aot_intrinsic_f32_to_f64", AOT_INTRINSIC_FLAG_F32_TO_F64 },
@ -134,7 +136,7 @@ aot_intrinsic_fdiv_f64(float64 a, float64 b)
float32
aot_intrinsic_fabs_f32(float32 a)
{
return (float32)fabs(a);
return fabsf(a);
}
float64
@ -146,7 +148,7 @@ aot_intrinsic_fabs_f64(float64 a)
float32
aot_intrinsic_ceil_f32(float32 a)
{
return (float32)ceilf(a);
return ceilf(a);
}
float64
@ -158,7 +160,7 @@ aot_intrinsic_ceil_f64(float64 a)
float32
aot_intrinsic_floor_f32(float32 a)
{
return (float32)floorf(a);
return floorf(a);
}
float64
@ -170,7 +172,7 @@ aot_intrinsic_floor_f64(float64 a)
float32
aot_intrinsic_trunc_f32(float32 a)
{
return (float32)trunc(a);
return truncf(a);
}
float64
@ -182,7 +184,7 @@ aot_intrinsic_trunc_f64(float64 a)
float32
aot_intrinsic_rint_f32(float32 a)
{
return (float32)rint(a);
return rintf(a);
}
float64
@ -194,7 +196,7 @@ aot_intrinsic_rint_f64(float64 a)
float32
aot_intrinsic_sqrt_f32(float32 a)
{
return (float32)sqrt(a);
return sqrtf(a);
}
float64
@ -206,7 +208,7 @@ aot_intrinsic_sqrt_f64(float64 a)
float32
aot_intrinsic_copysign_f32(float32 a, float32 b)
{
return signbit(b) ? (float32)-fabs(a) : (float32)fabs(a);
return signbit(b) ? -fabsf(a) : fabsf(a);
}
float64
@ -218,41 +220,45 @@ aot_intrinsic_copysign_f64(float64 a, float64 b)
float32
aot_intrinsic_fmin_f32(float32 a, float32 b)
{
if (isnan(a))
return a;
else if (isnan(b))
return b;
if (isnan(a) || isnan(b))
return NAN;
else if (a == 0 && a == b)
return signbit(a) ? a : b;
else
return (float32)fmin(a, b);
return a > b ? b : a;
}
float64
aot_intrinsic_fmin_f64(float64 a, float64 b)
{
float64 c = fmin(a, b);
if (c == 0 && a == b)
if (isnan(a) || isnan(b))
return NAN;
else if (a == 0 && a == b)
return signbit(a) ? a : b;
return c;
else
return a > b ? b : a;
}
float32
aot_intrinsic_fmax_f32(float32 a, float32 b)
{
if (isnan(a))
return a;
else if (isnan(b))
return b;
if (isnan(a) || isnan(b))
return NAN;
else if (a == 0 && a == b)
return signbit(a) ? b : a;
else
return (float32)fmax(a, b);
return a > b ? a : b;
}
float64
aot_intrinsic_fmax_f64(float64 a, float64 b)
{
float64 c = fmax(a, b);
if (c == 0 && a == b)
if (isnan(a) || isnan(b))
return NAN;
else if (a == 0 && a == b)
return signbit(a) ? b : a;
return c;
else
return a > b ? a : b;
}
uint32
@ -442,7 +448,7 @@ aot_intrinsic_f32_cmp(AOTFloatCond cond, float32 lhs, float32 rhs)
{
switch (cond) {
case FLOAT_EQ:
return (float32)fabs(lhs - rhs) <= WA_FLT_EPSILON ? 1 : 0;
return lhs == rhs ? 1 : 0;
case FLOAT_LT:
return lhs < rhs ? 1 : 0;
@ -473,7 +479,7 @@ aot_intrinsic_f64_cmp(AOTFloatCond cond, float64 lhs, float64 rhs)
{
switch (cond) {
case FLOAT_EQ:
return fabs(lhs - rhs) <= WA_DBL_EPSILON ? 1 : 0;
return lhs == rhs ? 1 : 0;
case FLOAT_LT:
return lhs < rhs ? 1 : 0;
@ -631,6 +637,12 @@ add_f64_common_intrinsics(AOTCompContext *comp_ctx)
add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_FADD);
add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_FSUB);
add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_FMUL);
add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_MIN);
add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_MAX);
add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_CEIL);
add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_FLOOR);
add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_TRUNC);
add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_RINT);
add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_FDIV);
add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_SQRT);
add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_F64_CMP);

View File

@ -101,6 +101,8 @@ typedef struct {
REG_SYM(aot_intrinsic_f64_to_f32), \
REG_SYM(aot_intrinsic_f32_to_i32), \
REG_SYM(aot_intrinsic_f32_to_u32), \
REG_SYM(aot_intrinsic_f32_to_i64), \
REG_SYM(aot_intrinsic_f32_to_u64), \
REG_SYM(aot_intrinsic_f64_to_i32), \
REG_SYM(aot_intrinsic_f64_to_u32), \
REG_SYM(aot_intrinsic_f64_to_i64), \

View File

@ -20,6 +20,7 @@ void __adddf3();
void __addsf3();
void __divdi3();
void __divsi3();
void __divdf3();
void __divsf3();
void __eqsf2();
void __eqdf2();
@ -42,9 +43,12 @@ void __floatunsisf();
void __floatunsidf();
void __gedf2();
void __gesf2();
void __gtdf2();
void __gtsf2();
void __ledf2();
void __lesf2();
void __ltdf2();
void __ltsf2();
void __moddi3();
void __modsi3();
void __muldf3();
@ -70,12 +74,16 @@ static SymbolMap target_sym_map[] = {
#ifndef __riscv_flen
REG_SYM(__adddf3),
REG_SYM(__addsf3),
REG_SYM(__divdf3),
REG_SYM(__divsf3),
REG_SYM(__gedf2),
REG_SYM(__gesf2),
REG_SYM(__gtdf2),
REG_SYM(__gtsf2),
REG_SYM(__ledf2),
REG_SYM(__lesf2),
REG_SYM(__ltdf2),
REG_SYM(__ltsf2),
REG_SYM(__muldf3),
REG_SYM(__nedf2),
REG_SYM(__nesf2),

View File

@ -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;

View File

@ -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;

View File

@ -2597,7 +2597,7 @@ fail:
}
static bool
veriy_module(AOTCompContext *comp_ctx)
verify_module(AOTCompContext *comp_ctx)
{
char *msg = NULL;
bool ret;
@ -2697,7 +2697,7 @@ aot_compile_wasm(AOTCompContext *comp_ctx)
the compilation process */
if (!comp_ctx->is_jit_mode) {
bh_print_time("Begin to verify LLVM module");
if (!veriy_module(comp_ctx)) {
if (!verify_module(comp_ctx)) {
return false;
}
}

View File

@ -307,6 +307,14 @@ compile_op_float_min_max(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
param_types, 2, left, right)))
return NULL;
/* The result of XIP intrinsic is 0 or 1, should return it directly */
if (comp_ctx->disable_llvm_intrinsics
&& aot_intrinsic_check_capability(comp_ctx,
is_f32 ? "f32_cmp" : "f64_cmp")) {
return cmp;
}
if (!(cmp = LLVMBuildSelect(comp_ctx->builder, is_eq, tmp, cmp, "cmp"))) {
aot_set_last_error("llvm build select fail.");
return NULL;

View File

@ -684,7 +684,7 @@ adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
}
}
#if WASM_ENABLE_MULTI_MODULE != 0
#if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
/**
* Find export item of a module with export info:
* module name, field name and export kind
@ -718,11 +718,15 @@ wasm_loader_find_export(const WASMModule *module, const char *module_name,
return NULL;
}
(void)module_name;
/* since there is a validation in load_export_section(), it is for sure
* export->index is valid*/
return export;
}
#endif
#if WASM_ENABLE_MULTI_MODULE != 0
static WASMFunction *
wasm_loader_resolve_function(const char *module_name, const char *function_name,
const WASMType *expected_function_type,
@ -1240,6 +1244,8 @@ load_table_import(const uint8 **p_buf, const uint8 *buf_end,
table->init_size = declare_init_size;
table->flags = declare_max_size_flag;
table->max_size = declare_max_size;
(void)parent_module;
return true;
fail:
return false;
@ -1373,6 +1379,8 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
*p_buf = p;
(void)parent_module;
return true;
fail:
return false;
@ -1439,6 +1447,8 @@ load_global_import(const uint8 **p_buf, const uint8 *buf_end,
global->field_name = global_name;
global->type = declare_type;
global->is_mutable = (declare_mutable == 1);
(void)parent_module;
return true;
fail:
return false;
@ -2381,6 +2391,7 @@ load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
}
#else
read_leb_uint32(p, p_end, function_index);
(void)use_init_expr;
#endif
/* since we are using -1 to indicate ref.null */
@ -2690,6 +2701,7 @@ load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
}
LOG_VERBOSE("Load code segment section success.\n");
(void)module;
return true;
fail:
return false;
@ -2794,8 +2806,8 @@ handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
read_leb_uint32(p, p_end, func_name_len);
CHECK_BUF(p, p_end, func_name_len);
/* Skip the import functions */
if (func_index >= module->import_count) {
func_index -= module->import_count;
if (func_index >= module->import_function_count) {
func_index -= module->import_function_count;
if (func_index >= module->function_count) {
set_error_buf(error_buf, error_buf_size,
"out-of-range function index");
@ -2900,6 +2912,8 @@ load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
LOG_VERBOSE("Ignore custom section [%s].", section_name);
(void)is_load_from_file_buf;
(void)module;
return true;
fail:
return false;
@ -4054,24 +4068,19 @@ fail:
return false;
}
#if (WASM_ENABLE_MULTI_MODULE != 0) && (WASM_ENABLE_LIBC_WASI != 0)
#if WASM_ENABLE_LIBC_WASI != 0
/**
* refer to
* https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
*/
static bool
check_wasi_abi_compatibility(const WASMModule *module, bool main_module,
check_wasi_abi_compatibility(const WASMModule *module,
#if WASM_ENABLE_MULTI_MODULE != 0
bool main_module,
#endif
char *error_buf, uint32 error_buf_size)
{
/**
* need to handle:
* - non-wasi compatiable modules
* - a fake wasi compatiable module
* - a command acts as a main_module
* - a command acts as a sub_module
* - a reactor acts as a main_module
* - a reactor acts as a sub_module
*
* be careful with:
* wasi compatiable modules(command/reactor) which don't import any wasi
* APIs. Usually, a command has to import a "prox_exit" at least, but a
@ -4087,8 +4096,20 @@ check_wasi_abi_compatibility(const WASMModule *module, bool main_module,
* - no one will define either `_start` or `_initialize` on purpose
* - `_start` should always be `void _start(void)`
* - `_initialize` should always be `void _initialize(void)`
*
*/
/* clang-format off */
/**
*
* | | import_wasi_api True | | import_wasi_api False | |
* | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
* | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
* | \_start() Y | N | COMMANDER | N | COMMANDER |
* | \_start() N | REACTOR | N | REACTOR | OTHERS |
*/
/* clang-format on */
WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
/* (func (export "_start") (...) */
@ -4147,6 +4168,7 @@ check_wasi_abi_compatibility(const WASMModule *module, bool main_module,
return false;
}
#if WASM_ENABLE_MULTI_MODULE != 0
/* filter out commands (with `_start`) cases */
if (start && !main_module) {
set_error_buf(
@ -4154,6 +4176,7 @@ check_wasi_abi_compatibility(const WASMModule *module, bool main_module,
"a command (with _start function) can not be a sub-module");
return false;
}
#endif
/*
* it is ok a reactor acts as a main module,
@ -4193,10 +4216,13 @@ wasm_loader_load(uint8 *buf, uint32 size,
goto fail;
}
#if (WASM_ENABLE_MULTI_MODULE != 0) && (WASM_ENABLE_LIBC_WASI != 0)
#if WASM_ENABLE_LIBC_WASI != 0
/* Check the WASI application ABI */
if (!check_wasi_abi_compatibility(module, main_module, error_buf,
error_buf_size)) {
if (!check_wasi_abi_compatibility(module,
#if WASM_ENABLE_MULTI_MODULE != 0
main_module,
#endif
error_buf, error_buf_size)) {
goto fail;
}
#endif
@ -4971,6 +4997,7 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
}
(void)u8;
(void)exec_env;
return false;
fail:
return false;
@ -5834,6 +5861,8 @@ preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
i += 2;
}
(void)error_buf;
(void)error_buf_size;
return true;
#if WASM_ENABLE_LABELS_AS_VALUES != 0
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
@ -6098,6 +6127,9 @@ wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
ctx->dynamic_offset -= 2;
}
emit_operand(ctx, *(ctx->frame_offset));
(void)error_buf;
(void)error_buf_size;
return true;
}

View File

@ -1708,8 +1708,8 @@ handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
read_leb_uint32(p, p_end, func_name_len);
CHECK_BUF(p, p_end, func_name_len);
/* Skip the import functions */
if (func_index >= module->import_count) {
func_index -= module->import_count;
if (func_index >= module->import_function_count) {
func_index -= module->import_function_count;
bh_assert(func_index < module->function_count);
if (!(module->functions[func_index]->field_name =
const_str_list_insert(
@ -1733,6 +1733,8 @@ handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
i++;
}
(void)previous_name_type;
(void)previous_func_index;
return true;
}
#endif

View File

@ -983,6 +983,18 @@ export_globals_instantiate(const WASMModule *module,
}
#endif
#if WASM_ENABLE_LIBC_WASI != 0
static bool
execute_initialize_function(WASMModuleInstance *module_inst)
{
WASMFunctionInstance *initialize =
wasm_lookup_function(module_inst, "_initialize", NULL);
return !initialize
|| wasm_create_exec_env_and_call_function(module_inst, initialize, 0,
NULL);
}
#endif
static bool
execute_post_inst_function(WASMModuleInstance *module_inst)
{
@ -1175,28 +1187,6 @@ sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
sub_module_list_node = bh_list_elem_next(sub_module_list_node);
#if WASM_ENABLE_LIBC_WASI != 0
{
/*
* reactor instances may assume that _initialize will be called by
* the environment at most once, and that none of their other
* exports are accessed before that call.
*
* let the loader decide how to act if there is no _initialize
* in a reactor
*/
WASMFunctionInstance *initialize =
wasm_lookup_function(sub_module_inst, "_initialize", NULL);
if (initialize
&& !wasm_create_exec_env_and_call_function(
sub_module_inst, initialize, 0, NULL)) {
set_error_buf(error_buf, error_buf_size,
"Call _initialize failed ");
goto failed;
}
}
#endif
continue;
failed:
if (sub_module_inst_list_node) {
@ -1844,8 +1834,21 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
&module_inst->e->functions[module->start_function];
}
/* Execute __post_instantiate function */
if (!execute_post_inst_function(module_inst)
if (
#if WASM_ENABLE_LIBC_WASI != 0
/*
* reactor instances may assume that _initialize will be called by
* the environment at most once, and that none of their other
* exports are accessed before that call.
*
* let the loader decide how to act if there is no _initialize
* in a reactor
*/
!execute_initialize_function(module_inst) ||
#endif
/* Execute __post_instantiate function */
!execute_post_inst_function(module_inst)
/* Execute the function in "start" section */
|| !execute_start_function(module_inst)) {
set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
goto fail;
@ -3233,3 +3236,26 @@ llvm_jit_free_frame(WASMExecEnv *exec_env)
|| WASM_ENABLE_PERF_PROFILING != 0 */
#endif /* end of WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 */
#if WASM_ENABLE_LIBC_WASI != 0 && WASM_ENABLE_MULTI_MODULE != 0
void
wasm_propagate_wasi_args(WASMModule *module)
{
if (!module->import_count)
return;
bh_assert(&module->import_module_list_head);
WASMRegisteredModule *node =
bh_list_first_elem(&module->import_module_list_head);
while (node) {
WASIArguments *wasi_args_impt_mod =
&((WASMModule *)(node->module))->wasi_args;
bh_assert(wasi_args_impt_mod);
bh_memcpy_s(wasi_args_impt_mod, sizeof(WASIArguments),
&module->wasi_args, sizeof(WASIArguments));
node = bh_list_elem_next(node);
}
}
#endif

View File

@ -626,6 +626,11 @@ llvm_jit_free_frame(WASMExecEnv *exec_env);
#endif
#endif /* end of WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 */
#if WASM_ENABLE_LIBC_WASI != 0 && WASM_ENABLE_MULTI_MODULE != 0
void
wasm_propagate_wasi_args(WASMModule *module);
#endif
#ifdef __cplusplus
}
#endif

View File

@ -2696,7 +2696,7 @@ wasmtime_ssp_poll_oneoff(
timeout = ts > INT_MAX ? -1 : (int)ts;
}
else {
timeout = 1000;
timeout = -1;
}
int ret = poll(pfds, nsubscriptions, timeout);

View File

@ -0,0 +1,43 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "platform_api_vmcore.h"
int
bh_platform_init()
{
return 0;
}
void
bh_platform_destroy()
{}
int
os_printf(const char *format, ...)
{
int ret = 0;
va_list ap;
va_start(ap, format);
#ifndef BH_VPRINTF
ret += vprintf(format, ap);
#else
ret += BH_VPRINTF(format, ap);
#endif
va_end(ap);
return ret;
}
int
os_vprintf(const char *format, va_list ap)
{
#ifndef BH_VPRINTF
return vprintf(format, ap);
#else
return BH_VPRINTF(format, ap);
#endif
}

View File

@ -0,0 +1,108 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _PLATFORM_INTERNAL_H
#define _PLATFORM_INTERNAL_H
#include <inttypes.h>
#include <stdbool.h>
#include <assert.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdarg.h>
#include <ctype.h>
#include <pthread.h>
#include <signal.h>
#include <semaphore.h>
#include <limits.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>
#include <sched.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/resource.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef BH_PLATFORM_FREEBSD
#define BH_PLATFORM_FREEBSD
#endif
#define BH_HAS_DLFCN 1
/* Stack size of applet threads's native part. */
#define BH_APPLET_PRESERVED_STACK_SIZE (32 * 1024)
/* Default thread priority */
#define BH_THREAD_DEFAULT_PRIORITY 0
typedef pthread_t korp_tid;
typedef pthread_mutex_t korp_mutex;
typedef pthread_cond_t korp_cond;
typedef pthread_t korp_thread;
typedef sem_t korp_sem;
#define OS_THREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
#define os_thread_local_attribute __thread
#define bh_socket_t int
#if WASM_DISABLE_HW_BOUND_CHECK == 0
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
|| defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
|| defined(BUILD_TARGET_RISCV64_LP64)
#include <setjmp.h>
#define OS_ENABLE_HW_BOUND_CHECK
typedef jmp_buf korp_jmpbuf;
#define os_setjmp setjmp
#define os_longjmp longjmp
#define os_alloca alloca
#define os_getpagesize getpagesize
typedef void (*os_signal_handler)(void *sig_addr);
int
os_thread_signal_init(os_signal_handler handler);
void
os_thread_signal_destroy();
bool
os_thread_signal_inited();
void
os_signal_unmask();
void
os_sigreturn();
#endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */
#endif /* end of WASM_DISABLE_HW_BOUND_CHECK */
#ifdef __cplusplus
}
#endif
#endif /* end of _PLATFORM_INTERNAL_H */

View File

@ -0,0 +1,18 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
set (PLATFORM_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR})
add_definitions(-DBH_PLATFORM_FREEBSD)
include_directories(${PLATFORM_SHARED_DIR})
include_directories(${PLATFORM_SHARED_DIR}/../include)
include (${CMAKE_CURRENT_LIST_DIR}/../common/posix/platform_api_posix.cmake)
file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c)
set (PLATFORM_SHARED_SOURCE ${source_all} ${PLATFORM_COMMON_POSIX_SOURCE})
file (GLOB header ${PLATFORM_SHARED_DIR}/../include/*.h)
LIST (APPEND RUNTIME_LIB_HEADER_LIST ${header})

View File

@ -35,9 +35,4 @@
#define WA_FREE wasm_runtime_free
#endif
/* The epsilon value is from https://www.cplusplus.com/reference/cfloat/ */
#define WA_FLT_EPSILON 1e-5f
#define WA_DBL_EPSILON 1e-9
#endif /* #ifndef _BH_PLATFORM_H */