• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CatastrophicErrorWrite(const char * buffer,uptr length)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 
RawWrite(const char * buffer)32 void RawWrite(const char *buffer) {
33   report_file.Write(buffer, internal_strlen(buffer));
34 }
35 
ReopenIfNecessary()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   error_t err;
62   fd = OpenFile(full_path, WrOnly, &err);
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));
67     char errmsg[100];
68     internal_snprintf(errmsg, sizeof(errmsg), " (reason: %d)", err);
69     WriteToFile(kStderrFd, errmsg, internal_strlen(errmsg));
70     Die();
71   }
72   fd_pid = pid;
73 }
74 
SetReportPath(const char * path)75 void ReportFile::SetReportPath(const char *path) {
76   if (path) {
77     uptr len = internal_strlen(path);
78     if (len > sizeof(path_prefix) - 100) {
79       Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n", path[0], path[1],
80              path[2], path[3], path[4], path[5], path[6], path[7]);
81       Die();
82     }
83   }
84 
85   SpinMutexLock l(mu);
86   if (fd != kStdoutFd && fd != kStderrFd && fd != kInvalidFd)
87     CloseFile(fd);
88   fd = kInvalidFd;
89   if (!path || internal_strcmp(path, "stderr") == 0) {
90     fd = kStderrFd;
91   } else if (internal_strcmp(path, "stdout") == 0) {
92     fd = kStdoutFd;
93   } else {
94     internal_snprintf(path_prefix, kMaxPathLength, "%s", path);
95   }
96 }
97 
GetReportPath()98 const char *ReportFile::GetReportPath() {
99   SpinMutexLock l(mu);
100   ReopenIfNecessary();
101   return full_path;
102 }
103 
ReadFileToBuffer(const char * file_name,char ** buff,uptr * buff_size,uptr * read_len,uptr max_len,error_t * errno_p)104 bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
105                       uptr *read_len, uptr max_len, error_t *errno_p) {
106   *buff = nullptr;
107   *buff_size = 0;
108   *read_len = 0;
109   if (!max_len)
110     return true;
111   uptr PageSize = GetPageSizeCached();
112   uptr kMinFileLen = Min(PageSize, max_len);
113 
114   // The files we usually open are not seekable, so try different buffer sizes.
115   for (uptr size = kMinFileLen;; size = Min(size * 2, max_len)) {
116     UnmapOrDie(*buff, *buff_size);
117     *buff = (char*)MmapOrDie(size, __func__);
118     *buff_size = size;
119     fd_t fd = OpenFile(file_name, RdOnly, errno_p);
120     if (fd == kInvalidFd) {
121       UnmapOrDie(*buff, *buff_size);
122       return false;
123     }
124     *read_len = 0;
125     // Read up to one page at a time.
126     bool reached_eof = false;
127     while (*read_len < size) {
128       uptr just_read;
129       if (!ReadFromFile(fd, *buff + *read_len, size - *read_len, &just_read,
130                         errno_p)) {
131         UnmapOrDie(*buff, *buff_size);
132         CloseFile(fd);
133         return false;
134       }
135       *read_len += just_read;
136       if (just_read == 0 || *read_len == max_len) {
137         reached_eof = true;
138         break;
139       }
140     }
141     CloseFile(fd);
142     if (reached_eof)  // We've read the whole file.
143       break;
144   }
145   return true;
146 }
147 
ReadFileToVector(const char * file_name,InternalMmapVectorNoCtor<char> * buff,uptr max_len,error_t * errno_p)148 bool ReadFileToVector(const char *file_name,
149                       InternalMmapVectorNoCtor<char> *buff, uptr max_len,
150                       error_t *errno_p) {
151   buff->clear();
152   if (!max_len)
153     return true;
154   uptr PageSize = GetPageSizeCached();
155   fd_t fd = OpenFile(file_name, RdOnly, errno_p);
156   if (fd == kInvalidFd)
157     return false;
158   uptr read_len = 0;
159   while (read_len < max_len) {
160     if (read_len >= buff->size())
161       buff->resize(Min(Max(PageSize, read_len * 2), max_len));
162     CHECK_LT(read_len, buff->size());
163     CHECK_LE(buff->size(), max_len);
164     uptr just_read;
165     if (!ReadFromFile(fd, buff->data() + read_len, buff->size() - read_len,
166                       &just_read, errno_p)) {
167       CloseFile(fd);
168       return false;
169     }
170     read_len += just_read;
171     if (!just_read)
172       break;
173   }
174   CloseFile(fd);
175   buff->resize(read_len);
176   return true;
177 }
178 
179 static const char kPathSeparator = SANITIZER_WINDOWS ? ';' : ':';
180 
FindPathToBinary(const char * name)181 char *FindPathToBinary(const char *name) {
182   if (FileExists(name)) {
183     return internal_strdup(name);
184   }
185 
186   const char *path = GetEnv("PATH");
187   if (!path)
188     return nullptr;
189   uptr name_len = internal_strlen(name);
190   InternalMmapVector<char> buffer(kMaxPathLength);
191   const char *beg = path;
192   while (true) {
193     const char *end = internal_strchrnul(beg, kPathSeparator);
194     uptr prefix_len = end - beg;
195     if (prefix_len + name_len + 2 <= kMaxPathLength) {
196       internal_memcpy(buffer.data(), beg, prefix_len);
197       buffer[prefix_len] = '/';
198       internal_memcpy(&buffer[prefix_len + 1], name, name_len);
199       buffer[prefix_len + 1 + name_len] = '\0';
200       if (FileExists(buffer.data()))
201         return internal_strdup(buffer.data());
202     }
203     if (*end == '\0') break;
204     beg = end + 1;
205   }
206   return nullptr;
207 }
208 
209 } // namespace __sanitizer
210 
211 using namespace __sanitizer;
212 
213 extern "C" {
__sanitizer_set_report_path(const char * path)214 void __sanitizer_set_report_path(const char *path) {
215   report_file.SetReportPath(path);
216 }
217 
__sanitizer_set_report_fd(void * fd)218 void __sanitizer_set_report_fd(void *fd) {
219   report_file.fd = (fd_t)reinterpret_cast<uptr>(fd);
220   report_file.fd_pid = internal_getpid();
221 }
222 
__sanitizer_get_report_path()223 const char *__sanitizer_get_report_path() {
224   return report_file.GetReportPath();
225 }
226 } // extern "C"
227 
228 #endif  // !SANITIZER_FUCHSIA
229