sanitizer_common.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //===-- sanitizer_common.cc -----------------------------------------------===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is shared between AddressSanitizer and ThreadSanitizer
  9. // run-time libraries.
  10. //===----------------------------------------------------------------------===//
  11. #include "sanitizer_common.h"
  12. #include "sanitizer_flags.h"
  13. #include "sanitizer_libc.h"
  14. namespace __sanitizer {
  15. const char *SanitizerToolName = "SanitizerTool";
  16. uptr GetPageSizeCached() {
  17. static uptr PageSize;
  18. if (!PageSize)
  19. PageSize = GetPageSize();
  20. return PageSize;
  21. }
  22. // By default, dump to stderr. If |log_to_file| is true and |report_fd_pid|
  23. // isn't equal to the current PID, try to obtain file descriptor by opening
  24. // file "report_path_prefix.<PID>".
  25. fd_t report_fd = kStderrFd;
  26. // Set via __sanitizer_set_report_path.
  27. bool log_to_file = false;
  28. char report_path_prefix[sizeof(report_path_prefix)];
  29. // PID of process that opened |report_fd|. If a fork() occurs, the PID of the
  30. // child thread will be different from |report_fd_pid|.
  31. uptr report_fd_pid = 0;
  32. // PID of the tracer task in StopTheWorld. It shares the address space with the
  33. // main process, but has a different PID and thus requires special handling.
  34. uptr stoptheworld_tracer_pid = 0;
  35. // Cached pid of parent process - if the parent process dies, we want to keep
  36. // writing to the same log file.
  37. uptr stoptheworld_tracer_ppid = 0;
  38. static DieCallbackType DieCallback;
  39. void SetDieCallback(DieCallbackType callback) {
  40. DieCallback = callback;
  41. }
  42. DieCallbackType GetDieCallback() {
  43. return DieCallback;
  44. }
  45. void NORETURN Die() {
  46. if (DieCallback) {
  47. DieCallback();
  48. }
  49. internal__exit(1);
  50. }
  51. static CheckFailedCallbackType CheckFailedCallback;
  52. void SetCheckFailedCallback(CheckFailedCallbackType callback) {
  53. CheckFailedCallback = callback;
  54. }
  55. void NORETURN CheckFailed(const char *file, int line, const char *cond,
  56. u64 v1, u64 v2) {
  57. if (CheckFailedCallback) {
  58. CheckFailedCallback(file, line, cond, v1, v2);
  59. }
  60. Report("Sanitizer CHECK failed: %s:%d %s (%lld, %lld)\n", file, line, cond,
  61. v1, v2);
  62. Die();
  63. }
  64. uptr ReadFileToBuffer(const char *file_name, char **buff,
  65. uptr *buff_size, uptr max_len) {
  66. uptr PageSize = GetPageSizeCached();
  67. uptr kMinFileLen = PageSize;
  68. uptr read_len = 0;
  69. *buff = 0;
  70. *buff_size = 0;
  71. // The files we usually open are not seekable, so try different buffer sizes.
  72. for (uptr size = kMinFileLen; size <= max_len; size *= 2) {
  73. uptr openrv = OpenFile(file_name, /*write*/ false);
  74. if (internal_iserror(openrv)) return 0;
  75. fd_t fd = openrv;
  76. UnmapOrDie(*buff, *buff_size);
  77. *buff = (char*)MmapOrDie(size, __func__);
  78. *buff_size = size;
  79. // Read up to one page at a time.
  80. read_len = 0;
  81. bool reached_eof = false;
  82. while (read_len + PageSize <= size) {
  83. uptr just_read = internal_read(fd, *buff + read_len, PageSize);
  84. if (just_read == 0) {
  85. reached_eof = true;
  86. break;
  87. }
  88. read_len += just_read;
  89. }
  90. internal_close(fd);
  91. if (reached_eof) // We've read the whole file.
  92. break;
  93. }
  94. return read_len;
  95. }
  96. typedef bool UptrComparisonFunction(const uptr &a, const uptr &b);
  97. template<class T>
  98. static inline bool CompareLess(const T &a, const T &b) {
  99. return a < b;
  100. }
  101. void SortArray(uptr *array, uptr size) {
  102. InternalSort<uptr*, UptrComparisonFunction>(&array, size, CompareLess);
  103. }
  104. // We want to map a chunk of address space aligned to 'alignment'.
  105. // We do it by maping a bit more and then unmaping redundant pieces.
  106. // We probably can do it with fewer syscalls in some OS-dependent way.
  107. void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type) {
  108. // uptr PageSize = GetPageSizeCached();
  109. CHECK(IsPowerOfTwo(size));
  110. CHECK(IsPowerOfTwo(alignment));
  111. uptr map_size = size + alignment;
  112. uptr map_res = (uptr)MmapOrDie(map_size, mem_type);
  113. uptr map_end = map_res + map_size;
  114. uptr res = map_res;
  115. if (res & (alignment - 1)) // Not aligned.
  116. res = (map_res + alignment) & ~(alignment - 1);
  117. uptr end = res + size;
  118. if (res != map_res)
  119. UnmapOrDie((void*)map_res, res - map_res);
  120. if (end != map_end)
  121. UnmapOrDie((void*)end, map_end - end);
  122. return (void*)res;
  123. }
  124. const char *StripPathPrefix(const char *filepath,
  125. const char *strip_path_prefix) {
  126. if (filepath == 0) return 0;
  127. if (strip_path_prefix == 0) return filepath;
  128. const char *pos = internal_strstr(filepath, strip_path_prefix);
  129. if (pos == 0) return filepath;
  130. pos += internal_strlen(strip_path_prefix);
  131. if (pos[0] == '.' && pos[1] == '/')
  132. pos += 2;
  133. return pos;
  134. }
  135. const char *StripModuleName(const char *module) {
  136. if (module == 0)
  137. return 0;
  138. if (const char *slash_pos = internal_strrchr(module, '/'))
  139. return slash_pos + 1;
  140. return module;
  141. }
  142. void ReportErrorSummary(const char *error_message) {
  143. if (!common_flags()->print_summary)
  144. return;
  145. InternalScopedBuffer<char> buff(kMaxSummaryLength);
  146. internal_snprintf(buff.data(), buff.size(),
  147. "SUMMARY: %s: %s", SanitizerToolName, error_message);
  148. __sanitizer_report_error_summary(buff.data());
  149. }
  150. void ReportErrorSummary(const char *error_type, const char *file,
  151. int line, const char *function) {
  152. if (!common_flags()->print_summary)
  153. return;
  154. InternalScopedBuffer<char> buff(kMaxSummaryLength);
  155. internal_snprintf(
  156. buff.data(), buff.size(), "%s %s:%d %s", error_type,
  157. file ? StripPathPrefix(file, common_flags()->strip_path_prefix) : "??",
  158. line, function ? function : "??");
  159. ReportErrorSummary(buff.data());
  160. }
  161. LoadedModule::LoadedModule(const char *module_name, uptr base_address) {
  162. full_name_ = internal_strdup(module_name);
  163. base_address_ = base_address;
  164. n_ranges_ = 0;
  165. }
  166. void LoadedModule::addAddressRange(uptr beg, uptr end, bool executable) {
  167. CHECK_LT(n_ranges_, kMaxNumberOfAddressRanges);
  168. ranges_[n_ranges_].beg = beg;
  169. ranges_[n_ranges_].end = end;
  170. exec_[n_ranges_] = executable;
  171. n_ranges_++;
  172. }
  173. bool LoadedModule::containsAddress(uptr address) const {
  174. for (uptr i = 0; i < n_ranges_; i++) {
  175. if (ranges_[i].beg <= address && address < ranges_[i].end)
  176. return true;
  177. }
  178. return false;
  179. }
  180. static atomic_uintptr_t g_total_mmaped;
  181. void IncreaseTotalMmap(uptr size) {
  182. if (!common_flags()->mmap_limit_mb) return;
  183. uptr total_mmaped =
  184. atomic_fetch_add(&g_total_mmaped, size, memory_order_relaxed) + size;
  185. if ((total_mmaped >> 20) > common_flags()->mmap_limit_mb) {
  186. // Since for now mmap_limit_mb is not a user-facing flag, just CHECK.
  187. uptr mmap_limit_mb = common_flags()->mmap_limit_mb;
  188. common_flags()->mmap_limit_mb = 0; // Allow mmap in CHECK.
  189. RAW_CHECK(total_mmaped >> 20 < mmap_limit_mb);
  190. }
  191. }
  192. void DecreaseTotalMmap(uptr size) {
  193. if (!common_flags()->mmap_limit_mb) return;
  194. atomic_fetch_sub(&g_total_mmaped, size, memory_order_relaxed);
  195. }
  196. } // namespace __sanitizer
  197. using namespace __sanitizer; // NOLINT
  198. extern "C" {
  199. void __sanitizer_set_report_path(const char *path) {
  200. if (!path)
  201. return;
  202. uptr len = internal_strlen(path);
  203. if (len > sizeof(report_path_prefix) - 100) {
  204. Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n",
  205. path[0], path[1], path[2], path[3],
  206. path[4], path[5], path[6], path[7]);
  207. Die();
  208. }
  209. if (report_fd != kStdoutFd &&
  210. report_fd != kStderrFd &&
  211. report_fd != kInvalidFd)
  212. internal_close(report_fd);
  213. report_fd = kInvalidFd;
  214. log_to_file = false;
  215. if (internal_strcmp(path, "stdout") == 0) {
  216. report_fd = kStdoutFd;
  217. } else if (internal_strcmp(path, "stderr") == 0) {
  218. report_fd = kStderrFd;
  219. } else {
  220. internal_strncpy(report_path_prefix, path, sizeof(report_path_prefix));
  221. report_path_prefix[len] = '\0';
  222. log_to_file = true;
  223. }
  224. }
  225. void __sanitizer_report_error_summary(const char *error_summary) {
  226. Printf("%s\n", error_summary);
  227. }
  228. } // extern "C"