[PATCH] D28718: [libFuzzer] Avoid undefined behavior. Properly discard output to stdout and stderr.

Marcos Pividori via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 21 18:10:02 PST 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL292743: [libFuzzer] Avoid undefined behavior, properly discard output to stdout/stderr. (authored by mpividori).

Changed prior to commit:
  https://reviews.llvm.org/D28718?vs=84412&id=85264#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D28718

Files:
  llvm/trunk/lib/Fuzzer/FuzzerIO.cpp
  llvm/trunk/lib/Fuzzer/FuzzerIO.h
  llvm/trunk/lib/Fuzzer/FuzzerIOPosix.cpp
  llvm/trunk/lib/Fuzzer/FuzzerIOWindows.cpp


Index: llvm/trunk/lib/Fuzzer/FuzzerIOPosix.cpp
===================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerIOPosix.cpp
+++ llvm/trunk/lib/Fuzzer/FuzzerIOPosix.cpp
@@ -75,6 +75,14 @@
   unlink(Path.c_str());
 }
 
+void DiscardOutput(int Fd) {
+  FILE* Temp = fopen("/dev/null", "w");
+  if (!Temp)
+    return;
+  dup2(fileno(Temp), Fd);
+  fclose(Temp);
+}
+
 std::string DirName(const std::string &FileName) {
   char *Tmp = new char[FileName.size() + 1];
   memcpy(Tmp, FileName.c_str(), FileName.size() + 1);
Index: llvm/trunk/lib/Fuzzer/FuzzerIO.cpp
===================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerIO.cpp
+++ llvm/trunk/lib/Fuzzer/FuzzerIO.cpp
@@ -97,13 +97,13 @@
       OutputFile = NewOutputFile;
       if (EF->__sanitizer_set_report_fd)
         EF->__sanitizer_set_report_fd(reinterpret_cast<void *>(OutputFd));
-      CloseFile(2);
+      DiscardOutput(2);
     }
   }
 }
 
 void CloseStdout() {
-  CloseFile(1);
+  DiscardOutput(1);
 }
 
 void Printf(const char *Fmt, ...) {
Index: llvm/trunk/lib/Fuzzer/FuzzerIOWindows.cpp
===================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerIOWindows.cpp
+++ llvm/trunk/lib/Fuzzer/FuzzerIOWindows.cpp
@@ -141,6 +141,14 @@
   _unlink(Path.c_str());
 }
 
+void DiscardOutput(int Fd) {
+  FILE* Temp = fopen("nul", "w");
+  if (!Temp)
+    return;
+  _dup2(_fileno(Temp), Fd);
+  fclose(Temp);
+}
+
 static bool IsSeparator(char C) {
   return C == '\\' || C == '/';
 }
Index: llvm/trunk/lib/Fuzzer/FuzzerIO.h
===================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerIO.h
+++ llvm/trunk/lib/Fuzzer/FuzzerIO.h
@@ -64,6 +64,8 @@
 
 void RemoveFile(const std::string &Path);
 
+void DiscardOutput(int Fd);
+
 }  // namespace fuzzer
 
 #endif  // LLVM_FUZZER_IO_H


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28718.85264.patch
Type: text/x-patch
Size: 1886 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170122/307cd862/attachment.bin>


More information about the llvm-commits mailing list