Skip to content

Commit 59e7a3d

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

9 files changed

Lines changed: 55 additions & 61 deletions

File tree

Sources/Fuzzilli/Base/ProgramBuilder.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4067,9 +4067,13 @@ public class ProgramBuilder {
40674067
}
40684068
}
40694069
#endif
4070-
let instr = b.emit(WasmBeginTryTable(with: signature, catches: catches), withInputs: args)
4070+
let signatureDef = b.wasmDefineAdHocSignatureType(signature: signature)
4071+
let instr = b.emit(
4072+
WasmBeginTryTable(parameterCount: signature.parameterTypes.count, catches: catches),
4073+
withInputs: [signatureDef] + args)
40714074
let results = body(instr.innerOutput(0), Array(instr.innerOutputs(1...)))
4072-
return Array(b.emit(WasmEndTryTable(outputTypes: signature.outputTypes), withInputs: results).outputs)
4075+
return Array(b.emit(WasmEndTryTable(outputCount: signature.outputTypes.count),
4076+
withInputs: [signatureDef] + results).outputs)
40734077
}
40744078

40754079
// Create a legacy try-catch with a void block signature. Mostly a convenience helper for
@@ -4977,13 +4981,10 @@ public class ProgramBuilder {
49774981
activeWasmModule!.functions.append(WasmFunction(forBuilder: self, withSignature: op.signature))
49784982
case .wasmBeginTry(_),
49794983
.wasmEndTryDelegate(_),
4980-
.wasmBeginTryDelegate(_):
4981-
break
4982-
case .wasmBeginTryTable(let op):
4983-
activeWasmModule!.blockSignatures.push(op.signature)
4984-
case .wasmEndTryTable(_):
4985-
activeWasmModule!.blockSignatures.pop()
4986-
case .wasmDefineAdHocModuleSignatureType(_):
4984+
.wasmBeginTryDelegate(_),
4985+
.wasmBeginTryTable(_),
4986+
.wasmEndTryTable(_),
4987+
.wasmDefineAdHocModuleSignatureType(_):
49874988
break
49884989

49894990
default:

Sources/Fuzzilli/CodeGen/WasmCodeGenerators.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1695,7 +1695,7 @@ public let WasmCodeGenerators: [CodeGenerator] = [
16951695
: (withExnRef ? .Ref : .NoRef)
16961696
}
16971697

1698-
var tryArgs = b.randomWasmBlockArguments(upTo: 5)
1698+
var tryArgs = b.randomWasmBlockArguments(upTo: 5, allowingGcTypes: true)
16991699
let tryParameters = tryArgs.map { b.type(of: $0) }
17001700
let tryOutputTypes = b.randomWasmBlockOutputTypes(upTo: 5)
17011701
tryArgs += zip(tags, labels).map { tag, label in

Sources/Fuzzilli/FuzzIL/Instruction.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,13 +1400,12 @@ extension Instruction: ProtobufConvertible {
14001400
}
14011401
case .wasmBeginTryTable(let op):
14021402
$0.wasmBeginTryTable = Fuzzilli_Protobuf_WasmBeginTryTable.with {
1403-
$0.parameterTypes = op.signature.parameterTypes.map(ILTypeToWasmTypeEnum)
1404-
$0.outputTypes = op.signature.outputTypes.map(ILTypeToWasmTypeEnum)
1403+
$0.parameterCount = Int32(op.parameterCount)
14051404
$0.catches = op.catches.map(convertWasmCatch)
14061405
}
14071406
case .wasmEndTryTable(let op):
14081407
$0.wasmEndTryTable = Fuzzilli_Protobuf_WasmEndTryTable.with {
1409-
$0.outputTypes = op.outputTypes.map(ILTypeToWasmTypeEnum)
1408+
$0.outputCount = Int32(op.numOutputs)
14101409
}
14111410
case .wasmBeginTry(let op):
14121411
$0.wasmBeginTry = Fuzzilli_Protobuf_WasmBeginTry.with {
@@ -2461,12 +2460,10 @@ extension Instruction: ProtobufConvertible {
24612460
case .wasmEndLoop(let p):
24622461
op = WasmEndLoop(outputCount: Int(p.outputCount))
24632462
case .wasmBeginTryTable(let p):
2464-
let parameters = p.parameterTypes.map(WasmTypeEnumToILType)
2465-
let outputs = p.outputTypes.map(WasmTypeEnumToILType)
24662463
let catches = p.catches.map(convertProtoWasmCatchKind)
2467-
op = WasmBeginTryTable(with: parameters => outputs, catches: catches)
2464+
op = WasmBeginTryTable(parameterCount: Int(p.parameterCount), catches: catches)
24682465
case .wasmEndTryTable(let p):
2469-
op = WasmEndTryTable(outputTypes: p.outputTypes.map(WasmTypeEnumToILType))
2466+
op = WasmEndTryTable(outputCount: Int(p.outputCount))
24702467
case .wasmBeginTry(let p):
24712468
op = WasmBeginTry(parameterCount: Int(p.parameterCount))
24722469
case .wasmBeginCatchAll(let p):

Sources/Fuzzilli/FuzzIL/JSTyper.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -846,16 +846,18 @@ public struct JSTyper: Analyzer {
846846
case .wasmEndLoop(_):
847847
let signature = type(of: instr.input(0)).wasmFunctionSignatureDefSignature
848848
wasmTypeEndBlock(instr, signature.outputTypes)
849-
case .wasmBeginTryTable(let op):
850-
wasmTypeBeginBlock(instr, op.signature)
849+
case .wasmBeginTryTable(_):
850+
let signature = type(of: instr.input(0)).wasmFunctionSignatureDefSignature
851+
wasmTypeBeginBlock(instr, signature)
851852
instr.inputs.forEach { input in
852853
if type(of: input).isWasmTagType {
853854
let definingInstruction = defUseAnalyzer.definition(of: input)
854855
dynamicObjectGroupManager.addWasmTag(withType: type(of: input), forDefinition: definingInstruction, forVariable: input)
855856
}
856857
}
857-
case .wasmEndTryTable(let op):
858-
wasmTypeEndBlock(instr, op.outputTypes)
858+
case .wasmEndTryTable(_):
859+
let signature = type(of: instr.input(0)).wasmFunctionSignatureDefSignature
860+
wasmTypeEndBlock(instr, signature.outputTypes)
859861
case .wasmBeginTry(_):
860862
let signature = type(of: instr.input(0)).wasmFunctionSignatureDefSignature
861863
wasmTypeBeginBlock(instr, signature)

Sources/Fuzzilli/FuzzIL/WasmOperations.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,25 +1351,27 @@ final class WasmBeginTryTable: WasmOperation {
13511351
}
13521352

13531353
override var opcode: Opcode { .wasmBeginTryTable(self) }
1354-
let signature: WasmSignature
13551354
let catches: [CatchKind]
13561355

1357-
init(with signature: WasmSignature, catches: [CatchKind]) {
1358-
self.signature = signature
1356+
init(parameterCount: Int, catches: [CatchKind]) {
13591357
self.catches = catches
13601358
let inputTagCount = catches.count {$0 == .Ref || $0 == .NoRef}
13611359
let inputLabelCount = catches.count
1362-
super.init(numInputs: signature.parameterTypes.count + inputLabelCount + inputTagCount , numInnerOutputs: signature.parameterTypes.count + 1, attributes: [.isBlockStart, .propagatesSurroundingContext], requiredContext: [.wasmFunction])
1360+
super.init(numInputs: 1 + parameterCount + inputLabelCount + inputTagCount,
1361+
numInnerOutputs: parameterCount + 1,
1362+
attributes: [.isBlockStart, .propagatesSurroundingContext],
1363+
requiredContext: [.wasmFunction])
13631364
}
1365+
1366+
var parameterCount: Int {numInnerOutputs - 1}
13641367
}
13651368

13661369
final class WasmEndTryTable: WasmOperation {
13671370
override var opcode: Opcode { .wasmEndTryTable(self) }
1368-
let outputTypes: [ILType]
13691371

1370-
init(outputTypes: [ILType]) {
1371-
self.outputTypes = outputTypes
1372-
super.init(numInputs: outputTypes.count, numOutputs: outputTypes.count, attributes: [.isBlockEnd, .resumesSurroundingContext], requiredContext: [.wasmFunction])
1372+
init(outputCount: Int) {
1373+
super.init(numInputs: 1 + outputCount, numOutputs: outputCount,
1374+
attributes: [.isBlockEnd, .resumesSurroundingContext], requiredContext: [.wasmFunction])
13731375
}
13741376
}
13751377

Sources/Fuzzilli/Lifting/FuzzILLifter.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,10 +1114,10 @@ public class FuzzILLifter: Lifter {
11141114

11151115
case .wasmBeginTryTable(let op):
11161116
let args = instr.inputs.map(lift)
1117-
let blockArgs = args.prefix(op.signature.parameterTypes.count).joined(separator: ", ")
1118-
w.emit("WasmBeginTryTable (\(op.signature)) [\(blockArgs)] -> L:\(instr.innerOutput(0)) [\(liftCallArguments(instr.innerOutputs(1...)))]")
1117+
let blockArgs = args.prefix(1 + op.parameterCount).joined(separator: ", ")
1118+
w.emit("WasmBeginTryTable [\(blockArgs)] -> L:\(instr.innerOutput(0)) [\(liftCallArguments(instr.innerOutputs(1...)))]")
11191119
w.increaseIndentionLevel(by: 2)
1120-
var inputIndex = op.signature.parameterTypes.count
1120+
var inputIndex = 1 + op.parameterCount
11211121
op.catches.forEach { kind in
11221122
if kind == .Ref || kind == .NoRef {
11231123
w.emit("catching \(kind) \(args[inputIndex]) to \(args[inputIndex + 1])")

Sources/Fuzzilli/Lifting/WasmLifter.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,8 +1248,7 @@ public class WasmLifter {
12481248
self.currentFunction!.labelBranchDepthMapping[instr.innerOutput(0)] = self.currentFunction!.variableAnalyzer.wasmBranchDepth - 1
12491249
// Needs typer analysis
12501250
return true
1251-
case .wasmBeginTryTable(let op):
1252-
registerSignature(op.signature)
1251+
case .wasmBeginTryTable(_):
12531252
self.currentFunction!.labelBranchDepthMapping[instr.innerOutput(0)] = self.currentFunction!.variableAnalyzer.wasmBranchDepth
12541253
// Needs typer analysis
12551254
return true
@@ -1912,7 +1911,8 @@ public class WasmLifter {
19121911
let signatureDesc = typer.getTypeDescription(of: wasmInstruction.input(0))
19131912
return Data([0x03] + Leb128.unsignedEncode(typeDescToIndex[signatureDesc]!))
19141913
case .wasmBeginTryTable(let op):
1915-
var inputIndex = op.signature.parameterTypes.count
1914+
let signatureDesc = typer.getTypeDescription(of: wasmInstruction.input(0))
1915+
var inputIndex = 1 + op.parameterCount
19161916
let catchTable: Data = try op.catches.map {
19171917
switch $0 {
19181918
case .Ref, .NoRef:
@@ -1928,7 +1928,7 @@ public class WasmLifter {
19281928
}
19291929
}.reduce(Data(), +)
19301930
return [0x1F]
1931-
+ Leb128.unsignedEncode(signatureIndexMap[op.signature]!)
1931+
+ Leb128.unsignedEncode(typeDescToIndex[signatureDesc]!)
19321932
+ Leb128.unsignedEncode(op.catches.count)
19331933
+ catchTable
19341934
case .wasmBeginTry(_):

Sources/Fuzzilli/Protobuf/operations.pb.swift

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5193,9 +5193,7 @@ public struct Fuzzilli_Protobuf_WasmBeginTryTable: Sendable {
51935193
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
51945194
// methods supported on all messages.
51955195

5196-
public var parameterTypes: [Fuzzilli_Protobuf_WasmILType] = []
5197-
5198-
public var outputTypes: [Fuzzilli_Protobuf_WasmILType] = []
5196+
public var parameterCount: Int32 = 0
51995197

52005198
public var catches: [Fuzzilli_Protobuf_WasmCatchKind] = []
52015199

@@ -5209,7 +5207,7 @@ public struct Fuzzilli_Protobuf_WasmEndTryTable: Sendable {
52095207
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
52105208
// methods supported on all messages.
52115209

5212-
public var outputTypes: [Fuzzilli_Protobuf_WasmILType] = []
5210+
public var outputCount: Int32 = 0
52135211

52145212
public var unknownFields = SwiftProtobuf.UnknownStorage()
52155213

@@ -13788,38 +13786,33 @@ extension Fuzzilli_Protobuf_WasmEndLoop: SwiftProtobuf.Message, SwiftProtobuf._M
1378813786

1378913787
extension Fuzzilli_Protobuf_WasmBeginTryTable: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
1379013788
public static let protoMessageName: String = _protobuf_package + ".WasmBeginTryTable"
13791-
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}parameterTypes\0\u{1}outputTypes\0\u{1}catches\0")
13789+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}parameterCount\0\u{1}catches\0")
1379213790

1379313791
public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
1379413792
while let fieldNumber = try decoder.nextFieldNumber() {
1379513793
// The use of inline closures is to circumvent an issue where the compiler
1379613794
// allocates stack space for every case branch when no optimizations are
1379713795
// enabled. https://github.com/apple/swift-protobuf/issues/1034
1379813796
switch fieldNumber {
13799-
case 1: try { try decoder.decodeRepeatedMessageField(value: &self.parameterTypes) }()
13800-
case 2: try { try decoder.decodeRepeatedMessageField(value: &self.outputTypes) }()
13801-
case 3: try { try decoder.decodeRepeatedEnumField(value: &self.catches) }()
13797+
case 1: try { try decoder.decodeSingularInt32Field(value: &self.parameterCount) }()
13798+
case 2: try { try decoder.decodeRepeatedEnumField(value: &self.catches) }()
1380213799
default: break
1380313800
}
1380413801
}
1380513802
}
1380613803

1380713804
public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
13808-
if !self.parameterTypes.isEmpty {
13809-
try visitor.visitRepeatedMessageField(value: self.parameterTypes, fieldNumber: 1)
13810-
}
13811-
if !self.outputTypes.isEmpty {
13812-
try visitor.visitRepeatedMessageField(value: self.outputTypes, fieldNumber: 2)
13805+
if self.parameterCount != 0 {
13806+
try visitor.visitSingularInt32Field(value: self.parameterCount, fieldNumber: 1)
1381313807
}
1381413808
if !self.catches.isEmpty {
13815-
try visitor.visitPackedEnumField(value: self.catches, fieldNumber: 3)
13809+
try visitor.visitPackedEnumField(value: self.catches, fieldNumber: 2)
1381613810
}
1381713811
try unknownFields.traverse(visitor: &visitor)
1381813812
}
1381913813

1382013814
public static func ==(lhs: Fuzzilli_Protobuf_WasmBeginTryTable, rhs: Fuzzilli_Protobuf_WasmBeginTryTable) -> Bool {
13821-
if lhs.parameterTypes != rhs.parameterTypes {return false}
13822-
if lhs.outputTypes != rhs.outputTypes {return false}
13815+
if lhs.parameterCount != rhs.parameterCount {return false}
1382313816
if lhs.catches != rhs.catches {return false}
1382413817
if lhs.unknownFields != rhs.unknownFields {return false}
1382513818
return true
@@ -13828,29 +13821,29 @@ extension Fuzzilli_Protobuf_WasmBeginTryTable: SwiftProtobuf.Message, SwiftProto
1382813821

1382913822
extension Fuzzilli_Protobuf_WasmEndTryTable: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
1383013823
public static let protoMessageName: String = _protobuf_package + ".WasmEndTryTable"
13831-
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}outputTypes\0")
13824+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}outputCount\0")
1383213825

1383313826
public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
1383413827
while let fieldNumber = try decoder.nextFieldNumber() {
1383513828
// The use of inline closures is to circumvent an issue where the compiler
1383613829
// allocates stack space for every case branch when no optimizations are
1383713830
// enabled. https://github.com/apple/swift-protobuf/issues/1034
1383813831
switch fieldNumber {
13839-
case 1: try { try decoder.decodeRepeatedMessageField(value: &self.outputTypes) }()
13832+
case 1: try { try decoder.decodeSingularInt32Field(value: &self.outputCount) }()
1384013833
default: break
1384113834
}
1384213835
}
1384313836
}
1384413837

1384513838
public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
13846-
if !self.outputTypes.isEmpty {
13847-
try visitor.visitRepeatedMessageField(value: self.outputTypes, fieldNumber: 1)
13839+
if self.outputCount != 0 {
13840+
try visitor.visitSingularInt32Field(value: self.outputCount, fieldNumber: 1)
1384813841
}
1384913842
try unknownFields.traverse(visitor: &visitor)
1385013843
}
1385113844

1385213845
public static func ==(lhs: Fuzzilli_Protobuf_WasmEndTryTable, rhs: Fuzzilli_Protobuf_WasmEndTryTable) -> Bool {
13853-
if lhs.outputTypes != rhs.outputTypes {return false}
13846+
if lhs.outputCount != rhs.outputCount {return false}
1385413847
if lhs.unknownFields != rhs.unknownFields {return false}
1385513848
return true
1385613849
}

Sources/Fuzzilli/Protobuf/operations.proto

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,13 +1260,12 @@ enum WasmCatchKind {
12601260
}
12611261

12621262
message WasmBeginTryTable {
1263-
repeated WasmILType parameterTypes = 1;
1264-
repeated WasmILType outputTypes = 2;
1265-
repeated WasmCatchKind catches = 3;
1263+
int32 parameterCount = 1;
1264+
repeated WasmCatchKind catches = 2;
12661265
}
12671266

12681267
message WasmEndTryTable {
1269-
repeated WasmILType outputTypes = 1;
1268+
int32 outputCount = 1;
12701269
}
12711270

12721271
message WasmBeginTry {

0 commit comments

Comments
 (0)