sanitizer_file.cpp 7.1 KB

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