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 fd
= OpenFile(full_path
, WrOnly
);
62 if (fd
== kInvalidFd
) {
63 const char *ErrorMsgPrefix
= "ERROR: Can't open file: ";
64 WriteToFile(kStderrFd
, ErrorMsgPrefix
, internal_strlen(ErrorMsgPrefix
));
65 WriteToFile(kStderrFd
, full_path
, internal_strlen(full_path
));
71 void ReportFile::SetReportPath(const char *path
) {
74 uptr len
= internal_strlen(path
);
75 if (len
> sizeof(path_prefix
) - 100) {
76 Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n",
77 path
[0], path
[1], path
[2], path
[3],
78 path
[4], path
[5], path
[6], path
[7]);
83 if (fd
!= kStdoutFd
&& fd
!= kStderrFd
&& fd
!= kInvalidFd
)
86 if (internal_strcmp(path
, "stdout") == 0) {
88 } else if (internal_strcmp(path
, "stderr") == 0) {
91 internal_snprintf(path_prefix
, kMaxPathLength
, "%s", path
);
95 bool ReadFileToBuffer(const char *file_name
, char **buff
, uptr
*buff_size
,
96 uptr
*read_len
, uptr max_len
, error_t
*errno_p
) {
102 uptr PageSize
= GetPageSizeCached();
103 uptr kMinFileLen
= Min(PageSize
, max_len
);
105 // The files we usually open are not seekable, so try different buffer sizes.
106 for (uptr size
= kMinFileLen
;; size
= Min(size
* 2, max_len
)) {
107 UnmapOrDie(*buff
, *buff_size
);
108 *buff
= (char*)MmapOrDie(size
, __func__
);
110 fd_t fd
= OpenFile(file_name
, RdOnly
, errno_p
);
111 if (fd
== kInvalidFd
) {
112 UnmapOrDie(*buff
, *buff_size
);
116 // Read up to one page at a time.
117 bool reached_eof
= false;
118 while (*read_len
< size
) {
120 if (!ReadFromFile(fd
, *buff
+ *read_len
, size
- *read_len
, &just_read
,
122 UnmapOrDie(*buff
, *buff_size
);
126 *read_len
+= just_read
;
127 if (just_read
== 0 || *read_len
== max_len
) {
133 if (reached_eof
) // We've read the whole file.
139 bool ReadFileToVector(const char *file_name
,
140 InternalMmapVectorNoCtor
<char> *buff
, uptr max_len
,
145 uptr PageSize
= GetPageSizeCached();
146 fd_t fd
= OpenFile(file_name
, RdOnly
, errno_p
);
147 if (fd
== kInvalidFd
)
150 while (read_len
< max_len
) {
151 if (read_len
>= buff
->size())
152 buff
->resize(Min(Max(PageSize
, read_len
* 2), max_len
));
153 CHECK_LT(read_len
, buff
->size());
154 CHECK_LE(buff
->size(), max_len
);
156 if (!ReadFromFile(fd
, buff
->data() + read_len
, buff
->size() - read_len
,
157 &just_read
, errno_p
)) {
161 read_len
+= just_read
;
166 buff
->resize(read_len
);
170 static const char kPathSeparator
= SANITIZER_WINDOWS
? ';' : ':';
172 char *FindPathToBinary(const char *name
) {
173 if (FileExists(name
)) {
174 return internal_strdup(name
);
177 const char *path
= GetEnv("PATH");
180 uptr name_len
= internal_strlen(name
);
181 InternalMmapVector
<char> buffer(kMaxPathLength
);
182 const char *beg
= path
;
184 const char *end
= internal_strchrnul(beg
, kPathSeparator
);
185 uptr prefix_len
= end
- beg
;
186 if (prefix_len
+ name_len
+ 2 <= kMaxPathLength
) {
187 internal_memcpy(buffer
.data(), beg
, prefix_len
);
188 buffer
[prefix_len
] = '/';
189 internal_memcpy(&buffer
[prefix_len
+ 1], name
, name_len
);
190 buffer
[prefix_len
+ 1 + name_len
] = '\0';
191 if (FileExists(buffer
.data()))
192 return internal_strdup(buffer
.data());
194 if (*end
== '\0') break;
200 } // namespace __sanitizer
202 using namespace __sanitizer
;
205 void __sanitizer_set_report_path(const char *path
) {
206 report_file
.SetReportPath(path
);
209 void __sanitizer_set_report_fd(void *fd
) {
210 report_file
.fd
= (fd_t
)reinterpret_cast<uptr
>(fd
);
211 report_file
.fd_pid
= internal_getpid();
215 #endif // !SANITIZER_FUCHSIA