Skip to content

Commit 0043c8b

Browse files
LiedtkeV8-internal LUCI CQ
authored andcommitted
[wasm] Migrate tags to use wasm-gc signatures
With all instructions interacting with Wasm tags switched over to using wasm-gc signatures in previous changes, tags can now also be adapted to use wasm-gc types in their signature (their parameter types). Note that it is also possible to define tags from JS, e.g.: > new WebAssembly.Tag({parameters: ['i32']}) However, these tags do not support index types in the JS API spec, so they can continue using the current mechanism for their type information. Bug: 448860865 Change-Id: If558f0562609d7a26a0119a4055184506351bd52 Reviewed-on: https://chrome-internal-review.googlesource.com/c/v8/fuzzilli/+/8956197 Reviewed-by: Doga Yüksel <dyuksel@google.com> Commit-Queue: Matthias Liedtke <mliedtke@google.com>
1 parent 59e7a3d commit 0043c8b

File tree

10 files changed

+41
-48
lines changed

10 files changed

+41
-48
lines changed

Sources/Fuzzilli/Base/ProgramBuilder.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4534,9 +4534,16 @@ public class ProgramBuilder {
45344534
return b.emit(WasmDefineDataSegment(segment: segment)).output
45354535
}
45364536

4537+
@discardableResult
4538+
public func addTag(signature: Variable) -> Variable {
4539+
return b.emit(WasmDefineTag(), withInputs: [signature]).output
4540+
}
4541+
4542+
// Convenience function to create a tag including an adhoc signature definition.
45374543
@discardableResult
45384544
public func addTag(parameterTypes: [ILType]) -> Variable {
4539-
return b.emit(WasmDefineTag(parameterTypes: parameterTypes)).output
4545+
let signatureDef = b.wasmDefineAdHocSignatureType(signature: parameterTypes => [])
4546+
return addTag(signature: signatureDef)
45404547
}
45414548

45424549
private func getModuleVariable() -> Variable {
@@ -4616,7 +4623,8 @@ public class ProgramBuilder {
46164623
}
46174624
}
46184625

4619-
public func randomTagParameters() -> [ILType] {
4626+
// Random tag parameters for Wasm tags defined via the JS API
4627+
public func randomTagParametersJs() -> [ILType] {
46204628
// TODO(mliedtke): The list of types should be shared with function signature generation
46214629
// etc. We should also support non-nullable references but that requires being able
46224630
// to generate valid ones which currently isn't the case for most of them.
@@ -4646,16 +4654,17 @@ public class ProgramBuilder {
46464654
return params => returnTypes
46474655
}
46484656

4649-
public func randomWasmGcSignature() -> (signature: WasmSignature, indexTypes: [Variable]) {
4657+
public func randomWasmGcSignature(withResults: Bool = true, allowNonNullable: Bool = true)
4658+
-> (signature: WasmSignature, indexTypes: [Variable]) {
46504659
let typeCount = Int.random(in: 0...10)
4651-
let returnCount = Int.random(in: 0...typeCount)
4660+
let returnCount = withResults ? Int.random(in: 0...typeCount) : 0
46524661
let parameterCount = typeCount - returnCount
46534662

46544663
var indexTypes: [Variable] = []
46554664
let chooseType = {
46564665
if let elementType = self.randomVariable(ofType: .wasmTypeDef()), probability(0.25) {
4657-
let nullability =
4658-
self.type(of: elementType).wasmTypeDefinition!.description == .selfReference
4666+
let nullability = !allowNonNullable
4667+
|| self.type(of: elementType).wasmTypeDefinition!.description == .selfReference
46594668
|| probability(0.5)
46604669
indexTypes.append(elementType)
46614670
return ILType.wasmRef(.Index(), nullability: nullability)

Sources/Fuzzilli/CodeGen/ProgramTemplates.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public let ProgramTemplates = [
146146

147147
// A few tags (wasm exception kinds) to be used later on.
148148
let wasmTags = (0...Int.random(in: 0..<5)).map { _ in
149-
b.createWasmTag(parameterTypes: b.randomTagParameters())
149+
b.createWasmTag(parameterTypes: b.randomTagParametersJs())
150150
}
151151
let tags = [b.createWasmJSTag()] + wasmTags
152152
let tagToThrow = chooseUniform(from: wasmTags)

Sources/Fuzzilli/CodeGen/WasmCodeGenerators.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public let WasmCodeGenerators: [CodeGenerator] = [
5252
if probability(0.5) {
5353
b.createWasmJSTag()
5454
} else {
55-
b.createWasmTag(parameterTypes: b.randomTagParameters())
55+
b.createWasmTag(parameterTypes: b.randomTagParametersJs())
5656
}
5757
},
5858
//
@@ -1589,7 +1589,13 @@ public let WasmCodeGenerators: [CodeGenerator] = [
15891589
"WasmDefineTagGenerator", inContext: .single(.wasm),
15901590
produces: [.object(ofGroup: "WasmTag")]
15911591
) { b in
1592-
b.currentWasmModule.addTag(parameterTypes: b.randomTagParameters())
1592+
// TODO(mliedtke): If we allow non-nullable reference types in signatures, we'll also need
1593+
// to be able to provide valid values for them when trying to throw an instance of this tag.
1594+
let (signature, indexTypes) =
1595+
b.randomWasmGcSignature(withResults: false, allowNonNullable: false)
1596+
let signatureDef =
1597+
b.wasmDefineAdHocSignatureType(signature: signature, indexTypes: indexTypes)
1598+
b.currentWasmModule.addTag(signature: signatureDef)
15931599
},
15941600

15951601
CodeGenerator(

Sources/Fuzzilli/FuzzIL/Instruction.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,10 +1438,8 @@ extension Instruction: ProtobufConvertible {
14381438
$0.wasmThrowRef = Fuzzilli_Protobuf_WasmThrowRef()
14391439
case .wasmRethrow(_):
14401440
$0.wasmRethrow = Fuzzilli_Protobuf_WasmRethrow()
1441-
case .wasmDefineTag(let op):
1442-
$0.wasmDefineTag = Fuzzilli_Protobuf_WasmDefineTag.with {
1443-
$0.parameterTypes = op.parameterTypes.map(ILTypeToWasmTypeEnum)
1444-
}
1441+
case .wasmDefineTag(_):
1442+
$0.wasmDefineTag = Fuzzilli_Protobuf_WasmDefineTag()
14451443
case .wasmBranch(_):
14461444
$0.wasmBranch = Fuzzilli_Protobuf_WasmBranch()
14471445
case .wasmBranchIf(let op):
@@ -2483,8 +2481,8 @@ extension Instruction: ProtobufConvertible {
24832481
op = WasmThrowRef()
24842482
case .wasmRethrow(_):
24852483
op = WasmRethrow()
2486-
case .wasmDefineTag(let p):
2487-
op = WasmDefineTag(parameterTypes: p.parameterTypes.map(WasmTypeEnumToILType))
2484+
case .wasmDefineTag(_):
2485+
op = WasmDefineTag()
24882486
case .wasmBranch(_):
24892487
op = WasmBranch(parameterCount: inouts.count - 1)
24902488
case .wasmBranchIf(let p):

Sources/Fuzzilli/FuzzIL/JSTyper.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,9 @@ public struct JSTyper: Analyzer {
754754
setType(of: instr.output, to: .wasmDataSegment(segmentLength: op.segment.count))
755755
case .wasmDropDataSegment(_):
756756
type(of: instr.input(0)).wasmDataSegmentType!.markAsDropped()
757-
case .wasmDefineTag(let op):
758-
setType(of: instr.output, to: .object(ofGroup: "WasmTag", withWasmType: WasmTagType(op.parameterTypes)))
757+
case .wasmDefineTag(_):
758+
let signature = type(of: instr.input(0)).wasmFunctionSignatureDefSignature
759+
setType(of: instr.output, to: .object(ofGroup: "WasmTag", withWasmType: WasmTagType(signature.parameterTypes)))
759760
dynamicObjectGroupManager.addWasmTag(withType: type(of: instr.output), forDefinition: instr, forVariable: instr.output)
760761
case .wasmThrow(_):
761762
let definingInstruction = defUseAnalyzer.definition(of: instr.input(0))

Sources/Fuzzilli/FuzzIL/WasmOperations.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -877,11 +877,10 @@ final class WasmDefineDataSegment: WasmOperation {
877877

878878
final class WasmDefineTag: WasmOperation {
879879
override var opcode: Opcode { .wasmDefineTag(self) }
880-
public let parameterTypes: [ILType]
881880

882-
init(parameterTypes: [ILType]) {
883-
self.parameterTypes = parameterTypes
884-
super.init(numOutputs: 1, attributes: [], requiredContext: [.wasm])
881+
init() {
882+
// Inputs: The signature.
883+
super.init(numInputs: 1, numOutputs: 1, attributes: [], requiredContext: [.wasm])
885884
}
886885
}
887886

Sources/Fuzzilli/Lifting/FuzzILLifter.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -873,8 +873,8 @@ public class FuzzILLifter: Lifter {
873873
case .wasmDefineDataSegment(_):
874874
w.emit("\(output()) <- WasmDefineDataSegment [...]")
875875

876-
case .wasmDefineTag(let op):
877-
w.emit("\(output()) <- WasmDefineTag \(op.parameterTypes)")
876+
case .wasmDefineTag(_):
877+
w.emit("\(output()) <- WasmDefineTag \(input(0))")
878878

879879
case .wasmLoadGlobal(_):
880880
w.emit("\(output()) <- WasmLoadGlobal \(input(0))")

Sources/Fuzzilli/Lifting/WasmLifter.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -622,12 +622,6 @@ public class WasmLifter {
622622
registerSignature(signature)
623623
}
624624

625-
// Special handling for defined Tags
626-
for case let .tag(instr) in self.exports {
627-
let tagSignature = (instr!.op as! WasmDefineTag).parameterTypes => []
628-
assert(tagSignature.outputTypes.isEmpty)
629-
registerSignature(tagSignature)
630-
}
631625
// Special handling for defined functions
632626
for case let .function(functionInfo) in self.exports {
633627
registerSignature(functionInfo!.signature)
@@ -1142,9 +1136,9 @@ public class WasmLifter {
11421136
section += Leb128.unsignedEncode(self.exports.count { $0.isTag })
11431137

11441138
for case let .tag(instr) in self.exports {
1145-
let tagSignature = (instr!.op as! WasmDefineTag).parameterTypes => []
1139+
let signatureDesc = typer.getTypeDescription(of: instr!.input(0))
11461140
section.append(0)
1147-
section.append(Leb128.unsignedEncode(try getSignatureIndex(tagSignature)))
1141+
section.append(Leb128.unsignedEncode(typeDescToIndex[signatureDesc]!))
11481142
}
11491143

11501144
self.bytecode.append(Leb128.unsignedEncode(section.count))

Sources/Fuzzilli/Protobuf/operations.pb.swift

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5323,8 +5323,6 @@ public struct Fuzzilli_Protobuf_WasmDefineTag: Sendable {
53235323
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
53245324
// methods supported on all messages.
53255325

5326-
public var parameterTypes: [Fuzzilli_Protobuf_WasmILType] = []
5327-
53285326
public var unknownFields = SwiftProtobuf.UnknownStorage()
53295327

53305328
public init() {}
@@ -14093,29 +14091,18 @@ extension Fuzzilli_Protobuf_WasmRethrow: SwiftProtobuf.Message, SwiftProtobuf._M
1409314091

1409414092
extension Fuzzilli_Protobuf_WasmDefineTag: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
1409514093
public static let protoMessageName: String = _protobuf_package + ".WasmDefineTag"
14096-
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}parameterTypes\0")
14094+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap()
1409714095

1409814096
public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
14099-
while let fieldNumber = try decoder.nextFieldNumber() {
14100-
// The use of inline closures is to circumvent an issue where the compiler
14101-
// allocates stack space for every case branch when no optimizations are
14102-
// enabled. https://github.com/apple/swift-protobuf/issues/1034
14103-
switch fieldNumber {
14104-
case 1: try { try decoder.decodeRepeatedMessageField(value: &self.parameterTypes) }()
14105-
default: break
14106-
}
14107-
}
14097+
// Load everything into unknown fields
14098+
while try decoder.nextFieldNumber() != nil {}
1410814099
}
1410914100

1411014101
public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
14111-
if !self.parameterTypes.isEmpty {
14112-
try visitor.visitRepeatedMessageField(value: self.parameterTypes, fieldNumber: 1)
14113-
}
1411414102
try unknownFields.traverse(visitor: &visitor)
1411514103
}
1411614104

1411714105
public static func ==(lhs: Fuzzilli_Protobuf_WasmDefineTag, rhs: Fuzzilli_Protobuf_WasmDefineTag) -> Bool {
14118-
if lhs.parameterTypes != rhs.parameterTypes {return false}
1411914106
if lhs.unknownFields != rhs.unknownFields {return false}
1412014107
return true
1412114108
}

Sources/Fuzzilli/Protobuf/operations.proto

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,6 @@ message WasmRethrow {
13031303
}
13041304

13051305
message WasmDefineTag {
1306-
repeated WasmILType parameterTypes = 1;
13071306
}
13081307

13091308
message WasmBranch {

0 commit comments

Comments
 (0)