Skip to content

Commit bc4838e

Browse files
authored
Validate strings in types (#8314)
Return an error when building a type that contains a string type when strings are not enabled. This prevents the fuzzer from trying to run modules that contain string types on V8.
1 parent e3adbad commit bc4838e

File tree

5 files changed

+54
-32
lines changed

5 files changed

+54
-32
lines changed

src/wasm-type.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,8 @@ struct TypeBuilder {
906906
InvalidFuncType,
907907
// A shared type with shared-everything disabled.
908908
InvalidSharedType,
909+
// A string type with strings disabled.
910+
InvalidStringType,
909911
// A non-shared field of a shared heap type.
910912
InvalidUnsharedField,
911913
// A describes clause on a non-struct type.

src/wasm/wasm-type.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,8 @@ std::ostream& operator<<(std::ostream& os, TypeBuilder::ErrorReason reason) {
14471447
return os << "Continuation has invalid function type";
14481448
case TypeBuilder::ErrorReason::InvalidSharedType:
14491449
return os << "Shared types require shared-everything";
1450+
case TypeBuilder::ErrorReason::InvalidStringType:
1451+
return os << "String types require strings feature";
14501452
case TypeBuilder::ErrorReason::InvalidUnsharedField:
14511453
return os << "Heap type has an invalid unshared field";
14521454
case TypeBuilder::ErrorReason::NonStructDescribes:
@@ -2435,6 +2437,9 @@ validateType(Type type, FeatureSet feats, bool isShared) {
24352437
if (heapType.isShared() && !feats.hasSharedEverything()) {
24362438
return TypeBuilder::ErrorReason::InvalidSharedType;
24372439
}
2440+
if (heapType.isString() && !feats.hasStrings()) {
2441+
return TypeBuilder::ErrorReason::InvalidStringType;
2442+
}
24382443
}
24392444
return std::nullopt;
24402445
}

test/lit/passes/gto-jsinterop.wast

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -206,38 +206,6 @@
206206
)
207207
)
208208

209-
(module
210-
(rec
211-
;; CHECK: (rec
212-
;; CHECK-NEXT: (type $struct (descriptor $desc) (struct))
213-
(type $struct (descriptor $desc) (struct))
214-
;; Strings cannot be prototypes. If the prototype field holds a string, it
215-
;; will appear as a null prototype. That is the default prototype value, so
216-
;; we can optimize out prototype fields if we know they will be strings.
217-
;; CHECK: (type $desc (describes $struct) (struct))
218-
(type $desc (describes $struct) (struct (field stringref)))
219-
)
220-
221-
;; CHECK: (type $2 (func))
222-
223-
;; CHECK: (func $externalize (type $2)
224-
;; CHECK-NEXT: (local $struct (ref null $struct))
225-
;; CHECK-NEXT: (drop
226-
;; CHECK-NEXT: (extern.convert_any
227-
;; CHECK-NEXT: (local.get $struct)
228-
;; CHECK-NEXT: )
229-
;; CHECK-NEXT: )
230-
;; CHECK-NEXT: )
231-
(func $externalize
232-
(local $struct (ref null $struct))
233-
(drop
234-
(extern.convert_any
235-
(local.get $struct)
236-
)
237-
)
238-
)
239-
)
240-
241209
(module
242210
(rec
243211
;; CHECK: (rec
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
2+
3+
;; RUN: foreach %s %t wasm-opt -all --closed-world --gto --preserve-type-order -S -o - | filecheck %s
4+
5+
(module
6+
(rec
7+
;; CHECK: (rec
8+
;; CHECK-NEXT: (type $struct (descriptor $desc) (struct))
9+
(type $struct (descriptor $desc) (struct))
10+
;; Strings cannot be prototypes. If the prototype field holds a string, it
11+
;; will appear as a null prototype. That is the default prototype value, so
12+
;; we can optimize out prototype fields if we know they will be strings.
13+
;; CHECK: (type $desc (describes $struct) (struct))
14+
(type $desc (describes $struct) (struct (field stringref)))
15+
)
16+
17+
;; CHECK: (type $2 (func))
18+
19+
;; CHECK: (func $externalize (type $2)
20+
;; CHECK-NEXT: (local $struct (ref null $struct))
21+
;; CHECK-NEXT: (drop
22+
;; CHECK-NEXT: (extern.convert_any
23+
;; CHECK-NEXT: (local.get $struct)
24+
;; CHECK-NEXT: )
25+
;; CHECK-NEXT: )
26+
;; CHECK-NEXT: )
27+
(func $externalize
28+
(local $struct (ref null $struct))
29+
(drop
30+
(extern.convert_any
31+
(local.get $struct)
32+
)
33+
)
34+
)
35+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
;; Test that string heap types require strings to be enabled.
2+
3+
;; RUN: not wasm-opt %s 2>&1 | filecheck %s --check-prefix NO-STRINGS
4+
;; RUN: wasm-opt %s --enable-reference-types --enable-strings -o - -S | filecheck %s --check-prefix STRINGS
5+
6+
;; NO-STRINGS: invalid type: String types require strings feature
7+
;; STRINGS: (type $s (func (param stringref)))
8+
9+
(module
10+
(type $s (func (param stringref)))
11+
(global $g (ref null $s) (ref.null nofunc))
12+
)

0 commit comments

Comments
 (0)