-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathStatistics.swift
More file actions
400 lines (350 loc) · 19.1 KB
/
Copy pathStatistics.swift
File metadata and controls
400 lines (350 loc) · 19.1 KB
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
// 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 Foundation
public class Statistics: Module {
/// The fuzzer instance this module belongs to.
private var fuzzer: Fuzzer?
/// The data just for this instance.
private var ownData = Fuzzilli_Protobuf_Statistics()
/// Logger used to print some internal statistics in regular intervals.
private let logger = Logger(withLabel: "Statistics")
/// Data required to compute executions per second.
private var currentExecs = 0.0
private var lastEpsUpdate = Date()
private var lastExecsPerSecond = 0.0
/// Data required to compute the fuzzer overhead (i.e. the fraction of the total time that is not spent executing generated programs in the target engine).
/// This includes time required for node synchronization, to mutate/generate a program, to lift it, to restart the target process after crashes/timeouts, etc.
private var fuzzerOverheadAvg = MovingAverage(n: 1000)
private var lastPreExecDate = Date()
private var lastExecDate = Date()
/// Data required to compute the minimization overhead (i.e. the fraction of executions spent on minimization).
/// Since we may easily spent hundreds of executions on a single minimization task, the context window here is larger than the other ones.
private var minimizationOverheadAvg = MovingAverage(n: 10000)
/// Current corpus size. Updated when new samples are added to the corpus.
private var corpusSize = 0
/// Moving average to keep track of average program size.
private var programSizeAvg = MovingAverage(n: 1000)
/// Moving average to keep track of average program size in the corpus.
/// Only computed locally, not across multiple nodes.
private var corpusProgramSizeAvg = MovingAverage(n: 1000)
/// Moving average to keep track of the average execution time of recently generated programs.
/// This is only computed for successful executions, and so excludes e.g. samples that timed out.
private var executionTimeAvg = MovingAverage(n: 1000)
/// Moving average of the number of valid programs in the last 1000 generated programs.
private var correctnessRate = MovingAverage(n: 1000)
/// Moving average of the number of timeouts in the last 1000 generated programs.
private var timeoutRate = MovingAverage(n: 1000)
/// All data from connected nodes.
private var nodes = [UUID: Fuzzilli_Protobuf_Statistics]()
/// The IDs of nodes that are currently inactive.
private var inactiveNodes = Set<UUID>()
public init() {}
public static func percentageOrNa(_ percentage: Double?, _ padding: Int) -> String {
return if let percentage = percentage {
String(format: "%.2f%%", percentage * 100).leftPadded(toLength: padding)
} else {
"N/A".leftPadded(toLength: padding)
}
}
/// Computes and returns the statistical data for this instance and all connected nodes.
public func compute() -> Fuzzilli_Protobuf_Statistics {
assert(nodes.count - inactiveNodes.count == ownData.numChildNodes)
// Update local statistics data
ownData.avgCorpusSize = Double(corpusSize)
ownData.avgProgramSize = programSizeAvg.currentValue
ownData.avgCorpusProgramSize = corpusProgramSizeAvg.currentValue
ownData.avgExecutionTime = executionTimeAvg.currentValue
ownData.fuzzerOverhead = fuzzerOverheadAvg.currentValue
ownData.minimizationOverhead = minimizationOverheadAvg.currentValue
ownData.correctnessRate = correctnessRate.currentValue
ownData.timeoutRate = timeoutRate.currentValue
if let fuzzer = fuzzer {
let contributorsList: [(prefix: String?, list: [Contributor], isCodeGenerator: Bool)] =
fuzzer.codeGenerators.map { ($0.parts.count > 1 ? $0.name : nil, $0.parts, true) }
+ [
(nil, Array(fuzzer.programTemplates), false),
(nil, Array(fuzzer.mutators), false),
]
ownData.contributorStats = contributorsList.flatMap { (prefix, list, isCodeGenerator) in
list.map { contributor in
Fuzzilli_Protobuf_Statistics.ContributorStats.with {
$0.name = "\(prefix.flatMap { "\($0)/" } ?? "")\(contributor.name)"
$0.invocationCount = UInt64(contributor.invocationCount)
$0.successfulGenerationCount = UInt64(contributor.successfulGenerationCount)
$0.totalSamples = UInt64(contributor.totalSamples)
$0.correctSamples = UInt64(contributor.correctSamples)
$0.isCodeGenerator = isCodeGenerator
$0.interestingSamples = UInt64(contributor.interestingSamples)
$0.invalidSamples = UInt64(contributor.invalidSamples)
$0.timedOutSamples = UInt64(contributor.timedOutSamples)
$0.crashingSamples = UInt64(contributor.crashingSamples)
$0.differentialSamples = UInt64(contributor.differentialSamples)
$0.failures = UInt64(contributor.failures)
$0.totalInstructionsProduced = UInt64(contributor.totalInstructionsProduced)
}
}
}
}
// Compute global statistics data
var data = ownData
var contributorStatsByName = Dictionary(
uniqueKeysWithValues: ownData.contributorStats.map { ($0.name, $0) }
)
for (id, node) in nodes {
// Add "global" fields, even from nodes that are no longer active
data.totalSamples += node.totalSamples
data.validSamples += node.validSamples
data.timedOutSamples += node.timedOutSamples
data.totalExecs += node.totalExecs
for stats in node.contributorStats {
if var existing = contributorStatsByName[stats.name] {
assert(existing.isCodeGenerator == stats.isCodeGenerator)
existing.invocationCount += stats.invocationCount
existing.successfulGenerationCount += stats.successfulGenerationCount
existing.totalSamples += stats.totalSamples
existing.correctSamples += stats.correctSamples
existing.interestingSamples += stats.interestingSamples
existing.invalidSamples += stats.invalidSamples
existing.timedOutSamples += stats.timedOutSamples
existing.crashingSamples += stats.crashingSamples
existing.differentialSamples += stats.differentialSamples
existing.failures += stats.failures
existing.totalInstructionsProduced += stats.totalInstructionsProduced
contributorStatsByName[stats.name] = existing
} else {
contributorStatsByName[stats.name] = stats
}
}
if !inactiveNodes.contains(id) {
// Add fields that only have meaning for active nodes
// For computing averages, we first multiply each average value with the number of nodes over which
// it was computed, then divide it by the total number of active nodes.
let numNodesRepresentedByData = Double(node.numChildNodes + 1)
data.numChildNodes += node.numChildNodes
data.avgCorpusSize += node.avgCorpusSize * numNodesRepresentedByData
data.avgProgramSize += node.avgProgramSize * numNodesRepresentedByData
data.avgCorpusProgramSize += node.avgCorpusProgramSize * numNodesRepresentedByData
data.avgExecutionTime += node.avgExecutionTime * numNodesRepresentedByData
data.execsPerSecond += node.execsPerSecond
data.fuzzerOverhead += node.fuzzerOverhead * numNodesRepresentedByData
data.minimizationOverhead += node.minimizationOverhead * numNodesRepresentedByData
data.correctnessRate += node.correctnessRate * numNodesRepresentedByData
data.timeoutRate += node.timeoutRate * numNodesRepresentedByData
}
// All other fields are already indirectly synchronized (e.g. number of interesting samples founds)
}
data.contributorStats = Array(contributorStatsByName.values)
// Divide each average by the toal number of nodes. See above.
let totalNumberOfNodes = Double(data.numChildNodes + 1)
data.avgCorpusSize /= totalNumberOfNodes
data.avgProgramSize /= totalNumberOfNodes
data.avgCorpusProgramSize /= totalNumberOfNodes
data.avgExecutionTime /= totalNumberOfNodes
data.fuzzerOverhead /= totalNumberOfNodes
data.minimizationOverhead /= totalNumberOfNodes
data.correctnessRate /= totalNumberOfNodes
data.timeoutRate /= totalNumberOfNodes
return data
}
public func initialize(with fuzzer: Fuzzer) {
self.fuzzer = fuzzer
fuzzer.registerEventListener(for: fuzzer.events.CrashFound) { _ in
self.ownData.crashingSamples += 1
}
fuzzer.registerEventListener(for: fuzzer.events.DifferentialFound) { _ in
self.ownData.differentialSamples += 1
}
fuzzer.registerEventListener(for: fuzzer.events.TimeOutFound) { _ in
self.ownData.timedOutSamples += 1
self.correctnessRate.add(0.0)
self.timeoutRate.add(1.0)
}
fuzzer.registerEventListener(for: fuzzer.events.InvalidProgramFound) { _ in
self.correctnessRate.add(0.0)
self.timeoutRate.add(0.0)
}
fuzzer.registerEventListener(for: fuzzer.events.ValidProgramFound) { _ in
self.ownData.validSamples += 1
self.correctnessRate.add(1.0)
self.timeoutRate.add(0.0)
}
fuzzer.registerEventListener(for: fuzzer.events.PreExecute) { (program, purpose) in
// Currently we only care about the fraction of executions spent on
// minimization, but we could extend this to get a detailed breakdown
// of exactly what our executions are spent on.
if purpose == .minimization {
self.minimizationOverheadAvg.add(1)
} else {
self.minimizationOverheadAvg.add(0)
}
}
fuzzer.registerEventListener(for: fuzzer.events.PostExecute) { exec in
self.ownData.totalExecs += 1
self.currentExecs += 1
if exec.outcome == .succeeded {
self.executionTimeAvg.add(exec.execTime)
}
let now = Date()
let totalTime = now.timeIntervalSince(self.lastExecDate)
self.lastExecDate = now
let overhead = 1.0 - (exec.execTime / totalTime)
self.fuzzerOverheadAvg.add(overhead)
}
fuzzer.registerEventListener(for: fuzzer.events.InterestingProgramFound) { ev in
self.ownData.interestingSamples += 1
self.ownData.coverage = fuzzer.evaluator.currentScore
self.corpusProgramSizeAvg.add(ev.program.size)
self.corpusSize = fuzzer.corpus.size
}
fuzzer.registerEventListener(for: fuzzer.events.ProgramGenerated) { program in
self.ownData.totalSamples += 1
self.programSizeAvg.add(program.size)
}
fuzzer.registerEventListener(for: fuzzer.events.ChildNodeConnected) { id in
self.ownData.numChildNodes += 1
self.nodes[id] = Fuzzilli_Protobuf_Statistics()
self.inactiveNodes.remove(id)
}
fuzzer.registerEventListener(for: fuzzer.events.ChildNodeDisconnected) { id in
self.ownData.numChildNodes -= 1
self.inactiveNodes.insert(id)
}
fuzzer.timers.scheduleTask(every: 30 * Seconds) {
let now = Date()
let interval = Double(now.timeIntervalSince(self.lastEpsUpdate))
guard interval >= 1.0 else {
return // This can happen due to delays in queue processing
}
let execsPerSecond = self.currentExecs / interval
self.ownData.execsPerSecond += execsPerSecond - self.lastExecsPerSecond
self.lastExecsPerSecond = execsPerSecond
self.lastEpsUpdate = now
self.currentExecs = 0.0
}
// Also schedule timers to print internal statistics in regular intervals.
if fuzzer.config.logLevel.isAtLeast(.info) {
fuzzer.timers.scheduleTask(every: 15 * Minutes) {
self.logger.info("Mutator Statistics:")
let globalStats = self.compute()
let statsByName = Dictionary(
uniqueKeysWithValues: globalStats.contributorStats.map { ($0.name, $0) }
)
let nameMaxLength = fuzzer.mutators.map({ $0.name.count }).max()!
let maxSamplesGeneratedStringLength = fuzzer.mutators.map({
let total = statsByName[$0.name]?.totalSamples ?? 0
return String(total).count
}).max()!
for mutator in fuzzer.mutators {
let stats =
statsByName[mutator.name]
?? Fuzzilli_Protobuf_Statistics.ContributorStats.with {
$0.name = mutator.name
}
let name = mutator.name.rightPadded(toLength: nameMaxLength)
let correctnessRate = Self.percentageOrNa(stats.correctnessRate, 7)
let failureRate = Self.percentageOrNa(stats.failureRate, 7)
let timeoutRate = Self.percentageOrNa(stats.timeoutRate, 6)
let interestingSamplesRate = Self.percentageOrNa(
stats.interestingSamplesRate, 7)
let avgInstructionsAdded = String(
format: "%.2f", stats.avgNumberOfInstructionsGenerated
).leftPadded(toLength: 5)
let samplesGenerated = String(stats.totalSamples).leftPadded(
toLength: maxSamplesGeneratedStringLength)
let crashesFound = stats.crashesFound
self.logger.info(
" \(name) : Correctness rate: \(correctnessRate), Failure rate: \(failureRate), Interesting sample rate: \(interestingSamplesRate), Timeout rate: \(timeoutRate), Avg. # of instructions added: \(avgInstructionsAdded), Total # of generated samples: \(samplesGenerated), Total # of crashes found: \(crashesFound)"
)
}
}
}
if fuzzer.config.logLevel.isAtLeast(.verbose) {
fuzzer.timers.scheduleTask(every: 30 * Minutes) {
self.logger.verbose("Code Generator Statistics:")
let globalStats = self.compute()
let statsByName = Dictionary(
uniqueKeysWithValues: globalStats.contributorStats.map { ($0.name, $0) }
)
let nameMaxLength = fuzzer.codeGenerators.map({ $0.name.count }).max()!
for generator in fuzzer.codeGenerators {
for stub in generator.parts {
let name =
generator.parts.count > 1 ? "\(generator.name)/\(stub.name)" : stub.name
let stats =
statsByName[name]
?? Fuzzilli_Protobuf_Statistics.ContributorStats.with { $0.name = name }
let namePadded = name.rightPadded(toLength: nameMaxLength)
let correctnessRate = Self.percentageOrNa(stats.correctnessRate, 7)
let interestingSamplesRate = Self.percentageOrNa(
stats.interestingSamplesRate, 7)
let timeoutRate = Self.percentageOrNa(stats.timeoutRate, 6)
let avgInstructionsAdded = String(
format: "%.2f", stats.avgNumberOfInstructionsGenerated
).leftPadded(toLength: 5)
let invocationSuccessRate = Self.percentageOrNa(
stats.invocationSuccessRate, 7)
let samplesGenerated = stats.totalSamples
self.logger.verbose(
" \(namePadded) : Invocation Success: \(invocationSuccessRate), Correctness rate: \(correctnessRate), Interesting sample rate: \(interestingSamplesRate), Timeout rate: \(timeoutRate), Avg. # of instructions added: \(avgInstructionsAdded), Total # of generated samples: \(samplesGenerated)"
)
}
}
}
}
}
/// Import statistics data from a child node.
public func importData(_ stats: Fuzzilli_Protobuf_Statistics, from child: UUID) {
nodes[child] = stats
}
}
extension Fuzzilli_Protobuf_Statistics {
/// The ratio of valid samples to produced samples over the entire runtime of the fuzzer.
public var overallCorrectnessRate: Double {
return totalSamples != 0 ? Double(validSamples) / Double(totalSamples) : 0
}
/// The ratio of timed-out samples to produced samples over the entire runtime of the fuzzer.
public var overallTimeoutRate: Double {
return totalSamples != 0 ? Double(timedOutSamples) / Double(totalSamples) : 0
}
}
extension Fuzzilli_Protobuf_Statistics.ContributorStats {
public var invocationSuccessRate: Double? {
guard invocationCount > 0 else { return nil }
return Double(successfulGenerationCount) / Double(invocationCount)
}
public var correctnessRate: Double? {
guard totalSamples > 0 else { return nil }
return Double(correctSamples) / Double(totalSamples)
}
public var interestingSamplesRate: Double? {
guard totalSamples > 0 else { return nil }
return Double(interestingSamples) / Double(totalSamples)
}
public var timeoutRate: Double? {
guard totalSamples > 0 else { return nil }
return Double(timedOutSamples) / Double(totalSamples)
}
public var failureRate: Double? {
let totalAttempts = totalSamples + failures
guard totalAttempts > 0 else { return nil }
return Double(failures) / Double(totalAttempts)
}
public var avgNumberOfInstructionsGenerated: Double {
guard totalSamples > 0 else { return 0.0 }
return Double(totalInstructionsProduced) / Double(totalSamples)
}
public var crashesFound: UInt64 {
return crashingSamples
}
}