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

Marco Vanotti via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 16 23:53:28 PDT 2019


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

This commit modifies the `DiscardOutput` function 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` in
libfuzerr, in which closing stdout was not working due to
`fopen("/dev/null", "w")` returning `NULL`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D69084

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


Index: compiler-rt/lib/fuzzer/FuzzerIOFuchsia.cpp
===================================================================
--- compiler-rt/lib/fuzzer/FuzzerIOFuchsia.cpp
+++ compiler-rt/lib/fuzzer/FuzzerIOFuchsia.cpp
@@ -18,6 +18,7 @@
 #include <fstream>
 #include <iterator>
 #include <libgen.h>
+#include <lib/fdio/fdio.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
@@ -125,11 +126,11 @@
 }
 
 void DiscardOutput(int Fd) {
-  FILE* Temp = fopen("/dev/null", "w");
-  if (!Temp)
-    return;
-  dup2(fileno(Temp), Fd);
-  fclose(Temp);
+  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);
 }
 
 intptr_t GetHandleFromFd(int fd) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69084.225357.patch
Type: text/x-patch
Size: 777 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191017/2346543d/attachment.bin>


More information about the llvm-commits mailing list