forked from googleprojectzero/fuzzilli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProgramBuilder.swift
1354 lines (1135 loc) · 51.8 KB
/
ProgramBuilder.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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.
/// Builds programs.
///
/// This provides methods for constructing and appending random
/// instances of the different kinds of operations in a program.
public class ProgramBuilder {
/// The fuzzer instance for which this builder is active.
public let fuzzer: Fuzzer
/// The code and type information of the program that is being constructed.
private var code = Code()
public var types = ProgramTypes()
/// Comments for the program that is being constructed.
private var comments = ProgramComments()
/// The parent program for the program being constructed.
private let parent: Program?
public enum Mode {
/// In this mode, the builder will try as hard as possible to generate semantically valid code.
/// However, the generated code is likely not as diverse as in aggressive mode.
case conservative
/// In this mode, the builder tries to generate more diverse code. However, the generated
/// code likely has a lower probability of being semantically correct.
case aggressive
}
/// The mode of this builder
public var mode: Mode
/// Whether to perform splicing as part of the code generation.
public var performSplicingDuringCodeGeneration = true
public var context: ProgramContext {
return contextAnalyzer.context
}
/// Counter to quickly determine the next free variable.
private var numVariables = 0
/// Property names and integer values previously seen in the current program.
private var seenPropertyNames = Set<String>()
private var seenIntegers = Set<Int64>()
/// Various analyzers for the current program.
private var scopeAnalyzer = ScopeAnalyzer()
private var contextAnalyzer = ContextAnalyzer()
/// Abstract interpreter to computer type information.
private var interpreter: AbstractInterpreter?
/// During code generation, contains the minimum number of remaining instructions
/// that should still be generated.
private var currentCodegenBudget = 0
/// Whether there are any variables currently in scope.
public var hasVisibleVariables: Bool {
return scopeAnalyzer.visibleVariables.count > 0
}
/// Constructs a new program builder for the given fuzzer.
init(for fuzzer: Fuzzer, parent: Program?, interpreter: AbstractInterpreter?, mode: Mode) {
self.fuzzer = fuzzer
self.interpreter = interpreter
self.mode = mode
self.parent = parent
}
/// Resets this builder.
public func reset() {
numVariables = 0
seenPropertyNames.removeAll()
seenIntegers.removeAll()
code.removeAll()
types = ProgramTypes()
scopeAnalyzer = ScopeAnalyzer()
contextAnalyzer = ContextAnalyzer()
interpreter?.reset()
currentCodegenBudget = 0
}
/// Finalizes and returns the constructed program, then resets this builder so it can be reused for building another program.
public func finalize() -> Program {
assert(openFunctions.isEmpty)
let program = Program(code: code, parent: parent, types: types, comments: comments)
// TODO set type status to something meaningful?
reset()
return program
}
/// Prints the current program as FuzzIL code to stdout. Useful for debugging.
public func dumpCurrentProgram() {
print(FuzzILLifter().lift(code))
}
/// Add a trace comment to the currently generated program at the current position.
/// This is only done if history inspection is enabled.
public func trace(_ commentGenerator: @autoclosure () -> String) {
if fuzzer.config.inspection.contains(.history) {
// Use an autoclosure here so that template strings are only evaluated when they are needed.
comments.add(commentGenerator(), at: .instruction(code.count))
}
}
/// Add a trace comment at the start of the currently generated program.
/// This is only done if history inspection is enabled.
public func traceHeader(_ commentGenerator: @autoclosure () -> String) {
if fuzzer.config.inspection.contains(.history) {
comments.add(commentGenerator(), at: .header)
}
}
/// Generates a random integer for the current program context.
public func genInt() -> Int64 {
// Either pick a previously seen integer or generate a random one
if probability(0.15) && seenIntegers.count >= 2 {
return chooseUniform(from: seenIntegers)
} else {
return withEqualProbability({
chooseUniform(from: self.fuzzer.environment.interestingIntegers)
}, {
Int64.random(in: -0x100000000...0x100000000)
})
}
}
/// Generates a random regex pattern.
public func genRegExp() -> String {
// Generate a "base" regexp
var regex = ""
let desiredLength = Int.random(in: 1...4)
while regex.count < desiredLength {
regex += withEqualProbability({
String.random(ofLength: 1)
}, {
chooseUniform(from: self.fuzzer.environment.interestingRegExps)
})
}
// Now optionally concatenate with another regexp
if probability(0.3) {
regex += genRegExp()
}
// Or add a quantifier, if there is not already a quantifier in the last position.
if probability(0.2) && !self.fuzzer.environment.interestingRegExpQuantifiers.contains(String(regex.last!)) {
regex += chooseUniform(from: self.fuzzer.environment.interestingRegExpQuantifiers)
}
// Or wrap in brackets
if probability(0.1) {
withEqualProbability({
// optionally invert the character set
if probability(0.2) {
regex = "^" + regex
}
regex = "[" + regex + "]"
}, {
regex = "(" + regex + ")"
})
}
return regex
}
/// Generates a random set of RegExpFlags
public func genRegExpFlags() -> RegExpFlags {
return RegExpFlags.random()
}
/// Generates a random index value for the current program context.
public func genIndex() -> Int64 {
return genInt()
}
/// Generates a random integer for the current program context.
public func genFloat() -> Double {
// TODO improve this
return withEqualProbability({
chooseUniform(from: self.fuzzer.environment.interestingFloats)
}, {
Double.random(in: -1000000...1000000)
})
}
/// Generates a random string value for the current program context.
public func genString() -> String {
return withEqualProbability({
self.genPropertyNameForRead()
}, {
chooseUniform(from: self.fuzzer.environment.interestingStrings)
}, {
String.random(ofLength: 10)
}, {
String(chooseUniform(from: self.fuzzer.environment.interestingIntegers))
})
}
/// Generates a random builtin name for the current program context.
public func genBuiltinName() -> String {
return chooseUniform(from: fuzzer.environment.builtins)
}
/// Generates a random property name for the current program context.
public func genPropertyNameForRead() -> String {
if probability(0.15) && seenPropertyNames.count >= 2 {
return chooseUniform(from: seenPropertyNames)
} else {
return chooseUniform(from: fuzzer.environment.readPropertyNames)
}
}
/// Generates a random property name for the current program context.
public func genPropertyNameForWrite() -> String {
if probability(0.15) && seenPropertyNames.count >= 2 {
return chooseUniform(from: seenPropertyNames)
} else {
return chooseUniform(from: fuzzer.environment.writePropertyNames)
}
}
/// Generates a random method name for the current program context.
public func genMethodName() -> String {
return chooseUniform(from: fuzzer.environment.methodNames)
}
///
/// Access to variables.
///
/// Returns a random variable.
public func randVar() -> Variable {
assert(hasVisibleVariables)
return randVarInternal()!
}
/// Returns a random variable of the given type.
///
/// In conservative mode, this function fails unless it finds a matching variable.
/// In aggressive mode, this function will also return variables that have unknown type, and may, if no matching variables are available, return variables of any type.
public func randVar(ofType type: Type) -> Variable? {
var wantedType = type
// As query/input type, .unknown is treated as .anything.
// This for example simplifies code that is attempting to replace a given variable with another one with a "compatible" type.
// If the real type of the replaced variable is unknown, it doesn't make sense to search for another variable of unknown type, so just use .anything.
if wantedType.Is(.unknown) {
wantedType = .anything
}
if mode == .aggressive {
wantedType |= .unknown
}
if let v = randVarInternal({ self.type(of: $0).Is(wantedType) }) {
return v
}
// Didn't find a matching variable. If we are in aggressive mode, we now simply return a random variable.
if mode == .aggressive {
return randVar()
}
// Otherwise, we give up
return nil
}
/// Returns a random variable of the given type. This is the same as calling randVar in conservative building mode.
public func randVar(ofConservativeType type: Type) -> Variable? {
let oldMode = mode
mode = .conservative
defer { mode = oldMode }
return randVar(ofType: type)
}
/// Returns a random variable satisfying the given constraints or nil if none is found.
private func randVarInternal(_ selector: ((Variable) -> Bool)? = nil) -> Variable? {
var candidates = [Variable]()
// Prefer inner scopes
withProbability(0.75) {
candidates = chooseBiased(from: scopeAnalyzer.scopes, factor: 1.25)
if let sel = selector {
candidates = candidates.filter(sel)
}
}
if candidates.isEmpty {
if let sel = selector {
candidates = scopeAnalyzer.visibleVariables.filter(sel)
} else {
candidates = scopeAnalyzer.visibleVariables
}
}
if candidates.isEmpty {
return nil
}
return chooseUniform(from: candidates)
}
/// Type information access.
public func type(of v: Variable) -> Type {
return types.getType(of: v, after: code.lastInstruction.index)
}
public func type(ofProperty property: String) -> Type {
return interpreter?.type(ofProperty: property) ?? .unknown
}
/// Returns the type of the `super` binding at the current position.
public func currentSuperType() -> Type {
return interpreter?.currentSuperType() ?? .unknown
}
public func methodSignature(of methodName: String, on object: Variable) -> FunctionSignature {
return interpreter?.inferMethodSignature(of: methodName, on: object) ?? FunctionSignature.forUnknownFunction
}
public func methodSignature(of methodName: String, on objType: Type) -> FunctionSignature {
return interpreter?.inferMethodSignature(of: methodName, on: objType) ?? FunctionSignature.forUnknownFunction
}
public func setType(ofProperty propertyName: String, to propertyType: Type) {
trace("Setting global property type: \(propertyName) => \(propertyType)")
interpreter?.setType(ofProperty: propertyName, to: propertyType)
}
public func setType(ofVariable variable: Variable, to variableType: Type) {
interpreter?.setType(of: variable, to: variableType)
}
public func setSignature(ofMethod methodName: String, to methodSignature: FunctionSignature) {
trace("Setting global method signature: \(methodName) => \(methodSignature)")
interpreter?.setSignature(ofMethod: methodName, to: methodSignature)
}
// This expands and collects types for arguments in function signatures.
private func prepareArgumentTypes(forSignature signature: FunctionSignature) -> [Type] {
var parameterTypes = signature.inputTypes
var argumentTypes = [Type]()
// "Expand" varargs parameters first
if signature.hasVarargsParameter() {
let varargsParam = parameterTypes.removeLast()
assert(varargsParam.isList)
for _ in 0..<Int.random(in: 0...5) {
parameterTypes.append(varargsParam.removingFlagTypes())
}
}
for var param in parameterTypes {
if param.isOptional {
// It's an optional argument, so stop here in some cases
if probability(0.25) {
break
}
// Otherwise, "unwrap" the optional
param = param.removingFlagTypes()
}
assert(!param.hasFlags)
argumentTypes.append(param)
}
return argumentTypes
}
public func generateCallArguments(for signature: FunctionSignature) -> [Variable] {
let argumentTypes = prepareArgumentTypes(forSignature: signature)
var arguments = [Variable]()
for argumentType in argumentTypes {
if let v = randVar(ofConservativeType: argumentType) {
arguments.append(v)
} else {
let argument = generateVariable(ofType: argumentType)
// make sure, that now after generation we actually have a
// variable of that type available.
assert(randVar(ofType: argumentType) != nil)
arguments.append(argument)
}
}
return arguments
}
public func randCallArguments(for signature: FunctionSignature) -> [Variable]? {
let argumentTypes = prepareArgumentTypes(forSignature: signature)
var arguments = [Variable]()
for argumentType in argumentTypes {
guard let v = randVar(ofType: argumentType) else { return nil }
arguments.append(v)
}
return arguments
}
public func randCallArguments(for function: Variable) -> [Variable]? {
let signature = type(of: function).signature ?? FunctionSignature.forUnknownFunction
return randCallArguments(for: signature)
}
public func generateCallArguments(for function: Variable) -> [Variable] {
let signature = type(of: function).signature ?? FunctionSignature.forUnknownFunction
return generateCallArguments(for: signature)
}
public func randCallArguments(forMethod methodName: String, on object: Variable) -> [Variable]? {
let signature = methodSignature(of: methodName, on: object)
return randCallArguments(for: signature)
}
public func randCallArguments(forMethod methodName: String, on objType: Type) -> [Variable]? {
let signature = methodSignature(of: methodName, on: objType)
return randCallArguments(for: signature)
}
public func generateCallArguments(forMethod methodName: String, on object: Variable) -> [Variable] {
let signature = methodSignature(of: methodName, on: object)
return generateCallArguments(for: signature)
}
/// Generates a sequence of instructions that generate the desired type.
/// This function can currently generate:
/// - primitive types
/// - arrays
/// - objects of certain types
/// - plain objects with properties that are either generated or selected
/// and methods that are selected from the environment.
/// It currently cannot generate:
/// - methods for objects
func generateVariable(ofType type: Type) -> Variable {
trace("Generating variable of type \(type)")
// Check primitive types
if type.Is(.integer) || type.Is(fuzzer.environment.intType) {
return loadInt(genInt())
}
if type.Is(.float) || type.Is(fuzzer.environment.floatType) {
return loadFloat(genFloat())
}
if type.Is(.string) || type.Is(fuzzer.environment.stringType) {
return loadString(genString())
}
if type.Is(.boolean) || type.Is(fuzzer.environment.booleanType) {
return loadBool(Bool.random())
}
if type.Is(.bigint) || type.Is(fuzzer.environment.bigIntType) {
return loadBigInt(genInt())
}
assert(type.Is(.object()), "Unexpected type encountered \(type)")
// The variable that we will return.
var obj: Variable
// Fast path for array creation.
if type.Is(fuzzer.environment.arrayType) && probability(0.9) {
let value = randVar()
return createArray(with: Array(repeating: value, count: Int.random(in: 1...5)))
}
if let group = type.group {
// We check this during Environment initialization, but let's keep this just in case.
assert(fuzzer.environment.type(ofBuiltin: group) != .unknown, "We don't know how to construct \(group)")
let constructionSignature = fuzzer.environment.type(ofBuiltin: group).constructorSignature!
let arguments = generateCallArguments(for: constructionSignature)
let constructor = loadBuiltin(group)
obj = construct(constructor, withArgs: arguments)
} else {
// Either generate a literal or use the store property stuff.
if probability(0.8) { // Do the literal
var initialProperties: [String: Variable] = [:]
// gather properties of the correct types
for prop in type.properties {
var value: Variable?
let type = self.type(ofProperty: prop)
if type != .unknown {
value = randVar(ofConservativeType: type)
if value == nil {
value = generateVariable(ofType: type)
}
} else {
if !hasVisibleVariables {
value = loadInt(genInt())
} else {
value = randVar()
}
}
initialProperties[prop] = value
}
// TODO: This should take the method type/signature into account!
_ = type.methods.map { initialProperties[$0] = randVar(ofType: .function())! }
obj = createObject(with: initialProperties)
} else { // Do it with storeProperty
obj = construct(loadBuiltin("Object"), withArgs: [])
for method in type.methods {
// TODO: This should take the method type/signature into account!
let methodVar = randVar(ofType: .function())
storeProperty(methodVar!, as: method, on: obj)
}
// These types might have been defined in the interpreter
for prop in type.properties {
var value: Variable?
let type = self.type(ofProperty: prop)
if type != .unknown {
value = randVar(ofConservativeType: type)
if value == nil {
value = generateVariable(ofType: type)
}
} else {
value = randVar()
}
storeProperty(value!, as: prop, on: obj)
}
}
}
return obj
}
///
/// Adoption of variables from a different program.
/// Required when copying instructions between program.
///
private var varMaps = [VariableMap<Variable>]()
/// Formatted ProgramTypes structure for easier adopting of runtimeTypes
private var runtimeTypesMaps = [[[(Variable, Type)]]]()
/// Prepare for adoption of variables from the given program.
///
/// This sets up a mapping for variables from the given program to the
/// currently constructed one to avoid collision of variable names.
public func beginAdoption(from program: Program) {
varMaps.append(VariableMap())
runtimeTypesMaps.append(program.types.onlyRuntimeTypes().indexedByInstruction(for: program))
}
/// Finishes the most recently started adoption.
public func endAdoption() {
varMaps.removeLast()
runtimeTypesMaps.removeLast()
}
/// Executes the given block after preparing for adoption from the provided program.
public func adopting(from program: Program, _ block: () -> Void) {
beginAdoption(from: program)
block()
endAdoption()
}
/// Maps a variable from the program that is currently configured for adoption into the program being constructed.
public func adopt(_ variable: Variable) -> Variable {
if !varMaps.last!.contains(variable) {
varMaps[varMaps.count - 1][variable] = nextVariable()
}
return varMaps.last![variable]!
}
private func createVariableMapping(from sourceVariable: Variable, to hostVariable: Variable) {
assert(!varMaps.last!.contains(sourceVariable))
varMaps[varMaps.count - 1][sourceVariable] = hostVariable
}
/// Maps a list of variables from the program that is currently configured for adoption into the program being constructed.
public func adopt<Variables: Collection>(_ variables: Variables) -> [Variable] where Variables.Element == Variable {
return variables.map(adopt)
}
private func adoptTypes(at origInstrIndex: Int) {
for (variable, type) in runtimeTypesMaps.last![origInstrIndex] {
// No need to keep unknown type nor type of not adopted variable
if let adoptedVariable = varMaps.last![variable] {
// Unknown runtime types should not be saved in ProgramTypes
assert(type != .unknown)
interpreter?.setType(of: adoptedVariable, to: type)
// We should save this type even if we do not have interpreter
// This way we can use runtime types without interpreter
types.setType(of: adoptedVariable, to: type, after: code.lastInstruction.index, quality: .runtime)
}
}
}
/// Adopts an instruction from the program that is currently configured for adoption into the program being constructed.
public func adopt(_ instr: Instruction, keepTypes: Bool) {
internalAppend(Instruction(instr.op, inouts: adopt(instr.inouts)))
if keepTypes {
adoptTypes(at: instr.index)
}
}
/// Append an instruction at the current position.
public func append(_ instr: Instruction) {
for v in instr.allOutputs {
numVariables = max(v.number + 1, numVariables)
}
internalAppend(instr)
}
/// Append a program at the current position.
///
/// This also renames any variable used in the given program so all variables
/// from the appended program refer to the same values in the current program.
public func append(_ program: Program) {
adopting(from: program) {
for instr in program.code {
adopt(instr, keepTypes: true)
}
}
}
/// Append a splice from another program.
public func splice(from program: Program, at index: Int) {
trace("Splicing instruction \(index) (\(program.code[index].op.name)) from \(program.id)")
var idx = index
// The input re-wiring algorithm modifies the code of the source program
// to implement the manual variable mapping
var source = program.code
// The placeholder variable is the next free variable in the victim program.
var nextFreeVariable = source.nextFreeVariable().number
func makePlaceholderVariable() -> Variable {
nextFreeVariable += 1
return Variable(number: nextFreeVariable - 1)
}
// We still adopt from the input program, just with slightly modified code :)
beginAdoption(from: program)
// Determine all necessary input instructions for the choosen instruction
// We need special handling for blocks:
// If the choosen instruction is a block instruction then copy the whole block
// If we need an inner output of a block instruction then only copy the block instructions, not the content
// Otherwise copy the whole block including its content
var requiredInstructions = Set<Int>()
var requiredInputs = VariableSet()
// This maps victim instruction indices to victim : host variable remap
// Instead of calling adopt and then using nextvar if the variable is
// not in the varMaps map, we do the adoption manually.
func rewireOrKeepInputs(of instr: Instruction) {
var inputs = Array(instr.inputs)
var neededInputs: [Variable] = []
for (idx, input) in instr.inputs.enumerated() {
neededInputs.append(input)
if probability(0.2) && mode != .conservative {
var type = program.type(of: input, before: instr.index)
if type == .unknown {
type = .anything
}
if let hostVar = randVar(ofConservativeType: type.generalize()) {
let placeholderVariable = makePlaceholderVariable()
inputs[idx] = placeholderVariable
createVariableMapping(from: placeholderVariable, to: hostVar)
neededInputs.removeLast()
}
}
}
// Rewrite the instruction with the new inputs only if we have modified it.
if inputs[...] != instr.inputs {
source.replace(instr, with: Instruction(instr.op, inouts: inputs + Array(instr.allOutputs)))
}
requiredInputs.formUnion(neededInputs)
requiredInstructions.insert(instr.index)
}
func keep(_ instr: Instruction, includeBlockContent: Bool = false) {
guard !requiredInstructions.contains(instr.index) else { return }
if instr.isBlock {
let group = BlockGroup(around: instr, in: source)
let instructions = includeBlockContent ? group.includingContent() : group.excludingContent()
for instr in instructions {
rewireOrKeepInputs(of: instr)
}
} else {
rewireOrKeepInputs(of: instr)
}
}
// Keep the selected instruction
keep(program.code[idx], includeBlockContent: true)
while idx > 0 {
idx -= 1
let current = source[idx]
if !requiredInputs.isDisjoint(with: current.allOutputs) {
let onlyNeedsInnerOutputs = requiredInputs.isDisjoint(with: current.outputs)
// If we only need inner outputs (e.g. function parameters), then we don't include
// the block's content in the slice. Otherwise we do.
keep(current, includeBlockContent: !onlyNeedsInnerOutputs)
}
// If we perform a potentially mutating operation (such as a property store or a method call)
// on a required variable, then we may decide to keep that instruction as well.
if mode == .conservative || (mode == .aggressive && probability(0.5)) {
if current.mayMutate(requiredInputs) {
keep(current, includeBlockContent: false)
}
}
}
for instr in source {
if requiredInstructions.contains(instr.index) {
adopt(instr, keepTypes: true)
}
}
endAdoption()
trace("End of splice")
}
func splice(from program: Program) {
// Pick a starting instruction from the selected program.
// For that, prefer dataflow "sinks" whose outputs are not used for anything else,
// as these are probably the most interesting instructions.
var idx = 0
var counter = 0
repeat {
counter += 1
idx = Int.random(in: 0..<program.size)
// Some instructions are less suited to be the start of a splice. Skip them.
} while counter < 25 && (program.code[idx].isJump || program.code[idx].isBlockEnd || program.code[idx].isPrimitive || program.code[idx].isLiteral)
splice(from: program, at: idx)
}
private var openFunctions = [Variable]()
private func callLikelyRecurses(function: Variable) -> Bool {
return openFunctions.contains(function)
}
/// Executes a code generator.
///
/// - Parameter generators: The code generator to run at the current position.
/// - Returns: the number of instructions added by all generators.
public func run(_ generator: CodeGenerator, recursiveCodegenBudget: Int? = nil) {
assert(generator.requiredContext.isSubset(of: context))
if let budget = recursiveCodegenBudget {
currentCodegenBudget = budget
}
var inputs: [Variable] = []
for type in generator.inputTypes {
guard let val = randVar(ofType: type) else { return }
// In conservative mode, attempt to prevent direct recursion to reduce the number of timeouts
// This is a very crude mechanism. It might be worth implementing a more sophisticated one.
if mode == .conservative && type.Is(.function()) && callLikelyRecurses(function: val) { return }
inputs.append(val)
}
self.trace("Executing code generator \(generator.name)")
generator.run(in: self, with: inputs)
self.trace("Code generator finished")
}
private func generateInternal() {
assert(!fuzzer.corpus.isEmpty)
while currentCodegenBudget > 0 {
// There are two modes of code generation:
// 1. Splice code from another program in the corpus
// 2. Pick a CodeGenerator, find or generate matching variables, and execute it
assert(performSplicingDuringCodeGeneration || hasVisibleVariables)
withEqualProbability({
guard self.performSplicingDuringCodeGeneration else { return }
let program = self.fuzzer.corpus.randomElement(increaseAge: false)
self.splice(from: program)
}, {
// We can't run code generators if we don't have any visible variables.
guard self.scopeAnalyzer.visibleVariables.count > 0 else { return }
let generator = self.fuzzer.codeGenerators.randomElement()
if generator.requiredContext.isSubset(of: self.context) {
self.run(generator)
}
})
// This effectively limits the size of recursively generated code fragments.
if probability(0.25) {
return
}
}
}
/// Generates random code at the current position.
///
/// Code generation involves executing the configured code generators as well as splicing code from other
/// programs in the corpus into the current one.
public func generate(n: Int = 1) {
currentCodegenBudget = n
while currentCodegenBudget > 0 {
generateInternal()
}
}
/// Called by a code generator to generate more additional code, for example inside a newly created block.
public func generateRecursive() {
// Generate at least one instruction, even if already below budget
if currentCodegenBudget <= 0 {
currentCodegenBudget = 1
}
generateInternal()
}
//
// Low-level instruction constructors.
//
// These create an instruction with the provided values and append it to the program at the current position.
// If the instruction produces a new variable, that variable is returned to the caller.
// Each class implementing the Operation protocol will have a constructor here.
//
@discardableResult
private func perform(_ op: Operation, withInputs inputs: [Variable] = []) -> Instruction {
var inouts = inputs
for _ in 0..<op.numOutputs {
inouts.append(nextVariable())
}
for _ in 0..<op.numInnerOutputs {
inouts.append(nextVariable())
}
let instr = Instruction(op, inouts: inouts)
internalAppend(instr)
return instr
}
@discardableResult
public func loadInt(_ value: Int64) -> Variable {
return perform(LoadInteger(value: value)).output
}
@discardableResult
public func loadBigInt(_ value: Int64) -> Variable {
return perform(LoadBigInt(value: value)).output
}
@discardableResult
public func loadFloat(_ value: Double) -> Variable {
return perform(LoadFloat(value: value)).output
}
@discardableResult
public func loadString(_ value: String) -> Variable {
return perform(LoadString(value: value)).output
}
@discardableResult
public func loadBool(_ value: Bool) -> Variable {
return perform(LoadBoolean(value: value)).output
}
@discardableResult
public func loadUndefined() -> Variable {
return perform(LoadUndefined()).output
}
@discardableResult
public func loadNull() -> Variable {
return perform(LoadNull()).output
}
@discardableResult
public func loadRegExp(_ value: String, _ flags: RegExpFlags) -> Variable {
return perform(LoadRegExp(value: value, flags: flags)).output
}
@discardableResult
public func createObject(with initialProperties: [String: Variable]) -> Variable {
// CreateObject expects sorted property names
var propertyNames = [String](), propertyValues = [Variable]()
for (k, v) in initialProperties.sorted(by: { $0.key < $1.key }) {
propertyNames.append(k)
propertyValues.append(v)
}
return perform(CreateObject(propertyNames: propertyNames), withInputs: propertyValues).output
}
@discardableResult
public func createArray(with initialValues: [Variable]) -> Variable {
return perform(CreateArray(numInitialValues: initialValues.count), withInputs: initialValues).output
}
@discardableResult
public func createObject(with initialProperties: [String: Variable], andSpreading spreads: [Variable]) -> Variable {
// CreateObjectWithgSpread expects sorted property names
var propertyNames = [String](), propertyValues = [Variable]()
for (k, v) in initialProperties.sorted(by: { $0.key < $1.key }) {
propertyNames.append(k)
propertyValues.append(v)
}
return perform(CreateObjectWithSpread(propertyNames: propertyNames, numSpreads: spreads.count), withInputs: propertyValues + spreads).output
}
@discardableResult
public func createArray(with initialValues: [Variable], spreading spreads: [Bool]) -> Variable {
return perform(CreateArrayWithSpread(numInitialValues: initialValues.count, spreads: spreads), withInputs: initialValues).output
}
@discardableResult
public func loadBuiltin(_ name: String) -> Variable {
return perform(LoadBuiltin(builtinName: name)).output
}
@discardableResult
public func loadProperty(_ name: String, of object: Variable) -> Variable {
return perform(LoadProperty(propertyName: name), withInputs: [object]).output
}
public func storeProperty(_ value: Variable, as name: String, on object: Variable) {
perform(StoreProperty(propertyName: name), withInputs: [object, value])
}
public func deleteProperty(_ name: String, of object: Variable) {
perform(DeleteProperty(propertyName: name), withInputs: [object])
}
@discardableResult
public func loadElement(_ index: Int64, of array: Variable) -> Variable {
return perform(LoadElement(index: index), withInputs: [array]).output
}
public func storeElement(_ value: Variable, at index: Int64, of array: Variable) {
perform(StoreElement(index: index), withInputs: [array, value])
}
public func deleteElement(_ index: Int64, of array: Variable) {
perform(DeleteElement(index: index), withInputs: [array])
}
@discardableResult
public func loadComputedProperty(_ name: Variable, of object: Variable) -> Variable {
return perform(LoadComputedProperty(), withInputs: [object, name]).output
}
public func storeComputedProperty(_ value: Variable, as name: Variable, on object: Variable) {
perform(StoreComputedProperty(), withInputs: [object, name, value])
}
public func deleteComputedProperty(_ name: Variable, of object: Variable) {
perform(DeleteComputedProperty(), withInputs: [object, name])
}
@discardableResult
public func doTypeof(_ v: Variable) -> Variable {
return perform(TypeOf(), withInputs: [v]).output
}
@discardableResult
public func doInstanceOf(_ v: Variable, _ type: Variable) -> Variable {
return perform(InstanceOf(), withInputs: [v, type]).output
}
@discardableResult
public func doIn(_ prop: Variable, _ obj: Variable) -> Variable {
return perform(In(), withInputs: [prop, obj]).output
}
@discardableResult
public func definePlainFunction(withSignature signature: FunctionSignature, _ body: ([Variable]) -> ()) -> Variable {
let instr = perform(BeginPlainFunctionDefinition(signature: signature))
body(Array(instr.innerOutputs))
perform(EndPlainFunctionDefinition())
return instr.output
}
@discardableResult
public func defineStrictFunction(withSignature signature: FunctionSignature, _ body: ([Variable]) -> ()) -> Variable {
let instr = perform(BeginStrictFunctionDefinition(signature: signature))
body(Array(instr.innerOutputs))
perform(EndStrictFunctionDefinition())
return instr.output
}