xref: /freebsd/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_file.cpp (revision e9e8876a4d6afc1ad5315faaa191b25121a813d7)
1 //===-- sanitizer_file.cpp -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===---------------------------------------------------------------------===//
8 //
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries.  It defines filesystem-related interfaces.  This
11 // is separate from sanitizer_common.cpp so that it's simpler to disable
12 // all the filesystem support code for a port that doesn't use it.
13 //
14 //===---------------------------------------------------------------------===//
15 
16 #include "sanitizer_platform.h"
17 
18 #if !SANITIZER_FUCHSIA
19 
20 #include "sanitizer_common.h"
21 #include "sanitizer_file.h"
22 
23 namespace __sanitizer {
24 
25 void CatastrophicErrorWrite(const char *buffer, uptr length) {
26   WriteToFile(kStderrFd, buffer, length);
27 }
28 
29 StaticSpinMutex report_file_mu;
30 ReportFile report_file = {&report_file_mu, kStderrFd, "", "", 0};
31 
32 void RawWrite(const char *buffer) {
33   report_file.Write(buffer, internal_strlen(buffer));
34 }
35 
36 void ReportFile::ReopenIfNecessary() {
37   mu->CheckLocked();
38   if (fd == kStdoutFd || fd == kStderrFd) return;
39 
40   uptr pid = internal_getpid();
41   // If in tracer, use the parent's file.
42   if (pid == stoptheworld_tracer_pid)
43     pid = stoptheworld_tracer_ppid;
44   if (fd != kInvalidFd) {
45     // If the report file is already opened by the current process,
46     // do nothing. Otherwise the report file was opened by the parent
47     // process, close it now.
48     if (fd_pid == pid)
49       return;
50     else
51       CloseFile(fd);
52   }
53 
54   const char *exe_name = GetProcessName();
55   if (common_flags()->log_exe_name && exe_name) {
56     internal_snprintf(full_path, kMaxPathLength, "%s.%s.%zu", path_prefix,
57                       exe_name, pid);
58   } else {
59     internal_snprintf(full_path, kMaxPathLength, "%s.%zu", path_prefix, pid);
60   }
61   if (common_flags()->log_suffix) {
62     internal_strlcat(full_path, common_flags()->log_suffix, kMaxPathLength);
63   }
64   error_t err;
65   fd = OpenFile(full_path, WrOnly, &err);
66   if (fd == kInvalidFd) {
67     const char *ErrorMsgPrefix = "ERROR: Can't open file: ";
68     WriteToFile(kStderrFd, ErrorMsgPrefix, internal_strlen(ErrorMsgPrefix));
69     WriteToFile(kStderrFd, full_path, internal_strlen(full_path));
70     char errmsg[100];
71     internal_snprintf(errmsg, sizeof(errmsg), " (reason: %d)", err);
72     WriteToFile(kStderrFd, errmsg, internal_strlen(errmsg));
73     Die();
74   }
75   fd_pid = pid;
76 }
77 
78 void ReportFile::SetReportPath(const char *path) {
79   if (path) {
80     uptr len = internal_strlen(path);
81     if (len > sizeof(path_prefix) - 100) {
82       Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n", path[0], path[1],
83              path[2], path[3], path[4], path[5], path[6], path[7]);
84       Die();
85     }
86   }
87 
88   SpinMutexLock l(mu);
89   if (fd != kStdoutFd && fd != kStderrFd && fd != kInvalidFd)
90     CloseFile(fd);
91   fd = kInvalidFd;
92   if (!path || internal_strcmp(path, "stderr") == 0) {
93     fd = kStderrFd;
94   } else if (internal_strcmp(path, "stdout") == 0) {
95     fd = kStdoutFd;
96   } else {
97     internal_snprintf(path_prefix, kMaxPathLength, "%s", path);
98   }
99 }
100 
101 const char *ReportFile::GetReportPath() {
102   SpinMutexLock l(mu);
103   ReopenIfNecessary();
104   return full_path;
105 }
106 
107 bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
108                       uptr *read_len, uptr max_len, error_t *errno_p) {
109   *buff = nullptr;
110   *buff_size = 0;
111   *read_len = 0;
112   if (!max_len)
113     return true;
114   uptr PageSize = GetPageSizeCached();
115   uptr kMinFileLen = Min(PageSize, max_len);
116 
117   // The files we usually open are not seekable, so try different buffer sizes.
118   for (uptr size = kMinFileLen;; size = Min(size * 2, max_len)) {
119     UnmapOrDie(*buff, *buff_size);
120     *buff = (char*)MmapOrDie(size, __func__);
121     *buff_size = size;
122     fd_t fd = OpenFile(file_name, RdOnly, errno_p);
123     if (fd == kInvalidFd) {
124       UnmapOrDie(*buff, *buff_size);
125       return false;
126     }
127     *read_len = 0;
128     // Read up to one page at a time.
129     bool reached_eof = false;
130     while (*read_len < size) {
131       uptr just_read;
132       if (!ReadFromFile(fd, *buff + *read_len, size - *read_len, &just_read,
133                         errno_p)) {
134         UnmapOrDie(*buff, *buff_size);
135         CloseFile(fd);
136         return false;
137       }
138       *read_len += just_read;
139       if (just_read == 0 || *read_len == max_len) {
140         reached_eof = true;
141         break;
142       }
143     }
144     CloseFile(fd);
145     if (reached_eof)  // We've read the whole file.
146       break;
147   }
148   return true;
149 }
150 
151 bool ReadFileToVector(const char *file_name,
152                       InternalMmapVectorNoCtor<char> *buff, uptr max_len,
153                       error_t *errno_p) {
154   buff->clear();
155   if (!max_len)
156     return true;
157   uptr PageSize = GetPageSizeCached();
158   fd_t fd = OpenFile(file_name, RdOnly, errno_p);
159   if (fd == kInvalidFd)
160     return false;
161   uptr read_len = 0;
162   while (read_len < max_len) {
163     if (read_len >= buff->size())
164       buff->resize(Min(Max(PageSize, read_len * 2), max_len));
165     CHECK_LT(read_len, buff->size());
166     CHECK_LE(buff->size(), max_len);
167     uptr just_read;
168     if (!ReadFromFile(fd, buff->data() + read_len, buff->size() - read_len,
169                       &just_read, errno_p)) {
170       CloseFile(fd);
171       return false;
172     }
173     read_len += just_read;
174     if (!just_read)
175       break;
176   }
177   CloseFile(fd);
178   buff->resize(read_len);
179   return true;
180 }
181 
182 static const char kPathSeparator = SANITIZER_WINDOWS ? ';' : ':';
183 
184 char *FindPathToBinary(const char *name) {
185   if (FileExists(name)) {
186     return internal_strdup(name);
187   }
188 
189   const char *path = GetEnv("PATH");
190   if (!path)
191     return nullptr;
192   uptr name_len = internal_strlen(name);
193   InternalMmapVector<char> buffer(kMaxPathLength);
194   const char *beg = path;
195   while (true) {
196     const char *end = internal_strchrnul(beg, kPathSeparator);
197     uptr prefix_len = end - beg;
198     if (prefix_len + name_len + 2 <= kMaxPathLength) {
199       internal_memcpy(buffer.data(), beg, prefix_len);
200       buffer[prefix_len] = '/';
201       internal_memcpy(&buffer[prefix_len + 1], name, name_len);
202       buffer[prefix_len + 1 + name_len] = '\0';
203       if (FileExists(buffer.data()))
204         return internal_strdup(buffer.data());
205     }
206     if (*end == '\0') break;
207     beg = end + 1;
208   }
209   return nullptr;
210 }
211 
212 } // namespace __sanitizer
213 
214 using namespace __sanitizer;
215 
216 extern "C" {
217 void __sanitizer_set_report_path(const char *path) {
218   report_file.SetReportPath(path);
219 }
220 
221 void __sanitizer_set_report_fd(void *fd) {
222   report_file.fd = (fd_t)reinterpret_cast<uptr>(fd);
223   report_file.fd_pid = internal_getpid();
224 }
225 
226 const char *__sanitizer_get_report_path() {
227   return report_file.GetReportPath();
228 }
229 } // extern "C"
230 
231 #endif  // !SANITIZER_FUCHSIA
232