forked from googleprojectzero/fuzzilli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCorpus.swift
124 lines (103 loc) · 4.08 KB
/
Corpus.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.
/// Corpus for mutation-based fuzzing.
///
/// The corpus contains FuzzIL programs that can be used as input for mutations.
/// Any newly found interesting program is added to the corpus.
/// Programs are evicted from the copus for two reasons:
///
/// - if the corpus grows too large (larger than maxCorpusSize), in which
/// case the oldest programs are removed.
/// - if a program has been mutated often enough (at least
/// minMutationsPerSample times).
///
/// However, once reached, the corpus will never shrink below minCorpusSize again.
public class Corpus: ComponentBase {
/// The minimum number of samples that should be kept in the corpus.
private let minSize: Int
/// The minimum number of times that a sample from the corpus was used
/// for mutation before it can be discarded from the active set.
private let minMutationsPerSample: Int
/// The current set of interesting programs used for mutations.
private var programs: RingBuffer<Program>
private var ages: RingBuffer<Int>
public init(minSize: Int, maxSize: Int, minMutationsPerSample: Int) {
assert(maxSize >= minSize)
self.minSize = minSize
self.minMutationsPerSample = minMutationsPerSample
self.programs = RingBuffer(maxSize: maxSize)
self.ages = RingBuffer(maxSize: maxSize)
super.init(name: "Corpus")
}
override func initialize() {
// Add interesting samples to the corpus
fuzzer.events.InterestingProgramFound.observe { event in
self.add(event.program)
}
// Schedule a timer to perform cleanup regularly
fuzzer.timers.scheduleTask(every: 30 * Minutes, cleanup)
}
public var size: Int {
return programs.count
}
public var isEmpty: Bool {
return size == 0
}
/// Adds a program to the corpus.
public func add(_ program: Program) {
if program.size > 0 {
programs.append(program)
ages.append(0)
}
}
/// Adds multiple programs to the corpus.
public func add(_ programs: [Program]) {
programs.forEach(add)
}
/// Returns a random program from this corpus and potentially increases its age by one.
public func randomElement(increaseAge: Bool = true) -> Program {
let idx = Int.random(in: 0..<programs.count)
if increaseAge {
ages[idx] += 1
}
let program = programs[idx]
assert(!program.isEmpty)
return program
}
public func exportState() -> [Program] {
return [Program](programs)
}
public func importState(_ state: [Program]) throws {
guard state.count > 0 else {
throw RuntimeError("Cannot import an empty corpus.")
}
self.programs.removeAll()
self.ages.removeAll()
state.forEach(add)
}
private func cleanup() {
var newPrograms = RingBuffer<Program>(maxSize: programs.maxSize)
var newAges = RingBuffer<Int>(maxSize: ages.maxSize)
for i in 0..<programs.count {
let remaining = programs.count - i
if ages[i] < minMutationsPerSample || remaining <= (minSize - newPrograms.count) {
newPrograms.append(programs[i])
newAges.append(ages[i])
}
}
logger.info("Corpus cleanup finished: \(self.programs.count) -> \(newPrograms.count)")
programs = newPrograms
ages = newAges
}
}