Skip to content

Commit f890d78

Browse files
LiedtkeV8-internal LUCI CQ
authored andcommitted
[wasm] Migrate legacy try-delegate to use wasm-gc signatures
Bug: 448860865 Change-Id: I89bdc92e1757a68dec64da8a7ab90e7c397694eb Reviewed-on: https://chrome-internal-review.googlesource.com/c/v8/fuzzilli/+/8956317 Reviewed-by: Doga Yüksel <dyuksel@google.com> Commit-Queue: Matthias Liedtke <mliedtke@google.com>
1 parent 782f399 commit f890d78

9 files changed

Lines changed: 59 additions & 61 deletions

File tree

Sources/Fuzzilli/Base/ProgramBuilder.swift

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4153,18 +4153,24 @@ public class ProgramBuilder {
41534153

41544154
public func wasmBuildLegacyTryDelegate(with signature: WasmSignature, args: [Variable], body: (Variable, [Variable]) -> Void, delegate: Variable) {
41554155
assert(signature.outputTypes.isEmpty)
4156-
let instr = b.emit(WasmBeginTryDelegate(with: signature), withInputs: args, types: signature.parameterTypes)
4156+
let signatureDef = b.wasmDefineAdHocSignatureType(signature: signature)
4157+
let instr = b.emit(WasmBeginTryDelegate(parameterCount: signature.parameterTypes.count),
4158+
withInputs: [signatureDef] + args,
4159+
types: [.wasmTypeDef()] + signature.parameterTypes)
41574160
body(instr.innerOutput(0), Array(instr.innerOutputs(1...)))
4158-
b.emit(WasmEndTryDelegate(), withInputs: [delegate])
4161+
b.emit(WasmEndTryDelegate(outputCount: 0), withInputs: [signatureDef, delegate])
41594162
}
41604163

41614164
@discardableResult
41624165
public func wasmBuildLegacyTryDelegateWithResult(with signature: WasmSignature, args: [Variable], body: (Variable, [Variable]) -> [Variable], delegate: Variable) -> [Variable] {
4163-
let instr = b.emit(WasmBeginTryDelegate(with: signature), withInputs: args, types: signature.parameterTypes)
4166+
let signatureDef = b.wasmDefineAdHocSignatureType(signature: signature)
4167+
let instr = b.emit(WasmBeginTryDelegate(parameterCount: signature.parameterTypes.count),
4168+
withInputs: [signatureDef] + args,
4169+
types: [.wasmTypeDef()] + signature.parameterTypes)
41644170
let results = body(instr.innerOutput(0), Array(instr.innerOutputs(1...)))
4165-
return Array(b.emit(WasmEndTryDelegate(outputTypes: signature.outputTypes),
4166-
withInputs: [delegate] + results,
4167-
types: [.anyLabel] + signature.outputTypes
4171+
return Array(b.emit(WasmEndTryDelegate(outputCount: signature.outputTypes.count),
4172+
withInputs: [signatureDef, delegate] + results,
4173+
types: [.wasmTypeDef(), .anyLabel] + signature.outputTypes
41684174
).outputs)
41694175
}
41704176

@@ -4969,14 +4975,13 @@ public class ProgramBuilder {
49694975
break
49704976
case .beginWasmFunction(let op):
49714977
activeWasmModule!.functions.append(WasmFunction(forBuilder: self, withSignature: op.signature))
4972-
case .wasmBeginTry(_):
4978+
case .wasmBeginTry(_),
4979+
.wasmEndTryDelegate(_),
4980+
.wasmBeginTryDelegate(_):
49734981
break
4974-
case .wasmBeginTryDelegate(let op):
4975-
activeWasmModule!.blockSignatures.push(op.signature)
49764982
case .wasmBeginTryTable(let op):
49774983
activeWasmModule!.blockSignatures.push(op.signature)
4978-
case .wasmEndTryDelegate(_),
4979-
.wasmEndTryTable(_):
4984+
case .wasmEndTryTable(_):
49804985
activeWasmModule!.blockSignatures.pop()
49814986
case .wasmDefineAdHocModuleSignatureType(_):
49824987
break

Sources/Fuzzilli/CodeGen/WasmCodeGenerators.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,7 @@ public let WasmCodeGenerators: [CodeGenerator] = [
14441444
) { b, label in
14451445
let function = b.currentWasmModule.currentWasmFunction
14461446
// Choose a few random wasm values as arguments if available.
1447-
let args = b.randomWasmBlockArguments(upTo: 5)
1447+
let args = b.randomWasmBlockArguments(upTo: 5, allowingGcTypes: true)
14481448
let outputTypes = b.randomWasmBlockOutputTypes(upTo: 3)
14491449
let parameters = args.map(b.type)
14501450
function.wasmBuildLegacyTryDelegateWithResult(

Sources/Fuzzilli/FuzzIL/Instruction.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,12 +1427,11 @@ extension Instruction: ProtobufConvertible {
14271427
}
14281428
case .wasmBeginTryDelegate(let op):
14291429
$0.wasmBeginTryDelegate = Fuzzilli_Protobuf_WasmBeginTryDelegate.with {
1430-
$0.parameterTypes = op.signature.parameterTypes.map(ILTypeToWasmTypeEnum)
1431-
$0.outputTypes = op.signature.outputTypes.map(ILTypeToWasmTypeEnum)
1430+
$0.parameterCount = Int32(op.numInputs - 1)
14321431
}
14331432
case .wasmEndTryDelegate(let op):
14341433
$0.wasmEndTryDelegate = Fuzzilli_Protobuf_WasmEndTryDelegate.with {
1435-
$0.outputTypes = op.outputTypes.map(ILTypeToWasmTypeEnum)
1434+
$0.outputCount = Int32(op.numOutputs)
14361435
}
14371436
case .wasmThrow(_):
14381437
$0.wasmThrow = Fuzzilli_Protobuf_WasmThrow()
@@ -2478,11 +2477,9 @@ extension Instruction: ProtobufConvertible {
24782477
case .wasmEndTry(let p):
24792478
op = WasmEndTry(blockOutputCount: Int(p.blockOutputCount))
24802479
case .wasmBeginTryDelegate(let p):
2481-
let parameters = p.parameterTypes.map(WasmTypeEnumToILType)
2482-
let outputs = p.outputTypes.map(WasmTypeEnumToILType)
2483-
op = WasmBeginTryDelegate(with: parameters => outputs)
2480+
op = WasmBeginTryDelegate(parameterCount: Int(p.parameterCount))
24842481
case .wasmEndTryDelegate(let p):
2485-
op = WasmEndTryDelegate(outputTypes: p.outputTypes.map(WasmTypeEnumToILType))
2482+
op = WasmEndTryDelegate(outputCount: Int(p.outputCount))
24862483
case .wasmThrow(_):
24872484
op = WasmThrow(parameterCount: inouts.count - 1)
24882485
case .wasmThrowRef(_):

Sources/Fuzzilli/FuzzIL/JSTyper.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -894,10 +894,12 @@ public struct JSTyper: Analyzer {
894894
case .wasmEndTry(_):
895895
let blockSignature = type(of: instr.input(0)).wasmFunctionSignatureDefSignature
896896
wasmTypeEndBlock(instr, blockSignature.outputTypes)
897-
case .wasmBeginTryDelegate(let op):
898-
wasmTypeBeginBlock(instr, op.signature)
899-
case .wasmEndTryDelegate(let op):
900-
wasmTypeEndBlock(instr, op.outputTypes)
897+
case .wasmBeginTryDelegate(_):
898+
let blockSignature = type(of: instr.input(0)).wasmFunctionSignatureDefSignature
899+
wasmTypeBeginBlock(instr, blockSignature)
900+
case .wasmEndTryDelegate(_):
901+
let blockSignature = type(of: instr.input(0)).wasmFunctionSignatureDefSignature
902+
wasmTypeEndBlock(instr, blockSignature.outputTypes)
901903
case .wasmCallDirect(_):
902904
let signature = type(of: instr.input(0)).wasmFunctionDefSignature!
903905
for (output, outputType) in zip(instr.outputs, signature.outputTypes) {

Sources/Fuzzilli/FuzzIL/WasmOperations.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,24 +1445,26 @@ final class WasmEndTry: WasmOperation {
14451445
/// A special try block that does not have any catch / catch_all handlers but ends with a delegate to handle the exception.
14461446
final class WasmBeginTryDelegate: WasmOperation {
14471447
override var opcode: Opcode { .wasmBeginTryDelegate(self) }
1448-
let signature: WasmSignature
14491448

1450-
init(with signature: WasmSignature) {
1451-
self.signature = signature
1452-
super.init(numInputs: signature.parameterTypes.count, numInnerOutputs: 1 + signature.parameterTypes.count, attributes: [.isBlockStart, .propagatesSurroundingContext], requiredContext: [.wasmFunction], contextOpened: [])
1449+
init(parameterCount: Int) {
1450+
// inputs: The signature and the arguments.
1451+
// innerOutputs: The label and the arguments.
1452+
super.init(numInputs: 1 + parameterCount, numInnerOutputs: 1 + parameterCount,
1453+
attributes: [.isBlockStart, .propagatesSurroundingContext],
1454+
requiredContext: [.wasmFunction])
14531455
}
14541456
}
14551457

14561458
/// Delegates any exception thrown inside WasmBeginTryDelegate and this end to another block defined by the label.
14571459
/// This can be a "proper" try block (in which case its catch blocks apply) or any other block like a loop or an if.
14581460
final class WasmEndTryDelegate: WasmOperation {
14591461
override var opcode: Opcode { .wasmEndTryDelegate(self) }
1460-
let outputTypes: [ILType]
14611462

1462-
init(outputTypes: [ILType] = []) {
1463-
self.outputTypes = outputTypes
1464-
// Inputs: 1 label to delegate an exception to plus all the outputs of the try block.
1465-
super.init(numInputs: 1 + outputTypes.count, numOutputs: outputTypes.count, attributes: [.isBlockEnd, .resumesSurroundingContext], requiredContext: [.wasmFunction])
1463+
init(outputCount: Int) {
1464+
// Inputs: The signature, the label to delegate an exception to plus all the outputs of the
1465+
// try block.
1466+
super.init(numInputs: 2 + outputCount, numOutputs: outputCount,
1467+
attributes: [.isBlockEnd, .resumesSurroundingContext], requiredContext: [.wasmFunction])
14661468
}
14671469
}
14681470

Sources/Fuzzilli/Lifting/FuzzILLifter.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,8 +1176,8 @@ public class FuzzILLifter: Lifter {
11761176
case .wasmRethrow(_):
11771177
w.emit("WasmRethrow \(instr.input(0))")
11781178

1179-
case .wasmBeginTryDelegate(let op):
1180-
w.emit("WasmBeginTryDelegate -> L:\(instr.innerOutput(0)) [\(liftCallArguments(instr.innerOutputs(1...)))] (\(op.signature))")
1179+
case .wasmBeginTryDelegate(_):
1180+
w.emit("WasmBeginTryDelegate -> L:\(instr.innerOutput(0)) [\(liftCallArguments(instr.innerOutputs(1...)))]")
11811181
w.increaseIndentionLevel()
11821182

11831183
case .wasmEndTryDelegate(_):

Sources/Fuzzilli/Lifting/WasmLifter.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,8 +1257,7 @@ public class WasmLifter {
12571257
self.currentFunction!.labelBranchDepthMapping[instr.innerOutput(0)] = self.currentFunction!.variableAnalyzer.wasmBranchDepth
12581258
// Needs typer analysis
12591259
return true
1260-
case .wasmBeginTryDelegate(let op):
1261-
registerSignature(op.signature)
1260+
case .wasmBeginTryDelegate(_):
12621261
self.currentFunction!.labelBranchDepthMapping[instr.innerOutput(0)] = self.currentFunction!.variableAnalyzer.wasmBranchDepth
12631262
// Needs typer analysis
12641263
return true
@@ -1935,8 +1934,9 @@ public class WasmLifter {
19351934
case .wasmBeginTry(_):
19361935
let signatureDesc = typer.getTypeDescription(of: wasmInstruction.input(0))
19371936
return Data([0x06] + Leb128.unsignedEncode(typeDescToIndex[signatureDesc]!))
1938-
case .wasmBeginTryDelegate(let op):
1939-
return Data([0x06] + Leb128.unsignedEncode(getSignatureIndexStrict(op.signature)))
1937+
case .wasmBeginTryDelegate(_):
1938+
let signatureDesc = typer.getTypeDescription(of: wasmInstruction.input(0))
1939+
return Data([0x06] + Leb128.unsignedEncode(typeDescToIndex[signatureDesc]!))
19401940
case .wasmBeginCatchAll(_):
19411941
return Data([0x19])
19421942
case .wasmBeginCatch(_):
@@ -1949,7 +1949,7 @@ public class WasmLifter {
19491949
// Basically the same as EndBlock, just an explicit instruction.
19501950
return Data([0x0B])
19511951
case .wasmEndTryDelegate(_):
1952-
let branchDepth = try branchDepthFor(label: wasmInstruction.input(0))
1952+
let branchDepth = try branchDepthFor(label: wasmInstruction.input(1))
19531953
// Mutation might make this EndTryDelegate branch to itself, which should not happen.
19541954
if branchDepth < 0 {
19551955
throw WasmLifter.CompileError.invalidBranch

Sources/Fuzzilli/Protobuf/operations.pb.swift

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5271,9 +5271,7 @@ public struct Fuzzilli_Protobuf_WasmBeginTryDelegate: Sendable {
52715271
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
52725272
// methods supported on all messages.
52735273

5274-
public var parameterTypes: [Fuzzilli_Protobuf_WasmILType] = []
5275-
5276-
public var outputTypes: [Fuzzilli_Protobuf_WasmILType] = []
5274+
public var parameterCount: Int32 = 0
52775275

52785276
public var unknownFields = SwiftProtobuf.UnknownStorage()
52795277

@@ -5285,7 +5283,7 @@ public struct Fuzzilli_Protobuf_WasmEndTryDelegate: Sendable {
52855283
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
52865284
// methods supported on all messages.
52875285

5288-
public var outputTypes: [Fuzzilli_Protobuf_WasmILType] = []
5286+
public var outputCount: Int32 = 0
52895287

52905288
public var unknownFields = SwiftProtobuf.UnknownStorage()
52915289

@@ -13985,64 +13983,59 @@ extension Fuzzilli_Protobuf_WasmEndTry: SwiftProtobuf.Message, SwiftProtobuf._Me
1398513983

1398613984
extension Fuzzilli_Protobuf_WasmBeginTryDelegate: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
1398713985
public static let protoMessageName: String = _protobuf_package + ".WasmBeginTryDelegate"
13988-
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}parameterTypes\0\u{1}outputTypes\0")
13986+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}parameterCount\0")
1398913987

1399013988
public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
1399113989
while let fieldNumber = try decoder.nextFieldNumber() {
1399213990
// The use of inline closures is to circumvent an issue where the compiler
1399313991
// allocates stack space for every case branch when no optimizations are
1399413992
// enabled. https://github.com/apple/swift-protobuf/issues/1034
1399513993
switch fieldNumber {
13996-
case 1: try { try decoder.decodeRepeatedMessageField(value: &self.parameterTypes) }()
13997-
case 2: try { try decoder.decodeRepeatedMessageField(value: &self.outputTypes) }()
13994+
case 1: try { try decoder.decodeSingularInt32Field(value: &self.parameterCount) }()
1399813995
default: break
1399913996
}
1400013997
}
1400113998
}
1400213999

1400314000
public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
14004-
if !self.parameterTypes.isEmpty {
14005-
try visitor.visitRepeatedMessageField(value: self.parameterTypes, fieldNumber: 1)
14006-
}
14007-
if !self.outputTypes.isEmpty {
14008-
try visitor.visitRepeatedMessageField(value: self.outputTypes, fieldNumber: 2)
14001+
if self.parameterCount != 0 {
14002+
try visitor.visitSingularInt32Field(value: self.parameterCount, fieldNumber: 1)
1400914003
}
1401014004
try unknownFields.traverse(visitor: &visitor)
1401114005
}
1401214006

1401314007
public static func ==(lhs: Fuzzilli_Protobuf_WasmBeginTryDelegate, rhs: Fuzzilli_Protobuf_WasmBeginTryDelegate) -> Bool {
14014-
if lhs.parameterTypes != rhs.parameterTypes {return false}
14015-
if lhs.outputTypes != rhs.outputTypes {return false}
14008+
if lhs.parameterCount != rhs.parameterCount {return false}
1401614009
if lhs.unknownFields != rhs.unknownFields {return false}
1401714010
return true
1401814011
}
1401914012
}
1402014013

1402114014
extension Fuzzilli_Protobuf_WasmEndTryDelegate: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
1402214015
public static let protoMessageName: String = _protobuf_package + ".WasmEndTryDelegate"
14023-
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}outputTypes\0")
14016+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}outputCount\0")
1402414017

1402514018
public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
1402614019
while let fieldNumber = try decoder.nextFieldNumber() {
1402714020
// The use of inline closures is to circumvent an issue where the compiler
1402814021
// allocates stack space for every case branch when no optimizations are
1402914022
// enabled. https://github.com/apple/swift-protobuf/issues/1034
1403014023
switch fieldNumber {
14031-
case 1: try { try decoder.decodeRepeatedMessageField(value: &self.outputTypes) }()
14024+
case 1: try { try decoder.decodeSingularInt32Field(value: &self.outputCount) }()
1403214025
default: break
1403314026
}
1403414027
}
1403514028
}
1403614029

1403714030
public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
14038-
if !self.outputTypes.isEmpty {
14039-
try visitor.visitRepeatedMessageField(value: self.outputTypes, fieldNumber: 1)
14031+
if self.outputCount != 0 {
14032+
try visitor.visitSingularInt32Field(value: self.outputCount, fieldNumber: 1)
1404014033
}
1404114034
try unknownFields.traverse(visitor: &visitor)
1404214035
}
1404314036

1404414037
public static func ==(lhs: Fuzzilli_Protobuf_WasmEndTryDelegate, rhs: Fuzzilli_Protobuf_WasmEndTryDelegate) -> Bool {
14045-
if lhs.outputTypes != rhs.outputTypes {return false}
14038+
if lhs.outputCount != rhs.outputCount {return false}
1404614039
if lhs.unknownFields != rhs.unknownFields {return false}
1404714040
return true
1404814041
}

Sources/Fuzzilli/Protobuf/operations.proto

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,12 +1287,11 @@ message WasmEndTry {
12871287
}
12881288

12891289
message WasmBeginTryDelegate {
1290-
repeated WasmILType parameterTypes = 1;
1291-
repeated WasmILType outputTypes = 2;
1290+
int32 parameterCount = 1;
12921291
}
12931292

12941293
message WasmEndTryDelegate {
1295-
repeated WasmILType outputTypes = 1;
1294+
int32 outputCount = 1;
12961295
}
12971296

12981297
message WasmThrow {

0 commit comments

Comments
 (0)