Skip to content

Validate heap types #5279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/wasm/wasm-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1087,9 +1087,6 @@ FeatureSet Type::getFeatures() const {
// A reference type implies we need that feature. Some also require more,
// such as GC or exceptions.
auto heapType = t.getHeapType();
if (heapType.isStruct() || heapType.isArray()) {
return FeatureSet::ReferenceTypes | FeatureSet::GC;
}
if (heapType.isBasic()) {
switch (heapType.getBasic()) {
case HeapType::ext:
Expand All @@ -1114,11 +1111,16 @@ FeatureSet Type::getFeatures() const {
return FeatureSet::ReferenceTypes;
}
}
// Note: Technically typed function references also require the typed
// function references feature, however, we use these types internally
// regardless of the presence of features (in particular, since during
// load of the wasm we don't know the features yet, so we apply the more
// refined types), so we don't add that in any case here.
if (heapType.isStruct() || heapType.isArray() ||
heapType.getRecGroup().size() > 1 || heapType.getSuperType()) {
return FeatureSet::ReferenceTypes | FeatureSet::GC;
}
// Note: Technically typed function references also require GC, however,
// we use these types internally regardless of the presence of GC (in
// particular, since during load of the wasm we don't know the features
// yet, so we apply the more refined types), so we don't add that in any
// case here.
assert(heapType.isSignature());
return FeatureSet::ReferenceTypes;
}
TODO_SINGLE_COMPOUND(t);
Expand Down
3 changes: 3 additions & 0 deletions src/wasm/wasm-validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2903,6 +2903,9 @@ void FunctionValidator::visitFunction(Function* curr) {
"Multivalue function results (multivalue is not enabled)");
}
FeatureSet features;
// Check for things like having a rec group with GC enabled.
features |=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are reference types ignored here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because a function reference type always requires ReferenceTypes as a baseline, but it's kind of a false positive in this situation. MVP functions will have some MVP signature type here, but getFeatures() will still return ReferenceTypes.

In practice the only thing this line checks for is rec groups.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right, the type itself is a reference type. A comment perhaps could help?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh oops, I saw this comment just after merging. Will add a comment in a follow-up.

(Type(curr->type, Nullable).getFeatures() & ~FeatureSet::ReferenceTypes);
for (const auto& param : curr->getParams()) {
features |= param.getFeatures();
shouldBeTrue(param.isConcrete(), curr, "params must be concretely typed");
Expand Down
16 changes: 16 additions & 0 deletions test/lit/validation/rec-groups-no-gc.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
;; Test that using rec groups types without GC is a validation error.

;; RUN: not wasm-opt %s --hybrid -all --disable-gc 2>&1 | filecheck %s

;; CHECK: all used types should be allowed

(module
(rec
(type $f1 (func))
(type $f2 (func))
)

(func $test (type $f1)
(unreachable)
)
)
14 changes: 14 additions & 0 deletions test/lit/validation/supertypes-no-gc.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
;; Test that declaring supertypes without GC is a validation error.

;; RUN: not wasm-opt %s --hybrid -all --disable-gc 2>&1 | filecheck %s

;; CHECK: all used types should be allowed

(module
(type $f1 (func_subtype func))
(type $f2 (func_subtype $f1))

(func $test (type $f2)
(unreachable)
)
)