X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FFuzzer%2FFuzzerLoop.cpp;h=6ac8997df78109f784d90261b3accced807b51ae;hb=41180c6ebaf1cadfc5085f6c5766822852c9a35a;hp=696811b45116a59744cadda24d40d5b4b63eb210;hpb=556425f9a9b6c0a2f4c7637b91893e186e62cf7a;p=oota-llvm.git diff --git a/lib/Fuzzer/FuzzerLoop.cpp b/lib/Fuzzer/FuzzerLoop.cpp index 696811b4511..6ac8997df78 100644 --- a/lib/Fuzzer/FuzzerLoop.cpp +++ b/lib/Fuzzer/FuzzerLoop.cpp @@ -12,15 +12,19 @@ #include "FuzzerInternal.h" #include #include -#include + +extern "C" { +__attribute__((weak)) void __sanitizer_print_stack_trace(); +} namespace fuzzer { +static const size_t kMaxUnitSizeToPrint = 256; // Only one Fuzzer per process. static Fuzzer *F; -Fuzzer::Fuzzer(UserCallback Callback, FuzzingOptions Options) - : Callback(Callback), Options(Options) { +Fuzzer::Fuzzer(UserSuppliedFuzzer &USF, FuzzingOptions Options) + : USF(USF), Options(Options) { SetDeathCallback(); InitializeTraceState(); assert(!F); @@ -31,15 +35,8 @@ void Fuzzer::SetDeathCallback() { __sanitizer_set_death_callback(StaticDeathCallback); } -void Fuzzer::PrintUnitInASCIIOrTokens(const Unit &U, const char *PrintAfter) { - if (Options.Tokens.empty()) { - PrintASCII(U, PrintAfter); - } else { - auto T = SubstituteTokens(U); - T.push_back(0); - std::cerr << T.data(); - std::cerr << PrintAfter; - } +void Fuzzer::PrintUnitInASCII(const Unit &U, const char *PrintAfter) { + PrintASCII(U, PrintAfter); } void Fuzzer::StaticDeathCallback() { @@ -48,10 +45,12 @@ void Fuzzer::StaticDeathCallback() { } void Fuzzer::DeathCallback() { - std::cerr << "DEATH: " << std::endl; - Print(CurrentUnit, "\n"); - PrintUnitInASCIIOrTokens(CurrentUnit, "\n"); - WriteToCrash(CurrentUnit, "crash-"); + Printf("DEATH:\n"); + if (CurrentUnit.size() <= kMaxUnitSizeToPrint) { + Print(CurrentUnit, "\n"); + PrintUnitInASCII(CurrentUnit, "\n"); + } + WriteUnitToFileWithPrefix(CurrentUnit, "crash-"); } void Fuzzer::StaticAlarmCallback() { @@ -65,29 +64,38 @@ void Fuzzer::AlarmCallback() { duration_cast(system_clock::now() - UnitStartTime).count(); if (Seconds == 0) return; if (Options.Verbosity >= 2) - std::cerr << "AlarmCallback " << Seconds << "\n"; + Printf("AlarmCallback %zd\n", Seconds); if (Seconds >= (size_t)Options.UnitTimeoutSec) { - std::cerr << "ALARM: working on the last Unit for " << Seconds << " seconds" - << std::endl; - Print(CurrentUnit, "\n"); - PrintUnitInASCIIOrTokens(CurrentUnit, "\n"); - WriteToCrash(CurrentUnit, "timeout-"); + Printf("ALARM: working on the last Unit for %zd seconds\n", Seconds); + Printf(" and the timeout value is %d (use -timeout=N to change)\n", + Options.UnitTimeoutSec); + if (CurrentUnit.size() <= kMaxUnitSizeToPrint) { + Print(CurrentUnit, "\n"); + PrintUnitInASCII(CurrentUnit, "\n"); + } + WriteUnitToFileWithPrefix(CurrentUnit, "timeout-"); + Printf("==%d== ERROR: libFuzzer: timeout after %d seconds\n", GetPid(), + Seconds); + if (__sanitizer_print_stack_trace) + __sanitizer_print_stack_trace(); + Printf("SUMMARY: libFuzzer: timeout\n"); exit(1); } } -void Fuzzer::PrintStats(const char *Where, size_t Cov, const char *End) { +void Fuzzer::PrintStats(const char *Where, const char *End) { if (!Options.Verbosity) return; size_t Seconds = secondsSinceProcessStartUp(); size_t ExecPerSec = (Seconds ? TotalNumberOfRuns / Seconds : 0); - std::cerr - << "#" << TotalNumberOfRuns - << "\t" << Where - << " cov " << Cov - << " bits " << TotalBits() - << " units " << Corpus.size() - << " exec/s " << ExecPerSec - << End; + Printf("#%zd\t%s", TotalNumberOfRuns, Where); + if (LastRecordedBlockCoverage) + Printf(" cov: %zd", LastRecordedBlockCoverage); + if (auto TB = TotalBits()) + Printf(" bits: %zd", TB); + Printf(" units: %zd exec/s: %zd", Corpus.size(), ExecPerSec); + if (TotalNumberOfExecutedTraceBasedMutations) + Printf(" tbm: %zd", TotalNumberOfExecutedTraceBasedMutations); + Printf("%s", End); } void Fuzzer::RereadOutputCorpus() { @@ -101,177 +109,116 @@ void Fuzzer::RereadOutputCorpus() { } if (!Options.Reload) return; if (Options.Verbosity >= 2) - std::cerr << "Reload: read " << AdditionalCorpus.size() << " new units.\n"; + Printf("Reload: read %zd new units.\n", AdditionalCorpus.size()); for (auto &X : AdditionalCorpus) { if (X.size() > (size_t)Options.MaxLen) X.resize(Options.MaxLen); if (UnitHashesAddedToCorpus.insert(Hash(X)).second) { CurrentUnit.clear(); CurrentUnit.insert(CurrentUnit.begin(), X.begin(), X.end()); - size_t NewCoverage = RunOne(CurrentUnit); - if (NewCoverage) { + if (RunOne(CurrentUnit)) { Corpus.push_back(X); if (Options.Verbosity >= 1) - PrintStats("RELOAD", NewCoverage); + PrintStats("RELOAD"); } } } } void Fuzzer::ShuffleAndMinimize() { - size_t MaxCov = 0; - bool PreferSmall = - (Options.PreferSmallDuringInitialShuffle == 1 || - (Options.PreferSmallDuringInitialShuffle == -1 && rand() % 2)); + bool PreferSmall = (Options.PreferSmallDuringInitialShuffle == 1 || + (Options.PreferSmallDuringInitialShuffle == -1 && + USF.GetRand().RandBool())); if (Options.Verbosity) - std::cerr << "PreferSmall: " << PreferSmall << "\n"; - PrintStats("READ ", 0); + Printf("PreferSmall: %d\n", PreferSmall); + PrintStats("READ "); std::vector NewCorpus; - std::random_shuffle(Corpus.begin(), Corpus.end()); - if (PreferSmall) - std::stable_sort( - Corpus.begin(), Corpus.end(), - [](const Unit &A, const Unit &B) { return A.size() < B.size(); }); + if (Options.ShuffleAtStartUp) { + std::random_shuffle(Corpus.begin(), Corpus.end(), USF.GetRand()); + if (PreferSmall) + std::stable_sort( + Corpus.begin(), Corpus.end(), + [](const Unit &A, const Unit &B) { return A.size() < B.size(); }); + } Unit &U = CurrentUnit; for (const auto &C : Corpus) { for (size_t First = 0; First < 1; First++) { U.clear(); size_t Last = std::min(First + Options.MaxLen, C.size()); U.insert(U.begin(), C.begin() + First, C.begin() + Last); - size_t NewCoverage = RunOne(U); - if (NewCoverage) { - MaxCov = NewCoverage; + if (Options.OnlyASCII) + ToASCII(U); + if (RunOne(U)) { NewCorpus.push_back(U); if (Options.Verbosity >= 2) - std::cerr << "NEW0: " << NewCoverage - << " L " << U.size() - << "\n"; + Printf("NEW0: %zd L %zd\n", LastRecordedBlockCoverage, U.size()); } } } Corpus = NewCorpus; for (auto &X : Corpus) UnitHashesAddedToCorpus.insert(Hash(X)); - PrintStats("INITED", MaxCov); + PrintStats("INITED"); } -size_t Fuzzer::RunOne(const Unit &U) { +bool Fuzzer::RunOne(const Unit &U) { UnitStartTime = system_clock::now(); TotalNumberOfRuns++; - size_t Res = 0; - if (Options.UseFullCoverageSet) - Res = RunOneMaximizeFullCoverageSet(U); - else if (Options.UseCoveragePairs) - Res = RunOneMaximizeCoveragePairs(U); - else - Res = RunOneMaximizeTotalCoverage(U); + + PrepareCoverageBeforeRun(); + ExecuteCallback(U); + bool Res = CheckCoverageAfterRun(); + auto UnitStopTime = system_clock::now(); auto TimeOfUnit = duration_cast(UnitStopTime - UnitStartTime).count(); - if (TimeOfUnit > TimeOfLongestUnitInSeconds) { + if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) && Options.Verbosity) + PrintStats("pulse "); + if (TimeOfUnit > TimeOfLongestUnitInSeconds && + TimeOfUnit >= Options.ReportSlowUnits) { TimeOfLongestUnitInSeconds = TimeOfUnit; - std::cerr << "Longest unit: " << TimeOfLongestUnitInSeconds - << " s:\n"; - Print(U, "\n"); + Printf("Slowest unit: %zd s:\n", TimeOfLongestUnitInSeconds); + WriteUnitToFileWithPrefix(U, "slow-unit-"); } return Res; } -void Fuzzer::RunOneAndUpdateCorpus(const Unit &U) { +void Fuzzer::RunOneAndUpdateCorpus(Unit &U) { if (TotalNumberOfRuns >= Options.MaxNumberOfRuns) return; - ReportNewCoverage(RunOne(U), U); -} - -static uintptr_t HashOfArrayOfPCs(uintptr_t *PCs, uintptr_t NumPCs) { - uintptr_t Res = 0; - for (uintptr_t i = 0; i < NumPCs; i++) { - Res = (Res + PCs[i]) * 7; - } - return Res; -} - -Unit Fuzzer::SubstituteTokens(const Unit &U) const { - Unit Res; - for (auto Idx : U) { - if (Idx < Options.Tokens.size()) { - std::string Token = Options.Tokens[Idx]; - Res.insert(Res.end(), Token.begin(), Token.end()); - } else { - Res.push_back(' '); - } - } - // FIXME: Apply DFSan labels. - return Res; + if (Options.OnlyASCII) + ToASCII(U); + if (RunOne(U)) + ReportNewCoverage(U); } void Fuzzer::ExecuteCallback(const Unit &U) { - if (Options.Tokens.empty()) { - Callback(U.data(), U.size()); - } else { - auto T = SubstituteTokens(U); - Callback(T.data(), T.size()); - } -} - -// Experimental. Does not yet scale. -// Fuly reset the current coverage state, run a single unit, -// collect all coverage pairs and return non-zero if a new pair is observed. -size_t Fuzzer::RunOneMaximizeCoveragePairs(const Unit &U) { - __sanitizer_reset_coverage(); - ExecuteCallback(U); - uintptr_t *PCs; - uintptr_t NumPCs = __sanitizer_get_coverage_guards(&PCs); - bool HasNewPairs = false; - for (uintptr_t i = 0; i < NumPCs; i++) { - if (!PCs[i]) continue; - for (uintptr_t j = 0; j < NumPCs; j++) { - if (!PCs[j]) continue; - uint64_t Pair = (i << 32) | j; - HasNewPairs |= CoveragePairs.insert(Pair).second; - } - } - if (HasNewPairs) - return CoveragePairs.size(); - return 0; + int Res = USF.TargetFunction(U.data(), U.size()); + (void)Res; + assert(Res == 0); } -// Experimental. -// Fuly reset the current coverage state, run a single unit, -// compute a hash function from the full coverage set, -// return non-zero if the hash value is new. -// This produces tons of new units and as is it's only suitable for small tests, -// e.g. test/FullCoverageSetTest.cpp. FIXME: make it scale. -size_t Fuzzer::RunOneMaximizeFullCoverageSet(const Unit &U) { - __sanitizer_reset_coverage(); - ExecuteCallback(U); - uintptr_t *PCs; - uintptr_t NumPCs =__sanitizer_get_coverage_guards(&PCs); - if (FullCoverageSets.insert(HashOfArrayOfPCs(PCs, NumPCs)).second) - return FullCoverageSets.size(); - return 0; +size_t Fuzzer::RecordBlockCoverage() { + return LastRecordedBlockCoverage = __sanitizer_get_total_unique_coverage(); } -size_t Fuzzer::RunOneMaximizeTotalCoverage(const Unit &U) { - size_t NumCounters = __sanitizer_get_number_of_counters(); +void Fuzzer::PrepareCoverageBeforeRun() { if (Options.UseCounters) { + size_t NumCounters = __sanitizer_get_number_of_counters(); CounterBitmap.resize(NumCounters); __sanitizer_update_counter_bitset_and_clear_counters(0); } - size_t OldCoverage = __sanitizer_get_total_unique_coverage(); - ExecuteCallback(U); - size_t NewCoverage = __sanitizer_get_total_unique_coverage(); + RecordBlockCoverage(); +} + +bool Fuzzer::CheckCoverageAfterRun() { + size_t OldCoverage = LastRecordedBlockCoverage; + size_t NewCoverage = RecordBlockCoverage(); size_t NumNewBits = 0; if (Options.UseCounters) NumNewBits = __sanitizer_update_counter_bitset_and_clear_counters( CounterBitmap.data()); - - if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) && Options.Verbosity) - PrintStats("pulse ", NewCoverage); - - if (NewCoverage > OldCoverage || NumNewBits) - return NewCoverage; - return 0; + return NewCoverage > OldCoverage || NumNewBits; } void Fuzzer::WriteToOutputCorpus(const Unit &U) { @@ -279,15 +226,21 @@ void Fuzzer::WriteToOutputCorpus(const Unit &U) { std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U)); WriteToFile(U, Path); if (Options.Verbosity >= 2) - std::cerr << "Written to " << Path << std::endl; + Printf("Written to %s\n", Path.c_str()); + assert(!Options.OnlyASCII || IsASCII(U)); } -void Fuzzer::WriteToCrash(const Unit &U, const char *Prefix) { - std::string Path = Prefix + Hash(U); +void Fuzzer::WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix) { + if (!Options.SaveArtifacts) + return; + std::string Path = Options.ArtifactPrefix + Prefix + Hash(U); WriteToFile(U, Path); - std::cerr << "CRASHED; file written to " << Path << std::endl; - std::cerr << "Base64: "; - PrintFileAsBase64(Path); + Printf("artifact_prefix='%s'; Test unit written to %s\n", + Options.ArtifactPrefix.c_str(), Path.c_str()); + if (U.size() <= kMaxUnitSizeToPrint) { + Printf("Base64: "); + PrintFileAsBase64(Path); + } } void Fuzzer::SaveCorpus() { @@ -295,23 +248,22 @@ void Fuzzer::SaveCorpus() { for (const auto &U : Corpus) WriteToFile(U, DirPlusFile(Options.OutputCorpus, Hash(U))); if (Options.Verbosity) - std::cerr << "Written corpus of " << Corpus.size() << " files to " - << Options.OutputCorpus << "\n"; + Printf("Written corpus of %zd files to %s\n", Corpus.size(), + Options.OutputCorpus.c_str()); } -void Fuzzer::ReportNewCoverage(size_t NewCoverage, const Unit &U) { - if (!NewCoverage) return; +void Fuzzer::ReportNewCoverage(const Unit &U) { Corpus.push_back(U); UnitHashesAddedToCorpus.insert(Hash(U)); - PrintStats("NEW ", NewCoverage, ""); + PrintStats("NEW ", ""); if (Options.Verbosity) { - std::cerr << " L: " << U.size(); + Printf(" L: %zd", U.size()); if (U.size() < 30) { - std::cerr << " "; - PrintUnitInASCIIOrTokens(U, "\t"); + Printf(" "); + PrintUnitInASCII(U, "\t"); Print(U); } - std::cerr << "\n"; + Printf("\n"); } WriteToOutputCorpus(U); if (Options.ExitOnFirst) @@ -321,34 +273,63 @@ void Fuzzer::ReportNewCoverage(size_t NewCoverage, const Unit &U) { void Fuzzer::MutateAndTestOne(Unit *U) { for (int i = 0; i < Options.MutateDepth; i++) { StartTraceRecording(); - Mutate(U, Options.MaxLen); + size_t Size = U->size(); + U->resize(Options.MaxLen); + size_t NewSize = USF.Mutate(U->data(), Size, U->size()); + assert(NewSize > 0 && "Mutator returned empty unit"); + assert(NewSize <= (size_t)Options.MaxLen && + "Mutator return overisized unit"); + U->resize(NewSize); RunOneAndUpdateCorpus(*U); size_t NumTraceBasedMutations = StopTraceRecording(); - for (size_t j = 0; j < NumTraceBasedMutations; j++) { - ApplyTraceBasedMutation(j, U); - RunOneAndUpdateCorpus(*U); + size_t TBMWidth = + std::min((size_t)Options.TBMWidth, NumTraceBasedMutations); + size_t TBMDepth = + std::min((size_t)Options.TBMDepth, NumTraceBasedMutations); + Unit BackUp = *U; + for (size_t w = 0; w < TBMWidth; w++) { + *U = BackUp; + for (size_t d = 0; d < TBMDepth; d++) { + TotalNumberOfExecutedTraceBasedMutations++; + ApplyTraceBasedMutation(USF.GetRand()(NumTraceBasedMutations), U); + RunOneAndUpdateCorpus(*U); + } } } } -void Fuzzer::Loop(size_t NumIterations) { - for (size_t i = 1; i <= NumIterations; i++) { +void Fuzzer::Loop() { + for (auto &U: Options.Dictionary) + USF.GetMD().AddWordToDictionary(U.data(), U.size()); + + while (true) { for (size_t J1 = 0; J1 < Corpus.size(); J1++) { SyncCorpus(); RereadOutputCorpus(); if (TotalNumberOfRuns >= Options.MaxNumberOfRuns) return; - // First, simply mutate the unit w/o doing crosses. + if (Options.MaxTotalTimeSec > 0 && + secondsSinceProcessStartUp() > + static_cast(Options.MaxTotalTimeSec)) + return; CurrentUnit = Corpus[J1]; - MutateAndTestOne(&CurrentUnit); - // Now, cross with others. - if (Options.DoCrossOver) { - for (size_t J2 = 0; J2 < Corpus.size(); J2++) { - CurrentUnit.clear(); - CrossOver(Corpus[J1], Corpus[J2], &CurrentUnit, Options.MaxLen); - MutateAndTestOne(&CurrentUnit); + // Optionally, cross with another unit. + if (Options.DoCrossOver && USF.GetRand().RandBool()) { + size_t J2 = USF.GetRand()(Corpus.size()); + if (!Corpus[J1].empty() && !Corpus[J2].empty()) { + assert(!Corpus[J2].empty()); + CurrentUnit.resize(Options.MaxLen); + size_t NewSize = USF.CrossOver( + Corpus[J1].data(), Corpus[J1].size(), Corpus[J2].data(), + Corpus[J2].size(), CurrentUnit.data(), CurrentUnit.size()); + assert(NewSize > 0 && "CrossOver returned empty unit"); + assert(NewSize <= (size_t)Options.MaxLen && + "CrossOver returned overisized unit"); + CurrentUnit.resize(NewSize); } } + // Perform several mutations and runs. + MutateAndTestOne(&CurrentUnit); } } }