[compiler-rt] r346279 - [fuzzer] Read files as binary

Jonathan Metzman via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 6 15:25:26 PST 2018


Author: metzman
Date: Tue Nov  6 15:25:25 2018
New Revision: 346279

URL: http://llvm.org/viewvc/llvm-project?rev=346279&view=rev
Log:
[fuzzer] Read files as binary

Summary: Read corpus files as binary to avoid automatic conversions

Reviewers: Dor1s, morehouse

Reviewed By: Dor1s, morehouse

Differential Revision: https://reviews.llvm.org/D54180

Added:
    compiler-rt/trunk/test/fuzzer/ReadBinaryTest.cpp
    compiler-rt/trunk/test/fuzzer/read-binary.test
Modified:
    compiler-rt/trunk/lib/fuzzer/FuzzerIO.cpp
    compiler-rt/trunk/lib/fuzzer/afl/afl_driver.cpp

Modified: compiler-rt/trunk/lib/fuzzer/FuzzerIO.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/fuzzer/FuzzerIO.cpp?rev=346279&r1=346278&r2=346279&view=diff
==============================================================================
--- compiler-rt/trunk/lib/fuzzer/FuzzerIO.cpp (original)
+++ compiler-rt/trunk/lib/fuzzer/FuzzerIO.cpp Tue Nov  6 15:25:25 2018
@@ -31,7 +31,7 @@ long GetEpoch(const std::string &Path) {
 }
 
 Unit FileToVector(const std::string &Path, size_t MaxSize, bool ExitOnError) {
-  std::ifstream T(Path);
+  std::ifstream T(Path, std::ios::binary);
   if (ExitOnError && !T) {
     Printf("No such directory: %s; exiting\n", Path.c_str());
     exit(1);
@@ -51,7 +51,7 @@ Unit FileToVector(const std::string &Pat
 }
 
 std::string FileToString(const std::string &Path) {
-  std::ifstream T(Path);
+  std::ifstream T(Path, std::ios::binary);
   return std::string((std::istreambuf_iterator<char>(T)),
                      std::istreambuf_iterator<char>());
 }

Modified: compiler-rt/trunk/lib/fuzzer/afl/afl_driver.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/fuzzer/afl/afl_driver.cpp?rev=346279&r1=346278&r2=346279&view=diff
==============================================================================
--- compiler-rt/trunk/lib/fuzzer/afl/afl_driver.cpp (original)
+++ compiler-rt/trunk/lib/fuzzer/afl/afl_driver.cpp Tue Nov  6 15:25:25 2018
@@ -289,7 +289,7 @@ extern "C" size_t LLVMFuzzerMutate(uint8
 // Execute any files provided as parameters.
 int ExecuteFilesOnyByOne(int argc, char **argv) {
   for (int i = 1; i < argc; i++) {
-    std::ifstream in(argv[i]);
+    std::ifstream in(argv[i], std::ios::binary);
     in.seekg(0, in.end);
     size_t length = in.tellg();
     in.seekg (0, in.beg);

Added: compiler-rt/trunk/test/fuzzer/ReadBinaryTest.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/fuzzer/ReadBinaryTest.cpp?rev=346279&view=auto
==============================================================================
--- compiler-rt/trunk/test/fuzzer/ReadBinaryTest.cpp (added)
+++ compiler-rt/trunk/test/fuzzer/ReadBinaryTest.cpp Tue Nov  6 15:25:25 2018
@@ -0,0 +1,18 @@
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+
+// Simple test for a fuzzer. Tests that fuzzer can read a file containing
+// carriage returns.
+#include <cstddef>
+#include <cstdint>
+#include <iostream>
+#include <string>
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) {
+  std::string InputStr(reinterpret_cast<const char*>(Data), Size);
+  std::string MagicStr("Hello\r\nWorld\r\n");
+  if (InputStr == MagicStr) {
+    std::cout << "BINGO!";
+  }
+  return 0;
+}

Added: compiler-rt/trunk/test/fuzzer/read-binary.test
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/fuzzer/read-binary.test?rev=346279&view=auto
==============================================================================
--- compiler-rt/trunk/test/fuzzer/read-binary.test (added)
+++ compiler-rt/trunk/test/fuzzer/read-binary.test Tue Nov  6 15:25:25 2018
@@ -0,0 +1,7 @@
+# Test that libFuzzer reads files properly.
+
+# Account for the fact that echo will add a trailing newline.
+RUN: echo -e "Hello\r\nWorld\r" > %t-testcase
+RUN: %cpp_compiler %S/ReadBinaryTest.cpp -o %t-fuzzer
+RUN: %run %t-fuzzer %t-testcase | FileCheck %s
+CHECK: BINGO!




More information about the llvm-commits mailing list