diff --git a/compiler-rt/lib/fuzzer/FuzzerIO.h b/compiler-rt/lib/fuzzer/FuzzerIO.h --- a/compiler-rt/lib/fuzzer/FuzzerIO.h +++ b/compiler-rt/lib/fuzzer/FuzzerIO.h @@ -58,6 +58,7 @@ FILE *GetOutputFile(); void SetOutputFile(FILE *NewOutputFile); +void Puts(const char *Str); void Printf(const char *Fmt, ...); void VPrintf(bool Verbose, const char *Fmt, ...); diff --git a/compiler-rt/lib/fuzzer/FuzzerIO.cpp b/compiler-rt/lib/fuzzer/FuzzerIO.cpp --- a/compiler-rt/lib/fuzzer/FuzzerIO.cpp +++ b/compiler-rt/lib/fuzzer/FuzzerIO.cpp @@ -65,7 +65,7 @@ } void CopyFileToErr(const std::string &Path) { - Printf("%s", FileToString(Path).c_str()); + Puts(FileToString(Path).c_str()); } void WriteToFile(const Unit &U, const std::string &Path) { @@ -151,6 +151,11 @@ DiscardOutput(1); } +void Puts(const char *Str) { + fputs(Str, OutputFile); + fflush(OutputFile); +} + void Printf(const char *Fmt, ...) { va_list ap; va_start(ap, Fmt); diff --git a/compiler-rt/test/fuzzer/BigFileCopy.cpp b/compiler-rt/test/fuzzer/BigFileCopy.cpp new file mode 100644 --- /dev/null +++ b/compiler-rt/test/fuzzer/BigFileCopy.cpp @@ -0,0 +1,31 @@ +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include +#include +#include +#include +#include + +#include "FuzzerIO.h" + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { + const char *FileName = "big-file.txt"; + FILE *f = fopen(FileName, "w"); + + // This is the biggest file possible unless CopyFileToErr() uses Puts() + fprintf(f, "%2147483646s", "2Gb-2"); + + // This makes the file too big if CopyFileToErr() uses fprintf("%s", ) + fprintf(f, "THIS LINE RESPONSIBLE FOR EXCEEDING 2Gb FILE SIZE\n"); + fclose(f); + + // Should now because CopyFileToErr() now uses Puts() + fuzzer::CopyFileToErr(FileName); + + // File is >2Gb so clean up + remove(FileName); + + return 0; +} diff --git a/compiler-rt/test/fuzzer/big-file-copy.test b/compiler-rt/test/fuzzer/big-file-copy.test new file mode 100644 --- /dev/null +++ b/compiler-rt/test/fuzzer/big-file-copy.test @@ -0,0 +1,4 @@ +RUN: %cpp_compiler %S/BigFileCopy.cpp -o %t +RUN: %run %t -runs=1 -rss_limit_mb=4096 2>big-file-out.txt; result=$? +RUN: %run rm -f big-file.txt big-file-out.txt +RUN: %run (exit $result)