diff options
| author | Jonathan Metzman <metzman@chromium.org> | 2018-11-06 23:25:25 +0000 |
|---|---|---|
| committer | Jonathan Metzman <metzman@chromium.org> | 2018-11-06 23:25:25 +0000 |
| commit | 39b6ba9f3311854672fab754b6263c5db0bb9166 (patch) | |
| tree | 1930111f861cfaedfa40e68ea353c8b01c413ec1 /compiler-rt | |
| parent | 8fcf0a74af2ac866ddeb2b06223f238d29b8770e (diff) | |
| download | bcm5719-llvm-39b6ba9f3311854672fab754b6263c5db0bb9166.tar.gz bcm5719-llvm-39b6ba9f3311854672fab754b6263c5db0bb9166.zip | |
[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
llvm-svn: 346279
Diffstat (limited to 'compiler-rt')
| -rw-r--r-- | compiler-rt/lib/fuzzer/FuzzerIO.cpp | 4 | ||||
| -rw-r--r-- | compiler-rt/lib/fuzzer/afl/afl_driver.cpp | 2 | ||||
| -rw-r--r-- | compiler-rt/test/fuzzer/ReadBinaryTest.cpp | 18 | ||||
| -rw-r--r-- | compiler-rt/test/fuzzer/read-binary.test | 7 |
4 files changed, 28 insertions, 3 deletions
diff --git a/compiler-rt/lib/fuzzer/FuzzerIO.cpp b/compiler-rt/lib/fuzzer/FuzzerIO.cpp index dac5ec658f1..c4c31e82471 100644 --- a/compiler-rt/lib/fuzzer/FuzzerIO.cpp +++ b/compiler-rt/lib/fuzzer/FuzzerIO.cpp @@ -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 &Path, size_t MaxSize, bool ExitOnError) { } 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>()); } diff --git a/compiler-rt/lib/fuzzer/afl/afl_driver.cpp b/compiler-rt/lib/fuzzer/afl/afl_driver.cpp index fa494c03bde..5a10c0d27f3 100644 --- a/compiler-rt/lib/fuzzer/afl/afl_driver.cpp +++ b/compiler-rt/lib/fuzzer/afl/afl_driver.cpp @@ -289,7 +289,7 @@ extern "C" size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) { // 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); diff --git a/compiler-rt/test/fuzzer/ReadBinaryTest.cpp b/compiler-rt/test/fuzzer/ReadBinaryTest.cpp new file mode 100644 index 00000000000..de7a4003698 --- /dev/null +++ b/compiler-rt/test/fuzzer/ReadBinaryTest.cpp @@ -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; +} diff --git a/compiler-rt/test/fuzzer/read-binary.test b/compiler-rt/test/fuzzer/read-binary.test new file mode 100644 index 00000000000..c80858e8113 --- /dev/null +++ b/compiler-rt/test/fuzzer/read-binary.test @@ -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! |

