Skip to content

Commit b6e75ab

Browse files
committed
workaround: prevent usage of anyref in struct fields and array elements
Disable cooresponding cases in spec test
1 parent 55699a7 commit b6e75ab

File tree

8 files changed

+259
-7
lines changed

8 files changed

+259
-7
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+
)

tests/wamr-test-suites/spec-test-script/gc_ignore_cases.patch

Lines changed: 120 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,30 @@ index bc1cc324..14af14ae 100644
138138
(assert_return (invoke "call_imported_elem") (i32.const 42))
139139
+;;)
140140
diff --git a/test/core/gc/array.wast b/test/core/gc/array.wast
141-
index 6ad95c08..a184435d 100644
141+
index 6ad95c08..17672d33 100644
142142
--- a/test/core/gc/array.wast
143143
+++ b/test/core/gc/array.wast
144-
@@ -95,7 +95,10 @@
144+
@@ -7,7 +7,8 @@
145+
(type (array i64))
146+
(type (array f32))
147+
(type (array f64))
148+
- (type (array anyref))
149+
+ ;; Disable because `anyref` in fileds of composite types is not supported yet
150+
+ ;; (type (array anyref))
151+
(type (array (ref struct)))
152+
(type (array (ref 0)))
153+
(type (array (ref null 1)))
154+
@@ -17,7 +18,8 @@
155+
(type (array (mut i64)))
156+
(type (array (mut i32)))
157+
(type (array (mut i64)))
158+
- (type (array (mut anyref)))
159+
+ ;; Disable because `anyref` in fileds of composite types is not supported yet
160+
+ ;; (type (array (mut anyref)))
161+
(type (array (mut (ref struct))))
162+
(type (array (mut (ref 0))))
163+
(type (array (mut (ref null i31))))
164+
@@ -95,7 +97,10 @@
145165
)
146166

147167
(assert_return (invoke "new") (ref.array))
@@ -153,7 +173,7 @@ index 6ad95c08..a184435d 100644
153173
(assert_return (invoke "get" (i32.const 0)) (f32.const 0))
154174
(assert_return (invoke "set_get" (i32.const 1) (f32.const 7)) (f32.const 7))
155175
(assert_return (invoke "len") (i32.const 3))
156-
@@ -140,7 +143,10 @@
176+
@@ -140,7 +145,10 @@
157177
)
158178

159179
(assert_return (invoke "new") (ref.array))
@@ -165,7 +185,7 @@ index 6ad95c08..a184435d 100644
165185
(assert_return (invoke "get" (i32.const 0)) (f32.const 1))
166186
(assert_return (invoke "set_get" (i32.const 1) (f32.const 7)) (f32.const 7))
167187
(assert_return (invoke "len") (i32.const 2))
168-
@@ -192,7 +198,10 @@
188+
@@ -192,7 +200,10 @@
169189
)
170190

171191
(assert_return (invoke "new") (ref.array))
@@ -177,15 +197,15 @@ index 6ad95c08..a184435d 100644
177197
(assert_return (invoke "get_u" (i32.const 2)) (i32.const 0xff))
178198
(assert_return (invoke "get_s" (i32.const 2)) (i32.const -1))
179199
(assert_return (invoke "set_get" (i32.const 1) (i32.const 7)) (i32.const 7))
180-
@@ -202,6 +211,7 @@
200+
@@ -202,6 +213,7 @@
181201
(assert_trap (invoke "get_s" (i32.const 10)) "out of bounds array access")
182202
(assert_trap (invoke "set_get" (i32.const 10) (i32.const 7)) "out of bounds array access")
183203

184204
+(;; Activate once aligned `array.new_elem`
185205
(module
186206
(type $bvec (array i8))
187207
(type $vec (array (ref $bvec)))
188-
@@ -260,6 +270,7 @@
208+
@@ -260,6 +272,7 @@
189209

190210
(assert_trap (invoke "get" (i32.const 10) (i32.const 0)) "out of bounds array access")
191211
(assert_trap (invoke "set_get" (i32.const 10) (i32.const 0) (i32.const 0)) "out of bounds array access")
@@ -309,6 +329,100 @@ index 6309e72b..39f35692 100644
309329
(assert_return (invoke "get" (i32.const 3)) (i32.const 789))
310330
+ ;;
311331
+ ;;)
332+
diff --git a/test/core/gc/struct.wast b/test/core/gc/struct.wast
333+
index 6151fe10..d501cd3c 100644
334+
--- a/test/core/gc/struct.wast
335+
+++ b/test/core/gc/struct.wast
336+
@@ -6,8 +6,9 @@
337+
(type (struct (field i8)))
338+
(type (struct (field i8 i8 i8 i8)))
339+
(type (struct (field $x1 i32) (field $y1 i32)))
340+
- (type (struct (field i8 i16 i32 i64 f32 f64 anyref funcref (ref 0) (ref null 1))))
341+
- (type (struct (field i32 i64 i8) (field) (field) (field (ref null i31) anyref)))
342+
+ ;; Disable because `anyref` in fileds of composite types is not supported yet
343+
+ ;; (type (struct (field i8 i16 i32 i64 f32 f64 anyref funcref (ref 0) (ref null 1))))
344+
+ ;; (type (struct (field i32 i64 i8) (field) (field) (field (ref null i31) anyref)))
345+
(type (struct (field $x2 i32) (field f32 f64) (field $y2 i32)))
346+
)
347+
348+
diff --git a/test/core/gc/type-subtyping.wast b/test/core/gc/type-subtyping.wast
349+
index f2b33d7c..a61560c2 100644
350+
--- a/test/core/gc/type-subtyping.wast
351+
+++ b/test/core/gc/type-subtyping.wast
352+
@@ -4,7 +4,8 @@
353+
(type $e0 (sub (array i32)))
354+
(type $e1 (sub $e0 (array i32)))
355+
356+
- (type $e2 (sub (array anyref)))
357+
+ ;; Disable because `anyref` in fileds of composite types is not supported yet
358+
+ ;; (type $e2 (sub (array anyref)))
359+
(type $e3 (sub (array (ref null $e0))))
360+
(type $e4 (sub (array (ref $e1))))
361+
362+
@@ -32,35 +33,36 @@
363+
)
364+
365+
366+
+;; Disable because `anyref` in fileds of composite types is not supported yet
367+
;; Recursive definitions
368+
369+
-(module
370+
- (type $t (sub (struct (field anyref))))
371+
- (rec (type $r (sub $t (struct (field (ref $r))))))
372+
- (type $t' (sub $r (struct (field (ref $r) i32))))
373+
-)
374+
-
375+
-(module
376+
- (rec
377+
- (type $r1 (sub (struct (field i32 (ref $r1)))))
378+
- )
379+
- (rec
380+
- (type $r2 (sub $r1 (struct (field i32 (ref $r3)))))
381+
- (type $r3 (sub $r1 (struct (field i32 (ref $r2)))))
382+
- )
383+
-)
384+
-
385+
-(module
386+
- (rec
387+
- (type $a1 (sub (struct (field i32 (ref $a2)))))
388+
- (type $a2 (sub (struct (field i64 (ref $a1)))))
389+
- )
390+
- (rec
391+
- (type $b1 (sub $a2 (struct (field i64 (ref $a1) i32))))
392+
- (type $b2 (sub $a1 (struct (field i32 (ref $a2) i32))))
393+
- (type $b3 (sub $a2 (struct (field i64 (ref $b2) i32))))
394+
- )
395+
-)
396+
+;; (module
397+
+;; (type $t (sub (struct (field anyref))))
398+
+;; (rec (type $r (sub $t (struct (field (ref $r))))))
399+
+;; (type $t' (sub $r (struct (field (ref $r) i32))))
400+
+;; )
401+
+
402+
+;; (module
403+
+;; (rec
404+
+;; (type $r1 (sub (struct (field i32 (ref $r1)))))
405+
+;; )
406+
+;; (rec
407+
+;; (type $r2 (sub $r1 (struct (field i32 (ref $r3)))))
408+
+;; (type $r3 (sub $r1 (struct (field i32 (ref $r2)))))
409+
+;; )
410+
+;; )
411+
+
412+
+;; (module
413+
+;; (rec
414+
+;; (type $a1 (sub (struct (field i32 (ref $a2)))))
415+
+;; (type $a2 (sub (struct (field i64 (ref $a1)))))
416+
+;; )
417+
+;; (rec
418+
+;; (type $b1 (sub $a2 (struct (field i64 (ref $a1) i32))))
419+
+;; (type $b2 (sub $a1 (struct (field i32 (ref $a2) i32))))
420+
+;; (type $b3 (sub $a2 (struct (field i64 (ref $b2) i32))))
421+
+;; )
422+
+;; )
423+
424+
425+
;; Subsumption
312426
diff --git a/test/core/global.wast b/test/core/global.wast
313427
index 8c47fde2..8d3d8228 100644
314428
--- a/test/core/global.wast

0 commit comments

Comments
 (0)