Skip to content

Commit 89d2b1e

Browse files
authored
[Binary] Preserve tag HeapTypes when reading (#9368)
PR #7220 updated Tag and WasmBinaryWriter to store and emit HeapTypes instead of Signatures so tag types retain rec group, supertype, and finality information, but WasmBinaryReader::readTagImport and WasmBinaryReader::readTags still called getSignatureByTypeIndex, which reconstructed a fresh canonical HeapType from the Signature. Replace getSignatureByTypeIndex with getSignatureTypeByIndex, which validates that the referenced type is a signature while returning the original HeapType, and call .getSignature() at the caller that still needs a Signature. Fixes #9365.
1 parent c35bdde commit 89d2b1e

3 files changed

Lines changed: 59 additions & 30 deletions

File tree

‎src/wasm-binary.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,7 @@ class WasmBinaryReader {
17281728
void readFunctionSignatures();
17291729
HeapType getTypeByIndex(Index index);
17301730
HeapType getTypeByFunctionIndex(Index index);
1731-
Signature getSignatureByTypeIndex(Index index);
1731+
HeapType getSignatureTypeByIndex(Index index);
17321732
Signature getSignatureByFunctionIndex(Index index);
17331733

17341734
// We read the names section first so we know in advance what names various

‎src/wasm/wasm-binary.cpp‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,7 +2669,7 @@ Signature WasmBinaryReader::getBlockType() {
26692669
// Single value types are negative; signature indices are non-negative.
26702670
auto code = getS32LEB();
26712671
if (code >= 0) {
2672-
return getSignatureByTypeIndex(code);
2672+
return getSignatureTypeByIndex(code).getSignature();
26732673
}
26742674
if (code == BinaryConsts::EncodedType::Empty) {
26752675
return Signature();
@@ -3300,7 +3300,7 @@ std::unique_ptr<Tag> WasmBinaryReader::readTagImport(Name module, Name base) {
33003300
Builder builder(wasm);
33013301
getInt8(); // Reserved 'attribute' field
33023302
auto index = getU32LEB();
3303-
auto curr = builder.makeTag("", getSignatureByTypeIndex(index));
3303+
auto curr = builder.makeTag("", getSignatureTypeByIndex(index));
33043304
curr->module = module;
33053305
curr->base = base;
33063306
return curr;
@@ -3458,10 +3458,8 @@ void WasmBinaryReader::readFunctionSignatures() {
34583458
auto [name, isExplicit] = getOrMakeName(
34593459
functionNames, numImports + i, makeName("", i), usedFunctionNames);
34603460
auto index = getU32LEB();
3461-
HeapType type = getTypeByIndex(index);
3461+
HeapType type = getSignatureTypeByIndex(index);
34623462
functionTypes.push_back(type);
3463-
// Check that the type is a signature.
3464-
getSignatureByTypeIndex(index);
34653463
auto func = Builder(wasm).makeFunction(
34663464
name, Type(type, NonNullable, Exact), {}, nullptr);
34673465
func->hasExplicitName = isExplicit;
@@ -3484,12 +3482,12 @@ HeapType WasmBinaryReader::getTypeByFunctionIndex(Index index) {
34843482
return functionTypes[index];
34853483
}
34863484

3487-
Signature WasmBinaryReader::getSignatureByTypeIndex(Index index) {
3485+
HeapType WasmBinaryReader::getSignatureTypeByIndex(Index index) {
34883486
auto heapType = getTypeByIndex(index);
34893487
if (!heapType.isSignature()) {
34903488
throwError("invalid signature type " + heapType.toString());
34913489
}
3492-
return heapType.getSignature();
3490+
return heapType;
34933491
}
34943492

34953493
Signature WasmBinaryReader::getSignatureByFunctionIndex(Index index) {
@@ -5519,7 +5517,7 @@ void WasmBinaryReader::readTags() {
55195517
auto [name, isExplicit] = getOrMakeName(
55205518
tagNames, numImports + i, makeName("tag$", i), usedTagNames);
55215519
auto typeIndex = getU32LEB();
5522-
auto tag = Builder::makeTag(name, getSignatureByTypeIndex(typeIndex));
5520+
auto tag = Builder::makeTag(name, getSignatureTypeByIndex(typeIndex));
55235521
tag->hasExplicitName = isExplicit;
55245522
wasm.addTag(std::move(tag));
55255523
}

‎test/lit/basic/tags.wast‎

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,61 @@
1212
;; Test tags
1313

1414
(module
15-
(tag $e-import (import "env" "im0") (param i32))
16-
(import "env" "im1" (tag (param i32 f32)))
15+
(rec
16+
;; CHECK-TEXT: (type $0 (func (param i32 f32)))
1717

18-
(tag (param i32))
18+
;; CHECK-TEXT: (type $1 (func (param i32)))
1919

20-
;; CHECK-TEXT: (type $0 (func (param i32 f32)))
20+
;; CHECK-TEXT: (rec
21+
;; CHECK-TEXT-NEXT: (type $super (sub (func)))
22+
;; CHECK-BIN: (type $0 (func (param i32 f32)))
2123

22-
;; CHECK-TEXT: (type $1 (func (param i32)))
24+
;; CHECK-BIN: (type $1 (func (param i32)))
2325

24-
;; CHECK-TEXT: (type $2 (func))
26+
;; CHECK-BIN: (rec
27+
;; CHECK-BIN-NEXT: (type $super (sub (func)))
28+
(type $super (sub (func)))
29+
;; CHECK-TEXT: (type $sub (sub $super (func)))
30+
;; CHECK-BIN: (type $sub (sub $super (func)))
31+
(type $sub (sub $super (func)))
32+
)
33+
34+
(tag $e-import (import "env" "im0") (param i32))
35+
(import "env" "im1" (tag (param i32 f32)))
36+
;; CHECK-TEXT: (type $4 (func))
2537

2638
;; CHECK-TEXT: (import "env" "im0" (tag $e-import (type $1) (param i32)))
2739

2840
;; CHECK-TEXT: (import "env" "im1" (tag $eimport$0 (type $0) (param i32 f32)))
2941

30-
;; CHECK-TEXT: (tag $tag$1 (type $1) (param i32))
42+
;; CHECK-TEXT: (import "env" "im2" (tag $e-import-sub (type $sub)))
43+
;; CHECK-BIN: (type $4 (func))
3144

32-
;; CHECK-TEXT: (tag $e (type $0) (param i32 f32))
33-
;; CHECK-BIN: (type $0 (func (param i32 f32)))
45+
;; CHECK-BIN: (import "env" "im0" (tag $e-import (type $1) (param i32)))
3446

35-
;; CHECK-BIN: (type $1 (func (param i32)))
47+
;; CHECK-BIN: (import "env" "im1" (tag $eimport$1 (type $0) (param i32 f32)))
3648

37-
;; CHECK-BIN: (type $2 (func))
49+
;; CHECK-BIN: (import "env" "im2" (tag $e-import-sub (type $sub)))
50+
(import "env" "im2" (tag $e-import-sub (type $sub)))
3851

39-
;; CHECK-BIN: (import "env" "im0" (tag $e-import (type $1) (param i32)))
52+
(tag (param i32))
4053

41-
;; CHECK-BIN: (import "env" "im1" (tag $eimport$1 (type $0) (param i32 f32)))
54+
;; CHECK-TEXT: (tag $tag$1 (type $1) (param i32))
4255

56+
;; CHECK-TEXT: (tag $e (type $0) (param i32 f32))
4357
;; CHECK-BIN: (tag $tag$0 (type $1) (param i32))
4458

4559
;; CHECK-BIN: (tag $e (type $0) (param i32 f32))
4660
(tag $e (param i32 f32))
47-
;; CHECK-TEXT: (tag $empty (type $2))
48-
;; CHECK-BIN: (tag $empty (type $2))
61+
;; CHECK-TEXT: (tag $empty (type $4))
62+
;; CHECK-BIN: (tag $empty (type $4))
4963
(tag $empty)
64+
;; CHECK-TEXT: (tag $e-super (type $super))
65+
;; CHECK-BIN: (tag $e-super (type $super))
66+
(tag $e-super (type $super))
67+
;; CHECK-TEXT: (tag $e-sub (type $sub))
68+
;; CHECK-BIN: (tag $e-sub (type $sub))
69+
(tag $e-sub (type $sub))
5070

5171
;; CHECK-TEXT: (tag $e-params0 (type $0) (param i32 f32))
5272
;; CHECK-BIN: (tag $e-params0 (type $0) (param i32 f32))
@@ -71,24 +91,35 @@
7191

7292
;; CHECK-BIN-NODEBUG: (type $1 (func (param i32)))
7393

74-
;; CHECK-BIN-NODEBUG: (type $2 (func))
94+
;; CHECK-BIN-NODEBUG: (rec
95+
;; CHECK-BIN-NODEBUG-NEXT: (type $2 (sub (func)))
96+
97+
;; CHECK-BIN-NODEBUG: (type $3 (sub $2 (func)))
98+
99+
;; CHECK-BIN-NODEBUG: (type $4 (func))
75100

76101
;; CHECK-BIN-NODEBUG: (import "env" "im0" (tag $eimport$0 (type $1) (param i32)))
77102

78103
;; CHECK-BIN-NODEBUG: (import "env" "im1" (tag $eimport$1 (type $0) (param i32 f32)))
79104

105+
;; CHECK-BIN-NODEBUG: (import "env" "im2" (tag $eimport$2 (type $3)))
106+
80107
;; CHECK-BIN-NODEBUG: (tag $tag$0 (type $1) (param i32))
81108

82109
;; CHECK-BIN-NODEBUG: (tag $tag$1 (type $0) (param i32 f32))
83110

84-
;; CHECK-BIN-NODEBUG: (tag $tag$2 (type $2))
111+
;; CHECK-BIN-NODEBUG: (tag $tag$2 (type $4))
112+
113+
;; CHECK-BIN-NODEBUG: (tag $tag$3 (type $2))
114+
115+
;; CHECK-BIN-NODEBUG: (tag $tag$4 (type $3))
85116

86-
;; CHECK-BIN-NODEBUG: (tag $tag$3 (type $0) (param i32 f32))
117+
;; CHECK-BIN-NODEBUG: (tag $tag$5 (type $0) (param i32 f32))
87118

88-
;; CHECK-BIN-NODEBUG: (tag $tag$4 (type $0) (param i32 f32))
119+
;; CHECK-BIN-NODEBUG: (tag $tag$6 (type $0) (param i32 f32))
89120

90-
;; CHECK-BIN-NODEBUG: (tag $tag$5 (type $1) (param i32))
121+
;; CHECK-BIN-NODEBUG: (tag $tag$7 (type $1) (param i32))
91122

92-
;; CHECK-BIN-NODEBUG: (export "ex0" (tag $tag$5))
123+
;; CHECK-BIN-NODEBUG: (export "ex0" (tag $tag$7))
93124

94125
;; CHECK-BIN-NODEBUG: (export "ex1" (tag $tag$1))

0 commit comments

Comments
 (0)