Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions compiler-rt/lib/sanitizer_common/sanitizer_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,17 @@ void RawWrite(const char *buffer) {

void ReportFile::ReopenIfNecessary() {
mu->CheckLocked();
if (fd == kStdoutFd || fd == kStderrFd) return;

uptr pid = internal_getpid();
if (lastOpenFailed && fd_pid != pid) {
// If lastOpenFailed is set then we fellback to stderr. If this is a new
// process, mark fd as invalid so we attempt to open again.
CHECK_EQ(fd, kStderrFd);
fd = kInvalidFd;
lastOpenFailed = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this flag is being cleared here. I think it would be clearer to use a more descriptive name, such as fallbackTostderrActive.

}
if (fd == kStdoutFd || fd == kStderrFd)
return;

// If in tracer, use the parent's file.
if (pid == stoptheworld_tracer_pid)
pid = stoptheworld_tracer_ppid;
Expand All @@ -48,8 +56,7 @@ void ReportFile::ReopenIfNecessary() {
// process, close it now.
if (fd_pid == pid)
return;
else
CloseFile(fd);
CloseFile(fd);
}

const char *exe_name = GetProcessName();
Expand All @@ -71,12 +78,17 @@ void ReportFile::ReopenIfNecessary() {
char errmsg[100];
internal_snprintf(errmsg, sizeof(errmsg), " (reason: %d)\n", err);
WriteToFile(kStderrFd, errmsg, internal_strlen(errmsg));
Die();
if (!common_flags()->log_fallback_to_stderr)
Die();
const char *errmsg2 = "ERROR: Fallback to stderr\n";
Copy link
Contributor

@kyulee-com kyulee-com Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message seems a bit awkward, as the code continues handling the situation gracefully instead of terminating immediately.
Ideally, I want to combine the error message under this flag into something like:

ERROR: Can't open file: {blah}. Falling back to stderr.

Alternatively, we could comment with WARNING: instead of ERROR: in a separate message, but combining them into one line as above seems slightly better.

WriteToFile(kStderrFd, errmsg2, internal_strlen(errmsg2));
lastOpenFailed = true;
fd = kStderrFd;
}
fd_pid = pid;
}

static void RecursiveCreateParentDirs(char *path) {
static void RecursiveCreateParentDirs(char *path, fd_t &fd) {
if (path[0] == '\0')
return;
for (int i = 1; path[i] != '\0'; ++i) {
Expand All @@ -90,7 +102,13 @@ static void RecursiveCreateParentDirs(char *path) {
WriteToFile(kStderrFd, path, internal_strlen(path));
const char *ErrorMsgSuffix = "\n";
WriteToFile(kStderrFd, ErrorMsgSuffix, internal_strlen(ErrorMsgSuffix));
Die();
if (!common_flags()->log_fallback_to_stderr)
Die();
path[i] = save;
const char *ErrorMsgSuffix2 = "ERROR: Fallback to stderr\n";
WriteToFile(kStderrFd, ErrorMsgSuffix2, internal_strlen(ErrorMsgSuffix2));
fd = kStderrFd;
return;
}
path[i] = save;
}
Expand Down Expand Up @@ -166,7 +184,11 @@ void ReportFile::SetReportPath(const char *path) {
WriteToFile(kStderrFd, path, 8);
message = "...\n";
WriteToFile(kStderrFd, message, internal_strlen(message));
Die();
if (!common_flags()->log_fallback_to_stderr)
Die();
const char *message2 = "ERROR: Fallback to stderr\n";
WriteToFile(kStderrFd, message2, internal_strlen(message2));
path = "stderr";
}
}

Expand All @@ -180,7 +202,7 @@ void ReportFile::SetReportPath(const char *path) {
fd = kStdoutFd;
} else {
ParseAndSetPath(path, path_prefix, kMaxPathLength);
RecursiveCreateParentDirs(path_prefix);
RecursiveCreateParentDirs(path_prefix, fd);
}
}

Expand Down
3 changes: 3 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ struct ReportFile {
// PID of the process that opened fd. If a fork() occurs,
// the PID of child will be different from fd_pid.
uptr fd_pid;
// Set to true if the last attempt to open the logfile failed, perhaps due to
// permission errors
bool lastOpenFailed = false;

private:
void ReopenIfNecessary();
Expand Down
2 changes: 2 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_flags.inc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ COMMON_FLAG(
bool, log_to_syslog, (bool)SANITIZER_ANDROID || (bool)SANITIZER_APPLE,
"Write all sanitizer output to syslog in addition to other means of "
"logging.")
COMMON_FLAG(bool, log_fallback_to_stderr, false,
"When set, fallback to stderr if we are unable to open log path.")
COMMON_FLAG(
int, verbosity, 0,
"Verbosity level (0 - silent, 1 - a bit of output, 2+ - more output).")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

// Case 1: Try setting a path that is an invalid/inaccessible directory.
// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=ERROR1
// RUN: %env_tool_opts=log_fallback_to_stderr=true %run %t 2>&1 | FileCheck %s --check-prefixes=ERROR1,FALLBACK

// Case 2: Try setting a path that is too large.
// RUN: not %run %t A 2>&1 | FileCheck %s --check-prefix=ERROR2
// RUN: %env_tool_opts=log_fallback_to_stderr=true %run %t A 2>&1 | FileCheck %s --check-prefixes=ERROR2,FALLBACK

#include <sanitizer/common_interface_defs.h>
#include <stdio.h>
Expand All @@ -22,3 +24,4 @@ int main(int argc, char **argv) {
}
__sanitizer_set_report_path(buff);
}
// FALLBACK: Fallback to stderr
Loading