diff options
| author | Evgeniy Stepanov <eugeni.stepanov@gmail.com> | 2018-12-19 23:45:17 +0000 |
|---|---|---|
| committer | Evgeniy Stepanov <eugeni.stepanov@gmail.com> | 2018-12-19 23:45:17 +0000 |
| commit | f762a9f8f0b95db3ec2e7ff9e17e8663df64b579 (patch) | |
| tree | 0b92f075ed924b4b666708cedb7798c30933c491 /compiler-rt/lib/sanitizer_common/sanitizer_posix.cc | |
| parent | b68cb5498b7db48c236ea5b9c234fbe2e47f1652 (diff) | |
| download | bcm5719-llvm-f762a9f8f0b95db3ec2e7ff9e17e8663df64b579.tar.gz bcm5719-llvm-f762a9f8f0b95db3ec2e7ff9e17e8663df64b579.zip | |
[sanitizer] Support running without fd 0,1,2.
Summary:
Support running with no open file descriptors (as may happen to
"init" process on linux).
* Remove a check that writing to stderr succeeds.
* When opening a file (ex. for log_path option), dup the new fd out of
[0, 2] range to avoid confusing the program.
Reviewers: pcc, vitalybuka
Subscribers: kubamracek, llvm-commits
Differential Revision: https://reviews.llvm.org/D55801
llvm-svn: 349699
Diffstat (limited to 'compiler-rt/lib/sanitizer_common/sanitizer_posix.cc')
| -rw-r--r-- | compiler-rt/lib/sanitizer_common/sanitizer_posix.cc | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc b/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc index 116270f8d23..c92986c1786 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc @@ -166,7 +166,7 @@ fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *errno_p) { fd_t res = internal_open(filename, flags, 0660); if (internal_iserror(res, errno_p)) return kInvalidFd; - return res; + return ReserveStandardFds(res); } void CloseFile(fd_t fd) { @@ -269,13 +269,8 @@ bool IsAbsolutePath(const char *path) { void ReportFile::Write(const char *buffer, uptr length) { SpinMutexLock l(mu); - static const char *kWriteError = - "ReportFile::Write() can't output requested buffer!\n"; ReopenIfNecessary(); - if (length != internal_write(fd, buffer, length)) { - internal_write(fd, kWriteError, internal_strlen(kWriteError)); - Die(); - } + internal_write(fd, buffer, length); } bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end) { @@ -323,6 +318,21 @@ const char *SignalContext::Describe() const { return "UNKNOWN SIGNAL"; } +fd_t ReserveStandardFds(fd_t fd) { + CHECK_GE(fd, 0); + if (fd > 2) + return fd; + bool used[3] = {false, false, false}; + while (fd <= 2) { + used[fd] = true; + fd = internal_dup(fd); + } + for (int i = 0; i <= 2; ++i) + if (used[i]) + internal_close(i); + return fd; +} + } // namespace __sanitizer #endif // SANITIZER_POSIX |

