Skip to content

Commit 706fda6

Browse files
LiedtkeV8-internal LUCI CQ
authored andcommitted
Add wasm select instruction
Change-Id: I69614762932b78d3d05d87528439035872d3bb90 Reviewed-on: https://chrome-internal-review.googlesource.com/c/v8/fuzzilli/+/7963008 Auto-Submit: Matthias Liedtke <[email protected]> Reviewed-by: Carl Smith <[email protected]> Commit-Queue: Matthias Liedtke <[email protected]>
1 parent bbc1879 commit 706fda6

16 files changed

+160
-1
lines changed

Sources/Fuzzilli/Base/ProgramBuilder.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3328,6 +3328,11 @@ public class ProgramBuilder {
33283328
b.emit(WasmUnreachable())
33293329
}
33303330

3331+
@discardableResult
3332+
public func wasmSelect(type: ILType, on condition: Variable, trueValue: Variable, falseValue: Variable) -> Variable {
3333+
return b.emit(WasmSelect(type: type), withInputs: [trueValue, falseValue, condition]).output
3334+
}
3335+
33313336
public func wasmReturn(_ returnVariable: Variable) {
33323337
let returnType = b.type(of: returnVariable)
33333338
b.emit(WasmReturn(returnType: returnType), withInputs: [returnVariable])

Sources/Fuzzilli/CodeGen/CodeGeneratorWeights.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,6 @@ public let codeGeneratorWeights = [
291291
"WasmI64x2SplatGenerator": 10,
292292
"WasmI64x2ExtractLaneGenerator": 10,
293293
"WasmI64x2LoadSplatGenerator": 10,
294+
295+
"WasmSelectGenerator": 10,
294296
]

Sources/Fuzzilli/CodeGen/WasmCodeGenerators.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,16 @@ public let WasmCodeGenerators: [CodeGenerator] = [
633633
}
634634
},
635635

636+
CodeGenerator("WasmSelectGenerator", inContext: .wasmFunction, inputs: .required(.wasmi32)) { b, condition in
637+
let function = b.currentWasmModule.currentWasmFunction
638+
let supportedTypes : ILType = .wasmi32 | .wasmi64 | .wasmf32 | .wasmf64 | .wasmExternRef
639+
// The condition is an i32, so we should always find at least that one as a possible input.
640+
let trueValue = b.randomVariable(ofType: supportedTypes)!
641+
let selectType = b.type(of: trueValue)
642+
let falseValue = b.randomVariable(ofType: selectType)!
643+
function.wasmSelect(type: selectType, on: condition, trueValue: trueValue, falseValue: falseValue)
644+
},
645+
636646
CodeGenerator("WasmThrowGenerator", inContext: .wasmFunction, inputs: .required(.object(ofGroup: "WasmTag"))) { b, tag in
637647
let function = b.currentWasmModule.currentWasmFunction
638648
let wasmTagType = b.type(of: tag).wasmTagType!

Sources/Fuzzilli/FuzzIL/Instruction.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,10 @@ extension Instruction: ProtobufConvertible {
12521252
fatalError("Should never be serialized")
12531253
case .wasmUnreachable(_):
12541254
$0.wasmUnreachable = Fuzzilli_Protobuf_WasmUnreachable()
1255+
case .wasmSelect(let op):
1256+
$0.wasmSelect = Fuzzilli_Protobuf_WasmSelect.with {
1257+
$0.type = ILTypeToWasmTypeEnum(op.type)
1258+
}
12551259
case .constSimd128(let op):
12561260
$0.constSimd128 = Fuzzilli_Protobuf_ConstSimd128.with { $0.value = op.value.map{ UInt32($0) } }
12571261
case .wasmSimd128IntegerUnOp(let op):
@@ -2037,6 +2041,8 @@ extension Instruction: ProtobufConvertible {
20372041
fatalError("Should never be deserialized!")
20382042
case .wasmUnreachable(_):
20392043
op = WasmUnreachable()
2044+
case .wasmSelect(let p):
2045+
op = WasmSelect(type: WasmTypeEnumToILType(p.type))
20402046
case .constSimd128(let p):
20412047
op = ConstSimd128(value: p.value.map{ UInt8($0) })
20422048
case .wasmSimd128IntegerUnOp(let p):

Sources/Fuzzilli/FuzzIL/Opcodes.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,5 @@ enum Opcode {
312312
case wasmI64x2LoadSplat(WasmI64x2LoadSplat)
313313

314314
case wasmUnreachable(WasmUnreachable)
315+
case wasmSelect(WasmSelect)
315316
}

Sources/Fuzzilli/FuzzIL/WasmOperations.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,19 @@ final class WasmJsCall: WasmOperation {
932932
}
933933
}
934934

935+
final class WasmSelect: WasmOperation {
936+
override var opcode: Opcode { .wasmSelect(self) }
937+
let type: ILType
938+
939+
init(type: ILType) {
940+
self.type = type
941+
// Note that the condition is the third input. This is due to the lifting that pushes all
942+
// inputs to the value stack in reverse order (and the select expects the condition as the
943+
// first value on the stack.)
944+
super.init(inputTypes: [type, type, .wasmi32], outputType: type, requiredContext: [.wasmFunction])
945+
}
946+
}
947+
935948
final class WasmBeginBlock: WasmOperation {
936949
override var opcode: Opcode { .wasmBeginBlock(self) }
937950

Sources/Fuzzilli/Lifting/FuzzILLifter.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,9 @@ public class FuzzILLifter: Lifter {
10691069
case .wasmUnreachable:
10701070
w.emit("WasmUnreachable")
10711071

1072+
case .wasmSelect(let op):
1073+
w.emit("\(output()) <- WasmSelect[\(op.type)] \(input(2)) ? \(input(0)) : \(input(1))")
1074+
10721075
case .constSimd128(let op):
10731076
w.emit("\(output()) <- ConstSimd128 \(op.value)")
10741077

Sources/Fuzzilli/Lifting/JavaScriptLifter.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,7 @@ public class JavaScriptLifter: Lifter {
15991599
.wasmEndIf(_),
16001600
.wasmNop(_),
16011601
.wasmUnreachable(_),
1602+
.wasmSelect(_),
16021603
.constSimd128(_),
16031604
.wasmSimd128IntegerUnOp(_),
16041605
.wasmSimd128IntegerBinOp(_),

Sources/Fuzzilli/Lifting/WasmLifter.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,8 @@ public class WasmLifter {
14361436
return Data([0x01])
14371437
case .wasmUnreachable(_):
14381438
return Data([0x00])
1439+
case .wasmSelect(let op):
1440+
return Data([0x1c, 0x01]) + ILTypeMapping[op.type]!
14391441
case .constSimd128(let op):
14401442
return Data([0xFD]) + Leb128.unsignedEncode(12) + Data(op.value)
14411443
case .wasmSimd128IntegerUnOp(let op):

Sources/Fuzzilli/Mutators/OperationMutator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ public class OperationMutator: BaseInstructionMutator {
558558
.wasmEndIf(_),
559559
.wasmNop(_),
560560
.wasmUnreachable(_),
561+
.wasmSelect(_),
561562
.wasmDefineTag(_):
562563
assert(!instr.isOperationMutable)
563564
fatalError("Unexpected Operation")

0 commit comments

Comments
 (0)