Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 8bbe924

Browse files
[libfuzzer] moving is_ascii handler inside mutation dispatcher.
Summary: It also fixes a bug, when first random might not be ascii. Differential Revision: http://reviews.llvm.org/D21573 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273611 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 205ddae commit 8bbe924

File tree

6 files changed

+65
-60
lines changed

6 files changed

+65
-60
lines changed

lib/Fuzzer/FuzzerDriver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ int FuzzerDriver(int *argc, char ***argv, UserCallback Callback) {
297297

298298
const size_t kMaxSaneLen = 1 << 20;
299299
const size_t kMinDefaultLen = 64;
300-
Fuzzer::FuzzingOptions Options;
300+
FuzzingOptions Options;
301301
Options.Verbosity = Flags.verbosity;
302302
Options.MaxLen = Flags.max_len;
303303
Options.UnitTimeoutSec = Flags.timeout;
@@ -347,7 +347,7 @@ int FuzzerDriver(int *argc, char ***argv, UserCallback Callback) {
347347
Printf("INFO: Seed: %u\n", Seed);
348348

349349
Random Rand(Seed);
350-
MutationDispatcher MD(Rand);
350+
MutationDispatcher MD(Rand, Options);
351351
Fuzzer F(Callback, MD, Options);
352352

353353
for (auto &U: Dictionary)

lib/Fuzzer/FuzzerInternal.h

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,43 @@ class Dictionary {
203203
size_t Size = 0;
204204
};
205205

206+
struct FuzzingOptions {
207+
int Verbosity = 1;
208+
size_t MaxLen = 0;
209+
int UnitTimeoutSec = 300;
210+
int TimeoutExitCode = 77;
211+
int ErrorExitCode = 77;
212+
int MaxTotalTimeSec = 0;
213+
int RssLimitMb = 0;
214+
bool DoCrossOver = true;
215+
int MutateDepth = 5;
216+
bool UseCounters = false;
217+
bool UseIndirCalls = true;
218+
bool UseTraces = false;
219+
bool UseMemcmp = true;
220+
bool UseFullCoverageSet = false;
221+
bool Reload = true;
222+
bool ShuffleAtStartUp = true;
223+
bool PreferSmall = true;
224+
size_t MaxNumberOfRuns = ULONG_MAX;
225+
int ReportSlowUnits = 10;
226+
bool OnlyASCII = false;
227+
std::string OutputCorpus;
228+
std::string ArtifactPrefix = "./";
229+
std::string ExactArtifactPath;
230+
bool SaveArtifacts = true;
231+
bool PrintNEW = true; // Print a status line when new units are found;
232+
bool OutputCSV = false;
233+
bool PrintNewCovPcs = false;
234+
bool PrintFinalStats = false;
235+
bool DetectLeaks = true;
236+
bool TruncateUnits = false;
237+
bool PruneCorpus = true;
238+
};
239+
206240
class MutationDispatcher {
207241
public:
208-
MutationDispatcher(Random &Rand);
242+
MutationDispatcher(Random &Rand, const FuzzingOptions &Options);
209243
~MutationDispatcher() {}
210244
/// Indicate that we are about to start a new sequence of mutations.
211245
void StartMutationSequence();
@@ -280,6 +314,8 @@ class MutationDispatcher {
280314
const std::vector<Mutator> &Mutators);
281315

282316
Random &Rand;
317+
const FuzzingOptions Options;
318+
283319
// Dictionary provided by the user via -dict=DICT_FILE.
284320
Dictionary ManualDictionary;
285321
// Temporary dictionary modified by the fuzzer itself,
@@ -299,39 +335,6 @@ class MutationDispatcher {
299335

300336
class Fuzzer {
301337
public:
302-
struct FuzzingOptions {
303-
int Verbosity = 1;
304-
size_t MaxLen = 0;
305-
int UnitTimeoutSec = 300;
306-
int TimeoutExitCode = 77;
307-
int ErrorExitCode = 77;
308-
int MaxTotalTimeSec = 0;
309-
int RssLimitMb = 0;
310-
bool DoCrossOver = true;
311-
int MutateDepth = 5;
312-
bool UseCounters = false;
313-
bool UseIndirCalls = true;
314-
bool UseTraces = false;
315-
bool UseMemcmp = true;
316-
bool UseFullCoverageSet = false;
317-
bool Reload = true;
318-
bool ShuffleAtStartUp = true;
319-
bool PreferSmall = true;
320-
size_t MaxNumberOfRuns = ULONG_MAX;
321-
int ReportSlowUnits = 10;
322-
bool OnlyASCII = false;
323-
std::string OutputCorpus;
324-
std::string ArtifactPrefix = "./";
325-
std::string ExactArtifactPath;
326-
bool SaveArtifacts = true;
327-
bool PrintNEW = true; // Print a status line when new units are found;
328-
bool OutputCSV = false;
329-
bool PrintNewCovPcs = false;
330-
bool PrintFinalStats = false;
331-
bool DetectLeaks = true;
332-
bool TruncateUnits = false;
333-
bool PruneCorpus = true;
334-
};
335338

336339
// Aggregates all available coverage measurements.
337340
struct Coverage {

lib/Fuzzer/FuzzerLoop.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,13 @@ struct CoverageController {
6060
PcMapResetCurrent();
6161
}
6262

63-
static void ResetCounters(const Fuzzer::FuzzingOptions &Options) {
63+
static void ResetCounters(const FuzzingOptions &Options) {
6464
if (Options.UseCounters) {
6565
EF->__sanitizer_update_counter_bitset_and_clear_counters(0);
6666
}
6767
}
6868

69-
static void Prepare(const Fuzzer::FuzzingOptions &Options,
70-
Fuzzer::Coverage *C) {
69+
static void Prepare(const FuzzingOptions &Options, Fuzzer::Coverage *C) {
7170
if (Options.UseCounters) {
7271
size_t NumCounters = EF->__sanitizer_get_number_of_counters();
7372
C->CounterBitmap.resize(NumCounters);
@@ -76,8 +75,7 @@ struct CoverageController {
7675

7776
// Records data to a maximum coverage tracker. Returns true if additional
7877
// coverage was discovered.
79-
static bool RecordMax(const Fuzzer::FuzzingOptions &Options,
80-
Fuzzer::Coverage *C) {
78+
static bool RecordMax(const FuzzingOptions &Options, Fuzzer::Coverage *C) {
8179
bool Res = false;
8280

8381
uint64_t NewBlockCoverage = EF->__sanitizer_get_total_unique_coverage();
@@ -675,8 +673,6 @@ void Fuzzer::MutateAndTestOne() {
675673
assert(NewSize <= Options.MaxLen &&
676674
"Mutator return overisized unit");
677675
Size = NewSize;
678-
if (Options.OnlyASCII)
679-
ToASCII(CurrentUnitData, Size);
680676
if (i == 0)
681677
StartTraceRecording();
682678
RunOneAndUpdateCorpus(CurrentUnitData, Size);

lib/Fuzzer/FuzzerMutate.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ namespace fuzzer {
1818

1919
const size_t Dictionary::kMaxDictSize;
2020

21-
MutationDispatcher::MutationDispatcher(Random &Rand) : Rand(Rand) {
21+
MutationDispatcher::MutationDispatcher(Random &Rand,
22+
const FuzzingOptions &Options)
23+
: Rand(Rand), Options(Options) {
2224
DefaultMutators.insert(
2325
DefaultMutators.begin(),
2426
{
@@ -285,6 +287,8 @@ size_t MutationDispatcher::MutateImpl(uint8_t *Data, size_t Size,
285287
if (Size == 0) {
286288
for (size_t i = 0; i < MaxSize; i++)
287289
Data[i] = RandCh(Rand);
290+
if (Options.OnlyASCII)
291+
ToASCII(Data, MaxSize);
288292
return MaxSize;
289293
}
290294
assert(Size > 0);
@@ -295,6 +299,8 @@ size_t MutationDispatcher::MutateImpl(uint8_t *Data, size_t Size,
295299
auto M = Mutators[Rand(Mutators.size())];
296300
size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
297301
if (NewSize) {
302+
if (Options.OnlyASCII)
303+
ToASCII(Data, NewSize);
298304
CurrentMutatorSequence.push_back(M);
299305
return NewSize;
300306
}

lib/Fuzzer/FuzzerTraceState.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ static bool RecordingTraces = false;
173173
static bool RecordingMemcmp = false;
174174

175175
class TraceState {
176-
public:
177-
TraceState(MutationDispatcher &MD, const Fuzzer::FuzzingOptions &Options,
176+
public:
177+
TraceState(MutationDispatcher &MD, const FuzzingOptions &Options,
178178
const Fuzzer *F)
179179
: MD(MD), Options(Options), F(F) {}
180180

@@ -209,7 +209,8 @@ class TraceState {
209209
}
210210

211211
void StopTraceRecording() {
212-
if (!RecordingTraces && !RecordingMemcmp) return;
212+
if (!RecordingTraces && !RecordingMemcmp)
213+
return;
213214
RecordingTraces = false;
214215
RecordingMemcmp = false;
215216
for (size_t i = 0; i < NumMutations; i++) {
@@ -287,7 +288,7 @@ class TraceState {
287288
LabelRange LabelRanges[1 << (sizeof(dfsan_label) * 8)];
288289
size_t LastDfsanLabel = 0;
289290
MutationDispatcher &MD;
290-
const Fuzzer::FuzzingOptions &Options;
291+
const FuzzingOptions Options;
291292
const Fuzzer *F;
292293
std::map<Word, size_t> AutoDictUnitCounts;
293294
size_t AutoDictAdds = 0;

lib/Fuzzer/test/FuzzerUnittest.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ TEST(Fuzzer, CrossOver) {
2222
std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
2323
fuzzer::EF = t.get();
2424
Random Rand(0);
25-
MutationDispatcher MD(Rand);
25+
MutationDispatcher MD(Rand, {});
2626
Unit A({0, 1, 2}), B({5, 6, 7});
2727
Unit C;
2828
Unit Expected[] = {
@@ -100,7 +100,7 @@ void TestEraseByte(Mutator M, int NumIter) {
100100
uint8_t REM6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x77};
101101
uint8_t REM7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
102102
Random Rand(0);
103-
MutationDispatcher MD(Rand);
103+
MutationDispatcher MD(Rand, {});
104104
int FoundMask = 0;
105105
for (int i = 0; i < NumIter; i++) {
106106
uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
@@ -128,7 +128,7 @@ void TestInsertByte(Mutator M, int NumIter) {
128128
std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
129129
fuzzer::EF = t.get();
130130
Random Rand(0);
131-
MutationDispatcher MD(Rand);
131+
MutationDispatcher MD(Rand, {});
132132
int FoundMask = 0;
133133
uint8_t INS0[8] = {0xF1, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
134134
uint8_t INS1[8] = {0x00, 0xF2, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
@@ -164,7 +164,7 @@ void TestChangeByte(Mutator M, int NumIter) {
164164
std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
165165
fuzzer::EF = t.get();
166166
Random Rand(0);
167-
MutationDispatcher MD(Rand);
167+
MutationDispatcher MD(Rand, {});
168168
int FoundMask = 0;
169169
uint8_t CH0[8] = {0xF0, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
170170
uint8_t CH1[8] = {0x00, 0xF1, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
@@ -200,7 +200,7 @@ void TestChangeBit(Mutator M, int NumIter) {
200200
std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
201201
fuzzer::EF = t.get();
202202
Random Rand(0);
203-
MutationDispatcher MD(Rand);
203+
MutationDispatcher MD(Rand, {});
204204
int FoundMask = 0;
205205
uint8_t CH0[8] = {0x01, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
206206
uint8_t CH1[8] = {0x00, 0x13, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
@@ -236,7 +236,7 @@ void TestShuffleBytes(Mutator M, int NumIter) {
236236
std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
237237
fuzzer::EF = t.get();
238238
Random Rand(0);
239-
MutationDispatcher MD(Rand);
239+
MutationDispatcher MD(Rand, {});
240240
int FoundMask = 0;
241241
uint8_t CH0[7] = {0x00, 0x22, 0x11, 0x33, 0x44, 0x55, 0x66};
242242
uint8_t CH1[7] = {0x11, 0x00, 0x33, 0x22, 0x44, 0x55, 0x66};
@@ -266,7 +266,7 @@ void TestAddWordFromDictionary(Mutator M, int NumIter) {
266266
std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
267267
fuzzer::EF = t.get();
268268
Random Rand(0);
269-
MutationDispatcher MD(Rand);
269+
MutationDispatcher MD(Rand, {});
270270
uint8_t Word1[4] = {0xAA, 0xBB, 0xCC, 0xDD};
271271
uint8_t Word2[3] = {0xFF, 0xEE, 0xEF};
272272
MD.AddWordToManualDictionary(Word(Word1, sizeof(Word1)));
@@ -308,7 +308,7 @@ void TestAddWordFromDictionaryWithHint(Mutator M, int NumIter) {
308308
std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
309309
fuzzer::EF = t.get();
310310
Random Rand(0);
311-
MutationDispatcher MD(Rand);
311+
MutationDispatcher MD(Rand, {});
312312
uint8_t W[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xFF, 0xEE, 0xEF};
313313
size_t PosHint = 7777;
314314
MD.AddWordToAutoDictionary(Word(W, sizeof(W)), PosHint);
@@ -337,7 +337,7 @@ void TestChangeASCIIInteger(Mutator M, int NumIter) {
337337
std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
338338
fuzzer::EF = t.get();
339339
Random Rand(0);
340-
MutationDispatcher MD(Rand);
340+
MutationDispatcher MD(Rand, {});
341341

342342
uint8_t CH0[8] = {'1', '2', '3', '4', '5', '6', '7', '7'};
343343
uint8_t CH1[8] = {'1', '2', '3', '4', '5', '6', '7', '9'};
@@ -431,9 +431,8 @@ TEST(Corpus, Distribution) {
431431
std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
432432
fuzzer::EF = t.get();
433433
Random Rand(0);
434-
MutationDispatcher MD(Rand);
435-
Fuzzer::FuzzingOptions Options;
436-
Fuzzer Fuzz(LLVMFuzzerTestOneInput, MD, Options);
434+
MutationDispatcher MD(Rand, {});
435+
Fuzzer Fuzz(LLVMFuzzerTestOneInput, MD, {});
437436
size_t N = 10;
438437
size_t TriesPerUnit = 1<<20;
439438
for (size_t i = 0; i < N; i++) {

0 commit comments

Comments
 (0)