Skip to content

Commit f9f25e2

Browse files
committed
Fix WAT parsing of handler types. Rename value_types field to results. Fix fuzzer stubs.
1 parent 4bbed5e commit f9f25e2

11 files changed

Lines changed: 109 additions & 43 deletions

src/parser/contexts.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ struct NullTypeParserCtx {
148148

149149
SignatureT makeFuncType(ParamsT*, ResultsT*) { return Ok{}; }
150150
ContinuationT makeContType(HeapTypeT) { return Ok{}; }
151-
HandlerT makeHandlerType(TypeT) { return Ok{}; }
151+
HandlerT makeHandlerType(ResultsT*) { return Ok{}; }
152152

153153
StorageT makeI8() { return Ok{}; }
154154
StorageT makeI16() { return Ok{}; }
@@ -298,7 +298,11 @@ template<typename Ctx> struct TypeParserCtx {
298298
}
299299

300300
ContinuationT makeContType(HeapTypeT ft) { return Continuation(ft); }
301-
HandlerT makeHandlerType(TypeT type) { return Handler(type); }
301+
HandlerT makeHandlerType(ResultsT* results) {
302+
std::vector<Type> empty;
303+
const auto& resultTypes = results ? *results : empty;
304+
return Handler(self().makeTupleType(resultTypes));
305+
}
302306

303307
StorageT makeI8() { return Field(Field::i8, Immutable); }
304308
StorageT makeI16() { return Field(Field::i16, Immutable); }

src/parser/parsers.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -698,14 +698,14 @@ MaybeResult<typename Ctx::HandlerT> handlertype(Ctx& ctx) {
698698
return {};
699699
}
700700

701-
auto elems = ctx.makeTupleElemList();
702-
while (!ctx.in.takeRParen()) {
703-
auto type = valtype(ctx);
704-
CHECK_ERR(type);
705-
ctx.appendTupleElem(elems, *type);
701+
auto parsedResults = results(ctx);
702+
CHECK_ERR(parsedResults);
703+
704+
if (!ctx.in.takeRParen()) {
705+
return ctx.in.err("expected end of handler name type");
706706
}
707707

708-
return ctx.makeHandlerType(ctx.makeTupleType(elems));
708+
return ctx.makeHandlerType(parsedResults.getPtr());
709709
}
710710

711711
// storagetype ::= valtype | packedtype

src/tools/fuzzing/heap-types.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ bool isUninhabitable(HeapType type,
10701070
}
10711071
break;
10721072
case HeapTypeKind::Handler:
1073-
if (isUninhabitable(type.getHandler().value_types, visited, visiting)) {
1073+
if (isUninhabitable(type.getHandler().results, visited, visiting)) {
10741074
return true;
10751075
}
10761076
break;

src/wasm-type.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,10 @@ struct Continuation {
606606
};
607607

608608
struct Handler {
609-
Type value_types;
610-
Handler(Type value_types) : value_types(value_types) {}
609+
Type results;
610+
Handler(Type results) : results(results) {}
611611
bool operator==(const Handler& other) const {
612-
return value_types == other.value_types;
612+
return results == other.results;
613613
}
614614
bool operator!=(const Handler& other) const { return !(*this == other); }
615615
std::string toString() const;
@@ -773,7 +773,7 @@ struct TypeBuilder {
773773
return;
774774
case HeapTypeKind::Handler: {
775775
Handler h = type.getHandler();
776-
setHeapType(i, Handler(copyType(h.value_types)));
776+
setHeapType(i, Handler(copyType(h.results)));
777777
return;
778778
}
779779
case HeapTypeKind::Basic:

src/wasm/wasm-binary.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,9 @@ void WasmBinaryWriter::writeTypes() {
318318
break;
319319
case HeapTypeKind::Handler: {
320320
o << uint8_t(BinaryConsts::EncodedType::Handler);
321-
auto value_types = type.getHandler().value_types;
322-
o << U32LEB(value_types.size());
323-
for (const auto& type : value_types) {
321+
auto results = type.getHandler().results;
322+
o << U32LEB(results.size());
323+
for (const auto& type : results) {
324324
writeType(type);
325325
}
326326
break;

src/wasm/wasm-type-shape.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ template<typename CompareTypes> struct RecGroupComparator {
121121
}
122122

123123
Comparison compare(Handler a, Handler b) {
124-
return compare(a.value_types, b.value_types);
124+
return compare(a.results, b.results);
125125
}
126126

127127
Comparison compare(Field a, Field b) {
@@ -278,7 +278,7 @@ struct RecGroupHasher {
278278

279279
size_t hash(Continuation cont) { return hash(cont.type); }
280280

281-
size_t hash(Handler handler) { return hash(handler.value_types); }
281+
size_t hash(Handler handler) { return hash(handler.results); }
282282

283283
size_t hash(Field field) {
284284
size_t digest = wasm::hash(field.mutable_);

src/wasm/wasm-type.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ template<typename Self> struct TypeGraphWalkerBase {
300300
taskList.push_back(Task::scan(&info->array.element.type));
301301
break;
302302
case HeapTypeKind::Handler:
303-
taskList.push_back(Task::scan(&info->handler.value_types));
303+
taskList.push_back(Task::scan(&info->handler.results));
304304
break;
305305
case HeapTypeKind::Basic:
306306
WASM_UNREACHABLE("unexpected kind");
@@ -1185,7 +1185,7 @@ std::vector<Type> HeapType::getTypeChildren() const {
11851185
return {};
11861186
case HeapTypeKind::Handler: {
11871187
std::vector<Type> children;
1188-
for (auto t : getHandler().value_types) {
1188+
for (auto t : getHandler().results) {
11891189
children.push_back(t);
11901190
}
11911191
return children;
@@ -1624,7 +1624,7 @@ bool SubTyper::isSubType(const Continuation& a, const Continuation& b) {
16241624
}
16251625

16261626
bool SubTyper::isSubType(const Handler& a, const Handler& b) {
1627-
return isSubType(a.value_types, b.value_types);
1627+
return isSubType(a.results, b.results);
16281628
}
16291629

16301630
bool SubTyper::isSubType(const Struct& a, const Struct& b) {
@@ -1938,7 +1938,7 @@ std::ostream& TypePrinter::print(const Continuation& continuation) {
19381938

19391939
std::ostream& TypePrinter::print(const Handler& handler) {
19401940
os << "(handler";
1941-
for (Type t : handler.value_types) {
1941+
for (Type t : handler.results) {
19421942
os << ' ';
19431943
print(t);
19441944
}
@@ -2082,7 +2082,7 @@ size_t RecGroupHasher::hash(const Continuation& continuation) const {
20822082

20832083
size_t RecGroupHasher::hash(const Handler& handler) const {
20842084
size_t magic = 0xe05a2;
2085-
size_t digest = hash(handler.value_types);
2085+
size_t digest = hash(handler.results);
20862086
rehash(digest, magic);
20872087
return digest;
20882088
}
@@ -2214,7 +2214,7 @@ bool RecGroupEquator::eq(const Continuation& a, const Continuation& b) const {
22142214
}
22152215

22162216
bool RecGroupEquator::eq(const Handler& a, const Handler& b) const {
2217-
return eq(a.value_types, b.value_types);
2217+
return eq(a.results, b.results);
22182218
}
22192219

22202220
bool RecGroupEquator::eq(const Struct& a, const Struct& b) const {

test/gtest/type-domains.cpp

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ std::ostream& operator<<(std::ostream& o, const TypeBuilderPlan& plan) {
184184
case ContKind:
185185
o << "s";
186186
break;
187+
case HandlerKind:
188+
o << "h";
189+
break;
187190
}
188191
if (auto super = plan.supertypes[i]) {
189192
o << "(" << *super << ")";
@@ -558,6 +561,9 @@ fuzztest::Domain<HeapTypePlan> AvailableStrictSubHeapType(TypeBuilderPlan plan,
558561
case ContKind:
559562
bottom = HeapTypes::nocont.getBasic(share);
560563
break;
564+
case HandlerKind:
565+
bottom = HeapTypes::nohandler.getBasic(share);
566+
break;
561567
}
562568
if (matches.empty()) {
563569
return fuzztest::Just(HeapTypePlan{bottom});
@@ -659,6 +665,9 @@ AvailableStrictSuperHeapType(TypeBuilderPlan plan, HeapTypePlan sub) {
659665
case ContKind:
660666
abstract = {HeapTypes::cont.getBasic(share)};
661667
break;
668+
case HandlerKind:
669+
abstract = {HeapTypes::handler.getBasic(share)};
670+
break;
662671
}
663672
assert(!abstract.empty());
664673
if (possibleIndices.empty()) {
@@ -894,6 +903,22 @@ fuzztest::Domain<ContPlan> SubContDef(TypeBuilderPlan plan, ContPlan super) {
894903
}
895904
}
896905

906+
fuzztest::Domain<HandlerPlan> HandlerDef(TypeBuilderPlan plan) {
907+
auto results = fuzztest::VectorOf(AvailableType(std::move(plan)))
908+
.WithMaxSize(MaxResultsSize);
909+
return fuzztest::Just(results);
910+
}
911+
912+
fuzztest::Domain<HandlerPlan> SubHandlerDef(TypeBuilderPlan plan,
913+
HandlerPlan super) {
914+
auto results = MapElements(
915+
[plan = std::move(plan)](TypePlan type) {
916+
return AvailableSubType(std::move(plan), type);
917+
},
918+
super.second);
919+
return fuzztest::Just(results);
920+
}
921+
897922
fuzztest::Domain<TypeBuilderPlan> StepTypeDefinition(TypeBuilderPlan plan);
898923

899924
template<typename T>
@@ -942,6 +967,12 @@ fuzztest::Domain<TypeBuilderPlan> StepTypeDefinition(TypeBuilderPlan plan) {
942967
return fuzztest::FlatMap(
943968
AppendTypeDef<ContPlan>, fuzztest::Just(std::move(plan)), def);
944969
}
970+
case ContKind: {
971+
auto def = super ? SubHandlerDef(plan, *plan.defs[*super].getHandler())
972+
: HandlerDef(plan);
973+
return fuzztest::FlatMap(
974+
AppendTypeDef<HandlerPlan>, fuzztest::Just(std::move(plan)), def);
975+
}
945976
}
946977
WASM_UNREACHABLE("unexpected kind");
947978
}
@@ -1146,10 +1177,10 @@ void TestBuiltTypes(std::pair<std::vector<HeapType>, TypeBuilderPlan> pair) {
11461177

11471178
auto checkHandler = [&](HandlerPlan& plan, HeapType type) {
11481179
ASSERT_TRUE(type.isHandler());
1149-
auto value_types = type.getHandler().value_types;
1150-
ASSERT_EQ(plan.size(), value_types.size());
1180+
auto results = type.getHandler().results;
1181+
ASSERT_EQ(plan.size(), results.size());
11511182
for (size_t i = 0; i < plan.size(); ++i) {
1152-
checkType(plan[i], value_types[i]);
1183+
checkType(plan[i], results[i]);
11531184
}
11541185
};
11551186

@@ -1163,7 +1194,7 @@ void TestBuiltTypes(std::pair<std::vector<HeapType>, TypeBuilderPlan> pair) {
11631194
} else if (auto* c = plan.getCont()) {
11641195
checkCont(*c, type);
11651196
} else if (auto* h = plan.getHandler()) {
1166-
checkCont(*h, type);
1197+
checkHandler(*h, type);
11671198
} else {
11681199
WASM_UNREACHABLE("unexpected variant");
11691200
}

test/lit/basic/stack_switching_named.wast

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,66 @@
1010
;; RUN: cat %t.bin.nodebug.wast | filecheck %s --check-prefix=CHECK-BIN-NODEBUG
1111

1212
(module
13-
;; CHECK-TEXT: (type $ht (handler i32))
14-
;; CHECK-BIN: (type $ht (handler i32))
15-
(type $ht (handler i32))
13+
;; CHECK-TEXT: (type $ht-1 (handler i32))
14+
;; CHECK-BIN: (type $ht-1 (handler i32))
15+
(type $ht-1 (handler (result i32)))
16+
;; CHECK-TEXT: (type $ht-2 (handler))
17+
;; CHECK-BIN: (type $ht-2 (handler))
18+
(type $ht-2 (handler))
1619

17-
(func (export "main") (param (ref $ht))
20+
(func (export "main-1") (param (ref $ht-1))
21+
(unreachable))
22+
(func (export "main=2") (param (ref $ht-2))
1823
(unreachable))
1924
)
20-
;; CHECK-TEXT: (type $1 (func (param (ref $ht))))
25+
;; CHECK-TEXT: (type $2 (func (param (ref $ht-1))))
26+
27+
;; CHECK-TEXT: (type $3 (func (param (ref $ht-2))))
28+
29+
;; CHECK-TEXT: (export "main-1" (func $0))
2130

22-
;; CHECK-TEXT: (export "main" (func $0))
31+
;; CHECK-TEXT: (export "main=2" (func $1))
2332

24-
;; CHECK-TEXT: (func $0 (type $1) (param $0 (ref $ht))
33+
;; CHECK-TEXT: (func $0 (type $2) (param $0 (ref $ht-1))
2534
;; CHECK-TEXT-NEXT: (unreachable)
2635
;; CHECK-TEXT-NEXT: )
2736

28-
;; CHECK-BIN: (type $1 (func (param (ref $ht))))
37+
;; CHECK-TEXT: (func $1 (type $3) (param $0 (ref $ht-2))
38+
;; CHECK-TEXT-NEXT: (unreachable)
39+
;; CHECK-TEXT-NEXT: )
2940

30-
;; CHECK-BIN: (export "main" (func $0))
41+
;; CHECK-BIN: (type $2 (func (param (ref $ht-1))))
3142

32-
;; CHECK-BIN: (func $0 (type $1) (param $0 (ref $ht))
43+
;; CHECK-BIN: (type $3 (func (param (ref $ht-2))))
44+
45+
;; CHECK-BIN: (export "main-1" (func $0))
46+
47+
;; CHECK-BIN: (export "main=2" (func $1))
48+
49+
;; CHECK-BIN: (func $0 (type $2) (param $0 (ref $ht-1))
50+
;; CHECK-BIN-NEXT: (unreachable)
51+
;; CHECK-BIN-NEXT: )
52+
53+
;; CHECK-BIN: (func $1 (type $3) (param $0 (ref $ht-2))
3354
;; CHECK-BIN-NEXT: (unreachable)
3455
;; CHECK-BIN-NEXT: )
3556

3657
;; CHECK-BIN-NODEBUG: (type $0 (handler i32))
3758

38-
;; CHECK-BIN-NODEBUG: (type $1 (func (param (ref $0))))
59+
;; CHECK-BIN-NODEBUG: (type $1 (handler))
60+
61+
;; CHECK-BIN-NODEBUG: (type $2 (func (param (ref $0))))
62+
63+
;; CHECK-BIN-NODEBUG: (type $3 (func (param (ref $1))))
3964

40-
;; CHECK-BIN-NODEBUG: (export "main" (func $0))
65+
;; CHECK-BIN-NODEBUG: (export "main-1" (func $0))
66+
67+
;; CHECK-BIN-NODEBUG: (export "main=2" (func $1))
68+
69+
;; CHECK-BIN-NODEBUG: (func $0 (type $2) (param $0 (ref $0))
70+
;; CHECK-BIN-NODEBUG-NEXT: (unreachable)
71+
;; CHECK-BIN-NODEBUG-NEXT: )
4172

42-
;; CHECK-BIN-NODEBUG: (func $0 (type $1) (param $0 (ref $0))
73+
;; CHECK-BIN-NODEBUG: (func $1 (type $3) (param $0 (ref $1))
4374
;; CHECK-BIN-NODEBUG-NEXT: (unreachable)
4475
;; CHECK-BIN-NODEBUG-NEXT: )

test/lit/basic/stack_switching_resume_with.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
(module
1313
;; CHECK-TEXT: (type $ht (handler i32))
1414
;; CHECK-BIN: (type $ht (handler i32))
15-
(type $ht (handler i32))
15+
(type $ht (handler (result i32)))
1616
;; CHECK-TEXT: (type $ft (func (param i32 (ref $ht)) (result i32)))
1717
;; CHECK-BIN: (type $ft (func (param i32 (ref $ht)) (result i32)))
1818
(type $ft (func (param i32 (ref $ht)) (result i32)))

0 commit comments

Comments
 (0)