llvm-project
251 строка · 7.3 Кб
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_FUCHSIA19
20#include "sanitizer_common.h"21#include "sanitizer_file.h"22# include "sanitizer_interface_internal.h"23
24namespace __sanitizer {25
26void CatastrophicErrorWrite(const char *buffer, uptr length) {27WriteToFile(kStderrFd, buffer, length);28}
29
30StaticSpinMutex report_file_mu;31ReportFile report_file = {&report_file_mu, kStderrFd, "", "", 0};32
33void RawWrite(const char *buffer) {34report_file.Write(buffer, internal_strlen(buffer));35}
36
37void ReportFile::ReopenIfNecessary() {38mu->CheckLocked();39if (fd == kStdoutFd || fd == kStderrFd) return;40
41uptr pid = internal_getpid();42// If in tracer, use the parent's file.43if (pid == stoptheworld_tracer_pid)44pid = stoptheworld_tracer_ppid;45if (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 parent48// process, close it now.49if (fd_pid == pid)50return;51else52CloseFile(fd);53}54
55const char *exe_name = GetProcessName();56if (common_flags()->log_exe_name && exe_name) {57internal_snprintf(full_path, kMaxPathLength, "%s.%s.%zu", path_prefix,58exe_name, pid);59} else {60internal_snprintf(full_path, kMaxPathLength, "%s.%zu", path_prefix, pid);61}62if (common_flags()->log_suffix) {63internal_strlcat(full_path, common_flags()->log_suffix, kMaxPathLength);64}65error_t err;66fd = OpenFile(full_path, WrOnly, &err);67if (fd == kInvalidFd) {68const char *ErrorMsgPrefix = "ERROR: Can't open file: ";69WriteToFile(kStderrFd, ErrorMsgPrefix, internal_strlen(ErrorMsgPrefix));70WriteToFile(kStderrFd, full_path, internal_strlen(full_path));71char errmsg[100];72internal_snprintf(errmsg, sizeof(errmsg), " (reason: %d)", err);73WriteToFile(kStderrFd, errmsg, internal_strlen(errmsg));74Die();75}76fd_pid = pid;77}
78
79static void RecursiveCreateParentDirs(char *path) {80if (path[0] == '\0')81return;82for (int i = 1; path[i] != '\0'; ++i) {83char save = path[i];84if (!IsPathSeparator(path[i]))85continue;86path[i] = '\0';87if (!DirExists(path) && !CreateDir(path)) {88const char *ErrorMsgPrefix = "ERROR: Can't create directory: ";89WriteToFile(kStderrFd, ErrorMsgPrefix, internal_strlen(ErrorMsgPrefix));90WriteToFile(kStderrFd, path, internal_strlen(path));91Die();92}93path[i] = save;94}95}
96
97void ReportFile::SetReportPath(const char *path) {98if (path) {99uptr len = internal_strlen(path);100if (len > sizeof(path_prefix) - 100) {101Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n", path[0], path[1],102path[2], path[3], path[4], path[5], path[6], path[7]);103Die();104}105}106
107SpinMutexLock l(mu);108if (fd != kStdoutFd && fd != kStderrFd && fd != kInvalidFd)109CloseFile(fd);110fd = kInvalidFd;111if (!path || internal_strcmp(path, "stderr") == 0) {112fd = kStderrFd;113} else if (internal_strcmp(path, "stdout") == 0) {114fd = kStdoutFd;115} else {116internal_snprintf(path_prefix, kMaxPathLength, "%s", path);117RecursiveCreateParentDirs(path_prefix);118}119}
120
121const char *ReportFile::GetReportPath() {122SpinMutexLock l(mu);123ReopenIfNecessary();124return full_path;125}
126
127bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,128uptr *read_len, uptr max_len, error_t *errno_p) {129*buff = nullptr;130*buff_size = 0;131*read_len = 0;132if (!max_len)133return true;134uptr PageSize = GetPageSizeCached();135uptr kMinFileLen = Min(PageSize, max_len);136
137// The files we usually open are not seekable, so try different buffer sizes.138for (uptr size = kMinFileLen;; size = Min(size * 2, max_len)) {139UnmapOrDie(*buff, *buff_size);140*buff = (char*)MmapOrDie(size, __func__);141*buff_size = size;142fd_t fd = OpenFile(file_name, RdOnly, errno_p);143if (fd == kInvalidFd) {144UnmapOrDie(*buff, *buff_size);145return false;146}147*read_len = 0;148// Read up to one page at a time.149bool reached_eof = false;150while (*read_len < size) {151uptr just_read;152if (!ReadFromFile(fd, *buff + *read_len, size - *read_len, &just_read,153errno_p)) {154UnmapOrDie(*buff, *buff_size);155CloseFile(fd);156return false;157}158*read_len += just_read;159if (just_read == 0 || *read_len == max_len) {160reached_eof = true;161break;162}163}164CloseFile(fd);165if (reached_eof) // We've read the whole file.166break;167}168return true;169}
170
171bool ReadFileToVector(const char *file_name,172InternalMmapVectorNoCtor<char> *buff, uptr max_len,173error_t *errno_p) {174buff->clear();175if (!max_len)176return true;177uptr PageSize = GetPageSizeCached();178fd_t fd = OpenFile(file_name, RdOnly, errno_p);179if (fd == kInvalidFd)180return false;181uptr read_len = 0;182while (read_len < max_len) {183if (read_len >= buff->size())184buff->resize(Min(Max(PageSize, read_len * 2), max_len));185CHECK_LT(read_len, buff->size());186CHECK_LE(buff->size(), max_len);187uptr just_read;188if (!ReadFromFile(fd, buff->data() + read_len, buff->size() - read_len,189&just_read, errno_p)) {190CloseFile(fd);191return false;192}193read_len += just_read;194if (!just_read)195break;196}197CloseFile(fd);198buff->resize(read_len);199return true;200}
201
202static const char kPathSeparator = SANITIZER_WINDOWS ? ';' : ':';203
204char *FindPathToBinary(const char *name) {205if (FileExists(name)) {206return internal_strdup(name);207}208
209const char *path = GetEnv("PATH");210if (!path)211return nullptr;212uptr name_len = internal_strlen(name);213InternalMmapVector<char> buffer(kMaxPathLength);214const char *beg = path;215while (true) {216const char *end = internal_strchrnul(beg, kPathSeparator);217uptr prefix_len = end - beg;218if (prefix_len + name_len + 2 <= kMaxPathLength) {219internal_memcpy(buffer.data(), beg, prefix_len);220buffer[prefix_len] = '/';221internal_memcpy(&buffer[prefix_len + 1], name, name_len);222buffer[prefix_len + 1 + name_len] = '\0';223if (FileExists(buffer.data()))224return internal_strdup(buffer.data());225}226if (*end == '\0') break;227beg = end + 1;228}229return nullptr;230}
231
232} // namespace __sanitizer233
234using namespace __sanitizer;235
236extern "C" {237void __sanitizer_set_report_path(const char *path) {238report_file.SetReportPath(path);239}
240
241void __sanitizer_set_report_fd(void *fd) {242report_file.fd = (fd_t)reinterpret_cast<uptr>(fd);243report_file.fd_pid = internal_getpid();244}
245
246const char *__sanitizer_get_report_path() {247return report_file.GetReportPath();248}
249} // extern "C"250
251#endif // !SANITIZER_FUCHSIA252