[flang][cuda] Adapt ExternalNameConversion to work in gpu module (#117039)
[llvm-project.git] / compiler-rt / lib / sanitizer_common / sanitizer_file.cpp
blob96af270f9d8b5030f580960a5a5a1addf81ccca5
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.
14 //===---------------------------------------------------------------------===//
16 #include "sanitizer_platform.h"
18 #if !SANITIZER_FUCHSIA
20 #include "sanitizer_common.h"
21 #include "sanitizer_file.h"
22 # include "sanitizer_interface_internal.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() {
38 mu->CheckLocked();
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.
49 if (fd_pid == pid)
50 return;
51 else
52 CloseFile(fd);
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,
58 exe_name, pid);
59 } else {
60 internal_snprintf(full_path, kMaxPathLength, "%s.%zu", path_prefix, pid);
62 if (common_flags()->log_suffix) {
63 internal_strlcat(full_path, common_flags()->log_suffix, kMaxPathLength);
65 error_t err;
66 fd = OpenFile(full_path, WrOnly, &err);
67 if (fd == kInvalidFd) {
68 const char *ErrorMsgPrefix = "ERROR: Can't open file: ";
69 WriteToFile(kStderrFd, ErrorMsgPrefix, internal_strlen(ErrorMsgPrefix));
70 WriteToFile(kStderrFd, full_path, internal_strlen(full_path));
71 char errmsg[100];
72 internal_snprintf(errmsg, sizeof(errmsg), " (reason: %d)\n", err);
73 WriteToFile(kStderrFd, errmsg, internal_strlen(errmsg));
74 Die();
76 fd_pid = pid;
79 static void RecursiveCreateParentDirs(char *path) {
80 if (path[0] == '\0')
81 return;
82 for (int i = 1; path[i] != '\0'; ++i) {
83 char save = path[i];
84 if (!IsPathSeparator(path[i]))
85 continue;
86 path[i] = '\0';
87 if (!DirExists(path) && !CreateDir(path)) {
88 const char *ErrorMsgPrefix = "ERROR: Can't create directory: ";
89 WriteToFile(kStderrFd, ErrorMsgPrefix, internal_strlen(ErrorMsgPrefix));
90 WriteToFile(kStderrFd, path, internal_strlen(path));
91 const char *ErrorMsgSuffix = "\n";
92 WriteToFile(kStderrFd, ErrorMsgSuffix, internal_strlen(ErrorMsgSuffix));
93 Die();
95 path[i] = save;
99 void ReportFile::SetReportPath(const char *path) {
100 if (path) {
101 uptr len = internal_strlen(path);
102 if (len > sizeof(path_prefix) - 100) {
103 Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n", path[0], path[1],
104 path[2], path[3], path[4], path[5], path[6], path[7]);
105 Die();
109 SpinMutexLock l(mu);
110 if (fd != kStdoutFd && fd != kStderrFd && fd != kInvalidFd)
111 CloseFile(fd);
112 fd = kInvalidFd;
113 if (!path || internal_strcmp(path, "stderr") == 0) {
114 fd = kStderrFd;
115 } else if (internal_strcmp(path, "stdout") == 0) {
116 fd = kStdoutFd;
117 } else {
118 internal_snprintf(path_prefix, kMaxPathLength, "%s", path);
119 RecursiveCreateParentDirs(path_prefix);
123 const char *ReportFile::GetReportPath() {
124 SpinMutexLock l(mu);
125 ReopenIfNecessary();
126 return full_path;
129 bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
130 uptr *read_len, uptr max_len, error_t *errno_p) {
131 *buff = nullptr;
132 *buff_size = 0;
133 *read_len = 0;
134 if (!max_len)
135 return true;
136 uptr PageSize = GetPageSizeCached();
137 uptr kMinFileLen = Min(PageSize, max_len);
139 // The files we usually open are not seekable, so try different buffer sizes.
140 for (uptr size = kMinFileLen;; size = Min(size * 2, max_len)) {
141 UnmapOrDie(*buff, *buff_size);
142 *buff = (char*)MmapOrDie(size, __func__);
143 *buff_size = size;
144 fd_t fd = OpenFile(file_name, RdOnly, errno_p);
145 if (fd == kInvalidFd) {
146 UnmapOrDie(*buff, *buff_size);
147 return false;
149 *read_len = 0;
150 // Read up to one page at a time.
151 bool reached_eof = false;
152 while (*read_len < size) {
153 uptr just_read;
154 if (!ReadFromFile(fd, *buff + *read_len, size - *read_len, &just_read,
155 errno_p)) {
156 UnmapOrDie(*buff, *buff_size);
157 CloseFile(fd);
158 return false;
160 *read_len += just_read;
161 if (just_read == 0 || *read_len == max_len) {
162 reached_eof = true;
163 break;
166 CloseFile(fd);
167 if (reached_eof) // We've read the whole file.
168 break;
170 return true;
173 bool ReadFileToVector(const char *file_name,
174 InternalMmapVectorNoCtor<char> *buff, uptr max_len,
175 error_t *errno_p) {
176 buff->clear();
177 if (!max_len)
178 return true;
179 uptr PageSize = GetPageSizeCached();
180 fd_t fd = OpenFile(file_name, RdOnly, errno_p);
181 if (fd == kInvalidFd)
182 return false;
183 uptr read_len = 0;
184 while (read_len < max_len) {
185 if (read_len >= buff->size())
186 buff->resize(Min(Max(PageSize, read_len * 2), max_len));
187 CHECK_LT(read_len, buff->size());
188 CHECK_LE(buff->size(), max_len);
189 uptr just_read;
190 if (!ReadFromFile(fd, buff->data() + read_len, buff->size() - read_len,
191 &just_read, errno_p)) {
192 CloseFile(fd);
193 return false;
195 read_len += just_read;
196 if (!just_read)
197 break;
199 CloseFile(fd);
200 buff->resize(read_len);
201 return true;
204 static const char kPathSeparator = SANITIZER_WINDOWS ? ';' : ':';
206 char *FindPathToBinary(const char *name) {
207 if (FileExists(name)) {
208 return internal_strdup(name);
211 const char *path = GetEnv("PATH");
212 if (!path)
213 return nullptr;
214 uptr name_len = internal_strlen(name);
215 InternalMmapVector<char> buffer(kMaxPathLength);
216 const char *beg = path;
217 while (true) {
218 const char *end = internal_strchrnul(beg, kPathSeparator);
219 uptr prefix_len = end - beg;
220 if (prefix_len + name_len + 2 <= kMaxPathLength) {
221 internal_memcpy(buffer.data(), beg, prefix_len);
222 buffer[prefix_len] = '/';
223 internal_memcpy(&buffer[prefix_len + 1], name, name_len);
224 buffer[prefix_len + 1 + name_len] = '\0';
225 if (FileExists(buffer.data()))
226 return internal_strdup(buffer.data());
228 if (*end == '\0') break;
229 beg = end + 1;
231 return nullptr;
234 } // namespace __sanitizer
236 using namespace __sanitizer;
238 extern "C" {
239 void __sanitizer_set_report_path(const char *path) {
240 report_file.SetReportPath(path);
243 void __sanitizer_set_report_fd(void *fd) {
244 report_file.fd = (fd_t)reinterpret_cast<uptr>(fd);
245 report_file.fd_pid = internal_getpid();
248 const char *__sanitizer_get_report_path() {
249 return report_file.GetReportPath();
251 } // extern "C"
253 #endif // !SANITIZER_FUCHSIA