diff options
Diffstat (limited to 'src/wasm/wasm.cpp')
-rw-r--r-- | src/wasm/wasm.cpp | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index 98146dfbc..87ae6ac5a 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -1008,7 +1008,25 @@ void CallRef::finalize() { return; } assert(target->type.isRef()); - if (target->type.getHeapType().isBottom()) { + if (target->type.isNull()) { + // If this call_ref has been optimized to have a null reference, then it + // will definitely trap. We could update the type to be unreachable, but + // that would violate the invariant that non-branch instructions other than + // `unreachable` can only be unreachable if they have unreachable children. + // Make the result type as close to `unreachable` as possible without + // actually making it unreachable. TODO: consider just making this + // unreachable instead (and similar in other GC accessors), although this + // would currently cause the parser to admit more invalid modules. + if (type.isRef()) { + type = Type(type.getHeapType().getBottom(), NonNullable); + } else if (type.isTuple()) { + Tuple elems; + for (auto t : type) { + elems.push_back( + t.isRef() ? Type(t.getHeapType().getBottom(), NonNullable) : t); + } + type = Type(elems); + } return; } assert(target->type.getHeapType().isSignature()); @@ -1136,7 +1154,12 @@ void StructNew::finalize() { void StructGet::finalize() { if (ref->type == Type::unreachable) { type = Type::unreachable; - } else if (!ref->type.isNull()) { + } else if (ref->type.isNull()) { + // See comment on CallRef for explanation. + if (type.isRef()) { + type = Type(type.getHeapType().getBottom(), NonNullable); + } + } else { type = ref->type.getHeapType().getStruct().fields[index].type; } } @@ -1180,7 +1203,12 @@ void ArrayNewFixed::finalize() { void ArrayGet::finalize() { if (ref->type == Type::unreachable || index->type == Type::unreachable) { type = Type::unreachable; - } else if (!ref->type.isNull()) { + } else if (ref->type.isNull()) { + // See comment on CallRef for explanation. + if (type.isRef()) { + type = Type(type.getHeapType().getBottom(), NonNullable); + } + } else { type = ref->type.getHeapType().getArray().element.type; } } |