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