Skip to content

Commit 6e80d80

Browse files
LiedtkeV8-internal LUCI CQ
authored andcommitted
[wasm] Introduce WasmDefineAdHocModuleSignatureType
Similar to commit 72dd5d7 but on a wasm module level instead of inside a wasm function. This is a conservative workaround to ensure that we don't lose any chances of emitting operations that previously used static ILTypes but will depend on a signature input for the migration to wasm-gc. Bug: 448860865 Change-Id: Ife60126cabb8c49a0493736603611b9b2dd3e67b Reviewed-on: https://chrome-internal-review.googlesource.com/c/v8/fuzzilli/+/8938986 Reviewed-by: Doga Yüksel <dyuksel@google.com> Commit-Queue: Matthias Liedtke <mliedtke@google.com>
1 parent e9a114a commit 6e80d80

15 files changed

Lines changed: 170 additions & 6 deletions

Sources/Fuzzilli/Base/ProgramBuilder.swift

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4615,6 +4615,29 @@ public class ProgramBuilder {
46154615
return params => returnTypes
46164616
}
46174617

4618+
public func randomWasmGcSignature() -> (signature: WasmSignature, indexTypes: [Variable]) {
4619+
let typeCount = Int.random(in: 0...10)
4620+
let returnCount = Int.random(in: 0...typeCount)
4621+
let parameterCount = typeCount - returnCount
4622+
4623+
var indexTypes: [Variable] = []
4624+
let chooseType = {
4625+
if let elementType = self.randomVariable(ofType: .wasmTypeDef()), probability(0.25) {
4626+
let nullability =
4627+
self.type(of: elementType).wasmTypeDefinition!.description == .selfReference
4628+
|| probability(0.5)
4629+
indexTypes.append(elementType)
4630+
return ILType.wasmRef(.Index(), nullability: nullability)
4631+
} else {
4632+
// TODO(mliedtke): Extend list with abstract heap types.
4633+
return chooseUniform(from: [.wasmi32, .wasmi64, .wasmf32, .wasmf64, .wasmSimd128])
4634+
}
4635+
}
4636+
let signature = (0..<parameterCount).map {_ in chooseType()}
4637+
=> (0..<returnCount).map {_ in chooseType()}
4638+
return (signature, indexTypes)
4639+
}
4640+
46184641
public func randomWasmBlockOutputTypes(upTo n: Int) -> [ILType] {
46194642
// TODO(mliedtke): This should allow more types as well as non-nullable references for all
46204643
// abstract heap types. To be able to emit them, generateRandomWasmVar() needs to be able
@@ -4694,12 +4717,12 @@ public class ProgramBuilder {
46944717
}
46954718

46964719
/// Like wasmDefineSignatureType but instead of within a type group this defines a signature
4697-
/// type directly inside a wasm function.
4720+
/// type directly inside a wasm function or wasm module.
46984721
/// This takes a signature with resolved index types for ease-of-use (meaning it accepts full
46994722
/// index reference types directly inside the signature).
47004723
@discardableResult
4701-
func wasmDefineAdHocSignatureType(signature: WasmSignature) -> Variable {
4702-
let indexTypes = (signature.parameterTypes + signature.outputTypes)
4724+
func wasmDefineAdHocSignatureType(signature: WasmSignature, indexTypes: [Variable]? = nil) -> Variable {
4725+
let indexTypes = indexTypes ?? (signature.parameterTypes + signature.outputTypes)
47034726
.filter {$0.Is(.anyIndexRef)}
47044727
.map(getWasmTypeDef)
47054728
let cleanIndexTypes = {(type: ILType) -> ILType in
@@ -4709,7 +4732,12 @@ public class ProgramBuilder {
47094732
}
47104733
let signature = signature.parameterTypes.map(cleanIndexTypes)
47114734
=> signature.outputTypes.map(cleanIndexTypes)
4712-
return emit(WasmDefineAdHocSignatureType(signature: signature), withInputs: indexTypes).output
4735+
if context.contains(.wasmFunction) {
4736+
return emit(WasmDefineAdHocSignatureType(signature: signature), withInputs: indexTypes).output
4737+
} else {
4738+
assert(context.contains(.wasm))
4739+
return emit(WasmDefineAdHocModuleSignatureType(signature: signature), withInputs: indexTypes).output
4740+
}
47134741
}
47144742

47154743
@discardableResult
@@ -4930,6 +4958,8 @@ public class ProgramBuilder {
49304958
.wasmEndTryDelegate(_),
49314959
.wasmEndTryTable(_):
49324960
activeWasmModule!.blockSignatures.pop()
4961+
case .wasmDefineAdHocModuleSignatureType(_):
4962+
break
49334963

49344964
default:
49354965
assert(!instr.op.requiredContext.contains(.objectLiteral))

Sources/Fuzzilli/CodeGen/CodeGeneratorWeights.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ public let codeGeneratorWeights = [
263263
"WasmGlobalLoadGenerator": 2,
264264
"WasmReassignmentGenerator": 2,
265265
"WasmDefineTagGenerator": 4,
266+
"WasmAdHocModuleSignature": 2,
266267

267268
// Primitive Value Generators
268269
"WasmLoadi32Generator": 4,

Sources/Fuzzilli/CodeGen/WasmCodeGenerators.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,15 @@ public let WasmCodeGenerators: [CodeGenerator] = [
16011601
function.wasmBuildThrowRef(exception: exception)
16021602
},
16031603

1604+
CodeGenerator(
1605+
"WasmAdHocModuleSignature",
1606+
inContext: .single(.wasm),
1607+
producesComplex: [.init(.wasmTypeDef(), .IsWasmFunction)]
1608+
) { b in
1609+
let (signature, indexTypes) = b.randomWasmGcSignature()
1610+
b.wasmDefineAdHocSignatureType(signature: signature, indexTypes: indexTypes)
1611+
},
1612+
16041613
CodeGenerator(
16051614
"WasmDefineTagGenerator", inContext: .single(.wasm),
16061615
produces: [.object(ofGroup: "WasmTag")]

Sources/Fuzzilli/FuzzIL/Instruction.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,11 @@ extension Instruction: ProtobufConvertible {
15531553
$0.parameterTypes = op.signature.parameterTypes.map(ILTypeToWasmTypeEnum)
15541554
$0.outputTypes = op.signature.outputTypes.map(ILTypeToWasmTypeEnum)
15551555
}
1556+
case .wasmDefineAdHocModuleSignatureType(let op):
1557+
$0.wasmDefineAdHocModuleSignatureType = Fuzzilli_Protobuf_WasmDefineAdHocModuleSignatureType.with {
1558+
$0.parameterTypes = op.signature.parameterTypes.map(ILTypeToWasmTypeEnum)
1559+
$0.outputTypes = op.signature.outputTypes.map(ILTypeToWasmTypeEnum)
1560+
}
15561561
case .wasmDefineSignatureType(let op):
15571562
$0.wasmDefineSignatureType = Fuzzilli_Protobuf_WasmDefineSignatureType.with {
15581563
$0.parameterTypes = op.signature.parameterTypes.map(ILTypeToWasmTypeEnum)
@@ -2565,6 +2570,8 @@ extension Instruction: ProtobufConvertible {
25652570
op = WasmDefineSignatureType(signature: p.parameterTypes.map(WasmTypeEnumToILType) => p.outputTypes.map(WasmTypeEnumToILType))
25662571
case .wasmDefineAdHocSignatureType(let p):
25672572
op = WasmDefineAdHocSignatureType(signature: p.parameterTypes.map(WasmTypeEnumToILType) => p.outputTypes.map(WasmTypeEnumToILType))
2573+
case .wasmDefineAdHocModuleSignatureType(let p):
2574+
op = WasmDefineAdHocModuleSignatureType(signature: p.parameterTypes.map(WasmTypeEnumToILType) => p.outputTypes.map(WasmTypeEnumToILType))
25682575
case .wasmDefineStructType(let p):
25692576
op = WasmDefineStructType(fields: p.fields.map { field in
25702577
return WasmDefineStructType.Field(type: WasmTypeEnumToILType(field.type), mutability: field.mutability)

Sources/Fuzzilli/FuzzIL/JSTyper.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,11 @@ public struct JSTyper: Analyzer {
939939
addSignatureType(def: instr.output, signature: op.signature, inputs: instr.inputs)
940940
finishTypeGroup()
941941
registerWasmTypeDef(instr.output)
942+
case .wasmDefineAdHocModuleSignatureType(let op):
943+
startTypeGroup()
944+
addSignatureType(def: instr.output, signature: op.signature, inputs: instr.inputs)
945+
finishTypeGroup()
946+
registerWasmTypeDef(instr.output)
942947
default:
943948
if instr.numInnerOutputs + instr.numOutputs != 0 {
944949
fatalError("Missing typing of outputs for \(instr.op.opcode)")

Sources/Fuzzilli/FuzzIL/Opcodes.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,5 @@ enum Opcode {
367367
case wasmStructNew(WasmStructNew)
368368
case wasmRefEq(WasmRefEq)
369369
case wasmRefTest(WasmRefTest)
370+
case wasmDefineAdHocModuleSignatureType(WasmDefineAdHocModuleSignatureType)
370371
}

Sources/Fuzzilli/FuzzIL/WasmOperations.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,3 +2433,20 @@ final class WasmDefineAdHocSignatureType: WasmOperation {
24332433
super.init(numInputs: numInputs, numOutputs: 1, requiredContext: [.wasmFunction])
24342434
}
24352435
}
2436+
2437+
// Same as WasmDefineAdHocSignatureType but not on the .wasmFunction context but .wasm (module).
2438+
// This is needed in cases where we aren't in the JS context any more but need a Wasm signature.
2439+
// TODO(mliedtke): We should explore alternative options. Right now this is a much better compromise
2440+
// than failing often to generate operations that require a signature, e.g. something simple like a
2441+
// Wasm function.
2442+
final class WasmDefineAdHocModuleSignatureType: WasmOperation {
2443+
override var opcode: Opcode { .wasmDefineAdHocModuleSignatureType(self) }
2444+
let signature: WasmSignature
2445+
2446+
init(signature: WasmSignature) {
2447+
self.signature = signature
2448+
let numInputs = (signature.outputTypes + signature.parameterTypes).map {
2449+
$0.requiredInputCount() }.reduce(0) { $0 + $1 }
2450+
super.init(numInputs: numInputs, numOutputs: 1, requiredContext: [.wasm])
2451+
}
2452+
}

Sources/Fuzzilli/Lifting/FuzzILLifter.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,10 @@ public class FuzzILLifter: Lifter {
13671367
let inputs = instr.inputs.map(lift).joined(separator: ", ")
13681368
w.emit("\(output()) <- WasmDefineAdHocSignatureType(\(op.signature)) [\(inputs)]")
13691369

1370+
case .wasmDefineAdHocModuleSignatureType(let op):
1371+
let inputs = instr.inputs.map(lift).joined(separator: ", ")
1372+
w.emit("\(output()) <- WasmDefineAdHocModuleSignatureType(\(op.signature)) [\(inputs)]")
1373+
13701374
case .wasmDefineArrayType(let op):
13711375
let typeInput = op.elementType.requiredInputCount() == 1 ? " \(input(0))" : ""
13721376
w.emit("\(output()) <- WasmDefineArrayType \(op.elementType) mutability=\(op.mutability)\(typeInput)")

Sources/Fuzzilli/Lifting/JavaScriptLifter.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,7 @@ public class JavaScriptLifter: Lifter {
17741774
.wasmEndTypeGroup(_),
17751775
.wasmDefineSignatureType(_),
17761776
.wasmDefineAdHocSignatureType(_),
1777+
.wasmDefineAdHocModuleSignatureType(_),
17771778
.wasmDefineArrayType(_),
17781779
.wasmDefineStructType(_),
17791780
.wasmDefineForwardOrSelfReference(_),

Sources/Fuzzilli/Lifting/WasmLifter.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,12 @@ public class WasmLifter {
12241224
}
12251225
}
12261226

1227-
/// This function updates the internal state of the lifter before actually emitting code to the wasm module. This should be invoked before we try to get the corresponding bytes for the Instruction
1227+
/// This function updates the internal state of the lifter before actually emitting code to the
1228+
/// wasm module. This should be invoked before we try to get the corresponding bytes for the
1229+
/// Instruction.
1230+
/// Returns true if the instruction requires emitting code into the module's code section (which
1231+
/// means it is true for almost all instructions appearing inside a wasm function and false for
1232+
/// all instructions outside of a wasm function like a Wasm memory definition).
12281233
private func updateLifterState(wasmInstruction instr: Instruction) -> Bool {
12291234
// Make sure that we actually have a Wasm operation here.
12301235
assert(instr.op is WasmOperation)
@@ -1321,6 +1326,8 @@ public class WasmLifter {
13211326
assert(self.exports.contains(where: {
13221327
$0.isTag && $0.getDefInstr()!.output == instr.output
13231328
}))
1329+
case .wasmDefineAdHocModuleSignatureType(_):
1330+
break
13241331
default:
13251332
return true
13261333
}

0 commit comments

Comments
 (0)