Skip to content

Commit cb5fd0f

Browse files
committed
Fix array.new_default validation for non-defaultable element types (#4853)
1 parent cd390ea commit cb5fd0f

4 files changed

Lines changed: 45 additions & 1 deletion

File tree

core/iwasm/interpreter/wasm_loader.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,17 @@ is_packed_type(uint8 type)
307307
{
308308
return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
309309
}
310+
311+
static bool
312+
is_defaultable_array_elem_type(uint8 elem_type, WASMRefType *elem_ref_type)
313+
{
314+
if (!wasm_is_type_multi_byte_type(elem_type)) {
315+
return true;
316+
}
317+
318+
bh_assert(elem_ref_type);
319+
return elem_ref_type->ref_ht_common.nullable;
320+
}
310321
#endif
311322

312323
static bool
@@ -1396,6 +1407,16 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
13961407
WASMValue len_val;
13971408
uint32 len;
13981409

1410+
if (!is_defaultable_array_elem_type(
1411+
array_type->elem_type,
1412+
array_type->elem_ref_type)) {
1413+
set_error_buf(
1414+
error_buf, error_buf_size,
1415+
"array.new_default requires a defaultable "
1416+
"element type");
1417+
goto fail;
1418+
}
1419+
13991420
/* POP(i32) */
14001421
if (!pop_const_expr_stack(
14011422
&const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
@@ -15020,6 +15041,16 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1502015041
else
1502115042
POP_REF(elem_type);
1502215043
}
15044+
else if (opcode1 == WASM_OP_ARRAY_NEW_DEFAULT) {
15045+
if (!is_defaultable_array_elem_type(
15046+
elem_type, array_type->elem_ref_type)) {
15047+
set_error_buf(
15048+
error_buf, error_buf_size,
15049+
"array.new_default requires a defaultable "
15050+
"element type");
15051+
goto fail;
15052+
}
15053+
}
1502315054
else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
1502415055
/* offset of data segment */
1502515056
POP_I32();

tests/unit/gc/gc_test.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,9 @@ TEST_F(WasmGCTest, Test_nested_struct)
9292
//FIXME: Revert the change when anyref support is added
9393
ASSERT_FALSE(load_wasm_file("nested_struct_field_any.wasm"));
9494
ASSERT_FALSE(load_wasm_file("nested_array_elem_any.wasm"));
95-
}
95+
}
96+
97+
TEST_F(WasmGCTest, Test_array_new_default_non_defaultable_elem)
98+
{
99+
ASSERT_FALSE(load_wasm_file("array_new_default_non_defaultable_elem.wasm"));
100+
}
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(module
2+
(type $elem (array i32))
3+
(type $outer (array (ref $elem)))
4+
5+
(func (export "new_invalid_array")
6+
(array.new_default $outer (i32.const 1))
7+
drop)
8+
)

0 commit comments

Comments
 (0)