[PATCH] D69593: [libFuzzer] don't use /dev/null for DiscardOuput in Fuchsia.

Marco Vanotti via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 29 15:42:33 PDT 2019


charco created this revision.
charco added reviewers: kcc, aarongreen.
Herald added projects: Sanitizers, LLVM.
Herald added subscribers: llvm-commits, Sanitizers.

This commit adds a specialized version of `DiscardOutput` for fuchsia,
removing an usage of `/dev/null`.

In fuchsia, accessing `/dev/null` is not supported, and there's nothing
similar to a file that discards everything that is written to it. The
way of doing something similar in fuchsia is by using `fdio_null_create`
and binding that to a file descriptor with `fdio_bind_to_fd`.

This change should fix one of the issues with the `-close_fd_mask` flag
in libfuzzer, in which closing stdout was not working due to
`fopen("/dev/null", "w")` returning `NULL`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D69593

Files:
  compiler-rt/lib/fuzzer/FuzzerIOPosix.cpp


Index: compiler-rt/lib/fuzzer/FuzzerIOPosix.cpp
===================================================================
--- compiler-rt/lib/fuzzer/FuzzerIOPosix.cpp
+++ compiler-rt/lib/fuzzer/FuzzerIOPosix.cpp
@@ -22,6 +22,10 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#if LIBFUZZER_FUCHSIA
+#include <lib/fdio/fdio.h>
+#endif
+
 namespace fuzzer {
 
 bool IsFile(const std::string &Path) {
@@ -124,6 +128,19 @@
   rename(OldPath.c_str(), NewPath.c_str());
 }
 
+#if LIBFUZZER_FUCHSIA
+// In fuchsia, accessing /dev/null is not supported. There's nothing
+// similar to a file that discards everything that is written to it.
+// The way of doing something similar in fuchsia is by using
+// fdio_null_create and binding that to a file descriptor.
+void DiscardOutput(int Fd) {
+  fdio_t *fdio_null = fdio_null_create();
+  if (fdio_null == nullptr) return;
+  int nullfd = fdio_bind_to_fd(fdio_null, -1, 0);
+  if (nullfd < 0) return;
+  dup2(nullfd, Fd);
+}
+#else
 void DiscardOutput(int Fd) {
   FILE* Temp = fopen("/dev/null", "w");
   if (!Temp)
@@ -131,6 +148,7 @@
   dup2(fileno(Temp), Fd);
   fclose(Temp);
 }
+#endif
 
 intptr_t GetHandleFromFd(int fd) {
   return static_cast<intptr_t>(fd);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69593.226985.patch
Type: text/x-patch
Size: 1205 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191029/894e3529/attachment.bin>


More information about the llvm-commits mailing list