1 //===-- sanitizer_file.cc ------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===---------------------------------------------------------------------===//
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries. It defines filesystem-related interfaces. This
12 // is separate from sanitizer_common.cc so that it's simpler to disable
13 // all the filesystem support code for a port that doesn't use it.
15 //===---------------------------------------------------------------------===//
17 #include "sanitizer_platform.h"
19 #if !SANITIZER_FUCHSIA
21 #include "sanitizer_common.h"
22 #include "sanitizer_file.h"
24 namespace __sanitizer {
26 void CatastrophicErrorWrite(const char *buffer, uptr length) {
27 WriteToFile(kStderrFd, buffer, length);
30 StaticSpinMutex report_file_mu;
31 ReportFile report_file = {&report_file_mu, kStderrFd, "", "", 0};
33 void RawWrite(const char *buffer) {
34 report_file.Write(buffer, internal_strlen(buffer));
37 void ReportFile::ReopenIfNecessary() {
39 if (fd == kStdoutFd || fd == kStderrFd) return;
41 uptr pid = internal_getpid();
42 // If in tracer, use the parent's file.
43 if (pid == stoptheworld_tracer_pid)
44 pid = stoptheworld_tracer_ppid;
45 if (fd != kInvalidFd) {
46 // If the report file is already opened by the current process,
47 // do nothing. Otherwise the report file was opened by the parent
48 // process, close it now.
55 const char *exe_name = GetProcessName();
56 if (common_flags()->log_exe_name && exe_name) {
57 internal_snprintf(full_path, kMaxPathLength, "%s.%s.%zu", path_prefix,
60 internal_snprintf(full_path, kMaxPathLength, "%s.%zu", path_prefix, pid);
62 fd = OpenFile(full_path, WrOnly);
63 if (fd == kInvalidFd) {
64 const char *ErrorMsgPrefix = "ERROR: Can't open file: ";
65 WriteToFile(kStderrFd, ErrorMsgPrefix, internal_strlen(ErrorMsgPrefix));
66 WriteToFile(kStderrFd, full_path, internal_strlen(full_path));
72 void ReportFile::SetReportPath(const char *path) {
75 uptr len = internal_strlen(path);
76 if (len > sizeof(path_prefix) - 100) {
77 Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n",
78 path[0], path[1], path[2], path[3],
79 path[4], path[5], path[6], path[7]);
84 if (fd != kStdoutFd && fd != kStderrFd && fd != kInvalidFd)
87 if (internal_strcmp(path, "stdout") == 0) {
89 } else if (internal_strcmp(path, "stderr") == 0) {
92 internal_snprintf(path_prefix, kMaxPathLength, "%s", path);
96 bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
97 uptr *read_len, uptr max_len, error_t *errno_p) {
103 uptr PageSize = GetPageSizeCached();
104 uptr kMinFileLen = Min(PageSize, max_len);
106 // The files we usually open are not seekable, so try different buffer sizes.
107 for (uptr size = kMinFileLen;; size = Min(size * 2, max_len)) {
108 UnmapOrDie(*buff, *buff_size);
109 *buff = (char*)MmapOrDie(size, __func__);
111 fd_t fd = OpenFile(file_name, RdOnly, errno_p);
112 if (fd == kInvalidFd) {
113 UnmapOrDie(*buff, *buff_size);
117 // Read up to one page at a time.
118 bool reached_eof = false;
119 while (*read_len < size) {
121 if (!ReadFromFile(fd, *buff + *read_len, size - *read_len, &just_read,
123 UnmapOrDie(*buff, *buff_size);
127 *read_len += just_read;
128 if (just_read == 0 || *read_len == max_len) {
134 if (reached_eof) // We've read the whole file.
140 bool ReadFileToVector(const char *file_name,
141 InternalMmapVectorNoCtor<char> *buff, uptr max_len,
146 uptr PageSize = GetPageSizeCached();
147 fd_t fd = OpenFile(file_name, RdOnly, errno_p);
148 if (fd == kInvalidFd)
151 while (read_len < max_len) {
152 if (read_len >= buff->size())
153 buff->resize(Min(Max(PageSize, read_len * 2), max_len));
154 CHECK_LT(read_len, buff->size());
155 CHECK_LE(buff->size(), max_len);
157 if (!ReadFromFile(fd, buff->data() + read_len, buff->size() - read_len,
158 &just_read, errno_p)) {
162 read_len += just_read;
167 buff->resize(read_len);
171 static const char kPathSeparator = SANITIZER_WINDOWS ? ';' : ':';
173 char *FindPathToBinary(const char *name) {
174 if (FileExists(name)) {
175 return internal_strdup(name);
178 const char *path = GetEnv("PATH");
181 uptr name_len = internal_strlen(name);
182 InternalMmapVector<char> buffer(kMaxPathLength);
183 const char *beg = path;
185 const char *end = internal_strchrnul(beg, kPathSeparator);
186 uptr prefix_len = end - beg;
187 if (prefix_len + name_len + 2 <= kMaxPathLength) {
188 internal_memcpy(buffer.data(), beg, prefix_len);
189 buffer[prefix_len] = '/';
190 internal_memcpy(&buffer[prefix_len + 1], name, name_len);
191 buffer[prefix_len + 1 + name_len] = '\0';
192 if (FileExists(buffer.data()))
193 return internal_strdup(buffer.data());
195 if (*end == '\0') break;
201 } // namespace __sanitizer
203 using namespace __sanitizer; // NOLINT
206 void __sanitizer_set_report_path(const char *path) {
207 report_file.SetReportPath(path);
210 void __sanitizer_set_report_fd(void *fd) {
211 report_file.fd = (fd_t)reinterpret_cast<uptr>(fd);
212 report_file.fd_pid = internal_getpid();
216 #endif // !SANITIZER_FUCHSIA