Skip to content

Commit 6f4484c

Browse files
authored
[Custom Descriptors] Test struct.new execution (#7687)
And fix a missing check for null descriptors. Passing a null descriptor to struct.new should trap.
1 parent dc50ee5 commit 6f4484c

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/wasm-interpreter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,9 @@ class ExpressionRunner : public OverriddenVisitor<SubType, Flow> {
18111811
if (desc.breaking()) {
18121812
return desc;
18131813
}
1814+
if (desc.getSingleValue().isNull()) {
1815+
trap("null descriptor");
1816+
}
18141817
return makeGCData(std::move(data), curr->type, desc.getSingleValue());
18151818
}
18161819
Flow visitStructGet(StructGet* curr) {

test/spec/struct.new-desc.wast

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,47 @@
33
(type $pair (descriptor $pair.desc (struct (field i32 i64))))
44
(type $pair.desc (describes $pair (struct)))
55
)
6-
(func $struct.new (result (ref (exact $pair)))
7-
(local $desc (ref null (exact $pair.desc)))
6+
(func $struct.new (param $desc (ref null (exact $pair.desc))) (result (ref (exact $pair)))
87
(struct.new $pair
9-
(i32.const 0)
10-
(i64.const 1)
8+
(i32.const 1)
9+
(i64.const 2)
1110
(local.get $desc)
1211
)
1312
)
14-
(func $struct.new_default (result (ref (exact $pair)))
15-
(local $desc (ref null (exact $pair.desc)))
13+
(func (export "check-new") (result i32)
14+
(local $pair (ref null $pair))
15+
(local.set $pair (call $struct.new (struct.new $pair.desc)))
16+
(i32.and
17+
(i32.eq (struct.get $pair 0 (local.get $pair)) (i32.const 1))
18+
(i64.eq (struct.get $pair 1 (local.get $pair)) (i64.const 2))
19+
)
20+
)
21+
(func (export "new-null-desc")
22+
(drop (call $struct.new (ref.null none)))
23+
)
24+
(func $struct.new_default (param $desc (ref null (exact $pair.desc))) (result (ref (exact $pair)))
1625
(struct.new_default $pair
1726
(local.get $desc)
1827
)
1928
)
29+
(func (export "check-new-default") (result i32)
30+
(local $pair (ref null $pair))
31+
(local.set $pair (call $struct.new_default (struct.new $pair.desc)))
32+
(i32.and
33+
(i32.eq (struct.get $pair 0 (local.get $pair)) (i32.const 0))
34+
(i64.eq (struct.get $pair 1 (local.get $pair)) (i64.const 0))
35+
)
36+
)
37+
(func (export "new-default-null-desc")
38+
(drop (call $struct.new_default (ref.null none)))
39+
)
2040
)
2141

42+
(assert_return (invoke "check-new") (i32.const 1))
43+
(assert_return (invoke "check-new-default") (i32.const 1))
44+
(assert_trap (invoke "new-null-desc") "null descriptor")
45+
(assert_trap (invoke "new-default-null-desc") "null descriptor")
46+
2247
(assert_invalid
2348
(module
2449
(rec

0 commit comments

Comments
 (0)