Skip to content

Commit e177651

Browse files
leonbettv8-internal-scoped@luci-project-accounts.iam.gserviceaccount.com
authored andcommitted
[wasm] Add superType input to WasmDefineSignatureType
This CL adds an optional superType input to the WasmDefineSignatureType JS operation. It also extends WasmSignatureTypeDescription to take an optional superType parameter that it passes on to the WasmTypeDescription constructor. Bug: 517707090 Change-Id: I0b6aa71450534d0a113d8bd4f3d57195d2d7245d Reviewed-on: https://chrome-internal-review.googlesource.com/c/v8/fuzzilli/+/9358622 Commit-Queue: Leon Bettscheider <bettscheider@chromium.org> Reviewed-by: Matthias Liedtke <mliedtke@google.com>
1 parent e58398f commit e177651

9 files changed

Lines changed: 91 additions & 15 deletions

File tree

Sources/Fuzzilli/Base/ProgramBuilder.swift

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6463,8 +6463,44 @@ public class ProgramBuilder {
64636463
}
64646464

64656465
@discardableResult
6466-
func wasmDefineSignatureType(signature: WasmSignature, indexTypes: [Variable]) -> Variable {
6467-
return emit(WasmDefineSignatureType(signature: signature), withInputs: indexTypes).output
6466+
func wasmDefineSignatureType(
6467+
signature: WasmSignature, indexTypes: [Variable], superType: Variable? = nil
6468+
) -> Variable {
6469+
var inputs: [Variable] = []
6470+
if let superType {
6471+
let superTypeDesc = type(of: superType).wasmTypeDefinition?.description
6472+
assert(
6473+
superTypeDesc != .selfReference, "Supertype cannot be a forward or self-reference")
6474+
guard let superSigType = superTypeDesc as? WasmSignatureTypeDescription else {
6475+
fatalError("Supertype of a signature must be a signature type")
6476+
}
6477+
6478+
assert(signature.parameterTypes.count == superSigType.signature.parameterTypes.count)
6479+
assert(signature.outputTypes.count == superSigType.signature.outputTypes.count)
6480+
6481+
// Contravariant parameters
6482+
for (superParam, subParam) in zip(
6483+
superSigType.signature.parameterTypes, signature.parameterTypes)
6484+
{
6485+
assert(subParam.subsumes(superParam))
6486+
}
6487+
6488+
// Covariant outputs
6489+
for (superOutput, subOutput) in zip(
6490+
superSigType.signature.outputTypes, signature.outputTypes)
6491+
{
6492+
assert(superOutput.subsumes(subOutput))
6493+
}
6494+
6495+
inputs.append(superType)
6496+
}
6497+
6498+
inputs += indexTypes
6499+
6500+
return emit(
6501+
WasmDefineSignatureType(signature: signature, hasSuperType: superType != nil),
6502+
withInputs: inputs
6503+
).output
64686504
}
64696505

64706506
/// Like wasmDefineSignatureType but instead of within a type group this defines a signature

Sources/Fuzzilli/FuzzIL/Instruction.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1750,6 +1750,7 @@ extension Instruction: ProtobufConvertible {
17501750
$0.wasmDefineSignatureType = Fuzzilli_Protobuf_WasmDefineSignatureType.with {
17511751
$0.parameterTypes = op.signature.parameterTypes.map(ILTypeToWasmTypeEnum)
17521752
$0.outputTypes = op.signature.outputTypes.map(ILTypeToWasmTypeEnum)
1753+
$0.hasSuperType_p = op.hasSuperType
17531754
}
17541755
case .wasmDefineArrayType(let op):
17551756
$0.wasmDefineArrayType = Fuzzilli_Protobuf_WasmDefineArrayType.with {
@@ -2921,7 +2922,8 @@ extension Instruction: ProtobufConvertible {
29212922
case .wasmDefineSignatureType(let p):
29222923
op = WasmDefineSignatureType(
29232924
signature: p.parameterTypes.map(WasmTypeEnumToILType)
2924-
=> p.outputTypes.map(WasmTypeEnumToILType))
2925+
=> p.outputTypes.map(WasmTypeEnumToILType),
2926+
hasSuperType: p.hasSuperType_p)
29252927
case .wasmDefineAdHocSignatureType(let p):
29262928
op = WasmDefineAdHocSignatureType(
29272929
signature: p.parameterTypes.map(WasmTypeEnumToILType)

Sources/Fuzzilli/FuzzIL/JSTyper.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,9 @@ public struct JSTyper: Analyzer {
513513
}
514514

515515
mutating func addSignatureType(
516-
def: Variable, signature: WasmSignature, inputs: ArraySlice<Variable>, isAdHoc: Bool = false
516+
def: Variable, signature: WasmSignature, inputs: ArraySlice<Variable>,
517+
isAdHoc: Bool = false,
518+
concreteHeapSupertype: WasmTypeDescription? = nil
517519
) {
518520
assert(isWithinTypeGroup)
519521
var inputs = inputs.makeIterator()
@@ -571,7 +573,8 @@ public struct JSTyper: Analyzer {
571573
description: WasmSignatureTypeDescription(
572574
signature: resolvedParameterTypes => resolvedOutputTypes,
573575
typeGroupIndex: tgIndex,
574-
isAdHoc: isAdHoc)))
576+
isAdHoc: isAdHoc,
577+
concreteHeapSupertype: concreteHeapSupertype)))
575578
typeGroups[typeGroups.count - 1].append(def)
576579
}
577580

@@ -2514,7 +2517,12 @@ public struct JSTyper: Analyzer {
25142517
}
25152518

25162519
case .wasmDefineSignatureType(let op):
2517-
addSignatureType(def: instr.output, signature: op.signature, inputs: instr.inputs)
2520+
let concreteHeapSupertype =
2521+
op.hasSuperType ? getTypeDescription(of: instr.inputs.first!) : nil
2522+
let sigInputs = op.hasSuperType ? instr.inputs.dropFirst() : instr.inputs
2523+
addSignatureType(
2524+
def: instr.output, signature: op.signature, inputs: sigInputs,
2525+
concreteHeapSupertype: concreteHeapSupertype)
25182526

25192527
case .wasmDefineArrayType(let op):
25202528
let elementRef = op.elementType.requiredInputCount() == 1 ? instr.inputs.last! : nil

Sources/Fuzzilli/FuzzIL/JsOperations.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2902,12 +2902,16 @@ class WasmDefineStructType: WasmTypeOperation {
29022902
class WasmDefineSignatureType: WasmTypeOperation {
29032903
override var opcode: Opcode { .wasmDefineSignatureType(self) }
29042904
let signature: WasmSignature
2905+
let hasSuperType: Bool
29052906

2906-
init(signature: WasmSignature) {
2907+
init(signature: WasmSignature, hasSuperType: Bool = false) {
29072908
self.signature = signature
2908-
let numInputs = (signature.outputTypes + signature.parameterTypes).map {
2909-
$0.requiredInputCount()
2910-
}.reduce(0) { $0 + $1 }
2909+
self.hasSuperType = hasSuperType
2910+
let numInputs =
2911+
(hasSuperType ? 1 : 0)
2912+
+ (signature.outputTypes + signature.parameterTypes).map {
2913+
$0.requiredInputCount()
2914+
}.reduce(0) { $0 + $1 }
29112915
super.init(numInputs: numInputs, numOutputs: 1, requiredContext: [.wasmTypeGroup])
29122916
}
29132917
}

Sources/Fuzzilli/FuzzIL/TypeSystem.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,13 +2718,17 @@ class WasmSignatureTypeDescription: WasmTypeDescription {
27182718
var signature: WasmSignature
27192719
let isAdHoc: Bool
27202720

2721-
init(signature: WasmSignature, typeGroupIndex: Int, isAdHoc: Bool = false) {
2721+
init(
2722+
signature: WasmSignature, typeGroupIndex: Int, isAdHoc: Bool = false,
2723+
concreteHeapSupertype: WasmTypeDescription? = nil
2724+
) {
27222725
self.signature = signature
27232726
self.isAdHoc = isAdHoc
27242727
// TODO(pawkra): support shared variant.
27252728
super.init(
27262729
typeGroupIndex: typeGroupIndex,
2727-
abstractHeapSupertype: HeapTypeInfo.init(.WasmFunc, shared: false))
2730+
abstractHeapSupertype: HeapTypeInfo.init(.WasmFunc, shared: false),
2731+
concreteHeapSupertype: concreteHeapSupertype)
27282732
}
27292733

27302734
override func format(abbreviate: Bool) -> String {

Sources/Fuzzilli/Lifting/FuzzILLifter.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,8 +1632,12 @@ public class FuzzILLifter: Lifter {
16321632
w.emit("\(outputs) <- WasmEndTypeGroup [\(inputs)]")
16331633

16341634
case .wasmDefineSignatureType(let op):
1635-
let inputs = instr.inputs.map(lift).joined(separator: ", ")
1636-
w.emit("\(output()) <- WasmDefineSignatureType(\(op.signature)) [\(inputs)]")
1635+
let superTypeInput = op.hasSuperType ? " superType=\(lift(instr.inputs.first!))" : ""
1636+
let sigInputs = (op.hasSuperType ? instr.inputs.dropFirst() : instr.inputs).map(lift)
1637+
.joined(separator: ", ")
1638+
w.emit(
1639+
"\(output()) <- WasmDefineSignatureType(\(op.signature))\(superTypeInput) [\(sigInputs)]"
1640+
)
16371641

16381642
case .wasmDefineAdHocSignatureType(let op):
16391643
let inputs = instr.inputs.map(lift).joined(separator: ", ")

Sources/Fuzzilli/Protobuf/operations.pb.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5849,6 +5849,8 @@ public struct Fuzzilli_Protobuf_WasmDefineSignatureType: Sendable {
58495849

58505850
public var outputTypes: [Fuzzilli_Protobuf_WasmILType] = []
58515851

5852+
public var hasSuperType_p: Bool = false
5853+
58525854
public var unknownFields = SwiftProtobuf.UnknownStorage()
58535855

58545856
public init() {}
@@ -15330,7 +15332,7 @@ extension Fuzzilli_Protobuf_WasmEndTypeGroup: SwiftProtobuf.Message, SwiftProtob
1533015332

1533115333
extension Fuzzilli_Protobuf_WasmDefineSignatureType: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
1533215334
public static let protoMessageName: String = _protobuf_package + ".WasmDefineSignatureType"
15333-
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}parameterTypes\0\u{1}outputTypes\0")
15335+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}parameterTypes\0\u{1}outputTypes\0\u{1}hasSuperType\0")
1533415336

1533515337
public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
1533615338
while let fieldNumber = try decoder.nextFieldNumber() {
@@ -15340,6 +15342,7 @@ extension Fuzzilli_Protobuf_WasmDefineSignatureType: SwiftProtobuf.Message, Swif
1534015342
switch fieldNumber {
1534115343
case 1: try { try decoder.decodeRepeatedMessageField(value: &self.parameterTypes) }()
1534215344
case 2: try { try decoder.decodeRepeatedMessageField(value: &self.outputTypes) }()
15345+
case 3: try { try decoder.decodeSingularBoolField(value: &self.hasSuperType_p) }()
1534315346
default: break
1534415347
}
1534515348
}
@@ -15352,12 +15355,16 @@ extension Fuzzilli_Protobuf_WasmDefineSignatureType: SwiftProtobuf.Message, Swif
1535215355
if !self.outputTypes.isEmpty {
1535315356
try visitor.visitRepeatedMessageField(value: self.outputTypes, fieldNumber: 2)
1535415357
}
15358+
if self.hasSuperType_p != false {
15359+
try visitor.visitSingularBoolField(value: self.hasSuperType_p, fieldNumber: 3)
15360+
}
1535515361
try unknownFields.traverse(visitor: &visitor)
1535615362
}
1535715363

1535815364
public static func ==(lhs: Fuzzilli_Protobuf_WasmDefineSignatureType, rhs: Fuzzilli_Protobuf_WasmDefineSignatureType) -> Bool {
1535915365
if lhs.parameterTypes != rhs.parameterTypes {return false}
1536015366
if lhs.outputTypes != rhs.outputTypes {return false}
15367+
if lhs.hasSuperType_p != rhs.hasSuperType_p {return false}
1536115368
if lhs.unknownFields != rhs.unknownFields {return false}
1536215369
return true
1536315370
}

Sources/Fuzzilli/Protobuf/operations.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,7 @@ message WasmEndTypeGroup {
15001500
message WasmDefineSignatureType {
15011501
repeated WasmILType parameterTypes = 1;
15021502
repeated WasmILType outputTypes = 2;
1503+
bool hasSuperType = 3;
15031504
}
15041505

15051506
message WasmDefineAdHocSignatureType {

Tests/FuzzilliTests/TypeSystemTest.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,16 @@ class TypeSystemTests: XCTestCase {
16821682
XCTAssertFalse(subStructDescMultiWidthAndDepth.subsumes(superStructDescMulti))
16831683
}
16841684

1685+
func testWasmSignatureSubtypingRules() {
1686+
let superSigDesc = WasmSignatureTypeDescription(
1687+
signature: [] => [], typeGroupIndex: 0)
1688+
let subSigDesc = WasmSignatureTypeDescription(
1689+
signature: [] => [], typeGroupIndex: 1, concreteHeapSupertype: superSigDesc)
1690+
1691+
XCTAssertTrue(superSigDesc.subsumes(subSigDesc))
1692+
XCTAssertFalse(subSigDesc.subsumes(superSigDesc))
1693+
}
1694+
16851695
func testWasmTypeExtensionUnionTypeExtensionVsWasmTypeExtension() {
16861696
let tagA = ILType.object(ofGroup: "WasmTag", withWasmType: WasmTagType([.wasmi32]))
16871697
let tagB = ILType.object(ofGroup: "WasmTag", withWasmType: WasmTagType([.wasmi64]))

0 commit comments

Comments
 (0)