forked from googleprojectzero/fuzzilli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OperationMutator.swift
124 lines (118 loc) · 5.78 KB
/
OperationMutator.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// A mutator that randomly mutates parameters of the Operations in the given program.
public class OperationMutator: BaseInstructionMutator {
public init() {
super.init(maxSimultaneousMutations: defaultMaxSimultaneousMutations)
}
public override func canMutate(_ instr: Instruction) -> Bool {
return instr.isParametric && instr.isMutable
}
public override func mutate(_ instr: Instruction, _ b: ProgramBuilder) {
var newOp: Operation
b.trace("Mutating next operation")
switch instr.op {
case is LoadInteger:
newOp = LoadInteger(value: b.genInt())
case is LoadBigInt:
newOp = LoadBigInt(value: b.genInt())
case is LoadFloat:
newOp = LoadFloat(value: b.genFloat())
case is LoadString:
newOp = LoadString(value: b.genString())
case let op as LoadRegExp:
if probability(0.5) {
newOp = LoadRegExp(value: b.genRegExp(), flags: op.flags)
} else {
newOp = LoadRegExp(value: op.value, flags: b.genRegExpFlags())
}
case let op as LoadBoolean:
newOp = LoadBoolean(value: !op.value)
case let op as CreateObject:
var propertyNames = op.propertyNames
assert(!propertyNames.isEmpty) // Otherwise operation would not be parametric
// Replace an existing property with another one
propertyNames[Int.random(in: 0..<propertyNames.count)] = b.genPropertyNameForWrite()
newOp = CreateObject(propertyNames: propertyNames)
case let op as CreateObjectWithSpread:
var propertyNames = op.propertyNames
assert(!propertyNames.isEmpty) // Otherwise operation would not be parametric
// Replace an existing property with another one
propertyNames[Int.random(in: 0..<propertyNames.count)] = b.genPropertyNameForWrite()
newOp = CreateObjectWithSpread(propertyNames: propertyNames, numSpreads: op.numSpreads)
case let op as CreateArrayWithSpread:
var spreads = op.spreads
if spreads.count > 0 {
let idx = Int.random(in: 0..<spreads.count)
spreads[idx] = !spreads[idx]
}
newOp = CreateArrayWithSpread(numInitialValues: spreads.count, spreads: spreads)
case is LoadBuiltin:
newOp = LoadBuiltin(builtinName: b.genBuiltinName())
case is LoadProperty:
newOp = LoadProperty(propertyName: b.genPropertyNameForRead())
case is StoreProperty:
newOp = StoreProperty(propertyName: b.genPropertyNameForWrite())
case is DeleteProperty:
newOp = DeleteProperty(propertyName: b.genPropertyNameForWrite())
case is LoadElement:
newOp = LoadElement(index: b.genIndex())
case is StoreElement:
newOp = StoreElement(index: b.genIndex())
case is DeleteElement:
newOp = DeleteElement(index: b.genIndex())
case let op as CallMethod:
newOp = CallMethod(methodName: b.genMethodName(), numArguments: op.numArguments)
case let op as CallFunctionWithSpread:
var spreads = op.spreads
if spreads.count > 0 {
let idx = Int.random(in: 0..<spreads.count)
spreads[idx] = !spreads[idx]
}
newOp = CallFunctionWithSpread(numArguments: op.numArguments, spreads: spreads)
case is UnaryOperation:
newOp = UnaryOperation(chooseUniform(from: allUnaryOperators))
case is BinaryOperation:
newOp = BinaryOperation(chooseUniform(from: allBinaryOperators))
case is Compare:
newOp = Compare(chooseUniform(from: allComparators))
case is LoadFromScope:
newOp = LoadFromScope(id: b.genPropertyNameForRead())
case is StoreToScope:
newOp = StoreToScope(id: b.genPropertyNameForWrite())
/*case let op as BeginClassMethodDefinition: TODO(saelo)
// TODO also mutate the signature?
newOp = BeginClassMethodDefinition(name: b.genMethodName(), signature: op.signature)*/
case let op as CallSuperMethod:
newOp = CallSuperMethod(methodName: b.genMethodName(), numArguments: op.numArguments)
case is LoadSuperProperty:
newOp = LoadSuperProperty(propertyName: b.genPropertyNameForRead())
case is StoreSuperProperty:
newOp = StoreSuperProperty(propertyName: b.genPropertyNameForWrite())
case is BeginWhile:
newOp = BeginWhile(comparator: chooseUniform(from: allComparators))
case is BeginDoWhile:
newOp = BeginDoWhile(comparator: chooseUniform(from: allComparators))
case let op as BeginFor:
if probability(0.5) {
newOp = BeginFor(comparator: chooseUniform(from: allComparators), op: op.op)
} else {
newOp = BeginFor(comparator: op.comparator, op: chooseUniform(from: allBinaryOperators))
}
default:
fatalError("Unhandled Operation: \(type(of: instr.op))")
}
b.adopt(Instruction(newOp, inouts: instr.inouts), keepTypes: false)
}
}