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 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 32 void RawWrite(const char *buffer) { 33 report_file.Write(buffer, internal_strlen(buffer)); 34 } 35 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 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)); 66 Die(); 67 } 68 fd_pid = pid; 69 } 70 71 void ReportFile::SetReportPath(const char *path) { 72 if (!path) 73 return; 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]); 79 Die(); 80 } 81 82 SpinMutexLock l(mu); 83 if (fd != kStdoutFd && fd != kStderrFd && fd != kInvalidFd) 84 CloseFile(fd); 85 fd = kInvalidFd; 86 if (internal_strcmp(path, "stdout") == 0) { 87 fd = kStdoutFd; 88 } else if (internal_strcmp(path, "stderr") == 0) { 89 fd = kStderrFd; 90 } else { 91 internal_snprintf(path_prefix, kMaxPathLength, "%s", path); 92 } 93 } 94 95 bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size, 96 uptr *read_len, uptr max_len, error_t *errno_p) { 97 *buff = nullptr; 98 *buff_size = 0; 99 *read_len = 0; 100 if (!max_len) 101 return true; 102 uptr PageSize = GetPageSizeCached(); 103 uptr kMinFileLen = Min(PageSize, max_len); 104 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__); 109 *buff_size = size; 110 fd_t fd = OpenFile(file_name, RdOnly, errno_p); 111 if (fd == kInvalidFd) { 112 UnmapOrDie(*buff, *buff_size); 113 return false; 114 } 115 *read_len = 0; 116 // Read up to one page at a time. 117 bool reached_eof = false; 118 while (*read_len < size) { 119 uptr just_read; 120 if (!ReadFromFile(fd, *buff + *read_len, size - *read_len, &just_read, 121 errno_p)) { 122 UnmapOrDie(*buff, *buff_size); 123 CloseFile(fd); 124 return false; 125 } 126 *read_len += just_read; 127 if (just_read == 0 || *read_len == max_len) { 128 reached_eof = true; 129 break; 130 } 131 } 132 CloseFile(fd); 133 if (reached_eof) // We've read the whole file. 134 break; 135 } 136 return true; 137 } 138 139 bool ReadFileToVector(const char *file_name, 140 InternalMmapVectorNoCtor<char> *buff, uptr max_len, 141 error_t *errno_p) { 142 buff->clear(); 143 if (!max_len) 144 return true; 145 uptr PageSize = GetPageSizeCached(); 146 fd_t fd = OpenFile(file_name, RdOnly, errno_p); 147 if (fd == kInvalidFd) 148 return false; 149 uptr read_len = 0; 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); 155 uptr just_read; 156 if (!ReadFromFile(fd, buff->data() + read_len, buff->size() - read_len, 157 &just_read, errno_p)) { 158 CloseFile(fd); 159 return false; 160 } 161 read_len += just_read; 162 if (!just_read) 163 break; 164 } 165 CloseFile(fd); 166 buff->resize(read_len); 167 return true; 168 } 169 170 static const char kPathSeparator = SANITIZER_WINDOWS ? ';' : ':'; 171 172 char *FindPathToBinary(const char *name) { 173 if (FileExists(name)) { 174 return internal_strdup(name); 175 } 176 177 const char *path = GetEnv("PATH"); 178 if (!path) 179 return nullptr; 180 uptr name_len = internal_strlen(name); 181 InternalMmapVector<char> buffer(kMaxPathLength); 182 const char *beg = path; 183 while (true) { 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()); 193 } 194 if (*end == '\0') break; 195 beg = end + 1; 196 } 197 return nullptr; 198 } 199 200 } // namespace __sanitizer 201 202 using namespace __sanitizer; 203 204 extern "C" { 205 void __sanitizer_set_report_path(const char *path) { 206 report_file.SetReportPath(path); 207 } 208 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(); 212 } 213 } // extern "C" 214 215 #endif // !SANITIZER_FUCHSIA 216