[llvm] r238078 - [lib/Fuzzer] start getting rid of std::cerr. Sadly, these parts of C++ library used in libFuzzer badly interract with the same code used in the target function and also with dfsan. It's easier to just not use std::cerr than to defeat these issues.
Kostya Serebryany
kcc at google.com
Fri May 22 18:07:47 PDT 2015
Author: kcc
Date: Fri May 22 20:07:46 2015
New Revision: 238078
URL: http://llvm.org/viewvc/llvm-project?rev=238078&view=rev
Log:
[lib/Fuzzer] start getting rid of std::cerr. Sadly, these parts of C++ library used in libFuzzer badly interract with the same code used in the target function and also with dfsan. It's easier to just not use std::cerr than to defeat these issues.
Modified:
llvm/trunk/lib/Fuzzer/FuzzerDriver.cpp
llvm/trunk/lib/Fuzzer/FuzzerIO.cpp
llvm/trunk/lib/Fuzzer/FuzzerInternal.h
llvm/trunk/lib/Fuzzer/FuzzerTraceState.cpp
Modified: llvm/trunk/lib/Fuzzer/FuzzerDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerDriver.cpp?rev=238078&r1=238077&r2=238078&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerDriver.cpp (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerDriver.cpp Fri May 22 20:07:46 2015
@@ -15,7 +15,6 @@
#include <cstring>
#include <chrono>
#include <unistd.h>
-#include <iostream>
#include <thread>
#include <atomic>
#include <mutex>
@@ -60,23 +59,23 @@ static std::vector<std::string> inputs;
static const char *ProgName;
static void PrintHelp() {
- std::cerr << "Usage: " << ProgName
- << " [-flag1=val1 [-flag2=val2 ...] ] [dir1 [dir2 ...] ]\n";
- std::cerr << "\nFlags: (strictly in form -flag=value)\n";
+ Printf("Usage: %s [-flag1=val1 [-flag2=val2 ...] ] [dir1 [dir2 ...] ]\n",
+ ProgName);
+ Printf("\nFlags: (strictly in form -flag=value)\n");
size_t MaxFlagLen = 0;
for (size_t F = 0; F < kNumFlags; F++)
MaxFlagLen = std::max(strlen(FlagDescriptions[F].Name), MaxFlagLen);
for (size_t F = 0; F < kNumFlags; F++) {
const auto &D = FlagDescriptions[F];
- std::cerr << " " << D.Name;
+ Printf(" %s", D.Name);
for (size_t i = 0, n = MaxFlagLen - strlen(D.Name); i < n; i++)
- std::cerr << " ";
- std::cerr << "\t";
- std::cerr << D.Default << "\t" << D.Description << "\n";
+ Printf(" ");
+ Printf("\t");
+ Printf("%d\t%s\n", D.Default, D.Description);
}
- std::cerr << "\nFlags starting with '--' will be ignored and "
- "will be passed verbatim to subprocesses.\n";
+ Printf("\nFlags starting with '--' will be ignored and "
+ "will be passed verbatim to subprocesses.\n");
}
static const char *FlagValue(const char *Param, const char *Name) {
@@ -93,7 +92,7 @@ static bool ParseOneFlag(const char *Par
static bool PrintedWarning = false;
if (!PrintedWarning) {
PrintedWarning = true;
- std::cerr << "WARNING: libFuzzer ignores flags that start with '--'\n";
+ Printf("WARNING: libFuzzer ignores flags that start with '--'\n");
}
return true;
}
@@ -105,12 +104,12 @@ static bool ParseOneFlag(const char *Par
int Val = std::stol(Str);
*FlagDescriptions[F].IntFlag = Val;
if (Flags.verbosity >= 2)
- std::cerr << "Flag: " << Name << " " << Val << "\n";
+ Printf("Flag: %s %d\n", Name, Val);;
return true;
} else if (FlagDescriptions[F].StrFlag) {
*FlagDescriptions[F].StrFlag = Str;
if (Flags.verbosity >= 2)
- std::cerr << "Flag: " << Name << " " << Str << "\n";
+ Printf("Flag: %s %s\n", Name, Str);
return true;
}
}
@@ -139,7 +138,7 @@ static void PulseThread() {
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(600));
std::lock_guard<std::mutex> Lock(Mu);
- std::cerr << "pulse...\n";
+ Printf("pulse...\n");
}
}
@@ -151,14 +150,13 @@ static void WorkerThread(const std::stri
std::string Log = "fuzz-" + std::to_string(C) + ".log";
std::string ToRun = Cmd + " > " + Log + " 2>&1\n";
if (Flags.verbosity)
- std::cerr << ToRun;
+ Printf("%s", ToRun.c_str());
int ExitCode = system(ToRun.c_str());
if (ExitCode != 0)
*HasErrors = true;
std::lock_guard<std::mutex> Lock(Mu);
- std::cerr << "================== Job " << C
- << " exited with exit code " << ExitCode
- << " =================\n";
+ Printf("================== Job %d exited with exit code %d ============\n",
+ C, ExitCode);
fuzzer::CopyFileToErr(Log);
}
}
@@ -199,7 +197,7 @@ int ApplyTokens(const Fuzzer &F, const c
Unit U = FileToVector(InputFilePath);
auto T = F.SubstituteTokens(U);
T.push_back(0);
- std::cout << T.data();
+ Printf("%s", T.data());
return 0;
}
@@ -221,7 +219,7 @@ int FuzzerDriver(int argc, char **argv,
if (Flags.jobs > 0 && Flags.workers == 0) {
Flags.workers = std::min(NumberOfCpuCores() / 2, Flags.jobs);
if (Flags.workers > 1)
- std::cerr << "Running " << Flags.workers << " workers\n";
+ Printf("Running %d workers\n", Flags.workers);
}
if (Flags.workers > 0 && Flags.jobs > 0)
@@ -250,28 +248,28 @@ int FuzzerDriver(int argc, char **argv,
Options.SyncTimeout = Flags.sync_timeout;
Fuzzer F(USF, Options);
- unsigned seed = Flags.seed;
- // Initialize seed.
- if (seed == 0)
- seed = time(0) * 10000 + getpid();
+ if (Flags.apply_tokens)
+ return ApplyTokens(F, Flags.apply_tokens);
+
+ unsigned Seed = Flags.seed;
+ // Initialize Seed.
+ if (Seed == 0)
+ Seed = time(0) * 10000 + getpid();
if (Flags.verbosity)
- std::cerr << "Seed: " << seed << "\n";
- srand(seed);
+ Printf("Seed: %u\n", Seed);
+ srand(Seed);
// Timer
if (Flags.timeout > 0)
SetTimer(Flags.timeout / 2 + 1);
if (Flags.verbosity >= 2) {
- std::cerr << "Tokens: {";
+ Printf("Tokens: {");
for (auto &T : Options.Tokens)
- std::cerr << T << ",";
- std::cerr << "}\n";
+ Printf("%s,", T.c_str());
+ Printf("}\n");
}
- if (Flags.apply_tokens)
- return ApplyTokens(F, Flags.apply_tokens);
-
F.RereadOutputCorpus();
for (auto &inp : inputs)
if (inp != Options.OutputCorpus)
@@ -284,9 +282,9 @@ int FuzzerDriver(int argc, char **argv,
F.SaveCorpus();
F.Loop(Flags.iterations < 0 ? INT_MAX : Flags.iterations);
if (Flags.verbosity)
- std::cerr << "Done " << F.getTotalNumberOfRuns()
- << " runs in " << F.secondsSinceProcessStartUp()
- << " seconds\n";
+ Printf("Done %d runs in %zd second(s)\n", F.getTotalNumberOfRuns(),
+ F.secondsSinceProcessStartUp());
+
return 0;
}
Modified: llvm/trunk/lib/Fuzzer/FuzzerIO.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerIO.cpp?rev=238078&r1=238077&r2=238078&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerIO.cpp (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerIO.cpp Fri May 22 20:07:46 2015
@@ -9,13 +9,13 @@
// IO functions.
//===----------------------------------------------------------------------===//
#include "FuzzerInternal.h"
-#include <iostream>
#include <iterator>
#include <fstream>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
+#include <cstdio>
namespace fuzzer {
@@ -56,9 +56,7 @@ std::string FileToString(const std::stri
}
void CopyFileToErr(const std::string &Path) {
- std::ifstream T(Path);
- std::copy(std::istreambuf_iterator<char>(T), std::istreambuf_iterator<char>(),
- std::ostream_iterator<char>(std::cerr, ""));
+ Printf("%s", FileToString(Path).c_str());
}
void WriteToFile(const Unit &U, const std::string &Path) {
@@ -86,4 +84,11 @@ void PrintFileAsBase64(const std::string
ExecuteCommand(Cmd);
}
+void Printf(const char *Fmt, ...) {
+ va_list ap;
+ va_start(ap, Fmt);
+ vfprintf(stderr, Fmt, ap);
+ va_end(ap);
+}
+
} // namespace fuzzer
Modified: llvm/trunk/lib/Fuzzer/FuzzerInternal.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerInternal.h?rev=238078&r1=238077&r2=238078&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerInternal.h (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerInternal.h Fri May 22 20:07:46 2015
@@ -38,6 +38,7 @@ size_t Mutate(uint8_t *Data, size_t Size
size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
size_t Size2, uint8_t *Out, size_t MaxOutSize);
+void Printf(const char *Fmt, ...);
void Print(const Unit &U, const char *PrintAfter = "");
void PrintASCII(const Unit &U, const char *PrintAfter = "");
std::string Hash(const Unit &U);
Modified: llvm/trunk/lib/Fuzzer/FuzzerTraceState.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerTraceState.cpp?rev=238078&r1=238077&r2=238078&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerTraceState.cpp (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerTraceState.cpp Fri May 22 20:07:46 2015
@@ -165,10 +165,6 @@ struct LabelRange {
}
};
-std::ostream &operator<<(std::ostream &os, const LabelRange &LR) {
- return os << "[" << LR.Beg << "," << LR.End << ")";
-}
-
// For now, very simple: put Size bytes of Data at position Pos.
struct TraceBasedMutation {
size_t Pos;
@@ -231,7 +227,7 @@ void TraceState::ApplyTraceBasedMutation
assert(Idx < Mutations.size());
auto &M = Mutations[Idx];
if (Options.Verbosity >= 3)
- std::cerr << "TBM " << M.Pos << " " << M.Size << " " << M.Data << "\n";
+ Printf("TBM %zd %zd %zd\n", M.Pos, M.Size, M.Data);
if (M.Pos + M.Size > U->size()) return;
memcpy(U->data() + M.Pos, &M.Data, M.Size);
}
@@ -260,16 +256,8 @@ void TraceState::DFSanCmpCallback(uintpt
if (Options.Verbosity >= 3)
- std::cerr << "DFSAN:"
- << " PC " << std::hex << PC << std::dec
- << " S " << CmpSize
- << " T " << CmpType
- << " A1 " << Arg1 << " A2 " << Arg2 << " R " << Res
- << " L" << L1
- << " L" << L2
- << " R" << LR
- << " MU " << Mutations.size()
- << "\n";
+ Printf("DFSAN: PC %lx S %zd T %zd A1 %llx A2 %llx R %d L1 %d L2 %d MU %zd\n",
+ PC, CmpSize, CmpType, Arg1, Arg2, Res, L1, L2, Mutations.size());
}
int TraceState::TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
@@ -281,7 +269,6 @@ int TraceState::TryToAddDesiredData(uint
Cur = (uint8_t *)memmem(Cur, End - Cur, &PresentData, DataSize);
if (!Cur)
break;
- // std::cerr << "Cur " << (void*)Cur << "\n";
size_t Pos = Cur - Beg;
assert(Pos < CurrentUnit.size());
Mutations.push_back({Pos, DataSize, DesiredData});
@@ -298,7 +285,7 @@ void TraceState::TraceCmpCallback(size_t
if (!RecordingTraces) return;
int Added = 0;
if (Options.Verbosity >= 3)
- std::cerr << "TraceCmp: " << Arg1 << " " << Arg2 << "\n";
+ Printf("TraceCmp: %zd %zd\n", Arg1, Arg2);
Added += TryToAddDesiredData(Arg1, Arg2, CmpSize);
Added += TryToAddDesiredData(Arg2, Arg1, CmpSize);
if (!Added && CmpSize == 4 && IsTwoByteData(Arg1) && IsTwoByteData(Arg2)) {
More information about the llvm-commits
mailing list