forked from googleprojectzero/fuzzilli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgramCoverageEvaluator.swift
159 lines (131 loc) · 5.8 KB
/
ProgramCoverageEvaluator.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
// 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
import libcoverage
class CovEdgeSet: ProgramAspects {
let count: UInt64
let edges: UnsafeMutablePointer<UInt32>?
init(edges: UnsafeMutablePointer<UInt32>?, count: UInt64) {
self.count = count
self.edges = edges
super.init(outcome: .succeeded)
}
deinit {
free(edges)
}
}
public class ProgramCoverageEvaluator: ComponentBase, ProgramEvaluator {
/// Counts the number of instances. Used to create unique shared memory regions in every instance.
private static var instances = 0
/// The current edge coverage percentage.
public var currentScore: Double {
return Double(context.found_edges) / Double(context.num_edges)
}
/// Context for the C library.
private var context = libcoverage.cov_context()
public init(runner: ScriptRunner) {
super.init(name: "Coverage")
let id = ProgramCoverageEvaluator.instances
ProgramCoverageEvaluator.instances += 1
context.id = Int32(id)
guard libcoverage.cov_initialize(&context) == 0 else {
fatalError("Could not initialize libcoverage")
}
runner.setEnvironmentVariable("SHM_ID", to: "shm_id_\(getpid())_\(id)")
}
override func initialize() {
// Must clear the shared memory bitmap before every execution
fuzzer.events.PreExecute.observe { execution in
libcoverage.cov_clear_bitmap(&self.context)
}
// Unlink the shared memory regions on shutdown
fuzzer.events.Shutdown.observe {
libcoverage.cov_shutdown(&self.context)
}
let _ = fuzzer.execute(Program())
libcoverage.cov_finish_initialization(&context)
logger.info("Initialized, \(context.num_edges) edges")
}
public func evaluate(_ execution: Execution) -> ProgramAspects? {
assert(execution.outcome == .succeeded)
var edgeSet = libcoverage.edge_set();
let result = libcoverage.cov_evaluate(&context, &edgeSet)
if result == -1 {
logger.error("Could not evaluate sample")
}
if result == 1 {
return CovEdgeSet(edges: edgeSet.edges, count: edgeSet.count)
} else {
assert(edgeSet.edges == nil && edgeSet.count == 0)
return nil
}
}
public func evaluateCrash(_ execution: Execution) -> ProgramAspects? {
assert(execution.outcome == .crashed)
let result = libcoverage.cov_evaluate_crash(&context)
if result == -1 {
logger.error("Could not evaluate crash")
}
if result == 1 {
// For minimization of crashes we only care about the outcome, not the edges covered.
return ProgramAspects(outcome: .crashed)
} else {
return nil
}
}
public func hasAspects(_ execution: Execution, _ aspects: ProgramAspects) -> Bool {
guard execution.outcome == aspects.outcome else {
return false
}
if let edgeSet = aspects as? CovEdgeSet {
let result = libcoverage.cov_compare_equal(&context, edgeSet.edges, edgeSet.count)
if result == -1 {
logger.error("Could not compare progam executions")
}
return result == 1
} else {
return true
}
}
public func exportState() -> Data {
var state = Data()
state.append(Data(bytes: &context.num_edges, count: 8))
state.append(Data(bytes: &context.bitmap_size, count: 8))
state.append(Data(bytes: &context.found_edges, count: 8))
state.append(context.virgin_bits, count: Int(context.bitmap_size))
state.append(context.crash_bits, count: Int(context.bitmap_size))
return state
}
public func importState(_ state: Data) throws {
assert(isInitialized)
guard state.count == 24 + context.bitmap_size * 2 else {
throw RuntimeError("Cannot import coverage state as it has an unexpected size. Ensure all instances use the same build of the target")
}
let numEdges = state.withUnsafeBytes { $0.load(fromByteOffset: 0, as: UInt64.self) }
let bitmapSize = state.withUnsafeBytes { $0.load(fromByteOffset: 8, as: UInt64.self) }
let foundEdges = state.withUnsafeBytes { $0.load(fromByteOffset: 16, as: UInt64.self) }
guard bitmapSize == context.bitmap_size && numEdges == context.num_edges else {
throw RuntimeError("Cannot import coverage state due to different bitmap sizes. Ensure all instances use the same build of the target")
}
if foundEdges < context.found_edges {
return logger.info("Not importing coverage state as it has less found edges than ours")
}
context.found_edges = foundEdges
var start = state.startIndex + 24
state.copyBytes(to: context.virgin_bits, from: start..<start + Int(bitmapSize))
start += Int(bitmapSize)
state.copyBytes(to: context.crash_bits, from: start..<start + Int(bitmapSize))
logger.info("Imported existing coverage state with \(foundEdges) edges already discovered")
}
}