[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
This commit is contained in:
Kostya Serebryany 2015-05-23 01:22:35 +00:00
parent 01461204b3
commit d434a0a1b2
3 changed files with 23 additions and 38 deletions

View File

@ -12,7 +12,6 @@
#include "FuzzerInternal.h"
#include <sanitizer/coverage_interface.h>
#include <algorithm>
#include <iostream>
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<seconds>(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<Unit> 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<seconds>(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)

View File

@ -77,7 +77,6 @@
#include <algorithm>
#include <cstring>
#include <iostream>
#include <unordered_map>
extern "C" {

View File

@ -12,7 +12,6 @@
#include "FuzzerInternal.h"
#include <sstream>
#include <iomanip>
#include <iostream>
#include <sys/time.h>
#include <cassert>
#include <cstring>
@ -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;