Refine codes and fix several issues (#882)
Refine some codes in wasm loader Add -Wshadow to gcc compile flags and fix some variable shadowed issues Fix function parameter/return types not checked issue Fix fast-interp loader reserve_block_ret() not handle V128 return type issue Fix mini loader load_table_segment_section() failed issue Add detailed comments for argc argument in wasm_runtime_call_wasm()
This commit is contained in:
@ -60,6 +60,25 @@ is_64bit_type(uint8 type)
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool
|
||||
is_value_type(uint8 type)
|
||||
{
|
||||
if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_I64
|
||||
|| type == VALUE_TYPE_F32 || type == VALUE_TYPE_F64
|
||||
#if WASM_ENABLE_REF_TYPES != 0
|
||||
|| type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
|
||||
#endif
|
||||
)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool
|
||||
is_byte_a_type(uint8 type)
|
||||
{
|
||||
return is_value_type(type) || (type == VALUE_TYPE_VOID);
|
||||
}
|
||||
|
||||
static void
|
||||
read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
|
||||
uint64 *p_result, char *error_buf, uint32 error_buf_size)
|
||||
@ -359,6 +378,9 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
||||
CHECK_BUF(p, p_end, 1);
|
||||
type->types[param_count + j] = read_uint8(p);
|
||||
}
|
||||
for (j = 0; j < param_count + result_count; j++) {
|
||||
bh_assert(is_value_type(type->types[j]));
|
||||
}
|
||||
|
||||
param_cell_num = wasm_get_cell_num(type->types, param_count);
|
||||
ret_cell_num =
|
||||
@ -971,12 +993,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
|
||||
CHECK_BUF(p_code, buf_code_end, 1);
|
||||
/* 0x7F/0x7E/0x7D/0x7C */
|
||||
type = read_uint8(p_code);
|
||||
bh_assert((type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32)
|
||||
#if WASM_ENABLE_REF_TYPES != 0
|
||||
|| type == VALUE_TYPE_FUNCREF
|
||||
|| type == VALUE_TYPE_EXTERNREF
|
||||
#endif
|
||||
);
|
||||
bh_assert(is_value_type(type));
|
||||
for (k = 0; k < sub_local_count; k++) {
|
||||
func->local_types[local_type_index++] = type;
|
||||
}
|
||||
@ -1228,7 +1245,6 @@ check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
|
||||
return true;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_REF_TYPES != 0
|
||||
static bool
|
||||
load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
|
||||
uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
|
||||
@ -1246,6 +1262,7 @@ load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
|
||||
return true;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_REF_TYPES != 0
|
||||
static bool
|
||||
load_elem_type(const uint8 **p_buf, const uint8 *buf_end, uint32 *p_elem_type,
|
||||
bool elemkind_zero, char *error_buf, uint32 error_buf_size)
|
||||
@ -1416,34 +1433,26 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
read_leb_uint32(p, p_end, table_index);
|
||||
bh_assert(table_index
|
||||
< module->import_table_count + module->table_count);
|
||||
|
||||
table_segment->table_index = table_index;
|
||||
|
||||
/* initialize expression */
|
||||
if (!load_init_expr(&p, p_end, &(table_segment->base_offset),
|
||||
/*
|
||||
* like: 00 41 05 0b 04 00 01 00 01
|
||||
* for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
|
||||
*/
|
||||
if (!load_table_index(&p, p_end, module,
|
||||
&table_segment->table_index, error_buf,
|
||||
error_buf_size))
|
||||
return false;
|
||||
if (!load_init_expr(&p, p_end, &table_segment->base_offset,
|
||||
VALUE_TYPE_I32, error_buf, error_buf_size))
|
||||
return false;
|
||||
|
||||
read_leb_uint32(p, p_end, function_count);
|
||||
table_segment->function_count = function_count;
|
||||
total_size = sizeof(uint32) * (uint64)function_count;
|
||||
if (total_size > 0
|
||||
&& !(table_segment->func_indexes = (uint32 *)loader_malloc(
|
||||
total_size, error_buf, error_buf_size))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!load_func_index_vec(&p, p_end, module, table_segment,
|
||||
table_segment->mode == 0 ? false : true,
|
||||
if (!load_func_index_vec(&p, p_end, module, table_segment, false,
|
||||
error_buf, error_buf_size))
|
||||
return false;
|
||||
#endif /* WASM_ENABLE_REF_TYPES != 0 */
|
||||
}
|
||||
}
|
||||
|
||||
(void)table_index;
|
||||
(void)function_count;
|
||||
bh_assert(p == p_end);
|
||||
LOG_VERBOSE("Load table segment section success.\n");
|
||||
return true;
|
||||
@ -3146,40 +3155,34 @@ static WASMLoaderContext *
|
||||
wasm_loader_ctx_init(WASMFunction *func)
|
||||
{
|
||||
WASMLoaderContext *loader_ctx =
|
||||
wasm_runtime_malloc(sizeof(WASMLoaderContext));
|
||||
loader_malloc(sizeof(WASMLoaderContext), NULL, 0);
|
||||
if (!loader_ctx)
|
||||
return false;
|
||||
memset(loader_ctx, 0, sizeof(WASMLoaderContext));
|
||||
|
||||
loader_ctx->frame_ref_size = 32;
|
||||
if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref =
|
||||
wasm_runtime_malloc(loader_ctx->frame_ref_size)))
|
||||
loader_malloc(loader_ctx->frame_ref_size, NULL, 0)))
|
||||
goto fail;
|
||||
memset(loader_ctx->frame_ref_bottom, 0, loader_ctx->frame_ref_size);
|
||||
loader_ctx->frame_ref_boundary =
|
||||
loader_ctx->frame_ref_bottom + loader_ctx->frame_ref_size;
|
||||
loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
|
||||
|
||||
loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
|
||||
if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp =
|
||||
wasm_runtime_malloc(loader_ctx->frame_csp_size)))
|
||||
loader_malloc(loader_ctx->frame_csp_size, NULL, 0)))
|
||||
goto fail;
|
||||
memset(loader_ctx->frame_csp_bottom, 0, loader_ctx->frame_csp_size);
|
||||
loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
|
||||
|
||||
#if WASM_ENABLE_FAST_INTERP != 0
|
||||
loader_ctx->frame_offset_size = sizeof(int16) * 32;
|
||||
if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
|
||||
wasm_runtime_malloc(loader_ctx->frame_offset_size)))
|
||||
loader_malloc(loader_ctx->frame_offset_size, NULL, 0)))
|
||||
goto fail;
|
||||
memset(loader_ctx->frame_offset_bottom, 0, loader_ctx->frame_offset_size);
|
||||
loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
|
||||
|
||||
loader_ctx->num_const = 0;
|
||||
loader_ctx->const_buf_size = sizeof(Const) * 8;
|
||||
if (!(loader_ctx->const_buf =
|
||||
wasm_runtime_malloc(loader_ctx->const_buf_size)))
|
||||
loader_malloc(loader_ctx->const_buf_size, NULL, 0)))
|
||||
goto fail;
|
||||
memset(loader_ctx->const_buf, 0, loader_ctx->const_buf_size);
|
||||
|
||||
loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
|
||||
loader_ctx->max_dynamic_offset =
|
||||
@ -3436,9 +3439,9 @@ wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
|
||||
static bool
|
||||
wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
|
||||
{
|
||||
if (!(ctx->p_code_compiled = wasm_runtime_malloc(ctx->code_compiled_size)))
|
||||
if (!(ctx->p_code_compiled =
|
||||
loader_malloc(ctx->code_compiled_size, NULL, 0)))
|
||||
return false;
|
||||
memset(ctx->p_code_compiled, 0, ctx->code_compiled_size);
|
||||
ctx->p_code_compiled_end = ctx->p_code_compiled + ctx->code_compiled_size;
|
||||
|
||||
/* clean up frame ref */
|
||||
@ -4240,7 +4243,8 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
|
||||
* of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
|
||||
if (return_count == 1) {
|
||||
uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
|
||||
if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
|
||||
if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
|
||||
&& block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
|
||||
/* insert op_copy before else opcode */
|
||||
if (opcode == WASM_OP_ELSE)
|
||||
skip_label();
|
||||
@ -4425,25 +4429,6 @@ fail:
|
||||
bh_assert(module->import_memory_count + module->memory_count > 0); \
|
||||
} while (0)
|
||||
|
||||
static bool
|
||||
is_value_type(uint8 type)
|
||||
{
|
||||
if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_I64
|
||||
|| type == VALUE_TYPE_F32 || type == VALUE_TYPE_F64
|
||||
#if WASM_ENABLE_REF_TYPES != 0
|
||||
|| type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
|
||||
#endif
|
||||
)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool
|
||||
is_byte_a_type(uint8 type)
|
||||
{
|
||||
return is_value_type(type) || (type == VALUE_TYPE_VOID);
|
||||
}
|
||||
|
||||
static bool
|
||||
wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
|
||||
char *error_buf, uint32 error_buf_size)
|
||||
@ -4715,10 +4700,10 @@ fail:
|
||||
#endif
|
||||
|
||||
/* set current block's stack polymorphic state */
|
||||
#define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
|
||||
do { \
|
||||
BranchBlock *cur_block = loader_ctx->frame_csp - 1; \
|
||||
cur_block->is_stack_polymorphic = flag; \
|
||||
#define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
|
||||
do { \
|
||||
BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
|
||||
_cur_block->is_stack_polymorphic = flag; \
|
||||
} while (0)
|
||||
|
||||
#define BLOCK_HAS_PARAM(block_type) \
|
||||
@ -4740,11 +4725,11 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
|
||||
uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
|
||||
uint32 param_count, local_count, global_count;
|
||||
uint8 *param_types, *local_types, local_type, global_type;
|
||||
BlockType func_type;
|
||||
BlockType func_block_type;
|
||||
uint16 *local_offsets, local_offset;
|
||||
uint32 count, i, local_idx, global_idx, u32, align, mem_offset;
|
||||
uint32 count, local_idx, global_idx, u32, align, mem_offset, i;
|
||||
int32 i32, i32_const = 0;
|
||||
int64 i64;
|
||||
int64 i64_const;
|
||||
uint8 opcode, u8;
|
||||
bool return_value = false;
|
||||
WASMLoaderContext *loader_ctx;
|
||||
@ -4757,8 +4742,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
|
||||
int16 operand_offset = 0;
|
||||
uint8 last_op = 0;
|
||||
bool disable_emit, preserve_local = false;
|
||||
float32 f32;
|
||||
float64 f64;
|
||||
float32 f32_const;
|
||||
float64 f64_const;
|
||||
|
||||
LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
|
||||
func->param_cell_num, func->local_cell_num, func->ret_cell_num);
|
||||
@ -4769,8 +4754,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
|
||||
param_count = func->func_type->param_count;
|
||||
param_types = func->func_type->types;
|
||||
|
||||
func_type.is_value_type = false;
|
||||
func_type.u.type = func->func_type;
|
||||
func_block_type.is_value_type = false;
|
||||
func_block_type.u.type = func->func_type;
|
||||
|
||||
local_count = func->local_count;
|
||||
local_types = func->local_types;
|
||||
@ -4794,7 +4779,7 @@ re_scan:
|
||||
}
|
||||
#endif
|
||||
|
||||
PUSH_CSP(LABEL_TYPE_FUNCTION, func_type, p);
|
||||
PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
|
||||
|
||||
while (p < p_end) {
|
||||
opcode = *p++;
|
||||
@ -5006,28 +4991,29 @@ re_scan:
|
||||
* fail */
|
||||
if (cur_block->label_type == LABEL_TYPE_IF
|
||||
&& !cur_block->else_addr) {
|
||||
uint32 param_count = 0, ret_count = 0;
|
||||
uint8 *param_types = NULL, *ret_types = NULL;
|
||||
BlockType *block_type = &cur_block->block_type;
|
||||
if (block_type->is_value_type) {
|
||||
if (block_type->u.value_type != VALUE_TYPE_VOID) {
|
||||
ret_count = 1;
|
||||
ret_types = &block_type->u.value_type;
|
||||
uint32 block_param_count = 0, block_ret_count = 0;
|
||||
uint8 *block_param_types = NULL, *block_ret_types = NULL;
|
||||
BlockType *cur_block_type = &cur_block->block_type;
|
||||
if (cur_block_type->is_value_type) {
|
||||
if (cur_block_type->u.value_type != VALUE_TYPE_VOID) {
|
||||
block_ret_count = 1;
|
||||
block_ret_types = &cur_block_type->u.value_type;
|
||||
}
|
||||
}
|
||||
else {
|
||||
param_count = block_type->u.type->param_count;
|
||||
ret_count = block_type->u.type->result_count;
|
||||
param_types = block_type->u.type->types;
|
||||
ret_types = block_type->u.type->types + param_count;
|
||||
block_param_count = cur_block_type->u.type->param_count;
|
||||
block_ret_count = cur_block_type->u.type->result_count;
|
||||
block_param_types = cur_block_type->u.type->types;
|
||||
block_ret_types =
|
||||
cur_block_type->u.type->types + block_param_count;
|
||||
}
|
||||
bh_assert(
|
||||
param_count == ret_count
|
||||
&& (!param_count
|
||||
|| !memcmp(param_types, ret_types, param_count)));
|
||||
(void)ret_types;
|
||||
(void)ret_count;
|
||||
(void)param_types;
|
||||
bh_assert(block_param_count == block_ret_count
|
||||
&& (!block_param_count
|
||||
|| !memcmp(block_param_types, block_ret_types,
|
||||
block_param_count)));
|
||||
(void)block_ret_types;
|
||||
(void)block_ret_count;
|
||||
(void)block_param_types;
|
||||
}
|
||||
|
||||
POP_CSP();
|
||||
@ -5291,8 +5277,7 @@ re_scan:
|
||||
&& !cur_block->is_stack_polymorphic));
|
||||
|
||||
if (available_stack_cell > 0) {
|
||||
if (*(loader_ctx->frame_ref - 1) == REF_I32
|
||||
|| *(loader_ctx->frame_ref - 1) == REF_F32) {
|
||||
if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
|
||||
loader_ctx->frame_ref--;
|
||||
loader_ctx->stack_cell_num--;
|
||||
#if WASM_ENABLE_FAST_INTERP != 0
|
||||
@ -5303,7 +5288,7 @@ re_scan:
|
||||
loader_ctx->dynamic_offset--;
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
|
||||
loader_ctx->frame_ref -= 2;
|
||||
loader_ctx->stack_cell_num -= 2;
|
||||
#if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0)
|
||||
@ -5317,6 +5302,9 @@ re_scan:
|
||||
loader_ctx->dynamic_offset -= 2;
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
bh_assert(0);
|
||||
}
|
||||
}
|
||||
else {
|
||||
#if WASM_ENABLE_FAST_INTERP != 0
|
||||
@ -5969,11 +5957,11 @@ re_scan:
|
||||
break;
|
||||
|
||||
case WASM_OP_I64_CONST:
|
||||
read_leb_int64(p, p_end, i64);
|
||||
read_leb_int64(p, p_end, i64_const);
|
||||
#if WASM_ENABLE_FAST_INTERP != 0
|
||||
skip_label();
|
||||
disable_emit = true;
|
||||
GET_CONST_OFFSET(VALUE_TYPE_I64, i64);
|
||||
GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
|
||||
#endif
|
||||
PUSH_I64();
|
||||
break;
|
||||
@ -5983,9 +5971,9 @@ re_scan:
|
||||
#if WASM_ENABLE_FAST_INTERP != 0
|
||||
skip_label();
|
||||
disable_emit = true;
|
||||
bh_memcpy_s((uint8 *)&f32, sizeof(float32), p_org,
|
||||
bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
|
||||
sizeof(float32));
|
||||
GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32);
|
||||
GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
|
||||
#endif
|
||||
PUSH_F32();
|
||||
break;
|
||||
@ -5996,9 +5984,9 @@ re_scan:
|
||||
skip_label();
|
||||
disable_emit = true;
|
||||
/* Some MCU may require 8-byte align */
|
||||
bh_memcpy_s((uint8 *)&f64, sizeof(float64), p_org,
|
||||
bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
|
||||
sizeof(float64));
|
||||
GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64);
|
||||
GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
|
||||
#endif
|
||||
PUSH_F64();
|
||||
break;
|
||||
@ -6605,25 +6593,28 @@ re_scan:
|
||||
goto re_scan;
|
||||
|
||||
func->const_cell_num = loader_ctx->const_cell_num;
|
||||
if (func->const_cell_num > 0
|
||||
&& !(func->consts = func_const = loader_malloc(
|
||||
func->const_cell_num * 4, error_buf, error_buf_size))) {
|
||||
goto fail;
|
||||
}
|
||||
func_const_end = func->consts + func->const_cell_num * 4;
|
||||
/* reverse the const buf */
|
||||
for (int i = loader_ctx->num_const - 1; i >= 0; i--) {
|
||||
Const *c = (Const *)(loader_ctx->const_buf + i * sizeof(Const));
|
||||
if (c->value_type == VALUE_TYPE_F64
|
||||
|| c->value_type == VALUE_TYPE_I64) {
|
||||
bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
|
||||
&(c->value.f64), (uint32)sizeof(int64));
|
||||
func_const += sizeof(int64);
|
||||
}
|
||||
else {
|
||||
bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
|
||||
&(c->value.f32), (uint32)sizeof(int32));
|
||||
func_const += sizeof(int32);
|
||||
if (func->const_cell_num > 0) {
|
||||
int32 j;
|
||||
|
||||
if (!(func->consts = func_const = loader_malloc(
|
||||
func->const_cell_num * 4, error_buf, error_buf_size)))
|
||||
goto fail;
|
||||
|
||||
func_const_end = func->consts + func->const_cell_num * 4;
|
||||
/* reverse the const buf */
|
||||
for (j = loader_ctx->num_const - 1; j >= 0; j--) {
|
||||
Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
|
||||
if (c->value_type == VALUE_TYPE_F64
|
||||
|| c->value_type == VALUE_TYPE_I64) {
|
||||
bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
|
||||
&(c->value.f64), (uint32)sizeof(int64));
|
||||
func_const += sizeof(int64);
|
||||
}
|
||||
else {
|
||||
bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
|
||||
&(c->value.f32), (uint32)sizeof(int32));
|
||||
func_const += sizeof(int32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -6641,7 +6632,7 @@ fail:
|
||||
(void)u8;
|
||||
(void)u32;
|
||||
(void)i32;
|
||||
(void)i64;
|
||||
(void)i64_const;
|
||||
(void)global_count;
|
||||
(void)local_count;
|
||||
(void)local_offset;
|
||||
|
||||
Reference in New Issue
Block a user