1 //===-- sanitizer_file.cpp -----------------------------------------------===//
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
7 //===---------------------------------------------------------------------===//
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"
23 namespace __sanitizer
{
25 void CatastrophicErrorWrite(const char *buffer
, uptr length
) {
26 WriteToFile(kStderrFd
, buffer
, length
);
29 StaticSpinMutex report_file_mu
;
30 ReportFile report_file
= {&report_file_mu
, kStderrFd
, "", "", 0};
32 void RawWrite(const char *buffer
) {
33 report_file
.Write(buffer
, internal_strlen(buffer
));
36 void ReportFile::ReopenIfNecessary() {
38 if (fd
== kStdoutFd
|| fd
== kStderrFd
) return;
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.
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
,
59 internal_snprintf(full_path
, kMaxPathLength
, "%s.%zu", path_prefix
, pid
);
61 if (common_flags()->log_suffix
) {
62 internal_strlcat(full_path
, common_flags()->log_suffix
, kMaxPathLength
);
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
));
71 internal_snprintf(errmsg
, sizeof(errmsg
), " (reason: %d)", err
);
72 WriteToFile(kStderrFd
, errmsg
, internal_strlen(errmsg
));
78 static void RecursiveCreateParentDirs(char *path
) {
81 for (int i
= 1; path
[i
] != '\0'; ++i
) {
83 if (!IsPathSeparator(path
[i
]))
86 /* Some of these will fail, because the directory exists, ignore it. */
92 void ReportFile::SetReportPath(const char *path
) {
94 uptr len
= internal_strlen(path
);
95 if (len
> sizeof(path_prefix
) - 100) {
96 Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n", path
[0], path
[1],
97 path
[2], path
[3], path
[4], path
[5], path
[6], path
[7]);
103 if (fd
!= kStdoutFd
&& fd
!= kStderrFd
&& fd
!= kInvalidFd
)
106 if (!path
|| internal_strcmp(path
, "stderr") == 0) {
108 } else if (internal_strcmp(path
, "stdout") == 0) {
111 internal_snprintf(path_prefix
, kMaxPathLength
, "%s", path
);
112 RecursiveCreateParentDirs(path_prefix
);
116 const char *ReportFile::GetReportPath() {
122 bool ReadFileToBuffer(const char *file_name
, char **buff
, uptr
*buff_size
,
123 uptr
*read_len
, uptr max_len
, error_t
*errno_p
) {
129 uptr PageSize
= GetPageSizeCached();
130 uptr kMinFileLen
= Min(PageSize
, max_len
);
132 // The files we usually open are not seekable, so try different buffer sizes.
133 for (uptr size
= kMinFileLen
;; size
= Min(size
* 2, max_len
)) {
134 UnmapOrDie(*buff
, *buff_size
);
135 *buff
= (char*)MmapOrDie(size
, __func__
);
137 fd_t fd
= OpenFile(file_name
, RdOnly
, errno_p
);
138 if (fd
== kInvalidFd
) {
139 UnmapOrDie(*buff
, *buff_size
);
143 // Read up to one page at a time.
144 bool reached_eof
= false;
145 while (*read_len
< size
) {
147 if (!ReadFromFile(fd
, *buff
+ *read_len
, size
- *read_len
, &just_read
,
149 UnmapOrDie(*buff
, *buff_size
);
153 *read_len
+= just_read
;
154 if (just_read
== 0 || *read_len
== max_len
) {
160 if (reached_eof
) // We've read the whole file.
166 bool ReadFileToVector(const char *file_name
,
167 InternalMmapVectorNoCtor
<char> *buff
, uptr max_len
,
172 uptr PageSize
= GetPageSizeCached();
173 fd_t fd
= OpenFile(file_name
, RdOnly
, errno_p
);
174 if (fd
== kInvalidFd
)
177 while (read_len
< max_len
) {
178 if (read_len
>= buff
->size())
179 buff
->resize(Min(Max(PageSize
, read_len
* 2), max_len
));
180 CHECK_LT(read_len
, buff
->size());
181 CHECK_LE(buff
->size(), max_len
);
183 if (!ReadFromFile(fd
, buff
->data() + read_len
, buff
->size() - read_len
,
184 &just_read
, errno_p
)) {
188 read_len
+= just_read
;
193 buff
->resize(read_len
);
197 static const char kPathSeparator
= SANITIZER_WINDOWS
? ';' : ':';
199 char *FindPathToBinary(const char *name
) {
200 if (FileExists(name
)) {
201 return internal_strdup(name
);
204 const char *path
= GetEnv("PATH");
207 uptr name_len
= internal_strlen(name
);
208 InternalMmapVector
<char> buffer(kMaxPathLength
);
209 const char *beg
= path
;
211 const char *end
= internal_strchrnul(beg
, kPathSeparator
);
212 uptr prefix_len
= end
- beg
;
213 if (prefix_len
+ name_len
+ 2 <= kMaxPathLength
) {
214 internal_memcpy(buffer
.data(), beg
, prefix_len
);
215 buffer
[prefix_len
] = '/';
216 internal_memcpy(&buffer
[prefix_len
+ 1], name
, name_len
);
217 buffer
[prefix_len
+ 1 + name_len
] = '\0';
218 if (FileExists(buffer
.data()))
219 return internal_strdup(buffer
.data());
221 if (*end
== '\0') break;
227 } // namespace __sanitizer
229 using namespace __sanitizer
;
232 void __sanitizer_set_report_path(const char *path
) {
233 report_file
.SetReportPath(path
);
236 void __sanitizer_set_report_fd(void *fd
) {
237 report_file
.fd
= (fd_t
)reinterpret_cast<uptr
>(fd
);
238 report_file
.fd_pid
= internal_getpid();
241 const char *__sanitizer_get_report_path() {
242 return report_file
.GetReportPath();
246 #endif // !SANITIZER_FUCHSIA