From d434a0a1b26ad8abc24edcd50bbf22a8150c8e8b Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Sat, 23 May 2015 01:22:35 +0000 Subject: [PATCH] [lib/Fuzzer] fully get rid of std::cerr in libFuzzer git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@238081 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Fuzzer/FuzzerLoop.cpp | 47 ++++++++++++--------------------- lib/Fuzzer/FuzzerTraceState.cpp | 1 - lib/Fuzzer/FuzzerUtil.cpp | 13 +++++---- 3 files changed, 23 insertions(+), 38 deletions(-) diff --git a/lib/Fuzzer/FuzzerLoop.cpp b/lib/Fuzzer/FuzzerLoop.cpp index d028f193eef..34ceda4fb4d 100644 --- a/lib/Fuzzer/FuzzerLoop.cpp +++ b/lib/Fuzzer/FuzzerLoop.cpp @@ -12,7 +12,6 @@ #include "FuzzerInternal.h" #include #include -#include namespace fuzzer { @@ -37,8 +36,7 @@ void Fuzzer::PrintUnitInASCIIOrTokens(const Unit &U, const char *PrintAfter) { } else { auto T = SubstituteTokens(U); T.push_back(0); - std::cerr << T.data(); - std::cerr << PrintAfter; + Printf("%s%s", T.data(), PrintAfter); } } @@ -48,7 +46,7 @@ void Fuzzer::StaticDeathCallback() { } void Fuzzer::DeathCallback() { - std::cerr << "DEATH: " << std::endl; + Printf("DEATH:\n"); Print(CurrentUnit, "\n"); PrintUnitInASCIIOrTokens(CurrentUnit, "\n"); WriteToCrash(CurrentUnit, "crash-"); @@ -65,10 +63,9 @@ 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; + Printf("ALARM: working on the last Unit for %zd seconds\n", Seconds); Print(CurrentUnit, "\n"); PrintUnitInASCIIOrTokens(CurrentUnit, "\n"); WriteToCrash(CurrentUnit, "timeout-"); @@ -80,14 +77,8 @@ void Fuzzer::PrintStats(const char *Where, size_t Cov, 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 cov %zd bits %zd units %zd exec/s %zd %s", TotalNumberOfRuns, + Where, Cov, TotalBits(), Corpus.size(), ExecPerSec, End); } void Fuzzer::RereadOutputCorpus() { @@ -101,7 +92,7 @@ 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); @@ -124,7 +115,7 @@ void Fuzzer::ShuffleAndMinimize() { (Options.PreferSmallDuringInitialShuffle == 1 || (Options.PreferSmallDuringInitialShuffle == -1 && rand() % 2)); if (Options.Verbosity) - std::cerr << "PreferSmall: " << PreferSmall << "\n"; + Printf("PreferSmall: %d\n", PreferSmall); PrintStats("READ ", 0); std::vector NewCorpus; std::random_shuffle(Corpus.begin(), Corpus.end()); @@ -143,9 +134,7 @@ void Fuzzer::ShuffleAndMinimize() { MaxCov = NewCoverage; NewCorpus.push_back(U); if (Options.Verbosity >= 2) - std::cerr << "NEW0: " << NewCoverage - << " L " << U.size() - << "\n"; + Printf("NEW0: %zd L %zd\n", NewCoverage, U.size()); } } } @@ -168,8 +157,7 @@ size_t Fuzzer::RunOne(const Unit &U) { duration_cast(UnitStopTime - UnitStartTime).count(); if (TimeOfUnit > TimeOfLongestUnitInSeconds) { TimeOfLongestUnitInSeconds = TimeOfUnit; - std::cerr << "Longest unit: " << TimeOfLongestUnitInSeconds - << " s:\n"; + Printf("Longest unit: %zd s:\n", TimeOfLongestUnitInSeconds); Print(U, "\n"); } return Res; @@ -255,14 +243,13 @@ 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()); } void Fuzzer::WriteToCrash(const Unit &U, const char *Prefix) { std::string Path = Prefix + Hash(U); WriteToFile(U, Path); - std::cerr << "CRASHED; file written to " << Path << std::endl; - std::cerr << "Base64: "; + Printf("CRASHED; file written to %s\nBase64: ", Path.c_str()); PrintFileAsBase64(Path); } @@ -271,8 +258,8 @@ 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) { @@ -281,13 +268,13 @@ void Fuzzer::ReportNewCoverage(size_t NewCoverage, const Unit &U) { UnitHashesAddedToCorpus.insert(Hash(U)); PrintStats("NEW ", NewCoverage, ""); if (Options.Verbosity) { - std::cerr << " L: " << U.size(); + Printf(" L: %zd", U.size()); if (U.size() < 30) { - std::cerr << " "; + Printf(" "); PrintUnitInASCIIOrTokens(U, "\t"); Print(U); } - std::cerr << "\n"; + Printf("\n"); } WriteToOutputCorpus(U); if (Options.ExitOnFirst) diff --git a/lib/Fuzzer/FuzzerTraceState.cpp b/lib/Fuzzer/FuzzerTraceState.cpp index 350be1496a1..ddb0764930f 100644 --- a/lib/Fuzzer/FuzzerTraceState.cpp +++ b/lib/Fuzzer/FuzzerTraceState.cpp @@ -77,7 +77,6 @@ #include #include -#include #include extern "C" { diff --git a/lib/Fuzzer/FuzzerUtil.cpp b/lib/Fuzzer/FuzzerUtil.cpp index 06852081f52..e381c040632 100644 --- a/lib/Fuzzer/FuzzerUtil.cpp +++ b/lib/Fuzzer/FuzzerUtil.cpp @@ -12,7 +12,6 @@ #include "FuzzerInternal.h" #include #include -#include #include #include #include @@ -23,18 +22,18 @@ namespace fuzzer { void Print(const Unit &v, const char *PrintAfter) { for (auto x : v) - std::cerr << "0x" << std::hex << (unsigned) x << std::dec << ","; - std::cerr << PrintAfter; + Printf("0x%x,", (unsigned) x); + Printf("%s", PrintAfter); } void PrintASCII(const Unit &U, const char *PrintAfter) { for (auto X : U) { if (isprint(X)) - std::cerr << X; + Printf("%c", X); else - std::cerr << "\\x" << std::hex << (int)(unsigned)X << std::dec; + Printf("\\x%x", (unsigned)X); } - std::cerr << PrintAfter; + Printf("%s", PrintAfter); } std::string Hash(const Unit &U) { @@ -52,7 +51,7 @@ static void AlarmHandler(int, siginfo_t *, void *) { void SetTimer(int Seconds) { struct itimerval T {{Seconds, 0}, {Seconds, 0}}; - std::cerr << "SetTimer " << Seconds << "\n"; + Printf("SetTimer %d\n", Seconds); int Res = setitimer(ITIMER_REAL, &T, nullptr); assert(Res == 0); struct sigaction sigact; -- 2.34.1