Skip to content

Commit ad4672e

Browse files
authored
Avoid errors in Type::with(HeapType) (#7551)
Previously doing e.g. `type.with(HeapType::none)` would cause an assertion failure if `type` was exact because `.with()` would only replace the heap type and exact references to basic heap types are disallowed. Rather than checking for and avoiding this error in all the callers, simply drop exactness when `.with()` is called with a basic heap type. This is reasonable behavior because the only alternative is never correct. Add a test that hits an assertion failure without this fix. AbstractTypeRefining replaces a defined type with `none` and the type updating utility does not check whether the new heap type is basic before doing the replacement.
1 parent d758b00 commit ad4672e

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

scripts/test/fuzzing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
'exact-references-lowering.wast',
115115
'exact-casts.wast',
116116
'exact-casts-trivial.wast',
117+
'abstract-type-refining-exact.wast',
117118
'optimize-instructions-exact.wast',
118119
'optimize-instructions-all-casts.wast',
119120
'optimize-instructions-all-casts-exact.wast',

src/wasm-type.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,12 @@ class Type {
418418
}
419419

420420
// Return a new reference type with some part updated to the specified value.
421+
// Always clear exactness when replacing the referenced type with a basic heap
422+
// type to avoid creating an invalid type.
421423
Type with(HeapType heapType) const {
422-
return Type(heapType, getNullability(), getExactness());
424+
return Type(heapType,
425+
getNullability(),
426+
heapType.isBasic() ? Inexact : getExactness());
423427
}
424428
Type with(Nullability nullability) const {
425429
return Type(getHeapType(), nullability, getExactness());
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
2+
3+
;; RUN: wasm-opt %s -all --closed-world --abstract-type-refining -S -o - | filecheck %s
4+
5+
(module
6+
;; CHECK: (rec
7+
;; CHECK-NEXT: (type $foo (struct))
8+
(type $foo (struct))
9+
10+
;; CHECK: (func $test (type $1)
11+
;; CHECK-NEXT: (local $0 (ref none))
12+
;; CHECK-NEXT: )
13+
(func $test
14+
;; $foo will be replaced with none` and the exactness should be dropped
15+
;; without errors.
16+
(local (ref (exact $foo)))
17+
)
18+
)

0 commit comments

Comments
 (0)