forked from googleprojectzero/fuzzilli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.swift
283 lines (242 loc) · 11.3 KB
/
main.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
// 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 Fuzzilli
//
// Process commandline arguments.
//
let args = Arguments.parse(from: CommandLine.arguments)
if args["-h"] != nil || args["--help"] != nil || args.numPositionalArguments != 1 {
print("""
Usage:
\(args.programName) [options] --profile=<profile> /path/to/jsshell
Options:
--profile=name : Select one of several preconfigured profiles.
Available profiles: \(profiles.keys).
--logLevel=level : The log level to use. Valid values: "verbose", info", "warning", "error", "fatal"
(default: "info").
--numIterations=n : Run for the specified number of iterations (default: unlimited).
--timeout=n : Timeout in ms after which to interrupt execution of programs (default: 250).
--minMutationsPerSample=n : Discard samples from the corpus after they have been mutated at least this
many times (default: 16).
--minCorpusSize=n : Keep at least this many samples in the corpus regardless of the number of times
they have been mutated (default: 1024).
--maxCorpusSize=n : Only allow the corpus to grow to this many samples. Otherwise the oldest samples
will be discarded (default: unlimited).
--consecutiveMutations=n : Perform this many consecutive mutations on each sample (default: 5).
--minimizationLimit=n : When minimizing corpus samples, keep at least this many instructions in the
program. See Minimizer.swift for an overview of this feature (default: 0).
--storagePath=path : Path at which to store runtime files (crashes, corpus, etc.) to.
--exportStatistics : If enabled, fuzzing statistics will be collected and saved to disk every 10 minutes.
Requires --storagePath.
--exportState : If enabled, the internal state of the fuzzer will be writen to disk every
6 hours. Requires --storagePath.
--importState=path : Import a previously exported fuzzer state and resuming fuzzing from it.
--networkMaster=host:port : Run as master and accept connections from workers over the network. Note: it is
*highly* recommended to run network fuzzers in an isolated network!
--networkWorker=host:port : Run as worker and connect to the specified master instance.
--dontFuzz : If used, this instace will not perform fuzzing. Can be useful for master instances.
--noAbstractInterpretation : Disable abstract interpretation of FuzzIL programs during fuzzing. See
Configuration.swift for more details.
""")
exit(0)
}
let jsShellPath = args[0]
if !FileManager.default.fileExists(atPath: jsShellPath) {
print("Invalid JS shell path \"\(jsShellPath)\", file does not exist")
exit(-1)
}
var profile: Profile! = nil
if let val = args["--profile"], let p = profiles[val] {
profile = p
}
if profile == nil {
print("Please provide a valid profile with --profile=profile_name. Available profiles: \(profiles.keys)")
exit(-1)
}
let logLevelName = args["--logLevel"] ?? "info"
let numIterations = args.int(for: "--numIterations") ?? -1
let timeout = args.int(for: "--timeout") ?? 250
let minMutationsPerSample = args.int(for: "--minMutationsPerSample") ?? 16
let minCorpusSize = args.int(for: "--minCorpusSize") ?? 1024
let maxCorpusSize = args.int(for: "--maxCorpusSize") ?? Int.max
let consecutiveMutations = args.int(for: "--consecutiveMutations") ?? 5
let minimizationLimit = args.uint(for: "--minimizationLimit") ?? 0
let storagePath = args["--storagePath"]
let exportStatistics = args.has("--exportStatistics")
let exportState = args.has("--exportState")
let stateImportFile = args["--importState"]
let disableAbstractInterpreter = args.has("--noAbstractInterpretation")
let dontFuzz = args.has("--dontFuzz")
let logLevelByName: [String: LogLevel] = ["verbose": .verbose, "info": .info, "warning": .warning, "error": .error, "fatal": .fatal]
guard let logLevel = logLevelByName[logLevelName] else {
print("Invalid log level \(logLevelName)")
exit(-1)
}
if exportState && storagePath == nil {
print("--exportState requires --storagePath")
exit(-1)
}
if exportStatistics && storagePath == nil {
print("--exportStatistics requires --storagePath")
exit(-1)
}
if maxCorpusSize < minCorpusSize {
print("--maxCorpusSize must be larger than --minCorpusSize")
exit(-1)
}
var networkMasterParams: (String, UInt16)? = nil
if let val = args["--networkMaster"] {
if let params = parseHostPort(val) {
networkMasterParams = params
} else {
print("Argument --networkMaster must be of the form \"host:port\"")
exit(-1)
}
}
var networkWorkerParams: (String, UInt16)? = nil
if let val = args["--networkWorker"] {
if let params = parseHostPort(val) {
networkWorkerParams = params
} else {
print("Argument --networkWorker must be of the form \"host:port\"")
exit(-1)
}
}
// Make it easy to detect typos etc. in command line arguments
if args.unusedOptionals.count > 0 {
print("Invalid arguments: \(args.unusedOptionals)")
exit(-1)
}
//
// Construct a fuzzer instance.
//
// The configuration of this fuzzer.
let config = Configuration(timeout: UInt32(timeout),
logLevel: logLevel,
crashTests: profile.crashTests,
isMaster: networkMasterParams != nil,
isWorker: networkWorkerParams != nil,
isFuzzing: !dontFuzz,
minimizationLimit: minimizationLimit,
useAbstractInterpretation: !disableAbstractInterpreter)
// A script runner to execute JavaScript code in an instrumented JS engine.
let runner = REPRL(executable: jsShellPath, processArguments: profile.processArguments, processEnvironment: profile.processEnv)
/// The core fuzzer responsible for mutating programs from the corpus and evaluating the outcome.
let mutators: [Mutator] = [
// Increase probability of insertion mutator as it tends to produce invalid samples more frequently.
InsertionMutator(),
InsertionMutator(),
OperationMutator(),
InputMutator(),
SpliceMutator(),
CombineMutator(),
JITStressMutator(),
]
let engine = MutationFuzzer(mutators: mutators, numConsecutiveMutations: consecutiveMutations)
// Code generators to use.
let codeGenerators = defaultCodeGenerators + profile.additionalCodeGenerators
// The evaluator to score produced samples.
let evaluator = ProgramCoverageEvaluator(runner: runner)
// The environment containing available builtins, property names, and method names.
let environment = JavaScriptEnvironment(additionalBuiltins: profile.additionalBuiltins, additionalObjectGroups: [])
// A lifter to translate FuzzIL programs to JavaScript.
let lifter = JavaScriptLifter(prefix: profile.codePrefix,
suffix: profile.codeSuffix,
inliningPolicy: InlineOnlyLiterals(),
ecmaVersion: profile.ecmaVersion)
// Corpus managing interesting programs that have been found during fuzzing.
let corpus = Corpus(minSize: minCorpusSize, maxSize: maxCorpusSize, minMutationsPerSample: minMutationsPerSample)
// Minimizer to minimize crashes and interesting programs.
let minimizer = Minimizer()
// Construct the fuzzer instance.
let fuzzer = Fuzzer(configuration: config,
scriptRunner: runner,
engine: engine,
codeGenerators: codeGenerators,
evaluator: evaluator,
environment: environment,
lifter: lifter,
corpus: corpus,
minimizer: minimizer)
// Create a "UI". We do this now, before fuzzer initialization, so
// we are able to print log messages generated during initialization.
let ui = TerminalUI(for: fuzzer)
// Remaining fuzzer initialization must happen on the fuzzer's dispatch queue.
fuzzer.sync {
let logger = fuzzer.makeLogger(withLabel: "Cli")
// Always want some statistics.
fuzzer.addModule(Statistics())
// Store samples to disk if requested.
if let path = storagePath {
fuzzer.addModule(Storage(for: fuzzer,
storageDir: path,
stateExportInterval: exportState ? 6 * Hours : nil,
statisticsExportInterval: exportStatistics ? 10 * Minutes : nil
))
}
// Synchronize over the network if requested.
if let (listenHost, listenPort) = networkMasterParams {
fuzzer.addModule(NetworkMaster(for: fuzzer, address: listenHost, port: listenPort))
}
if let (masterHost, masterPort) = networkWorkerParams {
fuzzer.addModule(NetworkWorker(for: fuzzer, hostname: masterHost, port: masterPort))
}
// Check for potential misconfiguration.
if !config.isWorker && storagePath == nil {
logger.warning("No filesystem storage configured, found crashes will be discarded!")
}
// Initialize the fuzzer.
fuzzer.initialize()
// Import a previously exported state if requested.
if let path = stateImportFile {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path))
try fuzzer.importState(from: data)
logger.info("Successfully imported previous state. Corpus now contains \(fuzzer.corpus.size) elements")
} catch {
logger.fatal("Failed to import state: \(error)")
}
}
// And start fuzzing.
fuzzer.start(runFor: numIterations)
// Exit this process when the fuzzer stops.
fuzzer.registerEventListener(for: fuzzer.events.ShutdownComplete) {
exit(0)
}
}
// Install signal handlers to terminate the fuzzer gracefully.
var signalSources: [DispatchSourceSignal] = []
for sig in [SIGINT, SIGTERM] {
// Seems like we need this so the dispatch sources work correctly?
signal(sig, SIG_IGN)
let source = DispatchSource.makeSignalSource(signal: sig, queue: DispatchQueue.main)
source.setEventHandler {
fuzzer.async {
fuzzer.stop()
}
}
source.activate()
signalSources.append(source)
}
// Install signal handler for SIGUSR1 to print the next program that is generated.
signal(SIGUSR1, SIG_IGN)
let source = DispatchSource.makeSignalSource(signal: SIGUSR1, queue: DispatchQueue.main)
source.setEventHandler {
ui.printNextGeneratedProgram = true
}
source.activate()
signalSources.append(source)
// Start dispatching tasks on the main queue.
RunLoop.main.run()