/
V8Profile.swift
545 lines (462 loc) · 21.7 KB
/
V8Profile.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// 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.
import Fuzzilli
fileprivate let ForceJITCompilationThroughLoopGenerator = CodeGenerator("ForceJITCompilationThroughLoopGenerator", input: .function()) { b, f in
// The MutationEngine may use variables of unknown type as input as well, however, we only want to call functions that we generated ourselves. Further, attempting to call a non-function will result in a runtime exception.
// For both these reasons, we abort here if we cannot prove that f is indeed a function.
guard b.type(of: f).Is(.function()) else { return }
let arguments = b.randomArguments(forCalling: f)
b.buildRepeatLoop(n: 100) { _ in
b.callFunction(f, withArgs: arguments)
}
}
fileprivate let ForceTurboFanCompilationGenerator = CodeGenerator("ForceTurboFanCompilationGenerator", input: .function()) { b, f in
// See comment in ForceJITCompilationThroughLoopGenerator.
guard b.type(of: f).Is(.function()) else { return }
let arguments = b.randomArguments(forCalling: f)
b.callFunction(f, withArgs: arguments)
b.eval("%PrepareFunctionForOptimization(%@)", with: [f]);
b.callFunction(f, withArgs: arguments)
b.callFunction(f, withArgs: arguments)
b.eval("%OptimizeFunctionOnNextCall(%@)", with: [f]);
b.callFunction(f, withArgs: arguments)
}
fileprivate let ForceMaglevCompilationGenerator = CodeGenerator("ForceMaglevCompilationGenerator", input: .function()) { b, f in
// See comment in ForceJITCompilationThroughLoopGenerator.
guard b.type(of: f).Is(.function()) else { return }
let arguments = b.randomArguments(forCalling: f)
b.callFunction(f, withArgs: arguments)
b.eval("%PrepareFunctionForOptimization(%@)", with: [f]);
b.callFunction(f, withArgs: arguments)
b.callFunction(f, withArgs: arguments)
b.eval("%OptimizeMaglevOnNextCall(%@)", with: [f]);
b.callFunction(f, withArgs: arguments)
}
fileprivate let TurbofanVerifyTypeGenerator = CodeGenerator("TurbofanVerifyTypeGenerator", input: .anything) { b, v in
b.eval("%VerifyType(%@)", with: [v])
}
fileprivate let WorkerGenerator = RecursiveCodeGenerator("WorkerGenerator") { b in
let workerSignature = Signature(withParameterCount: Int.random(in: 0...3))
// TODO(cffsmith): currently Fuzzilli does not know that this code is sent
// to another worker as a string. This has the consequence that we might
// use variables inside the worker that are defined in a different scope
// and as such they are not accessible / undefined. To fix this we should
// define an Operation attribute that tells Fuzzilli to ignore variables
// defined in outer scopes.
let workerFunction = b.buildPlainFunction(with: .parameters(workerSignature.parameters)) { args in
let this = b.loadThis()
// Generate a random onmessage handler for incoming messages.
let onmessageFunction = b.buildPlainFunction(with: .parameters(n: 1)) { args in
b.buildRecursive(block: 1, of: 2)
}
b.setProperty("onmessage", of: this, to: onmessageFunction)
b.buildRecursive(block: 2, of: 2)
}
let workerConstructor = b.loadBuiltin("Worker")
let functionString = b.loadString("function")
let argumentsArray = b.createArray(with: b.randomArguments(forCalling: workerFunction))
let configObject = b.createObject(with: ["type": functionString, "arguments": argumentsArray])
let worker = b.construct(workerConstructor, withArgs: [workerFunction, configObject])
// Fuzzilli can now use the worker.
}
fileprivate let SerializeDeserializeGenerator = CodeGenerator("SerializeDeserializeGenerator", input: .object()) { b, o in
// Load necessary builtins
let d8 = b.loadBuiltin("d8")
let serializer = b.getProperty("serializer", of: d8)
let Uint8Array = b.loadBuiltin("Uint8Array")
// Serialize a random object
let content = b.callMethod("serialize", on: serializer, withArgs: [o])
let u8 = b.construct(Uint8Array, withArgs: [content])
// Choose a random byte to change
let index = Int64.random(in: 0..<100)
// Either flip or replace the byte
let newByte: Variable
if probability(0.5) {
let bit = b.loadInt(1 << Int.random(in: 0..<8))
let oldByte = b.getElement(index, of: u8)
newByte = b.binary(oldByte, bit, with: .Xor)
} else {
newByte = b.loadInt(Int64.random(in: 0..<256))
}
b.setElement(index, of: u8, to: newByte)
// Deserialize the resulting buffer
let _ = b.callMethod("deserialize", on: serializer, withArgs: [content])
// Deserialized object is available in a variable now and can be used by following code
}
// Insert random GC calls throughout our code.
fileprivate let GcGenerator = CodeGenerator("GcGenerator") { b in
let gc = b.loadBuiltin("gc")
// Do minor GCs more frequently.
let type = b.loadString(probability(0.25) ? "major" : "minor")
// If the execution type is 'async', gc() returns a Promise, we currently
// do not really handle other than typing the return of gc to .undefined |
// .jsPromise. One could either chain a .then or create two wrapper
// functions that are differently typed such that fuzzilli always knows
// what the type of the return value is.
let execution = b.loadString(probability(0.5) ? "sync" : "async")
b.callFunction(gc, withArgs: [b.createObject(with: ["type": type, "execution": execution])])
}
fileprivate let MapTransitionsTemplate = ProgramTemplate("MapTransitionsTemplate") { b in
// This template is meant to stress the v8 Map transition mechanisms.
// Basically, it generates a bunch of CreateObject, GetProperty, SetProperty, FunctionDefinition,
// and CallFunction operations operating on a small set of objects and property names.
let propertyNames = b.fuzzer.environment.customProperties
assert(Set(propertyNames).isDisjoint(with: b.fuzzer.environment.customMethods))
// Use this as base object type. For one, this ensures that the initial map is stable.
// Moreover, this guarantees that when querying for this type, we will receive one of
// the objects we created and not e.g. a function (which is also an object).
assert(propertyNames.contains("a"))
let objType = JSType.object(withProperties: ["a"])
// Keep track of all objects created in this template so that they can be verified at the end.
var objects = [Variable]()
// Helper function to pick random properties and values.
func randomProperties(in b: ProgramBuilder) -> ([String], [Variable]) {
if !b.hasVisibleVariables {
// Use integer values if there are no visible variables, which should be a decent fallback.
b.loadInt(b.randomInt())
}
var properties = ["a"]
var values = [b.randomVariable()]
for _ in 0..<3 {
let property = chooseUniform(from: propertyNames)
guard !properties.contains(property) else { continue }
properties.append(property)
values.append(b.randomVariable())
}
assert(Set(properties).count == values.count)
return (properties, values)
}
// Temporarily overwrite the active code generators with the following generators...
let primitiveValueGenerator = ValueGenerator("PrimitiveValue") { b, n in
for _ in 0..<n {
// These should roughly correspond to the supported property representations of the engine.
withEqualProbability({
b.loadInt(b.randomInt())
}, {
b.loadFloat(b.randomFloat())
}, {
b.loadString(b.randomString())
})
}
}
let createObjectGenerator = ValueGenerator("CreateObject") { b, n in
for _ in 0..<n {
let (properties, values) = randomProperties(in: b)
let obj = b.createObject(with: Dictionary(uniqueKeysWithValues: zip(properties, values)))
assert(b.type(of: obj).Is(objType))
objects.append(obj)
}
}
let objectMakerGenerator = ValueGenerator("ObjectMaker") { b, n in
let f = b.buildPlainFunction(with: b.randomParameters()) { args in
let (properties, values) = randomProperties(in: b)
let o = b.createObject(with: Dictionary(uniqueKeysWithValues: zip(properties, values)))
b.doReturn(o)
}
for _ in 0..<n {
let obj = b.callFunction(f, withArgs: b.randomArguments(forCalling: f))
assert(b.type(of: obj).Is(objType))
objects.append(obj)
}
}
let objectConstructorGenerator = ValueGenerator("ObjectConstructor") { b, n in
let c = b.buildConstructor(with: b.randomParameters()) { args in
let this = args[0]
let (properties, values) = randomProperties(in: b)
for (p, v) in zip(properties, values) {
b.setProperty(p, of: this, to: v)
}
}
for _ in 0..<n {
let obj = b.construct(c, withArgs: b.randomArguments(forCalling: c))
assert(b.type(of: obj).Is(objType))
objects.append(obj)
}
}
let propertyLoadGenerator = CodeGenerator("PropertyLoad", input: objType) { b, obj in
b.getProperty(chooseUniform(from: propertyNames), of: obj)
}
let propertyStoreGenerator = CodeGenerator("PropertyStore", input: objType) { b, obj in
let numProperties = Int.random(in: 1...4)
for _ in 0..<numProperties {
b.setProperty(chooseUniform(from: propertyNames), of: obj, to: b.randomVariable())
}
}
let functionDefinitionGenerator = RecursiveCodeGenerator("FunctionDefinition") { b in
let prevSize = objects.count
// We use either a randomly generated signature or a fixed on that ensures we use our object type frequently.
var parameters = b.randomParameters()
if probability(0.5) && !objects.isEmpty {
parameters = .parameters(.plain(objType), .plain(objType), .anything, .anything)
}
let f = b.buildPlainFunction(with: parameters) { params in
for p in params where b.type(of: p).Is(objType) {
objects.append(p)
}
b.buildRecursive()
b.doReturn(b.randomVariable())
}
objects.removeLast(objects.count - prevSize)
for _ in 0..<3 {
let rval = b.callFunction(f, withArgs: b.randomArguments(forCalling: f))
if b.type(of: rval).Is(objType) {
objects.append(rval)
}
}
}
let functionCallGenerator = CodeGenerator("FunctionCall", input: .function()) { b, f in
let rval = b.callFunction(f, withArgs: b.randomArguments(forCalling: f))
if b.type(of: rval).Is(objType) {
objects.append(rval)
}
}
let constructorCallGenerator = CodeGenerator("ConstructorCall", input: .constructor()) { b, f in
let rval = b.construct(f, withArgs: b.randomArguments(forCalling: f))
if b.type(of: rval).Is(objType) {
objects.append(rval)
}
}
let functionJitCallGenerator = CodeGenerator("FunctionJitCall", input: .function()) { b, f in
let args = b.randomArguments(forCalling: f)
b.buildRepeatLoop(n: 100) { _ in
b.callFunction(f, withArgs: args) // Rval goes out-of-scope immediately, so no need to track it
}
}
let prevCodeGenerators = b.fuzzer.codeGenerators
b.fuzzer.setCodeGenerators(WeightedList<CodeGenerator>([
(primitiveValueGenerator, 2),
(createObjectGenerator, 1),
(objectMakerGenerator, 1),
(objectConstructorGenerator, 1),
(propertyLoadGenerator, 2),
(propertyStoreGenerator, 5),
(functionDefinitionGenerator, 1),
(functionCallGenerator, 2),
(constructorCallGenerator, 1),
(functionJitCallGenerator, 1)
]))
// ... run some of the ValueGenerators to create some initial objects ...
b.buildValues(5)
// ... and generate a bunch of code.
b.build(n: 100, by: .generating)
// Now, restore the previous code generators and generate some more code.
b.fuzzer.setCodeGenerators(prevCodeGenerators)
b.build(n: 10)
// Finally, run HeapObjectVerify on all our generated objects (that are still in scope).
for obj in objects {
b.eval("%HeapObjectVerify(%@)", with: [obj])
}
}
// This template fuzzes the RegExp engine.
// It finds bugs like: crbug.com/1437346 and crbug.com/1439691.
fileprivate let RegExpFuzzerTemplate = ProgramTemplate("RegExpFuzzerTemplate") { b in
// Taken from: https://source.chromium.org/chromium/chromium/src/+/refs/heads/main:v8/test/fuzzer/regexp-builtins.cc;l=212;drc=a61b95c63b0b75c1cfe872d9c8cdf927c226046e
let twoByteSubjectString = "f\\uD83D\\uDCA9ba\\u2603"
let replacementCandidates = [
"'X'",
"'$1$2$3'",
"'$$$&$`$\\'$1'",
"() => 'X'",
"(arg0, arg1, arg2, arg3, arg4) => arg0 + arg1 + arg2 + arg3 + arg4",
"() => 42"
]
let lastIndices = [
"undefined", "-1", "0",
"1", "2", "3",
"4", "5", "6",
"7", "8", "9",
"50", "4294967296", "2147483647",
"2147483648", "NaN", "Not a Number"
]
let f = b.buildPlainFunction(with: .parameters(n: 0)) { _ in
let pattern = probability(0.5) ? chooseUniform(from: b.fuzzer.environment.interestingRegExps) : b.randomString()
let regExpVar = b.loadRegExp(pattern, RegExpFlags.random())
let lastIndex = chooseUniform(from: lastIndices)
let lastIndexString = b.loadString(lastIndex)
b.setProperty("lastIndex", of: regExpVar, to: lastIndexString)
let subjectVar: Variable
if probability(0.1) {
subjectVar = b.loadString(twoByteSubjectString)
} else {
subjectVar = b.loadString(b.randomString())
}
let resultVar = b.loadNull()
b.buildTryCatchFinally(tryBody: {
let symbol = b.loadBuiltin("Symbol")
withEqualProbability({
let res = b.callMethod("exec", on: regExpVar, withArgs: [subjectVar])
b.reassign(resultVar, to: res)
}, {
let prop = b.getProperty("match", of: symbol)
let res = b.callComputedMethod(prop, on: regExpVar, withArgs: [subjectVar])
b.reassign(resultVar, to: res)
}, {
let prop = b.getProperty("replace", of: symbol)
let replacement = withEqualProbability({
b.loadString(b.randomString())
}, {
b.loadString(chooseUniform(from: replacementCandidates))
})
let res = b.callComputedMethod(prop, on: regExpVar, withArgs: [subjectVar, replacement])
b.reassign(resultVar, to: res)
}, {
let prop = b.getProperty("search", of: symbol)
let res = b.callComputedMethod(prop, on: regExpVar, withArgs: [subjectVar])
b.reassign(resultVar, to: res)
}, {
let prop = b.getProperty("split", of: symbol)
let randomSplitLimit = withEqualProbability({
"undefined"
}, {
"'not a number'"
}, {
String(b.randomInt())
})
let limit = b.loadString(randomSplitLimit)
let res = b.callComputedMethod(symbol, on: regExpVar, withArgs: [subjectVar, limit])
b.reassign(resultVar, to: res)
}, {
let res = b.callMethod("test", on: regExpVar, withArgs: [subjectVar])
b.reassign(resultVar, to: res)
})
}, catchBody: { _ in
})
b.build(n: 7)
b.doReturn(resultVar)
}
b.eval("%SetForceSlowPath(false)");
// compile the regexp once
b.callFunction(f)
let resFast = b.callFunction(f)
b.eval("%SetForceSlowPath(true)");
let resSlow = b.callFunction(f)
b.eval("%SetForceSlowPath(false)");
b.build(n: 15)
}
let v8Profile = Profile(
processArgs: { randomize in
var args = [
"--expose-gc",
"--omit-quit",
"--allow-natives-syntax",
"--fuzzing",
"--jit-fuzzing",
"--future",
"--harmony"
]
guard randomize else { return args }
//
// Future features that should sometimes be enabled.
//
if probability(0.25) {
args.append("--harmony-struct")
}
if probability(0.25) {
args.append("--minor-mc")
}
if probability(0.25) {
args.append("--shared-string-table")
}
if probability(0.25) {
args.append("--turboshaft")
if probability(0.25) {
args.append("--turboshaft-assert-types")
}
}
//
// Sometimes enable additional verification logic (which may be fairly expensive).
//
if probability(0.1) {
args.append("--verify-heap")
}
if probability(0.1) {
args.append("--turbo-verify")
}
if probability(0.1) {
args.append("--assert-types")
}
if probability(0.1) {
args.append("--turbo-verify-allocation")
}
//
// Existing features that should sometimes be disabled.
//
if probability(0.1) {
args.append("--no-turbofan")
}
if probability(0.1) {
args.append("--no-maglev")
}
if probability(0.1) {
args.append("--no-sparkplug")
}
//
// More exotic configuration changes.
//
if probability(0.05) {
args.append(probability(0.5) ? "--always-sparkplug" : "--no-always-sparkplug")
args.append(probability(0.5) ? "--always-osr" : "--no-always-osr")
args.append(probability(0.5) ? "--force-slow-path" : "--no-force-slow-path")
if !args.contains("--no-turbofan") {
args.append(probability(0.5) ? "--always-turbofan" : "--no-always-turbofan")
args.append(probability(0.5) ? "--turbo-move-optimization" : "--no-turbo-move-optimization")
args.append(probability(0.5) ? "--turbo-jt" : "--no-turbo-jt")
args.append(probability(0.5) ? "--turbo-loop-peeling" : "--no-turbo-loop-peeling")
args.append(probability(0.5) ? "--turbo-loop-variable" : "--no-turbo-loop-variable")
args.append(probability(0.5) ? "--turbo-loop-rotation" : "--no-turbo-loop-rotation")
args.append(probability(0.5) ? "--turbo-cf-optimization" : "--no-turbo-cf-optimization")
args.append(probability(0.5) ? "--turbo-escape" : "--no-turbo-escape")
args.append(probability(0.5) ? "--turbo-allocation-folding" : "--no-turbo-allocation-folding")
args.append(probability(0.5) ? "--turbo-instruction-scheduling" : "--no-turbo-instruction-scheduling")
args.append(probability(0.5) ? "--turbo-stress-instruction-scheduling" : "--no-turbo-stress-instruction-scheduling")
args.append(probability(0.5) ? "--turbo-store-elimination" : "--no-turbo-store-elimination")
args.append(probability(0.5) ? "--turbo-rewrite-far-jumps" : "--no-turbo-rewrite-far-jumps")
args.append(probability(0.5) ? "--turbo-optimize-apply" : "--no-turbo-optimize-apply")
args.append(chooseUniform(from: ["--no-enable-sse3", "--no-enable-ssse3", "--no-enable-sse4-1", "--no-enable-sse4-2", "--no-enable-avx", "--no-enable-avx2"]))
args.append(probability(0.5) ? "--turbo-load-elimination" : "--no-turbo-load-elimination")
args.append(probability(0.5) ? "--turbo-inlining" : "--no-turbo-inlining")
args.append(probability(0.5) ? "--turbo-splitting" : "--no-turbo-splitting")
}
}
return args
},
processEnv: [:],
maxExecsBeforeRespawn: 1000,
timeout: 250,
codePrefix: """
""",
codeSuffix: """
""",
ecmaVersion: ECMAScriptVersion.es6,
crashTests: ["fuzzilli('FUZZILLI_CRASH', 0)", "fuzzilli('FUZZILLI_CRASH', 1)", "fuzzilli('FUZZILLI_CRASH', 2)"],
additionalCodeGenerators: [
(ForceJITCompilationThroughLoopGenerator, 5),
(ForceTurboFanCompilationGenerator, 5),
(ForceMaglevCompilationGenerator, 5),
(TurbofanVerifyTypeGenerator, 10),
(SerializeDeserializeGenerator, 10),
(WorkerGenerator, 10),
(GcGenerator, 10),
],
additionalProgramTemplates: WeightedList<ProgramTemplate>([
(MapTransitionsTemplate, 1),
(RegExpFuzzerTemplate, 1),
]),
disabledCodeGenerators: [],
additionalBuiltins: [
"gc" : .function([] => (.undefined | .jsPromise)),
"d8" : .object(),
"Worker" : .constructor([.anything, .object()] => .object(withMethods: ["postMessage","getMessage"])),
]
)