forked from googleprojectzero/fuzzilli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeGenerators.swift
491 lines (411 loc) · 15.6 KB
/
CodeGenerators.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// 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.
//
// Code generators.
//
// These insert one or more instructions into a program.
//
public typealias CodeGenerator = (_ b: ProgramBuilder) -> ()
public func IntegerLiteralGenerator(_ b: ProgramBuilder) {
b.loadInt(b.genInt())
}
public func FloatLiteralGenerator(_ b: ProgramBuilder) {
b.loadFloat(b.genFloat())
}
public func StringLiteralGenerator(_ b: ProgramBuilder) {
b.loadString(b.genString())
}
public func BooleanLiteralGenerator(_ b: ProgramBuilder) {
b.loadBool(Bool.random())
}
public func UndefinedValueGenerator(_ b: ProgramBuilder) {
b.loadUndefined()
}
public func NullValueGenerator(_ b: ProgramBuilder) {
b.loadNull()
}
public func ObjectLiteralGenerator(_ b: ProgramBuilder) {
var initialProperties = [String: Variable]()
for _ in 0..<Int.random(in: 0..<10) {
initialProperties[b.genPropertyNameForWrite()] = b.randVar()
}
b.createObject(with: initialProperties)
}
public func ArrayLiteralGenerator(_ b: ProgramBuilder) {
var initialValues = [Variable]()
for _ in 0..<Int.random(in: 0..<5) {
initialValues.append(b.randVar())
}
b.createArray(with: initialValues)
}
public func ObjectLiteralWithSpreadGenerator(_ b: ProgramBuilder) {
var initialProperties = [String: Variable]()
var spreads = [Variable]()
for _ in 0..<Int.random(in: 0..<10) {
withProbability(0.5, do: {
initialProperties[b.genPropertyNameForWrite()] = b.randVar()
}, else: {
spreads.append(b.randVar())
})
}
b.createObject(with: initialProperties, andSpreading: spreads)
}
public func ArrayLiteralWithSpreadGenerator(_ b: ProgramBuilder) {
var initialValues = [Variable]()
for _ in 0..<Int.random(in: 0..<5) {
initialValues.append(b.randVar())
}
// Pick some random inputs to spread.
let spreads = initialValues.map({ _ in Bool.random() })
b.createArray(with: initialValues, spreading: spreads)
}
public func BuiltinGenerator(_ b: ProgramBuilder) {
b.loadBuiltin(b.genBuiltinName())
}
public func FunctionDefinitionGenerator(_ b: ProgramBuilder) {
// For functions, we always generate one random instruction and one return instruction as function body.
// This ensures that generating one random instruction does not accidentially generate multiple instructions
// (which increases the likelyhood of runtime exceptions), but also generates somewhat useful functions.
b.defineFunction(withSignature: FunctionSignature(withParameterCount: Int.random(in: 2...5), hasRestParam: probability(0.1)), isJSStrictMode: probability(0.2)) { _ in
b.generate()
b.doReturn(value: b.randVar())
}
}
public func PropertyRetrievalGenerator(_ b: ProgramBuilder) {
let object = b.randVar(ofType: .object())
let propertyName = b.type(of: object).randomProperty() ?? b.genPropertyNameForRead()
b.loadProperty(propertyName, of: object)
}
public func PropertyAssignmentGenerator(_ b: ProgramBuilder) {
let object = b.randVar(ofType: .object())
let propertyName: String
// Either change an existing property or define a new one
if probability(0.5) {
propertyName = b.type(of: object).randomProperty() ?? b.genPropertyNameForWrite()
} else {
propertyName = b.genPropertyNameForWrite()
}
let value = b.randVar()
b.storeProperty(value, as: propertyName, on: object)
}
public func PropertyRemovalGenerator(_ b: ProgramBuilder) {
let object = b.randVar(ofType: .object())
let propertyName = b.type(of: object).randomProperty() ?? b.genPropertyNameForWrite()
b.deleteProperty(propertyName, of: object)
}
public func ElementRetrievalGenerator(_ b: ProgramBuilder) {
let array = b.randVar(ofType: .object())
let index = b.genIndex()
b.loadElement(index, of: array)
}
public func ElementAssignmentGenerator(_ b: ProgramBuilder) {
let array = b.randVar(ofType: .object())
let index = b.genIndex()
let value = b.randVar()
b.storeElement(value, at: index, of: array)
}
public func ElementRemovalGenerator(_ b: ProgramBuilder) {
let array = b.randVar(ofType: .object())
let index = b.genIndex()
b.deleteElement(index, of: array)
}
public func ComputedPropertyAssignmentGenerator(_ b: ProgramBuilder) {
let object = b.randVar(ofType: .object())
let propertyName = b.randVar()
let value = b.randVar()
b.storeComputedProperty(value, as: propertyName, on: object)
}
public func ComputedPropertyRetrievalGenerator(_ b: ProgramBuilder) {
let object = b.randVar()
let propertyName = b.randVar()
b.loadComputedProperty(propertyName, of: object)
}
public func ComputedPropertyRemovalGenerator(_ b: ProgramBuilder) {
let object = b.randVar()
let propertyName = b.randVar()
b.deleteComputedProperty(propertyName, of: object)
}
public func TypeTestGenerator(_ b: ProgramBuilder) {
let v = b.randVar()
let type = b.doTypeof(v)
// Also generate a comparison here, since that's probably the only interesting thing you can do with the result.
let rhs = b.loadString(chooseUniform(from: JavaScriptEnvironment.jsTypeNames))
b.compare(type, rhs, with: .strictEqual)
}
public func InstanceOfGenerator(_ b: ProgramBuilder) {
let lhs = b.randVar()
let rhs = b.randVar()
b.doInstanceOf(lhs, rhs)
}
public func InGenerator(_ b: ProgramBuilder) {
let lhs = b.randVar()
let rhs = b.randVar()
b.doIn(lhs, rhs)
}
public func MethodCallGenerator(_ b: ProgramBuilder) {
let object = b.randVar(ofType: .object())
let methodName = b.type(of: object).randomMethod() ?? b.genMethodName()
let arguments = b.generateCallArguments(forMethod: methodName, on: object)
b.callMethod(methodName, on: object, withArgs: arguments)
}
public func FunctionCallGenerator(_ b: ProgramBuilder) {
let function = b.randVar(ofType: .function())
let arguments = b.generateCallArguments(for: function)
b.callFunction(function, withArgs: arguments)
}
public func FunctionReturnGenerator(_ b: ProgramBuilder) {
if b.isInFunction {
b.doReturn(value: b.randVar())
}
}
public func ConstructorCallGenerator(_ b: ProgramBuilder) {
let constructor = b.randVar(ofType: .constructor())
let arguments = b.generateCallArguments(for: constructor)
b.construct(constructor, withArgs: arguments)
}
public func FunctionCallWithSpreadGenerator(_ b: ProgramBuilder) {
let function = b.randVar(ofType: .function())
// Since we are spreading, the signature doesn't actually help, so ignore it completely
let arguments = b.generateCallArguments(for: FunctionSignature.forUnknownFunction)
// Pick some random arguments to spread.
let spreads = arguments.map({ _ in Bool.random() })
b.callFunction(function, withArgs: arguments, spreading: spreads)
}
public func UnaryOperationGenerator(_ b: ProgramBuilder) {
let input = b.randVar()
b.unary(chooseUniform(from: allUnaryOperators), input)
}
public func BinaryOperationGenerator(_ b: ProgramBuilder) {
let lhs = b.randVar()
let rhs = b.randVar()
b.binary(lhs, rhs, with: chooseUniform(from: allBinaryOperators))
}
public func PhiGenerator(_ b: ProgramBuilder) {
b.phi(b.randVar())
}
public func ReassignmentGenerator(_ b: ProgramBuilder) {
if let output = b.randVar(ofGuaranteedType: .phi(of: .anything)) {
let input = b.randVar()
b.copy(input, to: output)
}
}
public func ComparisonGenerator(_ b: ProgramBuilder) {
let lhs = b.randVar()
let rhs = b.randVar()
b.compare(lhs, rhs, with: chooseUniform(from: allComparators))
}
public func IfStatementGenerator(_ b: ProgramBuilder) {
// Optionally create some kind of comparison as well
withProbability(0.5) {
b.run(chooseUniform(from: [ComparisonGenerator, ComparisonGenerator, TypeTestGenerator, InstanceOfGenerator]))
}
let cond = b.randVar(ofType: .boolean)
let phi = b.phi(b.randVar())
b.beginIf(cond) {
b.generate()
b.copy(b.randVar(), to: phi)
}
b.beginElse() {
b.generate()
b.copy(b.randVar(), to: phi)
}
b.endIf()
}
public func WhileLoopGenerator(_ b: ProgramBuilder) {
let start = b.loadInt(0)
let end = b.loadInt(Int.random(in: 0...10))
let loopVar = b.phi(start)
b.whileLoop(loopVar, .lessThan, end) {
b.generate()
let newLoopVar = b.unary(.Inc, loopVar)
b.copy(newLoopVar, to: loopVar)
}
}
public func DoWhileLoopGenerator(_ b: ProgramBuilder) {
let start = b.loadInt(0)
let end = b.loadInt(Int.random(in: 0...10))
let loopVar = b.phi(start)
b.doWhileLoop(loopVar, .lessThan, end) {
b.generate()
let newLoopVar = b.unary(.Inc, loopVar)
b.copy(newLoopVar, to: loopVar)
}
}
public func ForLoopGenerator(_ b: ProgramBuilder) {
let start = b.loadInt(0)
let end = b.loadInt(Int.random(in: 0...10))
let step = b.loadInt(1)
b.forLoop(start, .lessThan, end, .Add, step) { _ in
b.generate()
}
}
public func ForInLoopGenerator(_ b: ProgramBuilder) {
let obj = b.randVar(ofType: .object())
b.forInLoop(obj) { _ in
b.generate()
}
}
public func ForOfLoopGenerator(_ b: ProgramBuilder) {
let obj = b.randVar(ofType: .object())
b.forOfLoop(obj) { _ in
b.generate()
}
}
public func BreakGenerator(_ b: ProgramBuilder) {
if b.isInLoop {
b.doBreak()
}
}
public func ContinueGenerator(_ b: ProgramBuilder) {
if b.isInLoop {
b.doContinue()
}
}
public func TryCatchGenerator(_ b: ProgramBuilder) {
let v = b.phi(b.randVar())
b.beginTry() {
b.generate()
b.copy(b.randVar(), to: v)
}
b.beginCatch() { _ in
b.generate()
b.copy(b.randVar(), to: v)
}
b.endTryCatch()
}
public func ThrowGenerator(_ b: ProgramBuilder) {
let v = b.randVar()
b.throwException(v)
}
//
// Language-specific Generators
//
public func TypedArrayGenerator(_ b: ProgramBuilder) {
let size = b.loadInt(Int.random(in: 0...0x10000))
let constructor = b.loadBuiltin(chooseUniform(from: ["Uint8Array", "Int8Array", "Uint16Array", "Int16Array", "Uint32Array", "Int32Array", "Float32Array", "Float64Array", "Uint8ClampedArray", "DataView"]))
b.construct(constructor, withArgs: [size])
}
public func FloatArrayGenerator(_ b: ProgramBuilder) {
let value = b.loadFloat(13.37)
b.createArray(with: Array(repeating: value, count: Int.random(in: 1...5)))
}
public func IntArrayGenerator(_ b: ProgramBuilder) {
let value = b.loadInt(1337)
b.createArray(with: Array(repeating: value, count: Int.random(in: 1...5)))
}
public func ObjectArrayGenerator(_ b: ProgramBuilder) {
let value = b.createObject(with: [:])
b.createArray(with: Array(repeating: value, count: Int.random(in: 1...5)))
}
public func WellKnownPropertyLoadGenerator(_ b: ProgramBuilder) {
let Symbol = b.loadBuiltin("Symbol")
let name = chooseUniform(from: ["isConcatSpreadable", "iterator", "match", "replace", "search", "species", "split", "toPrimitive", "toStringTag", "unscopables"])
let pname = b.loadProperty(name, of: Symbol)
let obj = b.randVar(ofType: .object())
b.loadComputedProperty(pname, of: obj)
}
public func WellKnownPropertyStoreGenerator(_ b: ProgramBuilder) {
let Symbol = b.loadBuiltin("Symbol")
let name = chooseUniform(from: ["isConcatSpreadable", "iterator", "match", "replace", "search", "species", "split", "toPrimitive", "toStringTag", "unscopables"])
let pname = b.loadProperty(name, of: Symbol)
let obj = b.randVar(ofType: .object())
let val = b.randVar()
b.storeComputedProperty(val, as: pname, on: obj)
}
public func PrototypeAccessGenerator(_ b: ProgramBuilder) {
let obj = b.randVar(ofType: .object())
b.loadProperty("__proto__", of: obj)
}
public func PrototypeOverwriteGenerator(_ b: ProgramBuilder) {
let obj = b.randVar(ofType: .object())
let proto = b.randVar(ofType: .object())
b.storeProperty(proto, as: "__proto__", on: obj)
}
public func CallbackPropertyGenerator(_ b: ProgramBuilder) {
let obj = b.randVar(ofType: .object())
let callback = b.randVar(ofType: .function())
let propertyName = chooseUniform(from: ["valueOf", "toString"])
b.storeProperty(callback, as: propertyName, on: obj)
}
public func PropertyAccessorGenerator(_ b: ProgramBuilder) {
let receiver = b.randVar(ofType: .object())
let propertyName = probability(0.5) ? b.loadString(b.genPropertyNameForWrite()) : b.loadInt(b.genIndex())
var initialProperties = [String: Variable]()
withEqualProbability({
initialProperties = ["get": b.randVar(ofType: .function())]
}, {
initialProperties = ["set": b.randVar(ofType: .function())]
}, {
initialProperties = ["get": b.randVar(ofType: .function()), "set": b.randVar(ofType: .function())]
})
let descriptor = b.createObject(with: initialProperties)
let Object = b.loadBuiltin("Object")
b.callMethod("defineProperty", on: Object, withArgs: [receiver, propertyName, descriptor])
}
public func ProxyGenerator(_ b: ProgramBuilder) {
let target = b.randVar()
var candidates = Set(["getPrototypeOf", "setPrototypeOf", "isExtensible", "preventExtensions", "getOwnPropertyDescriptor", "defineProperty", "has", "get", "set", "deleteProperty", "ownKeys", "apply", "call", "construct"])
var handlerProperties = [String: Variable]()
for _ in 0..<Int.random(in: 0..<candidates.count) {
let hook = chooseUniform(from: candidates)
candidates.remove(hook)
handlerProperties[hook] = b.randVar(ofType: .function())
}
let handler = b.createObject(with: handlerProperties)
let Proxy = b.loadBuiltin("Proxy")
b.construct(Proxy, withArgs: [target, handler])
}
// Tries to change the length property of some object
public func LengthChangeGenerator(_ b: ProgramBuilder) {
let target = b.randVar(ofType: .object())
let newLength: Variable
if probability(0.5) {
newLength = b.loadInt(Int.random(in: 0..<3))
} else {
newLength = b.loadInt(b.genIndex())
}
b.storeProperty(newLength, as: "length", on: target)
}
// Tries to change the element kind of an array
public func ElementKindChangeGenerator(_ b: ProgramBuilder) {
let target = b.randVar(ofType: .object())
let value = b.randVar()
b.storeElement(value, at: Int.random(in: 0..<3), of: target)
}
// Generates a JavaScript 'with' statement
public func WithStatementGenerator(_ b: ProgramBuilder) {
let obj = b.randVar(ofType: .object())
b.with(obj) {
withProbability(0.5, do: { () -> Void in
b.loadFromScope(id: b.genPropertyNameForRead())
}, else: { () -> Void in
let value = b.randVar()
b.storeToScope(value, as: b.genPropertyNameForWrite())
})
b.generate()
}
}
public func LoadFromScopeGenerator(_ b: ProgramBuilder) {
guard b.isInWithStatement else {
return
}
b.loadFromScope(id: b.genPropertyNameForRead())
}
public func StoreToScopeGenerator(_ b: ProgramBuilder) {
guard b.isInWithStatement else {
return
}
let value = b.randVar()
b.storeToScope(value, as: b.genPropertyNameForWrite())
}