]> granicus.if.org Git - llvm/commitdiff
[LibFuzzer] Split up some functions among different headers.
authorZachary Turner <zturner@google.com>
Wed, 30 Nov 2016 19:06:14 +0000 (19:06 +0000)
committerZachary Turner <zturner@google.com>
Wed, 30 Nov 2016 19:06:14 +0000 (19:06 +0000)
In an effort to get libfuzzer working on Windows, we need to make
a distinction between what functions require platform specific
code (e.g. different code on Windows vs Linux) and what code
doesn't.  IO functions, for example, tend to be platform
specific.

This patch separates out some of the functions which will need
to have platform specific implementations into different headers,
so that we can then provide different implementations for each
platform.

Aside from that, this patch contains no functional change.  It
is purely a re-organization.

Patch by Marcos Pividori
Differential Revision: https://reviews.llvm.org/D27230

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288264 91177308-0d34-0410-b5e6-96231b3b80d8

18 files changed:
lib/Fuzzer/FuzzerCorpus.h
lib/Fuzzer/FuzzerDefs.h
lib/Fuzzer/FuzzerDictionary.h
lib/Fuzzer/FuzzerDriver.cpp
lib/Fuzzer/FuzzerExtFunctionsWeak.cpp
lib/Fuzzer/FuzzerIO.cpp
lib/Fuzzer/FuzzerIO.h [new file with mode: 0644]
lib/Fuzzer/FuzzerInternal.h
lib/Fuzzer/FuzzerLoop.cpp
lib/Fuzzer/FuzzerMutate.cpp
lib/Fuzzer/FuzzerSHA1.cpp
lib/Fuzzer/FuzzerSHA1.h [new file with mode: 0644]
lib/Fuzzer/FuzzerTracePC.cpp
lib/Fuzzer/FuzzerTraceState.cpp
lib/Fuzzer/FuzzerUtil.cpp
lib/Fuzzer/FuzzerUtil.h [new file with mode: 0644]
lib/Fuzzer/FuzzerUtilDarwin.cpp
lib/Fuzzer/FuzzerUtilLinux.cpp

index 355c242e1f420de398255aac2cb295dd41640d40..bbe192f53f85698da3dfcc615ed1917a5c3bc199 100644 (file)
@@ -16,7 +16,9 @@
 #include <unordered_set>
 
 #include "FuzzerDefs.h"
+#include "FuzzerIO.h"
 #include "FuzzerRandom.h"
+#include "FuzzerSHA1.h"
 #include "FuzzerTracePC.h"
 
 namespace fuzzer {
index 13aa212c96c30a5638b7534c5675406017b994f2..89d675ac1a44189580842578197cdb3c960566f1 100644 (file)
@@ -63,64 +63,8 @@ extern ExternalFunctions *EF;
 typedef std::vector<uint8_t> Unit;
 typedef std::vector<Unit> UnitVector;
 typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
-int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
-
-bool IsFile(const std::string &Path);
-long GetEpoch(const std::string &Path);
-std::string FileToString(const std::string &Path);
-Unit FileToVector(const std::string &Path, size_t MaxSize = 0,
-                  bool ExitOnError = true);
-void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
-                            long *Epoch, size_t MaxSize, bool ExitOnError);
-void WriteToFile(const Unit &U, const std::string &Path);
-void CopyFileToErr(const std::string &Path);
-void DeleteFile(const std::string &Path);
-// Returns "Dir/FileName" or equivalent for the current OS.
-std::string DirPlusFile(const std::string &DirPath,
-                        const std::string &FileName);
-
-void DupAndCloseStderr();
-void CloseStdout();
-void Printf(const char *Fmt, ...);
-void PrintHexArray(const Unit &U, const char *PrintAfter = "");
-void PrintHexArray(const uint8_t *Data, size_t Size,
-                   const char *PrintAfter = "");
-void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
-void PrintASCII(const Unit &U, const char *PrintAfter = "");
-
-void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC);
-std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC);
-std::string Hash(const Unit &U);
-void SetTimer(int Seconds);
-void SetSigSegvHandler();
-void SetSigBusHandler();
-void SetSigAbrtHandler();
-void SetSigIllHandler();
-void SetSigFpeHandler();
-void SetSigIntHandler();
-void SetSigTermHandler();
-std::string Base64(const Unit &U);
-int ExecuteCommand(const std::string &Command);
-bool ExecuteCommandAndReadOutput(const std::string &Command, std::string *Out);
-
-size_t GetPeakRSSMb();
-
-// Private copy of SHA1 implementation.
-static const int kSHA1NumBytes = 20;
-// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
-void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
-std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]);
-
-// Changes U to contain only ASCII (isprint+isspace) characters.
-// Returns true iff U has been changed.
-bool ToASCII(uint8_t *Data, size_t Size);
-bool IsASCII(const Unit &U);
-bool IsASCII(const uint8_t *Data, size_t Size);
-
-int NumberOfCpuCores();
-int GetPid();
-void SleepSeconds(int Seconds);
 
+int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
 
 struct ScopedDoingMyOwnMemmem {
   ScopedDoingMyOwnMemmem();
index c009838ced6f6331945c4b591017142d934977f5..b23b4b022e3cb54fb3408f28325c0a6e1e815690 100644 (file)
 #ifndef LLVM_FUZZER_DICTIONARY_H
 #define LLVM_FUZZER_DICTIONARY_H
 
+#include "FuzzerDefs.h"
+#include "FuzzerIO.h"
+#include "FuzzerUtil.h"
 #include <algorithm>
 #include <limits>
 
-#include "FuzzerDefs.h"
-
 namespace fuzzer {
 // A simple POD sized array of bytes.
 template <size_t kMaxSize> class FixedWord {
index abf0597e0d38f5bfc06af5d52373c74ebb80e9dc..ab1fdf76ebf47a658b1caae9350e6fc522669a8c 100644 (file)
@@ -12,6 +12,7 @@
 #include "FuzzerCorpus.h"
 #include "FuzzerInterface.h"
 #include "FuzzerInternal.h"
+#include "FuzzerIO.h"
 #include "FuzzerMutate.h"
 #include "FuzzerRandom.h"
 
index cd4371ad44fea1345667c79fb23d8642df2d6f8e..cb3b31105d594a9219581c56abeef98c349aee86 100644 (file)
@@ -16,6 +16,7 @@
 #if LIBFUZZER_LINUX
 
 #include "FuzzerExtFunctions.h"
+#include "FuzzerIO.h"
 
 extern "C" {
 // Declare these symbols as weak to allow them to be optionally defined.
index a70af886c2b7352d3c35e59705f1fba61403f959..6cc8a8e0fd53ce8252ba5693dab6352178c41031 100644 (file)
@@ -8,8 +8,9 @@
 //===----------------------------------------------------------------------===//
 // IO functions.
 //===----------------------------------------------------------------------===//
-#include "FuzzerExtFunctions.h"
+#include "FuzzerIO.h"
 #include "FuzzerDefs.h"
+#include "FuzzerExtFunctions.h"
 #include <iterator>
 #include <fstream>
 #include <dirent.h>
diff --git a/lib/Fuzzer/FuzzerIO.h b/lib/Fuzzer/FuzzerIO.h
new file mode 100644 (file)
index 0000000..e3d22ad
--- /dev/null
@@ -0,0 +1,47 @@
+//===- FuzzerIO.h - Internal header for IO utils ----------------*- C++ -* ===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// IO interface.
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_FUZZER_IO_H
+#define LLVM_FUZZER_IO_H
+
+#include "FuzzerDefs.h"
+
+namespace fuzzer {
+
+bool IsFile(const std::string &Path);
+
+long GetEpoch(const std::string &Path);
+
+Unit FileToVector(const std::string &Path, size_t MaxSize = 0,
+                  bool ExitOnError = true);
+
+void DeleteFile(const std::string &Path);
+
+std::string FileToString(const std::string &Path);
+
+void CopyFileToErr(const std::string &Path);
+
+void WriteToFile(const Unit &U, const std::string &Path);
+
+void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
+                            long *Epoch, size_t MaxSize, bool ExitOnError);
+
+// Returns "Dir/FileName" or equivalent for the current OS.
+std::string DirPlusFile(const std::string &DirPath,
+                        const std::string &FileName);
+
+void DupAndCloseStderr();
+
+void CloseStdout();
+
+void Printf(const char *Fmt, ...);
+
+}  // namespace fuzzer
+#endif  // LLVM_FUZZER_IO_H
index 1b491eaafc52969eff173ebed9a19faefc912ed2..02e806c65a3e36170b37329306ed019bdacaff43 100644 (file)
@@ -23,6 +23,7 @@
 #include "FuzzerExtFunctions.h"
 #include "FuzzerInterface.h"
 #include "FuzzerOptions.h"
+#include "FuzzerSHA1.h"
 #include "FuzzerValueBitMap.h"
 
 namespace fuzzer {
index 0d2a38b6a8544b21abf7a204599e49f07e8315c0..63cb5c8bf1bd99a9c7ec79a6609e634b98d56b00 100644 (file)
@@ -9,16 +9,17 @@
 // Fuzzer's main loop.
 //===----------------------------------------------------------------------===//
 
-#include "FuzzerInternal.h"
 #include "FuzzerCorpus.h"
+#include "FuzzerInternal.h"
+#include "FuzzerIO.h"
 #include "FuzzerMutate.h"
-#include "FuzzerTracePC.h"
 #include "FuzzerRandom.h"
+#include "FuzzerTracePC.h"
 
 #include <algorithm>
 #include <cstring>
-#include <set>
 #include <memory>
+#include <set>
 
 #if defined(__has_include)
 #if __has_include(<sanitizer / coverage_interface.h>)
index 0109f5104feda6565274d0f9a2a5200196c514d6..eaef1bb494a8f5522fa3ed52fbb6ac207ab85930 100644 (file)
@@ -9,11 +9,10 @@
 // Mutate a test input.
 //===----------------------------------------------------------------------===//
 
-#include <cstring>
-
 #include "FuzzerCorpus.h"
 #include "FuzzerDefs.h"
 #include "FuzzerExtFunctions.h"
+#include "FuzzerIO.h"
 #include "FuzzerMutate.h"
 #include "FuzzerOptions.h"
 
index cab81a404db60570128d060ce0ab065f46758d60..d2f8e811bbf8b425313a166b4474016500587340 100644 (file)
 // For the same reason we do not want to depend on SHA1 from LLVM tree.
 //===----------------------------------------------------------------------===//
 
+#include "FuzzerSHA1.h"
 #include "FuzzerDefs.h"
 
 /* This code is public-domain - it is based on libcrypt
  * placed in the public domain by Wei Dai and other contributors.
  */
 
+#include <iomanip>
+#include <sstream>
 #include <stdint.h>
 #include <string.h>
 
@@ -193,10 +196,27 @@ uint8_t* sha1_result(sha1nfo *s) {
 
 }  // namespace; Added for LibFuzzer
 
+namespace fuzzer {
+
 // The rest is added for LibFuzzer
-void fuzzer::ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out) {
+void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out) {
   sha1nfo s;
   sha1_init(&s);
   sha1_write(&s, (const char*)Data, Len);
   memcpy(Out, sha1_result(&s), HASH_LENGTH);
 }
+
+std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]) {
+  std::stringstream SS;
+  for (int i = 0; i < kSHA1NumBytes; i++)
+    SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Sha1[i];
+  return SS.str();
+}
+
+std::string Hash(const Unit &U) {
+  uint8_t Hash[kSHA1NumBytes];
+  ComputeSHA1(U.data(), U.size(), Hash);
+  return Sha1ToString(Hash);
+}
+
+}
diff --git a/lib/Fuzzer/FuzzerSHA1.h b/lib/Fuzzer/FuzzerSHA1.h
new file mode 100644 (file)
index 0000000..b3e6111
--- /dev/null
@@ -0,0 +1,31 @@
+//===- FuzzerSHA1.h - Internal header for the SHA1 utils --------*- C++ -* ===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// SHA1 utils.
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_FUZZER_SHA1_H
+#define LLVM_FUZZER_SHA1_H
+
+#include "FuzzerDefs.h"
+#include <cstddef>
+#include <stdint.h>
+
+namespace fuzzer {
+
+// Private copy of SHA1 implementation.
+static const int kSHA1NumBytes = 20;
+
+// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
+void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
+
+std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]);
+
+std::string Hash(const Unit &U);
+
+}  // namespace fuzzer
+#endif  // LLVM_FUZZER_SHA1_H
index f85b150a0002d4a8b815c063ae225fc62cd22f9f..eabf0d087f4a2064cd9b0214ae839ac9a056f893 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
-#include <map>
-#include <set>
-#include <sstream>
-
 #include "FuzzerCorpus.h"
 #include "FuzzerDefs.h"
 #include "FuzzerDictionary.h"
 #include "FuzzerExtFunctions.h"
+#include "FuzzerIO.h"
 #include "FuzzerTracePC.h"
 #include "FuzzerValueBitMap.h"
+#include <map>
+#include <set>
+#include <sstream>
 
 namespace fuzzer {
 
index 9cccfcbc26f1460572f9e9b5cd31efe8b7364bb8..a920f57c4e20671cc9bbf86c13861d2319207bcb 100644 (file)
@@ -9,17 +9,17 @@
 // Data tracing.
 //===----------------------------------------------------------------------===//
 
-#include "FuzzerInternal.h"
 #include "FuzzerDictionary.h"
+#include "FuzzerInternal.h"
+#include "FuzzerIO.h"
 #include "FuzzerMutate.h"
 #include "FuzzerRandom.h"
 #include "FuzzerTracePC.h"
-
 #include <algorithm>
 #include <cstring>
-#include <thread>
 #include <map>
 #include <set>
+#include <thread>
 
 namespace fuzzer {
 
index d845333a1699aa680104d4334d8044c399fbfd71..579c4f83a87dddf9d6df5670651eb6fb2e7aa70e 100644 (file)
@@ -9,7 +9,9 @@
 // Misc utils.
 //===----------------------------------------------------------------------===//
 
+#include "FuzzerUtil.h"
 #include "FuzzerInternal.h"
+#include "FuzzerIO.h"
 #include <sstream>
 #include <iomanip>
 #include <sys/resource.h>
@@ -60,19 +62,6 @@ void PrintASCII(const Unit &U, const char *PrintAfter) {
   PrintASCII(U.data(), U.size(), PrintAfter);
 }
 
-std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]) {
-  std::stringstream SS;
-  for (int i = 0; i < kSHA1NumBytes; i++)
-    SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Sha1[i];
-  return SS.str();
-}
-
-std::string Hash(const Unit &U) {
-  uint8_t Hash[kSHA1NumBytes];
-  ComputeSHA1(U.data(), U.size(), Hash);
-  return Sha1ToString(Hash);
-}
-
 static void AlarmHandler(int, siginfo_t *, void *) {
   Fuzzer::StaticAlarmCallback();
 }
diff --git a/lib/Fuzzer/FuzzerUtil.h b/lib/Fuzzer/FuzzerUtil.h
new file mode 100644 (file)
index 0000000..97271b9
--- /dev/null
@@ -0,0 +1,65 @@
+//===- FuzzerUtil.h - Internal header for the Fuzzer Utils ------*- C++ -* ===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// Util functions.
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_FUZZER_UTIL_H
+#define LLVM_FUZZER_UTIL_H
+
+#include "FuzzerDefs.h"
+
+namespace fuzzer {
+
+void PrintHexArray(const Unit &U, const char *PrintAfter = "");
+
+void PrintHexArray(const uint8_t *Data, size_t Size,
+                   const char *PrintAfter = "");
+
+void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
+
+void PrintASCII(const Unit &U, const char *PrintAfter = "");
+
+// Changes U to contain only ASCII (isprint+isspace) characters.
+// Returns true iff U has been changed.
+bool ToASCII(uint8_t *Data, size_t Size);
+
+bool IsASCII(const Unit &U);
+
+bool IsASCII(const uint8_t *Data, size_t Size);
+
+std::string Base64(const Unit &U);
+
+void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC);
+
+std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC);
+
+int NumberOfCpuCores();
+
+// Platform specific functions.
+void SetTimer(int Seconds);
+
+void SetSigSegvHandler();
+void SetSigBusHandler();
+void SetSigAbrtHandler();
+void SetSigIllHandler();
+void SetSigFpeHandler();
+void SetSigIntHandler();
+void SetSigTermHandler();
+
+void SleepSeconds(int Seconds);
+
+int GetPid();
+
+size_t GetPeakRSSMb();
+
+bool ExecuteCommandAndReadOutput(const std::string &Command, std::string *Out);
+
+int ExecuteCommand(const std::string &Command);
+
+}  // namespace fuzzer
+#endif  // LLVM_FUZZER_UTIL_H
index 4c90998c897ebe7ced1eee0a440a2db2e0b53101..62f0f2271fe172a65194f55c4d70071d30fe453d 100644 (file)
@@ -10,6 +10,7 @@
 //===----------------------------------------------------------------------===//
 #include "FuzzerDefs.h"
 #if LIBFUZZER_APPLE
+#include "FuzzerIO.h"
 #include <mutex>
 #include <signal.h>
 #include <spawn.h>
index e63c7d9a07f40e457646ca659d312d55954b6159..060cec04542c2f9543e5b3314b1df1f99042fa77 100644 (file)
@@ -1,4 +1,4 @@
-//===- FuzzerUtilLinux.cpp - Misc utils -----------------------------------===//
+//===- FuzzerUtilLinux.cpp - Misc utils for Linux. ------------------------===//
 //
 //                     The LLVM Compiler Infrastructure
 //