forked from googleprojectzero/fuzzilli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCorpus.swift
92 lines (76 loc) · 2.86 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
// 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.
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
/// All interesting programs ever found.
private var all: [Program]
/// The current set of interesting programs used for mutations.
private var active: [(program: Program, age: Int)]
public init(minSize: Int, minMutationsPerSample: Int) {
self.minSize = minSize
self.minMutationsPerSample = minMutationsPerSample
self.all = []
self.active = []
super.init(name: "Corpus")
}
override func initialize() {
// Add interesting samples to the corpus
addEventListener(for: fuzzer.events.InterestingProgramFound) { 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 active.count
}
public var isEmpty: Bool {
return size == 0
}
/// Exports the entire corpus as a list of programs.
public func export() -> [Program] {
return all
}
/// Adds a program to the corpus.
func add(_ program: Program) {
if program.size > 0 {
active.append((program, 0))
all.append(program)
}
}
func takeSample(count: Bool = true) -> Program {
let idx = Int.random(in: 0..<active.count)
if count {
active[idx].age += 1
}
let program = active[idx].program
assert(!program.isEmpty)
return program
}
private func cleanup() {
var newSamples = [(Program, Int)]()
for i in 0..<active.count {
let remaining = active.count - i
if active[i].age < minMutationsPerSample || remaining <= (minSize - newSamples.count) {
newSamples.append(active[i])
}
}
logger.info("Corpus cleanup finished: \(self.active.count) -> \(newSamples.count)")
active = newSamples
}
}