sanitizer_file.cpp 7.3 KB

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