Commit e5b603a4 authored by Marco Vanotti's avatar Marco Vanotti
Browse files

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

Summary:

This commit moves the `DiscardOutput` function in FuzzerIO to
FuzzerUtil, so fuchsia can have its own specialized version.

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`.

Reviewers: kcc, aarongreen

Subscribers: #sanitizers, llvm-commits

Tags: #sanitizers, #llvm

Differential Revision: https://reviews.llvm.org/D69593
parent b2e6c2b9
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -94,8 +94,6 @@ int DuplicateFile(int Fd);
void RemoveFile(const std::string &Path);
void RenameFile(const std::string &OldPath, const std::string &NewPath);

void DiscardOutput(int Fd);

intptr_t GetHandleFromFd(int fd);

void MkDir(const std::string &Path);
+0 −8
Original line number Diff line number Diff line
@@ -124,14 +124,6 @@ void RenameFile(const std::string &OldPath, const std::string &NewPath) {
  rename(OldPath.c_str(), NewPath.c_str());
}

void DiscardOutput(int Fd) {
  FILE* Temp = fopen("/dev/null", "w");
  if (!Temp)
    return;
  dup2(fileno(Temp), Fd);
  fclose(Temp);
}

intptr_t GetHandleFromFd(int fd) {
  return static_cast<intptr_t>(fd);
}
+0 −8
Original line number Diff line number Diff line
@@ -223,14 +223,6 @@ void RenameFile(const std::string &OldPath, const std::string &NewPath) {
  rename(OldPath.c_str(), NewPath.c_str());
}

void DiscardOutput(int Fd) {
  FILE* Temp = fopen("nul", "w");
  if (!Temp)
    return;
  _dup2(_fileno(Temp), Fd);
  fclose(Temp);
}

intptr_t GetHandleFromFd(int fd) {
  return _get_osfhandle(fd);
}
+2 −0
Original line number Diff line number Diff line
@@ -79,6 +79,8 @@ inline std::pair<std::string, std::string> SplitBefore(std::string X,
  return std::make_pair(S.substr(0, Pos), S.substr(Pos));
}

void DiscardOutput(int Fd);

std::string DisassembleCmd(const std::string &FileName);

std::string SearchRegexCmd(const std::string &Regex);
+9 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

// There is no header for this on macOS so declare here
extern "C" char **environ;
@@ -156,6 +157,14 @@ int ExecuteCommand(const Command &Cmd) {
  return ProcessStatus;
}

void DiscardOutput(int Fd) {
  FILE* Temp = fopen("/dev/null", "w");
  if (!Temp)
    return;
  dup2(fileno(Temp), Fd);
  fclose(Temp);
}

} // namespace fuzzer

#endif // LIBFUZZER_APPLE
Loading