Skip to content

Commit f89bcde

Browse files
committed
workaround: prevent usage of anyref in struct fields and array elements
1 parent d7459e8 commit f89bcde

File tree

7 files changed

+139
-1
lines changed

7 files changed

+139
-1
lines changed

core/iwasm/interpreter/wasm_loader.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,6 +1920,11 @@ resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
19201920
if (need_ref_type_map)
19211921
ref_type_map_count++;
19221922

1923+
if (wasm_is_reftype_anyref(ref_type.ref_type)) {
1924+
LOG_ERROR("Not support using anyref in struct fields");
1925+
return false;
1926+
}
1927+
19231928
if (wasm_is_type_reftype(ref_type.ref_type))
19241929
ref_field_count++;
19251930

@@ -2039,6 +2044,11 @@ resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
20392044
return false;
20402045
}
20412046

2047+
if (wasm_is_reftype_anyref(ref_type.ref_type)) {
2048+
LOG_ERROR("Not support using anyref in array element type");
2049+
return false;
2050+
}
2051+
20422052
CHECK_BUF(p, p_end, 1);
20432053
mutable = read_uint8(p);
20442054
if (!check_mutability(mutable, error_buf, error_buf_size)) {

tests/unit/gc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ set (WAMR_BUILD_GC 1)
1111
set (WAMR_BUILD_INTERP 1)
1212
set (WAMR_BUILD_AOT 0)
1313
set (WAMR_BUILD_APP_FRAMEWORK 0)
14+
set (WAMR_BUILD_SANITIZER "asan")
1415

1516
include (../unit_common.cmake)
1617

tests/unit/gc/gc_test.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,16 @@ class WasmGCTest : public testing::Test
5353
public:
5454
bool load_wasm_file(const char *wasm_file)
5555
{
56-
const char *file;
56+
char *file;
5757
unsigned char *wasm_file_buf;
5858
uint32 wasm_file_size;
5959

6060
file = strdup((CWD + "/" + wasm_file).c_str());
6161

6262
wasm_file_buf =
6363
(unsigned char *)bh_read_file_to_buffer(file, &wasm_file_size);
64+
free(file);
65+
6466
if (!wasm_file_buf)
6567
return false;
6668

@@ -100,3 +102,10 @@ TEST_F(WasmGCTest, Test_app1)
100102
ASSERT_TRUE(load_wasm_file("func1.wasm"));
101103
ASSERT_TRUE(load_wasm_file("func2.wasm"));
102104
}
105+
106+
TEST_F(WasmGCTest, Test_nested_struct)
107+
{
108+
//FIXME: Revert the change when anyref support is added
109+
ASSERT_FALSE(load_wasm_file("nested_struct_field_any.wasm"));
110+
ASSERT_FALSE(load_wasm_file("nested_array_elem_any.wasm"));
111+
}
279 Bytes
Binary file not shown.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
(module
2+
(type $array_type (array (mut anyref)))
3+
4+
(global $g_array
5+
(mut (ref $array_type))
6+
(array.new_fixed $array_type 2
7+
(ref.i31 (i32.const 10))
8+
(array.new_fixed $array_type 2
9+
(ref.i31 (i32.const 20))
10+
(array.new_default $array_type (i32.const 2))
11+
)
12+
)
13+
)
14+
15+
;; assert_return(invoke "get_elem0"), 10)
16+
(func (export "get_elem0") (result i32)
17+
(i31.get_s (ref.cast i31ref (array.get $array_type (global.get $g_array) (i32.const 0))))
18+
)
19+
20+
;; assert_return(invoke "get_elem1"), array.new_fixed $array_type ...)
21+
(func (export "get_elem1") (result anyref)
22+
(array.get $array_type (global.get $g_array) (i32.const 1))
23+
)
24+
25+
;; assert_return(invoke "get_elem1_elem0"), 20)
26+
(func (export "get_elem1_elem0") (result i32)
27+
(i31.get_s (ref.cast i31ref
28+
(array.get $array_type
29+
(ref.cast (ref $array_type)
30+
(array.get $array_type (global.get $g_array) (i32.const 1))
31+
)
32+
(i32.const 0)
33+
)
34+
))
35+
)
36+
37+
;; assert_return(invoke "get_elem1_elem1"), array.new_default $array_type ...)
38+
(func (export "get_elem1_elem1") (result anyref)
39+
(array.get $array_type
40+
(ref.cast (ref $array_type)
41+
(array.get $array_type (global.get $g_array) (i32.const 1))
42+
)
43+
(i32.const 1)
44+
)
45+
)
46+
47+
;; assert_return(invoke "get_elem1_elem1_elem0"), 0)
48+
(func (export "get_elem1_elem1_elem0") (result i32)
49+
(i31.get_s (ref.cast i31ref
50+
(array.get $array_type
51+
(ref.cast (ref $array_type)
52+
(array.get $array_type
53+
(ref.cast (ref $array_type)
54+
(array.get $array_type (global.get $g_array) (i32.const 1))
55+
)
56+
(i32.const 1)
57+
)
58+
)
59+
(i32.const 0)
60+
)
61+
))
62+
)
63+
)
261 Bytes
Binary file not shown.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
(module
2+
(type $struct_type (struct (field (mut i32)) (field (mut anyref))))
3+
4+
(global $g_struct
5+
(mut (ref $struct_type))
6+
(struct.new $struct_type
7+
(i32.const 10)
8+
(struct.new $struct_type
9+
(i32.const 20)
10+
(struct.new_default $struct_type)
11+
)
12+
)
13+
)
14+
15+
;; assert_return(invoke "get_field1"), 10)
16+
(func (export "get_field1") (result i32)
17+
(struct.get $struct_type 0 (global.get $g_struct))
18+
)
19+
20+
;; assert_return(invoke "get_field1"), struct.new $struct_type ...)
21+
(func (export "get_field2") (result anyref)
22+
(struct.get $struct_type 1 (global.get $g_struct))
23+
)
24+
25+
;; assert_return(invoke "get_field2_field1"), 20)
26+
(func (export "get_field2_field1") (result i32)
27+
(struct.get $struct_type 0
28+
(ref.cast structref
29+
(struct.get $struct_type 1 (global.get $g_struct))
30+
)
31+
)
32+
)
33+
34+
;; assert_return(invoke "get_field2_field2"), struct.new_default $struct_type ...)
35+
(func (export "get_field2_field2") (result anyref)
36+
(struct.get $struct_type 1
37+
(ref.cast structref
38+
(struct.get $struct_type 1 (global.get $g_struct))
39+
)
40+
)
41+
)
42+
43+
;; assert_return(invoke "get_field2_field2_field1"), 0)
44+
(func (export "get_field2_field2_field1") (result i32)
45+
(struct.get $struct_type 0
46+
(ref.cast structref
47+
(struct.get $struct_type 1
48+
(ref.cast structref
49+
(struct.get $struct_type 1 (global.get $g_struct))
50+
)
51+
)
52+
)
53+
)
54+
)
55+
)

0 commit comments

Comments
 (0)