forked from googleprojectzero/Jackalope
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
202 lines (162 loc) · 6.85 KB
/
main.cpp
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
/*
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.
*/
#include "common.h"
#include "fuzzer.h"
#include "mutator.h"
#include "mersenne.h"
#include "mutators/grammar/grammar.h"
#include "mutators/grammar/grammarmutator.h"
#include "mutators/grammar/grammarminimizer.h"
class BinaryFuzzer : public Fuzzer {
Mutator *CreateMutator(int argc, char **argv, ThreadContext *tc) override;
bool TrackHotOffsets() override { return true; }
};
Mutator * BinaryFuzzer::CreateMutator(int argc, char **argv, ThreadContext *tc) {
bool use_deterministic_mutations = true;
if(GetBinaryOption("-server", argc, argv, false)) {
// don't do deterministic mutation if a server is specified
use_deterministic_mutations = false;
}
use_deterministic_mutations = GetBinaryOption("-deterministic_mutations",
argc, argv,
use_deterministic_mutations);
bool deterministic_only = GetBinaryOption("-deterministic_only",
argc, argv,
false);
int nrounds = GetIntOption("-iterations_per_round", argc, argv, 1000);
char* dictionary = GetOption("-dict", argc, argv);
// a pretty simple mutation strategy
PSelectMutator *pselect = new PSelectMutator();
// select one of the mutators below with corresponding
// probablilities
pselect->AddMutator(new ByteFlipMutator(), 0.8);
pselect->AddMutator(new ArithmeticMutator(), 0.2);
pselect->AddMutator(new AppendMutator(1, 128), 0.2);
pselect->AddMutator(new BlockInsertMutator(1, 128), 0.1);
pselect->AddMutator(new BlockFlipMutator(2, 16), 0.1);
pselect->AddMutator(new BlockFlipMutator(16, 64), 0.1);
pselect->AddMutator(new BlockFlipMutator(1, 64, true), 0.1);
pselect->AddMutator(new BlockDuplicateMutator(1, 128, 1, 8), 0.1);
InterestingValueMutator *iv_mutator = new InterestingValueMutator(true);
if (dictionary) iv_mutator->AddDictionary(dictionary);
pselect->AddMutator(iv_mutator, 0.1);
// SpliceMutator is not compatible with -keep_samples_in_memory=0
// as it requires other samples in memory besides the one being
// fuzzed.
if (GetBinaryOption("-keep_samples_in_memory", argc, argv, true)) {
pselect->AddMutator(new SpliceMutator(1, 0.5), 0.1);
pselect->AddMutator(new SpliceMutator(2, 0.5), 0.1);
}
Mutator* pselect_or_range = pselect;
// if we are tracking ranges, insert a RangeMutator
// between RepeatMutator and individual mutators
if (GetBinaryOption("-track_ranges", argc, argv, false)) {
RangeMutator* range_mutator = new RangeMutator(pselect);
pselect_or_range = range_mutator;
}
// potentially repeat the mutation
// (do two or more mutations in a single cycle
// 0 indicates that actual mutation rate will be adapted
RepeatMutator *repeater = new RepeatMutator(pselect_or_range, 0);
if(!use_deterministic_mutations && !deterministic_only) {
// and have nrounds of this per sample cycle
NRoundMutator *mutator = new NRoundMutator(repeater, nrounds);
return mutator;
} else {
MutatorSequence *deterministic_sequence = new MutatorSequence(false, true);
// do deterministic byte flip mutations (around hot bits)
deterministic_sequence->AddMutator(new DeterministicByteFlipMutator());
// ..followed by deterministc interesting values
deterministic_sequence->AddMutator(new DeterministicInterestingValueMutator(true));
size_t deterministic_rounds, nondeterministic_rounds;
if (deterministic_only) {
deterministic_rounds = nrounds;
} else {
deterministic_rounds = nrounds / 2;
}
nondeterministic_rounds = nrounds - deterministic_rounds;
// do 1000 rounds of derministic mutations, will switch to nondeterministic mutations
// once deterministic mutator is "done"
DtermininsticNondeterministicMutator *mutator =
new DtermininsticNondeterministicMutator(
deterministic_sequence,
deterministic_rounds,
repeater,
nondeterministic_rounds);
return mutator;
}
}
class GrammarFuzzer : public Fuzzer {
public:
GrammarFuzzer(const char *grammar_file);
protected:
Grammar grammar;
Mutator* CreateMutator(int argc, char** argv, ThreadContext* tc) override;
Minimizer* CreateMinimizer(int argc, char** argv, ThreadContext* tc) override;
bool OutputFilter(Sample* original_sample, Sample* output_sample, ThreadContext* tc) override;
bool IsReturnValueInteresting(uint64_t return_value) override;
};
GrammarFuzzer::GrammarFuzzer(const char* grammar_file) {
if (!grammar.Read(grammar_file)) {
FATAL("Error reading grammar");
}
}
Mutator* GrammarFuzzer::CreateMutator(int argc, char** argv, ThreadContext* tc) {
GrammarMutator* grammar_mutator = new GrammarMutator(&grammar);
NRoundMutator* mutator = new NRoundMutator(grammar_mutator, 20);
return mutator;
}
Minimizer* GrammarFuzzer::CreateMinimizer(int argc, char** argv, ThreadContext* tc) {
return new GrammarMinimizer(&grammar);
}
bool GrammarFuzzer::OutputFilter(Sample* original_sample, Sample* output_sample, ThreadContext* tc) {
uint64_t string_size = *((uint64_t*)original_sample->bytes);
if (original_sample->size < (string_size + sizeof(string_size))) {
FATAL("Incorrectly encoded grammar sample");
}
output_sample->Init(original_sample->bytes + sizeof(string_size), string_size);
return true;
}
bool GrammarFuzzer::IsReturnValueInteresting(uint64_t return_value) {
return (return_value == 0);
}
void TestGrammar(char* grammar_path) {
Grammar grammar;
grammar.Read(grammar_path);
PRNG* prng = new MTPRNG();
Grammar::TreeNode *tree = grammar.GenerateTree("root", prng);
if (!tree) {
printf("Grammar failed to generate sample\n");
} else {
std::string out;
grammar.ToString(tree, out);
printf("Generated sample:\n%s\n", out.c_str());
}
}
int main(int argc, char **argv)
{
Fuzzer* fuzzer;
char* grammar = GetOption("-test_grammar", argc, argv);
if (grammar) {
TestGrammar(grammar);
return 0;
}
grammar = GetOption("-grammar", argc, argv);
if (grammar) {
fuzzer = new GrammarFuzzer(grammar);
} else {
fuzzer = new BinaryFuzzer();
}
fuzzer->Run(argc, argv);
return 0;
}