Support extended constant expressions (#4432)
* implement extended const expr (#4318) * add a toggle to enable extended const on wamrc (#4412)
This commit is contained in:
@ -135,6 +135,12 @@ typedef void *table_elem_type_t;
|
||||
#define INIT_EXPR_TYPE_F64_CONST 0x44
|
||||
#define INIT_EXPR_TYPE_V128_CONST 0xFD
|
||||
#define INIT_EXPR_TYPE_GET_GLOBAL 0x23
|
||||
#define INIT_EXPR_TYPE_I32_ADD 0x6A
|
||||
#define INIT_EXPR_TYPE_I32_SUB 0x6B
|
||||
#define INIT_EXPR_TYPE_I32_MUL 0x6C
|
||||
#define INIT_EXPR_TYPE_I64_ADD 0x7C
|
||||
#define INIT_EXPR_TYPE_I64_SUB 0x7D
|
||||
#define INIT_EXPR_TYPE_I64_MUL 0x7E
|
||||
#define INIT_EXPR_TYPE_REFNULL_CONST 0xD0
|
||||
#define INIT_EXPR_TYPE_FUNCREF_CONST 0xD2
|
||||
#define INIT_EXPR_TYPE_STRUCT_NEW 0xD3
|
||||
@ -277,9 +283,41 @@ typedef struct InitializerExpression {
|
||||
/* type of INIT_EXPR_TYPE_XXX, which is an instruction of
|
||||
constant expression */
|
||||
uint8 init_expr_type;
|
||||
WASMValue u;
|
||||
union {
|
||||
struct {
|
||||
WASMValue v;
|
||||
} unary;
|
||||
struct {
|
||||
struct InitializerExpression *l_expr;
|
||||
struct InitializerExpression *r_expr;
|
||||
} binary;
|
||||
} u;
|
||||
} InitializerExpression;
|
||||
|
||||
static inline bool
|
||||
is_expr_binary_op(uint8 flag)
|
||||
{
|
||||
return flag == INIT_EXPR_TYPE_I32_ADD || flag == INIT_EXPR_TYPE_I32_SUB
|
||||
|| flag == INIT_EXPR_TYPE_I32_MUL || flag == INIT_EXPR_TYPE_I64_ADD
|
||||
|| flag == INIT_EXPR_TYPE_I64_SUB || flag == INIT_EXPR_TYPE_I64_MUL;
|
||||
}
|
||||
|
||||
/* check if table or data offset is valid for i32 offset */
|
||||
static inline bool
|
||||
is_valid_i32_offset(uint8 flag)
|
||||
{
|
||||
return flag == INIT_EXPR_TYPE_I32_CONST || flag == INIT_EXPR_TYPE_I32_ADD
|
||||
|| flag == INIT_EXPR_TYPE_I32_SUB || flag == INIT_EXPR_TYPE_I32_MUL;
|
||||
}
|
||||
|
||||
/* check if table or data offset is valid for i64 offset */
|
||||
static inline bool
|
||||
is_valid_i64_offset(uint8 flag)
|
||||
{
|
||||
return flag == INIT_EXPR_TYPE_I64_CONST || flag == INIT_EXPR_TYPE_I64_ADD
|
||||
|| flag == INIT_EXPR_TYPE_I64_SUB || flag == INIT_EXPR_TYPE_I64_MUL;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_GC != 0
|
||||
/**
|
||||
* Reference type of (ref null ht) or (ref ht),
|
||||
|
||||
@ -5958,12 +5958,14 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
||||
|| init_values[i].init_expr_type
|
||||
== INIT_EXPR_TYPE_FUNCREF_CONST);
|
||||
#if WASM_ENABLE_GC == 0
|
||||
table_elems[i] =
|
||||
(table_elem_type_t)init_values[i].u.ref_index;
|
||||
table_elems[i] = (table_elem_type_t)init_values[i]
|
||||
.u.unary.v.ref_index;
|
||||
#else
|
||||
if (init_values[i].u.ref_index != UINT32_MAX) {
|
||||
if (init_values[i].u.unary.v.ref_index
|
||||
!= UINT32_MAX) {
|
||||
if (!(func_obj = wasm_create_func_obj(
|
||||
module, init_values[i].u.ref_index,
|
||||
module,
|
||||
init_values[i].u.unary.v.ref_index,
|
||||
true, NULL, 0))) {
|
||||
goto got_exception;
|
||||
}
|
||||
|
||||
@ -5350,12 +5350,14 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
||||
|| init_values[i].init_expr_type
|
||||
== INIT_EXPR_TYPE_FUNCREF_CONST);
|
||||
#if WASM_ENABLE_GC == 0
|
||||
table_elems[i] =
|
||||
(table_elem_type_t)init_values[i].u.ref_index;
|
||||
table_elems[i] = (table_elem_type_t)init_values[i]
|
||||
.u.unary.v.ref_index;
|
||||
#else
|
||||
if (init_values[i].u.ref_index != UINT32_MAX) {
|
||||
if (init_values[i].u.unary.v.ref_index
|
||||
!= UINT32_MAX) {
|
||||
if (!(func_obj = wasm_create_func_obj(
|
||||
module, init_values[i].u.ref_index,
|
||||
module,
|
||||
init_values[i].u.unary.v.ref_index,
|
||||
true, NULL, 0))) {
|
||||
goto got_exception;
|
||||
}
|
||||
|
||||
@ -453,6 +453,9 @@ typedef struct InitValue {
|
||||
WASMRefType ref_type;
|
||||
#endif
|
||||
WASMValue value;
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
InitializerExpression *expr;
|
||||
#endif
|
||||
} InitValue;
|
||||
|
||||
typedef struct ConstExprContext {
|
||||
@ -477,7 +480,11 @@ push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
|
||||
#if WASM_ENABLE_GC != 0
|
||||
WASMRefType *ref_type, uint8 gc_opcode,
|
||||
#endif
|
||||
WASMValue *value, char *error_buf, uint32 error_buf_size)
|
||||
WASMValue *value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
InitializerExpression *expr,
|
||||
#endif
|
||||
char *error_buf, uint32 error_buf_size)
|
||||
{
|
||||
InitValue *cur_value;
|
||||
|
||||
@ -503,6 +510,10 @@ push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
|
||||
cur_value->flag = flag;
|
||||
cur_value->value = *value;
|
||||
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
cur_value->expr = expr;
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_GC != 0
|
||||
cur_value->gc_opcode = gc_opcode;
|
||||
if (wasm_is_type_multi_byte_type(type)) {
|
||||
@ -587,7 +598,11 @@ pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
|
||||
#if WASM_ENABLE_GC != 0
|
||||
WASMRefType *ref_type, uint8 *p_gc_opcode,
|
||||
#endif
|
||||
WASMValue *p_value, char *error_buf, uint32 error_buf_size)
|
||||
WASMValue *p_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
InitializerExpression **p_expr,
|
||||
#endif
|
||||
char *error_buf, uint32 error_buf_size)
|
||||
{
|
||||
InitValue *cur_value;
|
||||
|
||||
@ -623,7 +638,10 @@ pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
|
||||
if (p_gc_opcode)
|
||||
*p_gc_opcode = cur_value->gc_opcode;
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
if (p_expr)
|
||||
*p_expr = cur_value->expr;
|
||||
#endif
|
||||
return true;
|
||||
|
||||
#if WASM_ENABLE_GC != 0
|
||||
@ -639,7 +657,7 @@ fail:
|
||||
}
|
||||
|
||||
static void
|
||||
destroy_const_expr_stack(ConstExprContext *ctx)
|
||||
destroy_const_expr_stack(ConstExprContext *ctx, bool free_exprs)
|
||||
{
|
||||
#if WASM_ENABLE_GC != 0
|
||||
uint32 i;
|
||||
@ -654,24 +672,62 @@ destroy_const_expr_stack(ConstExprContext *ctx)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
if (free_exprs) {
|
||||
for (uint32 j = 0; j < ctx->sp; j++) {
|
||||
if (is_expr_binary_op(ctx->stack[j].expr->init_expr_type)) {
|
||||
destroy_init_expr_recursive(ctx->stack[j].expr);
|
||||
ctx->stack[j].expr = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (ctx->stack != ctx->data) {
|
||||
wasm_runtime_free(ctx->stack);
|
||||
}
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_GC != 0
|
||||
#if WASM_ENABLE_GC != 0 || WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
static void
|
||||
destroy_init_expr(WASMModule *module, InitializerExpression *expr)
|
||||
{
|
||||
#if WASM_ENABLE_GC != 0
|
||||
if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
|
||||
|| expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
|
||||
|| expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
|
||||
destroy_init_expr_data_recursive(module, expr->u.data);
|
||||
destroy_init_expr_data_recursive(module, expr->u.unary.v.data);
|
||||
}
|
||||
}
|
||||
#endif /* end of WASM_ENABLE_GC != 0 */
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
// free left expr and right exprs for binary oprand
|
||||
if (!is_expr_binary_op(expr->init_expr_type)) {
|
||||
return;
|
||||
}
|
||||
if (expr->u.binary.l_expr) {
|
||||
destroy_init_expr_recursive(expr->u.binary.l_expr);
|
||||
}
|
||||
if (expr->u.binary.r_expr) {
|
||||
destroy_init_expr_recursive(expr->u.binary.r_expr);
|
||||
}
|
||||
expr->u.binary.l_expr = expr->u.binary.r_expr = NULL;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
/* for init expr
|
||||
* (data (i32.add (i32.const 0) (i32.sub (i32.const 1) (i32.const 2)))),
|
||||
* the binary format is
|
||||
* 0x11: 41 00 ; i32.const 0
|
||||
* 0x13: 41 01 ; i32.const 1
|
||||
* 0x15: 41 02 ; i32.const 2
|
||||
* 0x17: 6b ; i32.sub
|
||||
* 0x18: 6a ; i32.add
|
||||
* for traversal: read opcodes and push them onto the stack. When encountering
|
||||
* a binary opcode, pop two values from the stack which become the left and
|
||||
* right child nodes of this binary operation node.
|
||||
*/
|
||||
static bool
|
||||
load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
InitializerExpression *init_expr, uint8 type, void *ref_type,
|
||||
@ -687,6 +743,9 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
uint8 opcode;
|
||||
WASMRefType cur_ref_type = { 0 };
|
||||
#endif
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
InitializerExpression *cur_expr = NULL;
|
||||
#endif
|
||||
|
||||
init_const_expr_stack(&const_expr_ctx, module);
|
||||
|
||||
@ -699,24 +758,32 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
case INIT_EXPR_TYPE_I32_CONST:
|
||||
read_leb_int32(p, p_end, cur_value.i32);
|
||||
|
||||
if (!push_const_expr_stack(
|
||||
&const_expr_ctx, flag, VALUE_TYPE_I32,
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag,
|
||||
VALUE_TYPE_I32,
|
||||
#if WASM_ENABLE_GC != 0
|
||||
NULL, 0,
|
||||
NULL, 0,
|
||||
#endif
|
||||
&cur_value, error_buf, error_buf_size))
|
||||
&cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size))
|
||||
goto fail;
|
||||
break;
|
||||
/* i64.const */
|
||||
case INIT_EXPR_TYPE_I64_CONST:
|
||||
read_leb_int64(p, p_end, cur_value.i64);
|
||||
|
||||
if (!push_const_expr_stack(
|
||||
&const_expr_ctx, flag, VALUE_TYPE_I64,
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag,
|
||||
VALUE_TYPE_I64,
|
||||
#if WASM_ENABLE_GC != 0
|
||||
NULL, 0,
|
||||
NULL, 0,
|
||||
#endif
|
||||
&cur_value, error_buf, error_buf_size))
|
||||
&cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size))
|
||||
goto fail;
|
||||
break;
|
||||
/* f32.const */
|
||||
@ -726,12 +793,16 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
for (i = 0; i < sizeof(float32); i++)
|
||||
*p_float++ = *p++;
|
||||
|
||||
if (!push_const_expr_stack(
|
||||
&const_expr_ctx, flag, VALUE_TYPE_F32,
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag,
|
||||
VALUE_TYPE_F32,
|
||||
#if WASM_ENABLE_GC != 0
|
||||
NULL, 0,
|
||||
NULL, 0,
|
||||
#endif
|
||||
&cur_value, error_buf, error_buf_size))
|
||||
&cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size))
|
||||
goto fail;
|
||||
break;
|
||||
/* f64.const */
|
||||
@ -741,12 +812,16 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
for (i = 0; i < sizeof(float64); i++)
|
||||
*p_float++ = *p++;
|
||||
|
||||
if (!push_const_expr_stack(
|
||||
&const_expr_ctx, flag, VALUE_TYPE_F64,
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag,
|
||||
VALUE_TYPE_F64,
|
||||
#if WASM_ENABLE_GC != 0
|
||||
NULL, 0,
|
||||
NULL, 0,
|
||||
#endif
|
||||
&cur_value, error_buf, error_buf_size))
|
||||
&cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size))
|
||||
goto fail;
|
||||
break;
|
||||
#if WASM_ENABLE_SIMD != 0
|
||||
@ -767,12 +842,16 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
cur_value.v128.i64x2[0] = high;
|
||||
cur_value.v128.i64x2[1] = low;
|
||||
|
||||
if (!push_const_expr_stack(
|
||||
&const_expr_ctx, flag, VALUE_TYPE_V128,
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag,
|
||||
VALUE_TYPE_V128,
|
||||
#if WASM_ENABLE_GC != 0
|
||||
NULL, 0,
|
||||
NULL, 0,
|
||||
#endif
|
||||
&cur_value, error_buf, error_buf_size))
|
||||
&cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size))
|
||||
goto fail;
|
||||
#if WASM_ENABLE_WAMR_COMPILER != 0
|
||||
/* If any init_expr is v128.const, mark SIMD used */
|
||||
@ -783,7 +862,92 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
#endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
|
||||
(WASM_ENABLE_FAST_INTERP != 0) */
|
||||
#endif /* end of WASM_ENABLE_SIMD */
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
case INIT_EXPR_TYPE_I32_ADD:
|
||||
case INIT_EXPR_TYPE_I32_SUB:
|
||||
case INIT_EXPR_TYPE_I32_MUL:
|
||||
case INIT_EXPR_TYPE_I64_ADD:
|
||||
case INIT_EXPR_TYPE_I64_SUB:
|
||||
case INIT_EXPR_TYPE_I64_MUL:
|
||||
{
|
||||
|
||||
InitializerExpression *l_expr, *r_expr;
|
||||
WASMValue l_value, r_value;
|
||||
uint8 l_flag, r_flag;
|
||||
uint8 value_type;
|
||||
|
||||
if (flag == INIT_EXPR_TYPE_I32_ADD
|
||||
|| flag == INIT_EXPR_TYPE_I32_SUB
|
||||
|| flag == INIT_EXPR_TYPE_I32_MUL) {
|
||||
value_type = VALUE_TYPE_I32;
|
||||
}
|
||||
else {
|
||||
value_type = VALUE_TYPE_I64;
|
||||
}
|
||||
|
||||
/* If right flag indicates a binary operation, right expr will
|
||||
* be popped from stack. Otherwise, allocate a new expr for
|
||||
* right expr. Same for left expr.
|
||||
*/
|
||||
if (!(pop_const_expr_stack(&const_expr_ctx, &r_flag, value_type,
|
||||
#if WASM_ENABLE_GC != 0
|
||||
NULL, NULL,
|
||||
#endif
|
||||
&r_value, &r_expr, error_buf,
|
||||
error_buf_size))) {
|
||||
goto fail;
|
||||
}
|
||||
if (!is_expr_binary_op(r_flag)) {
|
||||
if (!(r_expr = loader_malloc(sizeof(InitializerExpression),
|
||||
error_buf, error_buf_size))) {
|
||||
goto fail;
|
||||
}
|
||||
r_expr->init_expr_type = r_flag;
|
||||
r_expr->u.unary.v = r_value;
|
||||
}
|
||||
|
||||
if (!(pop_const_expr_stack(&const_expr_ctx, &l_flag, value_type,
|
||||
#if WASM_ENABLE_GC != 0
|
||||
NULL, NULL,
|
||||
#endif
|
||||
&l_value, &l_expr, error_buf,
|
||||
error_buf_size))) {
|
||||
destroy_init_expr_recursive(r_expr);
|
||||
goto fail;
|
||||
}
|
||||
if (!is_expr_binary_op(l_flag)) {
|
||||
if (!(l_expr = loader_malloc(sizeof(InitializerExpression),
|
||||
error_buf, error_buf_size))) {
|
||||
destroy_init_expr_recursive(r_expr);
|
||||
goto fail;
|
||||
}
|
||||
l_expr->init_expr_type = l_flag;
|
||||
l_expr->u.unary.v = l_value;
|
||||
}
|
||||
|
||||
if (!(cur_expr = loader_malloc(sizeof(InitializerExpression),
|
||||
error_buf, error_buf_size))) {
|
||||
destroy_init_expr_recursive(l_expr);
|
||||
destroy_init_expr_recursive(r_expr);
|
||||
goto fail;
|
||||
}
|
||||
cur_expr->init_expr_type = flag;
|
||||
cur_expr->u.binary.l_expr = l_expr;
|
||||
cur_expr->u.binary.r_expr = r_expr;
|
||||
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag, value_type,
|
||||
#if WASM_ENABLE_GC != 0
|
||||
NULL, 0,
|
||||
#endif
|
||||
&cur_value, cur_expr, error_buf,
|
||||
error_buf_size)) {
|
||||
destroy_init_expr_recursive(cur_expr);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
#endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR */
|
||||
#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
|
||||
/* ref.func */
|
||||
case INIT_EXPR_TYPE_FUNCREF_CONST:
|
||||
@ -799,6 +963,9 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
#if WASM_ENABLE_GC == 0
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag,
|
||||
VALUE_TYPE_FUNCREF, &cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size))
|
||||
goto fail;
|
||||
#else
|
||||
@ -816,8 +983,11 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
false, type_idx);
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag,
|
||||
cur_ref_type.ref_type, &cur_ref_type,
|
||||
0, &cur_value, error_buf,
|
||||
error_buf_size))
|
||||
0, &cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size))
|
||||
goto fail;
|
||||
#endif
|
||||
#if WASM_ENABLE_WAMR_COMPILER != 0
|
||||
@ -837,8 +1007,11 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
|
||||
cur_value.ref_index = NULL_REF;
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
|
||||
&cur_value, error_buf,
|
||||
error_buf_size))
|
||||
&cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size))
|
||||
goto fail;
|
||||
#else
|
||||
int32 heap_type;
|
||||
@ -861,13 +1034,19 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag,
|
||||
cur_ref_type.ref_type,
|
||||
&cur_ref_type, 0, &cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size))
|
||||
goto fail;
|
||||
}
|
||||
else {
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
|
||||
NULL, 0, &cur_value, error_buf,
|
||||
error_buf_size))
|
||||
NULL, 0, &cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size))
|
||||
goto fail;
|
||||
}
|
||||
#endif
|
||||
@ -956,8 +1135,11 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
#if WASM_ENABLE_GC != 0
|
||||
&cur_ref_type, 0,
|
||||
#endif
|
||||
&cur_value, error_buf,
|
||||
error_buf_size))
|
||||
&cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size))
|
||||
goto fail;
|
||||
|
||||
break;
|
||||
@ -1020,6 +1202,9 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
&const_expr_ctx, NULL, field_type,
|
||||
field_ref_type, NULL,
|
||||
&struct_init_values->fields[field_idx],
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
destroy_init_expr_data_recursive(
|
||||
module, struct_init_values);
|
||||
@ -1033,6 +1218,9 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
if (!push_const_expr_stack(
|
||||
&const_expr_ctx, flag, cur_ref_type.ref_type,
|
||||
&cur_ref_type, (uint8)opcode1, &cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
destroy_init_expr_data_recursive(
|
||||
module, struct_init_values);
|
||||
@ -1064,6 +1252,9 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
if (!push_const_expr_stack(
|
||||
&const_expr_ctx, flag, cur_ref_type.ref_type,
|
||||
&cur_ref_type, (uint8)opcode1, &cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
@ -1112,8 +1303,11 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
|
||||
if (!pop_const_expr_stack(
|
||||
&const_expr_ctx, NULL, VALUE_TYPE_I32,
|
||||
NULL, NULL, &len_val, error_buf,
|
||||
error_buf_size)) {
|
||||
NULL, NULL, &len_val,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
@ -1132,6 +1326,9 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
&const_expr_ctx, NULL, elem_type,
|
||||
elem_ref_type, NULL,
|
||||
&array_init_values->elem_data[0],
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
destroy_init_expr_data_recursive(
|
||||
module, array_init_values);
|
||||
@ -1164,6 +1361,9 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
elem_ref_type, NULL,
|
||||
&array_init_values
|
||||
->elem_data[i - 1],
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
destroy_init_expr_data_recursive(
|
||||
module, array_init_values);
|
||||
@ -1180,10 +1380,13 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
uint32 len;
|
||||
|
||||
/* POP(i32) */
|
||||
if (!pop_const_expr_stack(&const_expr_ctx, NULL,
|
||||
VALUE_TYPE_I32, NULL,
|
||||
NULL, &len_val, error_buf,
|
||||
error_buf_size)) {
|
||||
if (!pop_const_expr_stack(
|
||||
&const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
|
||||
NULL, &len_val,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
len = len_val.i32;
|
||||
@ -1197,6 +1400,9 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
if (!push_const_expr_stack(
|
||||
&const_expr_ctx, flag, cur_ref_type.ref_type,
|
||||
&cur_ref_type, (uint8)opcode1, &cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
if (array_init_values) {
|
||||
destroy_init_expr_data_recursive(
|
||||
@ -1223,9 +1429,13 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
case WASM_OP_REF_I31:
|
||||
{
|
||||
/* POP(i32) */
|
||||
if (!pop_const_expr_stack(
|
||||
&const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
|
||||
NULL, &cur_value, error_buf, error_buf_size)) {
|
||||
if (!pop_const_expr_stack(&const_expr_ctx, NULL,
|
||||
VALUE_TYPE_I32, NULL, NULL,
|
||||
&cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
@ -1234,6 +1444,9 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
if (!push_const_expr_stack(
|
||||
&const_expr_ctx, flag, cur_ref_type.ref_type,
|
||||
&cur_ref_type, (uint8)opcode1, &cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
@ -1268,7 +1481,11 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
#if WASM_ENABLE_GC != 0
|
||||
ref_type, &opcode,
|
||||
#endif
|
||||
&cur_value, error_buf, error_buf_size)) {
|
||||
&cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
&cur_expr,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
@ -1278,8 +1495,21 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
goto fail;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
if (cur_expr != NULL) {
|
||||
bh_memcpy_s(init_expr, sizeof(InitializerExpression), cur_expr,
|
||||
sizeof(InitializerExpression));
|
||||
wasm_runtime_free(cur_expr);
|
||||
}
|
||||
else {
|
||||
init_expr->init_expr_type = flag;
|
||||
init_expr->u.unary.v = cur_value;
|
||||
}
|
||||
|
||||
#else
|
||||
init_expr->init_expr_type = flag;
|
||||
init_expr->u = cur_value;
|
||||
init_expr->u.unary.v = cur_value;
|
||||
#endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR != 0 */
|
||||
|
||||
#if WASM_ENABLE_GC != 0
|
||||
if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
|
||||
@ -1310,11 +1540,11 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
#endif /* end of WASM_ENABLE_GC != 0 */
|
||||
|
||||
*p_buf = p;
|
||||
destroy_const_expr_stack(&const_expr_ctx);
|
||||
destroy_const_expr_stack(&const_expr_ctx, false);
|
||||
return true;
|
||||
|
||||
fail:
|
||||
destroy_const_expr_stack(&const_expr_ctx);
|
||||
destroy_const_expr_stack(&const_expr_ctx, true);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -4072,9 +4302,9 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
||||
if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
|
||||
uint8 global_type;
|
||||
WASMRefType *global_ref_type;
|
||||
uint32 global_idx = global->init_expr.u.global_index;
|
||||
uint32 global_idx = global->init_expr.u.unary.v.global_index;
|
||||
|
||||
if (global->init_expr.u.global_index
|
||||
if (global->init_expr.u.unary.v.global_index
|
||||
>= module->import_global_count + i) {
|
||||
set_error_buf(error_buf, error_buf_size, "unknown global");
|
||||
return false;
|
||||
@ -4471,7 +4701,7 @@ load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
|
||||
}
|
||||
|
||||
init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
|
||||
init_expr->u.ref_index = function_index;
|
||||
init_expr->u.unary.v.ref_index = function_index;
|
||||
}
|
||||
|
||||
*p_buf = p;
|
||||
@ -4744,7 +4974,7 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
|
||||
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
if (table_elem_idx_type == VALUE_TYPE_I64
|
||||
&& table_segment->base_offset.u.u64 > UINT32_MAX) {
|
||||
&& table_segment->base_offset.u.unary.v.u64 > UINT32_MAX) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"In table64, table base offset can't be "
|
||||
"larger than UINT32_MAX");
|
||||
@ -4904,6 +5134,9 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
|
||||
|
||||
if (!(dataseg = module->data_segments[i] = loader_malloc(
|
||||
sizeof(WASMDataSeg), error_buf, error_buf_size))) {
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
destroy_init_expr(module, &init_expr);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -6031,7 +6264,8 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
||||
&& global->init_expr.init_expr_type
|
||||
== INIT_EXPR_TYPE_I32_CONST) {
|
||||
aux_heap_base_global = global;
|
||||
aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
|
||||
aux_heap_base =
|
||||
(uint64)(uint32)global->init_expr.u.unary.v.i32;
|
||||
aux_heap_base_global_index = export->index;
|
||||
LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
|
||||
aux_heap_base);
|
||||
@ -6052,7 +6286,8 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
||||
&& global->init_expr.init_expr_type
|
||||
== INIT_EXPR_TYPE_I32_CONST) {
|
||||
aux_data_end_global = global;
|
||||
aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
|
||||
aux_data_end =
|
||||
(uint64)(uint32)global->init_expr.u.unary.v.i32;
|
||||
aux_data_end_global_index = export->index;
|
||||
LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
|
||||
aux_data_end);
|
||||
@ -6093,10 +6328,11 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
||||
&& global->type.val_type == VALUE_TYPE_I32
|
||||
&& global->init_expr.init_expr_type
|
||||
== INIT_EXPR_TYPE_I32_CONST
|
||||
&& (uint64)(uint32)global->init_expr.u.i32
|
||||
&& (uint64)(uint32)global->init_expr.u.unary.v.i32
|
||||
<= aux_heap_base) {
|
||||
aux_stack_top_global = global;
|
||||
aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
|
||||
aux_stack_top =
|
||||
(uint64)(uint32)global->init_expr.u.unary.v.i32;
|
||||
module->aux_stack_top_global_index =
|
||||
module->import_global_count + global_index;
|
||||
module->aux_stack_bottom = aux_stack_top;
|
||||
@ -6947,7 +7183,7 @@ wasm_loader_unload(WASMModule *module)
|
||||
wasm_runtime_free(module->memories);
|
||||
|
||||
if (module->globals) {
|
||||
#if WASM_ENABLE_GC != 0
|
||||
#if WASM_ENABLE_GC != 0 || WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
for (i = 0; i < module->global_count; i++) {
|
||||
destroy_init_expr(module, &module->globals[i].init_expr);
|
||||
}
|
||||
@ -6980,6 +7216,9 @@ wasm_loader_unload(WASMModule *module)
|
||||
#endif
|
||||
wasm_runtime_free(module->table_segments[i].init_values);
|
||||
}
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
destroy_init_expr(module, &module->table_segments[i].base_offset);
|
||||
#endif
|
||||
}
|
||||
wasm_runtime_free(module->table_segments);
|
||||
}
|
||||
@ -6989,6 +7228,10 @@ wasm_loader_unload(WASMModule *module)
|
||||
if (module->data_segments[i]) {
|
||||
if (module->data_segments[i]->is_data_cloned)
|
||||
wasm_runtime_free(module->data_segments[i]->data);
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
destroy_init_expr(module,
|
||||
&(module->data_segments[i]->base_offset));
|
||||
#endif
|
||||
wasm_runtime_free(module->data_segments[i]);
|
||||
}
|
||||
}
|
||||
@ -13260,7 +13503,8 @@ re_scan:
|
||||
== VALUE_TYPE_FUNCREF
|
||||
&& module->globals[i].init_expr.init_expr_type
|
||||
== INIT_EXPR_TYPE_FUNCREF_CONST
|
||||
&& module->globals[i].init_expr.u.u32 == func_idx) {
|
||||
&& module->globals[i].init_expr.u.unary.v.u32
|
||||
== func_idx) {
|
||||
func_declared = true;
|
||||
break;
|
||||
}
|
||||
@ -13289,7 +13533,8 @@ re_scan:
|
||||
#endif
|
||||
) {
|
||||
for (j = 0; j < table_seg->value_count; j++) {
|
||||
if (table_seg->init_values[j].u.ref_index
|
||||
if (table_seg->init_values[j]
|
||||
.u.unary.v.ref_index
|
||||
== func_idx) {
|
||||
func_declared = true;
|
||||
break;
|
||||
|
||||
@ -261,6 +261,9 @@ typedef struct InitValue {
|
||||
uint8 type;
|
||||
uint8 flag;
|
||||
WASMValue value;
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
InitializerExpression *expr;
|
||||
#endif
|
||||
} InitValue;
|
||||
|
||||
typedef struct ConstExprContext {
|
||||
@ -282,7 +285,11 @@ init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
|
||||
|
||||
static bool
|
||||
push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
|
||||
WASMValue *value, char *error_buf, uint32 error_buf_size)
|
||||
WASMValue *value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
InitializerExpression *expr,
|
||||
#endif
|
||||
char *error_buf, uint32 error_buf_size)
|
||||
{
|
||||
InitValue *cur_value;
|
||||
|
||||
@ -305,6 +312,9 @@ push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
|
||||
cur_value->type = type;
|
||||
cur_value->flag = flag;
|
||||
cur_value->value = *value;
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
cur_value->expr = expr;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
fail:
|
||||
@ -313,7 +323,11 @@ fail:
|
||||
|
||||
static bool
|
||||
pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
|
||||
WASMValue *p_value, char *error_buf, uint32 error_buf_size)
|
||||
WASMValue *p_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
InitializerExpression **p_expr,
|
||||
#endif
|
||||
char *error_buf, uint32 error_buf_size)
|
||||
{
|
||||
InitValue *cur_value;
|
||||
|
||||
@ -331,18 +345,50 @@ pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
|
||||
*p_flag = cur_value->flag;
|
||||
if (p_value)
|
||||
*p_value = cur_value->value;
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
if (p_expr)
|
||||
*p_expr = cur_value->expr;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
destroy_const_expr_stack(ConstExprContext *ctx)
|
||||
destroy_const_expr_stack(ConstExprContext *ctx, bool free_exprs)
|
||||
{
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
if (free_exprs) {
|
||||
for (uint32 j = 0; j < ctx->sp; j++) {
|
||||
if (is_expr_binary_op(ctx->stack[j].expr->init_expr_type)) {
|
||||
destroy_init_expr_recursive(ctx->stack[j].expr);
|
||||
ctx->stack[j].expr = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (ctx->stack != ctx->data) {
|
||||
wasm_runtime_free(ctx->stack);
|
||||
}
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
static void
|
||||
destroy_init_expr(InitializerExpression *expr)
|
||||
{
|
||||
// free left expr and right exprs for binary oprand
|
||||
if (is_expr_binary_op(expr->init_expr_type)) {
|
||||
return;
|
||||
}
|
||||
if (expr->u.binary.l_expr) {
|
||||
destroy_init_expr_recursive(expr->u.binary.l_expr);
|
||||
}
|
||||
if (expr->u.binary.r_expr) {
|
||||
destroy_init_expr_recursive(expr->u.binary.r_expr);
|
||||
}
|
||||
expr->u.binary.l_expr = expr->u.binary.r_expr = NULL;
|
||||
}
|
||||
#endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR != 0 */
|
||||
|
||||
static bool
|
||||
load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
InitializerExpression *init_expr, uint8 type, char *error_buf,
|
||||
@ -353,6 +399,9 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
uint32 i;
|
||||
ConstExprContext const_expr_ctx = { 0 };
|
||||
WASMValue cur_value = { 0 };
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
InitializerExpression *cur_expr = NULL;
|
||||
#endif
|
||||
|
||||
init_const_expr_stack(&const_expr_ctx, module);
|
||||
|
||||
@ -367,8 +416,11 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag,
|
||||
VALUE_TYPE_I32, &cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
bh_assert(0);
|
||||
goto fail;
|
||||
}
|
||||
break;
|
||||
/* i64.const */
|
||||
@ -377,8 +429,11 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag,
|
||||
VALUE_TYPE_I64, &cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
bh_assert(0);
|
||||
goto fail;
|
||||
}
|
||||
break;
|
||||
/* f32.const */
|
||||
@ -390,8 +445,11 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag,
|
||||
VALUE_TYPE_F32, &cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
bh_assert(0);
|
||||
goto fail;
|
||||
}
|
||||
break;
|
||||
/* f64.const */
|
||||
@ -403,8 +461,11 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag,
|
||||
VALUE_TYPE_F64, &cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
bh_assert(0);
|
||||
goto fail;
|
||||
}
|
||||
break;
|
||||
|
||||
@ -417,13 +478,16 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
cur_value.ref_index = func_idx;
|
||||
if (!check_function_index(module, func_idx, error_buf,
|
||||
error_buf_size)) {
|
||||
bh_assert(0);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag,
|
||||
VALUE_TYPE_FUNCREF, &cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
bh_assert(0);
|
||||
goto fail;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -438,9 +502,12 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
|
||||
cur_value.ref_index = UINT32_MAX;
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
|
||||
&cur_value, error_buf,
|
||||
error_buf_size)) {
|
||||
bh_assert(0);
|
||||
&cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -471,15 +538,93 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
}
|
||||
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
|
||||
&cur_value, error_buf,
|
||||
error_buf_size))
|
||||
bh_assert(0);
|
||||
&cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
NULL,
|
||||
#endif
|
||||
error_buf, error_buf_size))
|
||||
goto fail;
|
||||
|
||||
break;
|
||||
}
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
case INIT_EXPR_TYPE_I32_ADD:
|
||||
case INIT_EXPR_TYPE_I64_ADD:
|
||||
case INIT_EXPR_TYPE_I32_SUB:
|
||||
case INIT_EXPR_TYPE_I64_SUB:
|
||||
case INIT_EXPR_TYPE_I32_MUL:
|
||||
case INIT_EXPR_TYPE_I64_MUL:
|
||||
{
|
||||
InitializerExpression *l_expr, *r_expr;
|
||||
WASMValue l_value, r_value;
|
||||
uint8 l_flag, r_flag;
|
||||
uint8 value_type;
|
||||
|
||||
if (flag == INIT_EXPR_TYPE_I32_ADD
|
||||
|| flag == INIT_EXPR_TYPE_I32_SUB
|
||||
|| flag == INIT_EXPR_TYPE_I32_MUL) {
|
||||
value_type = VALUE_TYPE_I32;
|
||||
}
|
||||
else {
|
||||
value_type = VALUE_TYPE_I64;
|
||||
}
|
||||
|
||||
/* If right flag indicates a binary operation, right expr will
|
||||
* be popped from stack. Otherwise, allocate a new expr for
|
||||
* right expr. Same for left expr.
|
||||
*/
|
||||
if (!(pop_const_expr_stack(&const_expr_ctx, &r_flag, value_type,
|
||||
&r_value, &r_expr, error_buf,
|
||||
error_buf_size))) {
|
||||
goto fail;
|
||||
}
|
||||
if (!is_expr_binary_op(r_flag)) {
|
||||
if (!(r_expr = loader_malloc(sizeof(InitializerExpression),
|
||||
error_buf, error_buf_size))) {
|
||||
goto fail;
|
||||
}
|
||||
r_expr->init_expr_type = r_flag;
|
||||
r_expr->u.unary.v = r_value;
|
||||
}
|
||||
|
||||
if (!(pop_const_expr_stack(&const_expr_ctx, &l_flag, value_type,
|
||||
&l_value, &l_expr, error_buf,
|
||||
error_buf_size))) {
|
||||
destroy_init_expr_recursive(r_expr);
|
||||
goto fail;
|
||||
}
|
||||
if (!is_expr_binary_op(l_flag)) {
|
||||
if (!(l_expr = loader_malloc(sizeof(InitializerExpression),
|
||||
error_buf, error_buf_size))) {
|
||||
destroy_init_expr_recursive(r_expr);
|
||||
goto fail;
|
||||
}
|
||||
l_expr->init_expr_type = l_flag;
|
||||
l_expr->u.unary.v = l_value;
|
||||
}
|
||||
|
||||
if (!(cur_expr = loader_malloc(sizeof(InitializerExpression),
|
||||
error_buf, error_buf_size))) {
|
||||
destroy_init_expr_recursive(l_expr);
|
||||
destroy_init_expr_recursive(r_expr);
|
||||
goto fail;
|
||||
}
|
||||
cur_expr->init_expr_type = flag;
|
||||
cur_expr->u.binary.l_expr = l_expr;
|
||||
cur_expr->u.binary.r_expr = r_expr;
|
||||
|
||||
if (!push_const_expr_stack(&const_expr_ctx, flag, value_type,
|
||||
&cur_value, cur_expr, error_buf,
|
||||
error_buf_size)) {
|
||||
destroy_init_expr_recursive(cur_expr);
|
||||
goto fail;
|
||||
}
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
default:
|
||||
{
|
||||
bh_assert(0);
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
@ -489,18 +634,42 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
||||
|
||||
/* There should be only one value left on the init value stack */
|
||||
if (!pop_const_expr_stack(&const_expr_ctx, &flag, type, &cur_value,
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
&cur_expr,
|
||||
#endif
|
||||
error_buf, error_buf_size)) {
|
||||
bh_assert(0);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
bh_assert(const_expr_ctx.sp == 0);
|
||||
if (const_expr_ctx.sp != 0) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"type mismatch: illegal constant opcode sequence");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
if (cur_expr != NULL) {
|
||||
bh_memcpy_s(init_expr, sizeof(InitializerExpression), cur_expr,
|
||||
sizeof(InitializerExpression));
|
||||
wasm_runtime_free(cur_expr);
|
||||
}
|
||||
else {
|
||||
init_expr->init_expr_type = flag;
|
||||
init_expr->u.unary.v = cur_value;
|
||||
}
|
||||
|
||||
#else
|
||||
init_expr->init_expr_type = flag;
|
||||
init_expr->u = cur_value;
|
||||
init_expr->u.unary.v = cur_value;
|
||||
#endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR != 0 */
|
||||
|
||||
*p_buf = p;
|
||||
destroy_const_expr_stack(&const_expr_ctx);
|
||||
destroy_const_expr_stack(&const_expr_ctx, false);
|
||||
return true;
|
||||
|
||||
fail:
|
||||
destroy_const_expr_stack(&const_expr_ctx, true);
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool
|
||||
@ -1385,13 +1554,14 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
||||
* global.get instructions are
|
||||
* only allowed to refer to imported globals.
|
||||
*/
|
||||
uint32 target_global_index = global->init_expr.u.global_index;
|
||||
uint32 target_global_index =
|
||||
global->init_expr.u.unary.v.global_index;
|
||||
bh_assert(target_global_index < module->import_global_count);
|
||||
(void)target_global_index;
|
||||
}
|
||||
else if (INIT_EXPR_TYPE_FUNCREF_CONST
|
||||
== global->init_expr.init_expr_type) {
|
||||
bh_assert(global->init_expr.u.ref_index
|
||||
bh_assert(global->init_expr.u.unary.v.ref_index
|
||||
< module->import_function_count
|
||||
+ module->function_count);
|
||||
}
|
||||
@ -1575,7 +1745,7 @@ load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
|
||||
}
|
||||
|
||||
init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
|
||||
init_expr->u.ref_index = function_index;
|
||||
init_expr->u.unary.v.ref_index = function_index;
|
||||
}
|
||||
|
||||
*p_buf = p;
|
||||
@ -1890,6 +2060,9 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
|
||||
|
||||
if (!(dataseg = module->data_segments[i] = loader_malloc(
|
||||
sizeof(WASMDataSeg), error_buf, error_buf_size))) {
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
destroy_init_expr(&init_expr);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -2778,7 +2951,8 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
||||
&& global->init_expr.init_expr_type
|
||||
== INIT_EXPR_TYPE_I32_CONST) {
|
||||
aux_heap_base_global = global;
|
||||
aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
|
||||
aux_heap_base =
|
||||
(uint64)(uint32)global->init_expr.u.unary.v.i32;
|
||||
aux_heap_base_global_index = export->index;
|
||||
LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
|
||||
aux_heap_base);
|
||||
@ -2798,7 +2972,8 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
||||
&& global->init_expr.init_expr_type
|
||||
== INIT_EXPR_TYPE_I32_CONST) {
|
||||
aux_data_end_global = global;
|
||||
aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
|
||||
aux_data_end =
|
||||
(uint64)(uint32)global->init_expr.u.unary.v.i32;
|
||||
aux_data_end_global_index = export->index;
|
||||
LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
|
||||
aux_data_end);
|
||||
@ -2838,10 +3013,11 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
||||
&& global->type.val_type == VALUE_TYPE_I32
|
||||
&& global->init_expr.init_expr_type
|
||||
== INIT_EXPR_TYPE_I32_CONST
|
||||
&& (uint64)(uint32)global->init_expr.u.i32
|
||||
&& (uint64)(uint32)global->init_expr.u.unary.v.i32
|
||||
<= aux_heap_base) {
|
||||
aux_stack_top_global = global;
|
||||
aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
|
||||
aux_stack_top =
|
||||
(uint64)(uint32)global->init_expr.u.unary.v.i32;
|
||||
module->aux_stack_top_global_index =
|
||||
module->import_global_count + global_index;
|
||||
module->aux_stack_bottom = aux_stack_top;
|
||||
@ -3448,8 +3624,14 @@ wasm_loader_unload(WASMModule *module)
|
||||
if (module->memories)
|
||||
wasm_runtime_free(module->memories);
|
||||
|
||||
if (module->globals)
|
||||
if (module->globals) {
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
for (i = 0; i < module->global_count; i++) {
|
||||
destroy_init_expr(&module->globals[i].init_expr);
|
||||
}
|
||||
#endif
|
||||
wasm_runtime_free(module->globals);
|
||||
}
|
||||
|
||||
if (module->exports)
|
||||
wasm_runtime_free(module->exports);
|
||||
@ -3458,6 +3640,9 @@ wasm_loader_unload(WASMModule *module)
|
||||
for (i = 0; i < module->table_seg_count; i++) {
|
||||
if (module->table_segments[i].init_values)
|
||||
wasm_runtime_free(module->table_segments[i].init_values);
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
destroy_init_expr(&module->table_segments[i].base_offset);
|
||||
#endif
|
||||
}
|
||||
wasm_runtime_free(module->table_segments);
|
||||
}
|
||||
@ -3467,6 +3652,9 @@ wasm_loader_unload(WASMModule *module)
|
||||
if (module->data_segments[i]) {
|
||||
if (module->data_segments[i]->is_data_cloned)
|
||||
wasm_runtime_free(module->data_segments[i]->data);
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
destroy_init_expr(&module->data_segments[i]->base_offset);
|
||||
#endif
|
||||
wasm_runtime_free(module->data_segments[i]);
|
||||
}
|
||||
}
|
||||
@ -7320,7 +7508,8 @@ re_scan:
|
||||
== VALUE_TYPE_FUNCREF
|
||||
&& module->globals[i].init_expr.init_expr_type
|
||||
== INIT_EXPR_TYPE_FUNCREF_CONST
|
||||
&& module->globals[i].init_expr.u.u32 == func_idx) {
|
||||
&& module->globals[i].init_expr.u.unary.v.ref_index
|
||||
== func_idx) {
|
||||
func_declared = true;
|
||||
break;
|
||||
}
|
||||
@ -7334,7 +7523,8 @@ re_scan:
|
||||
i++, table_seg++) {
|
||||
if (table_seg->elem_type == VALUE_TYPE_FUNCREF) {
|
||||
for (j = 0; j < table_seg->value_count; j++) {
|
||||
if (table_seg->init_values[j].u.ref_index
|
||||
if (table_seg->init_values[j]
|
||||
.u.unary.v.ref_index
|
||||
== func_idx) {
|
||||
func_declared = true;
|
||||
break;
|
||||
|
||||
@ -1165,6 +1165,81 @@ instantiate_array_global_recursive(WASMModule *module,
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool
|
||||
get_init_value_recursive(WASMModule *module, InitializerExpression *expr,
|
||||
WASMGlobalInstance *globals, WASMValue *value,
|
||||
char *error_buf, uint32 error_buf_size)
|
||||
{
|
||||
uint8 flag = expr->init_expr_type;
|
||||
switch (flag) {
|
||||
case INIT_EXPR_TYPE_GET_GLOBAL:
|
||||
{
|
||||
if (!check_global_init_expr(module, expr->u.unary.v.global_index,
|
||||
error_buf, error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
*value = globals[expr->u.unary.v.global_index].initial_value;
|
||||
break;
|
||||
}
|
||||
case INIT_EXPR_TYPE_I32_CONST:
|
||||
case INIT_EXPR_TYPE_I64_CONST:
|
||||
{
|
||||
*value = expr->u.unary.v;
|
||||
break;
|
||||
}
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
case INIT_EXPR_TYPE_I32_ADD:
|
||||
case INIT_EXPR_TYPE_I32_SUB:
|
||||
case INIT_EXPR_TYPE_I32_MUL:
|
||||
case INIT_EXPR_TYPE_I64_ADD:
|
||||
case INIT_EXPR_TYPE_I64_SUB:
|
||||
case INIT_EXPR_TYPE_I64_MUL:
|
||||
{
|
||||
WASMValue l_value, r_value;
|
||||
if (!expr->u.binary.l_expr || !expr->u.binary.r_expr) {
|
||||
goto fail;
|
||||
}
|
||||
if (!get_init_value_recursive(module, expr->u.binary.l_expr,
|
||||
globals, &l_value, error_buf,
|
||||
error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
if (!get_init_value_recursive(module, expr->u.binary.r_expr,
|
||||
globals, &r_value, error_buf,
|
||||
error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (flag == INIT_EXPR_TYPE_I32_ADD) {
|
||||
value->i32 = l_value.i32 + r_value.i32;
|
||||
}
|
||||
else if (flag == INIT_EXPR_TYPE_I32_SUB) {
|
||||
value->i32 = l_value.i32 - r_value.i32;
|
||||
}
|
||||
else if (flag == INIT_EXPR_TYPE_I32_MUL) {
|
||||
value->i32 = l_value.i32 * r_value.i32;
|
||||
}
|
||||
else if (flag == INIT_EXPR_TYPE_I64_ADD) {
|
||||
value->i64 = l_value.i64 + r_value.i64;
|
||||
}
|
||||
else if (flag == INIT_EXPR_TYPE_I64_SUB) {
|
||||
value->i64 = l_value.i64 - r_value.i64;
|
||||
}
|
||||
else if (flag == INIT_EXPR_TYPE_I64_MUL) {
|
||||
value->i64 = l_value.i64 * r_value.i64;
|
||||
}
|
||||
break;
|
||||
}
|
||||
#endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR != 0 */
|
||||
default:
|
||||
goto fail;
|
||||
}
|
||||
return true;
|
||||
fail:
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate globals in a module.
|
||||
*/
|
||||
@ -1209,7 +1284,7 @@ globals_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
|
||||
/* The linked global instance has been initialized, we
|
||||
just need to copy the value. */
|
||||
global->initial_value =
|
||||
global_import->import_global_linked->init_expr.u;
|
||||
global_import->import_global_linked->init_expr.u.unary.v;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
@ -1245,17 +1320,23 @@ globals_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
|
||||
#endif
|
||||
|
||||
switch (flag) {
|
||||
case INIT_EXPR_TYPE_I32_CONST:
|
||||
case INIT_EXPR_TYPE_I64_CONST:
|
||||
case INIT_EXPR_TYPE_GET_GLOBAL:
|
||||
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
|
||||
case INIT_EXPR_TYPE_I32_ADD:
|
||||
case INIT_EXPR_TYPE_I32_SUB:
|
||||
case INIT_EXPR_TYPE_I32_MUL:
|
||||
case INIT_EXPR_TYPE_I64_ADD:
|
||||
case INIT_EXPR_TYPE_I64_SUB:
|
||||
case INIT_EXPR_TYPE_I64_MUL:
|
||||
#endif
|
||||
{
|
||||
if (!check_global_init_expr(module, init_expr->u.global_index,
|
||||
error_buf, error_buf_size)) {
|
||||
if (!get_init_value_recursive(module, init_expr, globals,
|
||||
&global->initial_value, error_buf,
|
||||
error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
bh_memcpy_s(
|
||||
&(global->initial_value), sizeof(WASMValue),
|
||||
&(globals[init_expr->u.global_index].initial_value),
|
||||
sizeof(globals[init_expr->u.global_index].initial_value));
|
||||
break;
|
||||
}
|
||||
#if WASM_ENABLE_GC != 0
|
||||
@ -1267,11 +1348,12 @@ globals_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
|
||||
uint32 type_idx;
|
||||
|
||||
if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
|
||||
init_values = (WASMStructNewInitValues *)init_expr->u.data;
|
||||
init_values =
|
||||
(WASMStructNewInitValues *)init_expr->u.unary.v.data;
|
||||
type_idx = init_values->type_idx;
|
||||
}
|
||||
else {
|
||||
type_idx = init_expr->u.type_index;
|
||||
type_idx = init_expr->u.unary.v.type_index;
|
||||
}
|
||||
|
||||
struct_obj = instantiate_struct_global_recursive(
|
||||
@ -1294,12 +1376,14 @@ globals_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
|
||||
uint32 type_idx, len;
|
||||
|
||||
if (flag == INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT) {
|
||||
type_idx = init_expr->u.array_new_default.type_index;
|
||||
len = init_expr->u.array_new_default.length;
|
||||
type_idx =
|
||||
init_expr->u.unary.v.array_new_default.type_index;
|
||||
len = init_expr->u.unary.v.array_new_default.length;
|
||||
array_init_value = &empty_value;
|
||||
}
|
||||
else {
|
||||
init_values = (WASMArrayNewInitValues *)init_expr->u.data;
|
||||
init_values =
|
||||
(WASMArrayNewInitValues *)init_expr->u.unary.v.data;
|
||||
type_idx = init_values->type_idx;
|
||||
len = init_values->length;
|
||||
|
||||
@ -1318,13 +1402,12 @@ globals_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
|
||||
case INIT_EXPR_TYPE_I31_NEW:
|
||||
{
|
||||
global->initial_value.gc_obj =
|
||||
(wasm_obj_t)wasm_i31_obj_new(init_expr->u.i32);
|
||||
(wasm_obj_t)wasm_i31_obj_new(init_expr->u.unary.v.i32);
|
||||
break;
|
||||
}
|
||||
#endif /* end of WASM_ENABLE_GC != 0 */
|
||||
default:
|
||||
bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
|
||||
&(init_expr->u), sizeof(init_expr->u));
|
||||
global->initial_value = init_expr->u.unary.v;
|
||||
break;
|
||||
}
|
||||
|
||||
@ -2698,6 +2781,7 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
||||
uint8 *memory_data = NULL;
|
||||
uint64 memory_size = 0;
|
||||
WASMDataSeg *data_seg = module->data_segments[i];
|
||||
WASMValue offset_value;
|
||||
|
||||
#if WASM_ENABLE_BULK_MEMORY != 0
|
||||
if (data_seg->is_passive)
|
||||
@ -2717,54 +2801,37 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
||||
(uint64)memory->num_bytes_per_page * memory->cur_page_count;
|
||||
bh_assert(memory_data || memory_size == 0);
|
||||
|
||||
bh_assert(data_seg->base_offset.init_expr_type
|
||||
== INIT_EXPR_TYPE_GET_GLOBAL
|
||||
|| data_seg->base_offset.init_expr_type
|
||||
== (memory->is_memory64 ? INIT_EXPR_TYPE_I64_CONST
|
||||
: INIT_EXPR_TYPE_I32_CONST));
|
||||
uint8 offset_flag = data_seg->base_offset.init_expr_type;
|
||||
bh_assert(offset_flag == INIT_EXPR_TYPE_GET_GLOBAL
|
||||
|| (memory->is_memory64 ? is_valid_i64_offset(offset_flag)
|
||||
: is_valid_i32_offset(offset_flag)));
|
||||
|
||||
if (data_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
|
||||
if (!check_global_init_expr(module,
|
||||
data_seg->base_offset.u.global_index,
|
||||
error_buf, error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
if (!get_init_value_recursive(module, &data_seg->base_offset, globals,
|
||||
&offset_value, error_buf,
|
||||
error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (offset_flag == INIT_EXPR_TYPE_GET_GLOBAL) {
|
||||
if (!globals
|
||||
|| globals[data_seg->base_offset.u.global_index].type
|
||||
|| globals[data_seg->base_offset.u.unary.v.global_index].type
|
||||
!= (memory->is_memory64 ? VALUE_TYPE_I64
|
||||
: VALUE_TYPE_I32)) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"data segment does not fit");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
if (memory->is_memory64) {
|
||||
base_offset =
|
||||
(uint64)globals[data_seg->base_offset.u.global_index]
|
||||
.initial_value.i64;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
base_offset =
|
||||
(uint32)globals[data_seg->base_offset.u.global_index]
|
||||
.initial_value.i32;
|
||||
}
|
||||
}
|
||||
else {
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
if (memory->is_memory64) {
|
||||
base_offset = (uint64)data_seg->base_offset.u.i64;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
base_offset = (uint32)data_seg->base_offset.u.i32;
|
||||
}
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
if (memory->is_memory64) {
|
||||
base_offset = (uint64)offset_value.i64;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
base_offset = (uint32)offset_value.i32;
|
||||
}
|
||||
/* check offset */
|
||||
if (base_offset > memory_size) {
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
@ -2842,36 +2909,39 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
||||
|| table->init_expr.init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST);
|
||||
|
||||
if (table->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
|
||||
if (!check_global_init_expr(module, table->init_expr.u.global_index,
|
||||
if (!check_global_init_expr(module,
|
||||
table->init_expr.u.unary.v.global_index,
|
||||
error_buf, error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
table->init_expr.u.gc_obj =
|
||||
globals[table->init_expr.u.global_index].initial_value.gc_obj;
|
||||
table->init_expr.u.unary.v.gc_obj =
|
||||
globals[table->init_expr.u.unary.v.global_index]
|
||||
.initial_value.gc_obj;
|
||||
}
|
||||
else if (table->init_expr.init_expr_type
|
||||
== INIT_EXPR_TYPE_FUNCREF_CONST) {
|
||||
uint32 func_idx = table->init_expr.u.ref_index;
|
||||
uint32 func_idx = table->init_expr.u.unary.v.ref_index;
|
||||
if (func_idx != UINT32_MAX) {
|
||||
if (!(table->init_expr.u.gc_obj =
|
||||
if (!(table->init_expr.u.unary.v.gc_obj =
|
||||
wasm_create_func_obj(module_inst, func_idx, false,
|
||||
error_buf, error_buf_size)))
|
||||
goto fail;
|
||||
}
|
||||
else {
|
||||
table->init_expr.u.gc_obj = NULL_REF;
|
||||
table->init_expr.u.unary.v.gc_obj = NULL_REF;
|
||||
}
|
||||
}
|
||||
else if (table->init_expr.init_expr_type
|
||||
== INIT_EXPR_TYPE_REFNULL_CONST) {
|
||||
table->init_expr.u.gc_obj = NULL_REF;
|
||||
table->init_expr.u.unary.v.gc_obj = NULL_REF;
|
||||
}
|
||||
|
||||
LOG_DEBUG("Init table [%d] elements from [%d] to [%d] as: %p", i, 0,
|
||||
table_inst->cur_size, (void *)table->init_expr.u.gc_obj);
|
||||
table_inst->cur_size,
|
||||
(void *)table->init_expr.u.unary.v.gc_obj);
|
||||
for (j = 0; j < table_inst->cur_size; j++) {
|
||||
*(table_data + j) = table->init_expr.u.gc_obj;
|
||||
*(table_data + j) = table->init_expr.u.unary.v.gc_obj;
|
||||
}
|
||||
}
|
||||
#endif /* end of WASM_ENABLE_GC != 0 */
|
||||
@ -2883,6 +2953,7 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
||||
/* has check it in loader */
|
||||
WASMTableInstance *table = module_inst->tables[table_seg->table_index];
|
||||
table_elem_type_t *table_data;
|
||||
WASMValue offset_value;
|
||||
uint32 j;
|
||||
#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
|
||||
uint8 tbl_elem_type;
|
||||
@ -2941,48 +3012,37 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
||||
continue;
|
||||
#endif
|
||||
|
||||
uint8 offset_flag = table_seg->base_offset.init_expr_type;
|
||||
#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
|
||||
bh_assert(table_seg->base_offset.init_expr_type
|
||||
== INIT_EXPR_TYPE_I32_CONST
|
||||
|| table_seg->base_offset.init_expr_type
|
||||
== INIT_EXPR_TYPE_GET_GLOBAL
|
||||
|| table_seg->base_offset.init_expr_type
|
||||
== INIT_EXPR_TYPE_FUNCREF_CONST
|
||||
|| table_seg->base_offset.init_expr_type
|
||||
== INIT_EXPR_TYPE_REFNULL_CONST);
|
||||
bh_assert(offset_flag == INIT_EXPR_TYPE_GET_GLOBAL
|
||||
|| offset_flag == INIT_EXPR_TYPE_FUNCREF_CONST
|
||||
|| offset_flag == INIT_EXPR_TYPE_REFNULL_CONST
|
||||
|| is_valid_i32_offset(offset_flag));
|
||||
#else
|
||||
bh_assert(table_seg->base_offset.init_expr_type
|
||||
== INIT_EXPR_TYPE_I32_CONST
|
||||
|| table_seg->base_offset.init_expr_type
|
||||
== INIT_EXPR_TYPE_GET_GLOBAL);
|
||||
bh_assert(offset_flag == INIT_EXPR_TYPE_GET_GLOBAL
|
||||
|| is_valid_i32_offset(offset_flag));
|
||||
#endif
|
||||
|
||||
/* init vec(funcidx) or vec(expr) */
|
||||
if (table_seg->base_offset.init_expr_type
|
||||
== INIT_EXPR_TYPE_GET_GLOBAL) {
|
||||
if (!check_global_init_expr(module,
|
||||
table_seg->base_offset.u.global_index,
|
||||
error_buf, error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
if (!get_init_value_recursive(module, &table_seg->base_offset, globals,
|
||||
&offset_value, error_buf,
|
||||
error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (offset_flag == INIT_EXPR_TYPE_GET_GLOBAL) {
|
||||
if (!globals
|
||||
|| globals[table_seg->base_offset.u.global_index].type
|
||||
|| globals[table_seg->base_offset.u.unary.v.global_index].type
|
||||
!= VALUE_TYPE_I32) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"type mismatch: elements segment does not fit");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
table_seg->base_offset.u.i32 =
|
||||
globals[table_seg->base_offset.u.global_index]
|
||||
.initial_value.i32;
|
||||
}
|
||||
|
||||
/* check offset since length might negative */
|
||||
if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
|
||||
LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
|
||||
table_seg->base_offset.u.i32, table->cur_size);
|
||||
if ((uint32)offset_value.i32 > table->cur_size) {
|
||||
LOG_DEBUG("base_offset(%d) > table->cur_size(%d)", offset_value.i32,
|
||||
table->cur_size);
|
||||
#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"out of bounds table access");
|
||||
@ -2995,9 +3055,9 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
||||
|
||||
/* check offset + length(could be zero) */
|
||||
length = table_seg->value_count;
|
||||
if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
|
||||
if ((uint32)offset_value.i32 + length > table->cur_size) {
|
||||
LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
|
||||
table_seg->base_offset.u.i32, length, table->cur_size);
|
||||
offset_value.i32, length, table->cur_size);
|
||||
#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"out of bounds table access");
|
||||
@ -3027,10 +3087,10 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
||||
case INIT_EXPR_TYPE_FUNCREF_CONST:
|
||||
{
|
||||
#if WASM_ENABLE_GC == 0
|
||||
ref = (void *)(uintptr_t)init_expr->u.ref_index;
|
||||
ref = (void *)(uintptr_t)init_expr->u.unary.v.ref_index;
|
||||
#else
|
||||
WASMFuncObjectRef func_obj;
|
||||
uint32 func_idx = init_expr->u.ref_index;
|
||||
uint32 func_idx = init_expr->u.unary.v.ref_index;
|
||||
/* UINT32_MAX indicates that it is a null reference */
|
||||
if (func_idx != UINT32_MAX) {
|
||||
if (!(func_obj = wasm_create_func_obj(
|
||||
@ -3049,14 +3109,14 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
||||
#if WASM_ENABLE_GC != 0
|
||||
case INIT_EXPR_TYPE_GET_GLOBAL:
|
||||
{
|
||||
if (!check_global_init_expr(module,
|
||||
init_expr->u.global_index,
|
||||
error_buf, error_buf_size)) {
|
||||
if (!check_global_init_expr(
|
||||
module, init_expr->u.unary.v.global_index,
|
||||
error_buf, error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ref =
|
||||
globals[init_expr->u.global_index].initial_value.gc_obj;
|
||||
ref = globals[init_expr->u.unary.v.global_index]
|
||||
.initial_value.gc_obj;
|
||||
break;
|
||||
}
|
||||
case INIT_EXPR_TYPE_STRUCT_NEW:
|
||||
@ -3069,12 +3129,12 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
||||
uint32 type_idx;
|
||||
|
||||
if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
|
||||
init_values =
|
||||
(WASMStructNewInitValues *)init_expr->u.data;
|
||||
init_values = (WASMStructNewInitValues *)
|
||||
init_expr->u.unary.v.data;
|
||||
type_idx = init_values->type_idx;
|
||||
}
|
||||
else {
|
||||
type_idx = init_expr->u.type_index;
|
||||
type_idx = init_expr->u.unary.v.type_index;
|
||||
}
|
||||
|
||||
struct_type = (WASMStructType *)module->types[type_idx];
|
||||
@ -3125,13 +3185,14 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
||||
uint32 type_idx, len;
|
||||
|
||||
if (flag == INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT) {
|
||||
type_idx = init_expr->u.array_new_default.type_index;
|
||||
len = init_expr->u.array_new_default.length;
|
||||
type_idx =
|
||||
init_expr->u.unary.v.array_new_default.type_index;
|
||||
len = init_expr->u.unary.v.array_new_default.length;
|
||||
arr_init_val = &empty_val;
|
||||
}
|
||||
else {
|
||||
init_values =
|
||||
(WASMArrayNewInitValues *)init_expr->u.data;
|
||||
(WASMArrayNewInitValues *)init_expr->u.unary.v.data;
|
||||
type_idx = init_values->type_idx;
|
||||
len = init_values->length;
|
||||
|
||||
@ -3177,14 +3238,14 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
||||
}
|
||||
case INIT_EXPR_TYPE_I31_NEW:
|
||||
{
|
||||
ref = (wasm_obj_t)wasm_i31_obj_new(init_expr->u.i32);
|
||||
ref =
|
||||
(wasm_obj_t)wasm_i31_obj_new(init_expr->u.unary.v.i32);
|
||||
break;
|
||||
}
|
||||
#endif /* end of WASM_ENABLE_GC != 0 */
|
||||
}
|
||||
|
||||
*(table_data + table_seg->base_offset.u.i32 + j) =
|
||||
(table_elem_type_t)ref;
|
||||
*(table_data + offset_value.i32 + j) = (table_elem_type_t)ref;
|
||||
}
|
||||
}
|
||||
|
||||
@ -4706,10 +4767,10 @@ llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx,
|
||||
for (i = 0; i < length; i++) {
|
||||
#if WASM_ENABLE_GC != 0
|
||||
/* UINT32_MAX indicates that it is a null ref */
|
||||
if (init_values[i].u.ref_index != UINT32_MAX) {
|
||||
if (!(func_obj = wasm_create_func_obj(module_inst,
|
||||
init_values[i].u.ref_index,
|
||||
true, NULL, 0))) {
|
||||
if (init_values[i].u.unary.v.ref_index != UINT32_MAX) {
|
||||
if (!(func_obj = wasm_create_func_obj(
|
||||
module_inst, init_values[i].u.unary.v.ref_index, true,
|
||||
NULL, 0))) {
|
||||
wasm_set_exception(module_inst, "null function reference");
|
||||
return;
|
||||
}
|
||||
@ -4719,7 +4780,7 @@ llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx,
|
||||
table_elems[i] = NULL_REF;
|
||||
}
|
||||
#else
|
||||
table_elems[i] = init_values[i].u.ref_index;
|
||||
table_elems[i] = init_values[i].u.unary.v.ref_index;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user