forked from googleprojectzero/fuzzilli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFuzzEngine.swift
More file actions
105 lines (88 loc) · 3.96 KB
/
Copy pathFuzzEngine.swift
File metadata and controls
105 lines (88 loc) · 3.96 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
// Copyright 2020 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
/// Number of instructions that should be generated in a program prefix
let programPrefixSize = 5
public protocol FuzzEngine: ComponentBase {
// Performs a single round of fuzzing using the engine.
func fuzzOne(_ group: DispatchGroup)
}
extension FuzzEngine {
public func execute(_ program: Program, stats: inout ProgramGeneratorStats) -> ExecutionOutcome {
fuzzer.dispatchEvent(fuzzer.events.ProgramGenerated, data: program)
let execution = fuzzer.execute(program)
switch execution.outcome {
case .crashed(let termsig):
fuzzer.processCrash(program, withSignal: termsig, withStderr: execution.stderr, origin: .local)
case .succeeded:
fuzzer.dispatchEvent(fuzzer.events.ValidProgramFound, data: program)
if let aspects = fuzzer.evaluator.evaluate(execution) {
if fuzzer.config.inspection.contains(.history) {
program.comments.add("Program is interesting due to \(aspects)", at: .footer)
}
fuzzer.processInteresting(program, havingAspects: aspects, origin: .local)
}
stats.producedValidSample()
case .failed(_):
if fuzzer.config.enableDiagnostics {
program.comments.add("Stdout:\n" + execution.stdout, at: .footer)
}
fuzzer.dispatchEvent(fuzzer.events.InvalidProgramFound, data: program)
stats.producedInvalidSample()
case .timedOut:
fuzzer.dispatchEvent(fuzzer.events.TimeOutFound, data: program)
stats.producedInvalidSample()
}
if fuzzer.config.enableDiagnostics {
// Ensure deterministic execution behaviour. This can for example help detect and debug REPRL issues.
ensureDeterministicExecutionOutcomeForDiagnostic(of: program)
}
return execution.outcome
}
/// Generate some basic Prefix such that samples have some basic types available.
public func generateProgramPrefix() -> Program {
let b = fuzzer.makeBuilder(mode: .conservative)
for _ in 1...programPrefixSize {
let generator = chooseUniform(from: fuzzer.trivialCodeGenerators)
b.run(generator)
}
let prefixProgram = b.finalize()
fuzzer.updateTypeInformation(for: prefixProgram)
return prefixProgram
}
private func ensureDeterministicExecutionOutcomeForDiagnostic(of program: Program) {
let execution1 = fuzzer.execute(program)
let stdout1 = execution1.stdout, stderr1 = execution1.stderr
let execution2 = fuzzer.execute(program)
switch (execution1.outcome, execution2.outcome) {
case (.succeeded, .failed(_)),
(.failed(_), .succeeded):
let stdout2 = execution2.stdout, stderr2 = execution2.stderr
logger.warning("""
Non-deterministic execution detected for program
\(fuzzer.lifter.lift(program))
// Stdout of first execution
\(stdout1)
// Stderr of first execution
\(stderr1)
// Stdout of second execution
\(stdout2)
// Stderr of second execution
\(stderr2)
""")
default:
break
}
}
}