Skip to content

Commit 38f4ca1

Browse files
pkk33V8-internal LUCI CQ
authored andcommitted
Add support for memory.copy instruction
Bug: 427115604 Change-Id: I523ff10c7ecdefeb98b523ebfbc3783c3bef4bf7 Reviewed-on: https://chrome-internal-review.googlesource.com/c/v8/fuzzilli/+/8539457 Commit-Queue: Pawel Krawczyk <pawkra@google.com> Reviewed-by: Matthias Liedtke <mliedtke@google.com> Auto-Submit: Pawel Krawczyk <pawkra@google.com>
1 parent efb40b7 commit 38f4ca1

15 files changed

Lines changed: 168 additions & 3 deletions

Sources/Fuzzilli/Base/ProgramBuilder.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3367,6 +3367,16 @@ public class ProgramBuilder {
33673367
types: [.object(ofGroup: "WasmMemory"), addrType]).output
33683368
}
33693369

3370+
public func wasmMemoryCopy(dstMemory: Variable, srcMemory: Variable, dstOffset: Variable, srcOffset: Variable, size: Variable) {
3371+
let dstMemoryType = b.type(of: dstMemory).wasmMemoryType!
3372+
let srcMemoryType = b.type(of: srcMemory).wasmMemoryType!
3373+
assert(dstMemoryType.isMemory64 == srcMemoryType.isMemory64)
3374+
3375+
let addrType = dstMemoryType.addrType
3376+
b.emit(WasmMemoryCopy(), withInputs: [dstMemory, srcMemory, dstOffset, srcOffset, size],
3377+
types: [.object(ofGroup: "WasmMemory"), .object(ofGroup: "WasmMemory"), addrType, addrType, addrType])
3378+
}
3379+
33703380
public func wasmMemoryFill(memory: Variable, offset: Variable, byteToSet: Variable, nrOfBytesToUpdate: Variable) {
33713381
let addrType = b.type(of: memory).wasmMemoryType!.addrType
33723382
b.emit(WasmMemoryFill(), withInputs: [memory, offset, byteToSet, nrOfBytesToUpdate],

Sources/Fuzzilli/CodeGen/CodeGeneratorWeights.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ public let codeGeneratorWeights = [
228228
"WasmAtomicCmpxchgGenerator": 10,
229229
"WasmMemorySizeGenerator": 5,
230230
"WasmMemoryGrowGenerator": 1,
231+
"WasmMemoryCopyGenerator": 5,
231232
"WasmMemoryFillGenerator": 5,
232233
"WasmMemoryInitGenerator": 5,
233234
"WasmDefineGlobalGenerator": 2,

Sources/Fuzzilli/CodeGen/WasmCodeGenerators.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,31 @@ public let WasmCodeGenerators: [CodeGenerator] = [
420420
function.wasmMemoryGrow(memory: memory, growByPages: growBy)
421421
},
422422

423+
CodeGenerator("WasmMemoryCopyGenerator", inContext: .wasmFunction, inputs: .required(.object(ofGroup: "WasmMemory"))) { b, srcMemory in
424+
let function = b.currentWasmModule.currentWasmFunction
425+
let srcMemoryTypeInfo = b.type(of: srcMemory).wasmMemoryType!
426+
let dstMemory = b.findVariable {v in
427+
let type = b.type(of: v)
428+
return type.Is(.object(ofGroup: "WasmMemory"))
429+
&& type.wasmMemoryType!.isMemory64 == srcMemoryTypeInfo.isMemory64
430+
}!
431+
let dstMemoryTypeInfo = b.type(of: srcMemory).wasmMemoryType!
432+
let memArg = {v in function.memoryArgument(v, dstMemoryTypeInfo)}
433+
434+
let srcMemSize = srcMemoryTypeInfo.limits.min * WasmConstants.specWasmMemPageSize
435+
let dstMemSize = dstMemoryTypeInfo.limits.min * WasmConstants.specWasmMemPageSize
436+
let srcOffsetValue = b.randomNonNegativeIndex(upTo: Int64(srcMemSize))
437+
let srcOffset = memArg(srcOffsetValue)
438+
let dstOffsetValue = b.randomNonNegativeIndex(upTo: Int64(dstMemSize))
439+
let dstOffset = memArg(dstOffsetValue)
440+
441+
let maxCopySize = min(Int64(srcMemSize) - srcOffsetValue, Int64(dstMemSize) - dstOffsetValue)
442+
let copySizeValue = b.randomSize(upTo:maxCopySize)
443+
let copySize = memArg(copySizeValue)
444+
445+
function.wasmMemoryCopy(dstMemory: dstMemory, srcMemory: srcMemory, dstOffset: dstOffset, srcOffset: srcOffset, size: copySize)
446+
},
447+
423448
CodeGenerator("WasmMemoryFillGenerator", inContext: .wasmFunction, inputs: .required(.object(ofGroup: "WasmMemory"))) { b, memory in
424449
if (b.hasZeroPages(memory: memory)) { return }
425450

@@ -446,7 +471,7 @@ public let WasmCodeGenerators: [CodeGenerator] = [
446471
let memoryOffset = function.memoryArgument(memoryOffsetValue, memoryTypeInfo)
447472

448473
let dataSegmentTypeInfo = b.type(of: dataSegment).wasmDataSegmentType!
449-
let dataSegmentOffsetValue = b.randomSize(upTo: Int64(dataSegmentTypeInfo.segmentLength))
474+
let dataSegmentOffsetValue = b.randomNonNegativeIndex(upTo: Int64(dataSegmentTypeInfo.segmentLength))
450475
let dataSegmentOffset = function.consti32(Int32(dataSegmentOffsetValue))
451476

452477
let maxNrOfBytesToUpdate = min(memSize - memoryOffsetValue, Int64(dataSegmentTypeInfo.segmentLength) - dataSegmentOffsetValue)

Sources/Fuzzilli/FuzzIL/Instruction.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,8 @@ extension Instruction: ProtobufConvertible {
13311331
$0.wasmTableSize = Fuzzilli_Protobuf_WasmTableSize()
13321332
case .wasmTableGrow(_):
13331333
$0.wasmTableGrow = Fuzzilli_Protobuf_WasmTableGrow()
1334+
case .wasmMemoryCopy(_):
1335+
$0.wasmMemoryCopy = Fuzzilli_Protobuf_WasmMemoryCopy()
13341336
case .wasmMemoryFill(_):
13351337
$0.wasmMemoryFill = Fuzzilli_Protobuf_WasmMemoryFill()
13361338
case .wasmMemoryInit(_):
@@ -2365,6 +2367,8 @@ extension Instruction: ProtobufConvertible {
23652367
op = WasmTableSize()
23662368
case .wasmTableGrow(_):
23672369
op = WasmTableGrow()
2370+
case .wasmMemoryCopy(_):
2371+
op = WasmMemoryCopy()
23682372
case .wasmMemoryFill(_):
23692373
op = WasmMemoryFill()
23702374
case .wasmMemoryInit(_):

Sources/Fuzzilli/FuzzIL/Opcodes.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,5 @@ enum Opcode {
351351
case wasmI31Get(WasmI31Get)
352352
case wasmAnyConvertExtern(WasmAnyConvertExtern)
353353
case wasmExternConvertAny(WasmExternConvertAny)
354+
case wasmMemoryCopy(WasmMemoryCopy)
354355
}

Sources/Fuzzilli/FuzzIL/WasmOperations.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,15 @@ class WasmMemoryFill: WasmOperation {
11591159
}
11601160
}
11611161

1162+
class WasmMemoryCopy: WasmOperation {
1163+
override var opcode: Opcode { .wasmMemoryCopy(self) }
1164+
1165+
init() {
1166+
super.init(
1167+
numInputs: 5, numOutputs: 0, requiredContext: [.wasmFunction])
1168+
}
1169+
}
1170+
11621171
class WasmMemoryInit: WasmOperation {
11631172
override var opcode: Opcode { .wasmMemoryInit(self) }
11641173

Sources/Fuzzilli/Lifting/FuzzILLifter.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,8 +878,11 @@ public class FuzzILLifter: Lifter {
878878
case .wasmMemoryFill(_):
879879
w.emit("WasmMemoryFill \(input(0)), \(input(1)), \(input(2)), \(input(3))")
880880

881+
case .wasmMemoryCopy(_):
882+
w.emit("WasmMemoryCopy \(input(0)), \(input(1)), \(input(2)), \(input(3)), \(input(4))")
883+
881884
case .wasmMemoryInit(_):
882-
w.emit("WasmMemoryInit \(input(0)), \(input(1)), \(input(2)), \(input(3)) \(input(4))")
885+
w.emit("WasmMemoryInit \(input(0)), \(input(1)), \(input(2)), \(input(3)), \(input(4))")
883886

884887
case .wasmDropDataSegment(_):
885888
w.emit("WasmDropDataSegment \(input(0))")

Sources/Fuzzilli/Lifting/JavaScriptLifter.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,7 @@ public class JavaScriptLifter: Lifter {
16441644
.wasmAtomicCmpxchg(_),
16451645
.wasmMemorySize(_),
16461646
.wasmMemoryGrow(_),
1647+
.wasmMemoryCopy(_),
16471648
.wasmMemoryFill(_),
16481649
.wasmMemoryInit(_),
16491650
.wasmDropDataSegment(_),

Sources/Fuzzilli/Lifting/WasmLifter.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,10 @@ public class WasmLifter {
18291829
case .wasmMemoryGrow(_):
18301830
let memoryIdx = try resolveIdx(ofType: .memory, for: wasmInstruction.input(0))
18311831
return Data([0x40]) + Leb128.unsignedEncode(memoryIdx)
1832+
case .wasmMemoryCopy(_):
1833+
let dstMemIdx = try resolveIdx(ofType: .memory, for: wasmInstruction.input(0))
1834+
let srcMemIdx = try resolveIdx(ofType: .memory, for: wasmInstruction.input(1))
1835+
return Data([0xFC, 0x0a]) + Leb128.unsignedEncode(dstMemIdx) + Leb128.unsignedEncode(srcMemIdx)
18321836
case .wasmMemoryFill(_):
18331837
let memoryIdx = try resolveIdx(ofType: .memory, for: wasmInstruction.input(0))
18341838
return Data([0xFC, 0x0b]) + Leb128.unsignedEncode(memoryIdx)

Sources/Fuzzilli/Mutators/OperationMutator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ public class OperationMutator: BaseInstructionMutator {
620620
.wasmSignExtend32Intoi64(_),
621621
.wasmMemorySize(_),
622622
.wasmMemoryGrow(_),
623+
.wasmMemoryCopy(_),
623624
.wasmMemoryFill(_),
624625
.wasmTableSize(_),
625626
.wasmTableGrow(_),

0 commit comments

Comments
 (0)