| Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 1 | //===-- sanitizer_common.cc -----------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is shared between AddressSanitizer and ThreadSanitizer |
| 11 | // run-time libraries. |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "sanitizer_common.h" |
| 15 | #include "sanitizer_libc.h" |
| Alexey Samsonov | e4a8898 | 2012-06-15 07:00:31 +0000 | [diff] [blame] | 16 | |
| Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 17 | namespace __sanitizer { |
| 18 | |
| Alexey Samsonov | fd67c83 | 2012-11-02 09:23:36 +0000 | [diff] [blame] | 19 | // By default, dump to stderr. If report_fd is kInvalidFd, try to obtain file |
| 20 | // descriptor by opening file in report_path. |
| Alexey Samsonov | 20ba98f | 2012-11-02 09:38:47 +0000 | [diff] [blame] | 21 | static fd_t report_fd = kStderrFd; |
| Kostya Serebryany | 45d849c | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 22 | static char report_path[4096]; // Set via __sanitizer_set_report_path. |
| 23 | |
| Alexey Samsonov | 5c6b93b | 2012-09-11 09:44:48 +0000 | [diff] [blame] | 24 | static void (*DieCallback)(void); |
| 25 | void SetDieCallback(void (*callback)(void)) { |
| 26 | DieCallback = callback; |
| 27 | } |
| 28 | |
| 29 | void NORETURN Die() { |
| 30 | if (DieCallback) { |
| 31 | DieCallback(); |
| 32 | } |
| 33 | Exit(1); |
| 34 | } |
| 35 | |
| 36 | static CheckFailedCallbackType CheckFailedCallback; |
| 37 | void SetCheckFailedCallback(CheckFailedCallbackType callback) { |
| 38 | CheckFailedCallback = callback; |
| 39 | } |
| 40 | |
| 41 | void NORETURN CheckFailed(const char *file, int line, const char *cond, |
| 42 | u64 v1, u64 v2) { |
| 43 | if (CheckFailedCallback) { |
| 44 | CheckFailedCallback(file, line, cond, v1, v2); |
| 45 | } |
| 46 | Report("Sanitizer CHECK failed: %s:%d %s (%zd, %zd)\n", file, line, cond, |
| 47 | v1, v2); |
| 48 | Die(); |
| 49 | } |
| 50 | |
| Alexey Samsonov | 5835889 | 2012-11-02 15:18:34 +0000 | [diff] [blame^] | 51 | static void MaybeOpenReportFile() { |
| 52 | if (report_fd != kInvalidFd) |
| 53 | return; |
| 54 | fd_t fd = internal_open(report_path, true); |
| 55 | if (fd == kInvalidFd) { |
| 56 | report_fd = kStderrFd; |
| 57 | Report("ERROR: Can't open file: %s\n", report_path); |
| 58 | Die(); |
| 59 | } |
| 60 | report_fd = fd; |
| 61 | } |
| 62 | |
| 63 | bool PrintsToTty() { |
| 64 | MaybeOpenReportFile(); |
| 65 | return internal_isatty(report_fd); |
| 66 | } |
| 67 | |
| Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 68 | void RawWrite(const char *buffer) { |
| 69 | static const char *kRawWriteError = "RawWrite can't output requested buffer!"; |
| 70 | uptr length = (uptr)internal_strlen(buffer); |
| Alexey Samsonov | 5835889 | 2012-11-02 15:18:34 +0000 | [diff] [blame^] | 71 | MaybeOpenReportFile(); |
| Kostya Serebryany | 45d849c | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 72 | if (length != internal_write(report_fd, buffer, length)) { |
| 73 | internal_write(report_fd, kRawWriteError, internal_strlen(kRawWriteError)); |
| Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 74 | Die(); |
| 75 | } |
| 76 | } |
| 77 | |
| Alexey Samsonov | fe44fbd | 2012-06-07 05:38:26 +0000 | [diff] [blame] | 78 | uptr ReadFileToBuffer(const char *file_name, char **buff, |
| 79 | uptr *buff_size, uptr max_len) { |
| 80 | const uptr kMinFileLen = kPageSize; |
| 81 | uptr read_len = 0; |
| 82 | *buff = 0; |
| 83 | *buff_size = 0; |
| 84 | // The files we usually open are not seekable, so try different buffer sizes. |
| 85 | for (uptr size = kMinFileLen; size <= max_len; size *= 2) { |
| 86 | fd_t fd = internal_open(file_name, /*write*/ false); |
| 87 | if (fd == kInvalidFd) return 0; |
| 88 | UnmapOrDie(*buff, *buff_size); |
| 89 | *buff = (char*)MmapOrDie(size, __FUNCTION__); |
| 90 | *buff_size = size; |
| 91 | // Read up to one page at a time. |
| 92 | read_len = 0; |
| 93 | bool reached_eof = false; |
| 94 | while (read_len + kPageSize <= size) { |
| 95 | uptr just_read = internal_read(fd, *buff + read_len, kPageSize); |
| 96 | if (just_read == 0) { |
| 97 | reached_eof = true; |
| 98 | break; |
| 99 | } |
| 100 | read_len += just_read; |
| 101 | } |
| 102 | internal_close(fd); |
| 103 | if (reached_eof) // We've read the whole file. |
| 104 | break; |
| 105 | } |
| 106 | return read_len; |
| 107 | } |
| 108 | |
| Alexey Samsonov | d77fbba | 2012-07-16 11:27:17 +0000 | [diff] [blame] | 109 | // We don't want to use std::sort to avoid including <algorithm>, as |
| 110 | // we may end up with two implementation of std::sort - one in instrumented |
| 111 | // code, and the other in runtime. |
| 112 | // qsort() from stdlib won't work as it calls malloc(), which results |
| 113 | // in deadlock in ASan allocator. |
| 114 | // We re-implement in-place sorting w/o recursion as straightforward heapsort. |
| Alexey Samsonov | e4a8898 | 2012-06-15 07:00:31 +0000 | [diff] [blame] | 115 | void SortArray(uptr *array, uptr size) { |
| Alexey Samsonov | d77fbba | 2012-07-16 11:27:17 +0000 | [diff] [blame] | 116 | if (size < 2) |
| 117 | return; |
| 118 | // Stage 1: insert elements to the heap. |
| 119 | for (uptr i = 1; i < size; i++) { |
| 120 | uptr j, p; |
| 121 | for (j = i; j > 0; j = p) { |
| 122 | p = (j - 1) / 2; |
| 123 | if (array[j] > array[p]) |
| 124 | Swap(array[j], array[p]); |
| 125 | else |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | // Stage 2: swap largest element with the last one, |
| 130 | // and sink the new top. |
| 131 | for (uptr i = size - 1; i > 0; i--) { |
| 132 | Swap(array[0], array[i]); |
| 133 | uptr j, max_ind; |
| 134 | for (j = 0; j < i; j = max_ind) { |
| 135 | uptr left = 2 * j + 1; |
| 136 | uptr right = 2 * j + 2; |
| 137 | max_ind = j; |
| 138 | if (left < i && array[left] > array[max_ind]) |
| 139 | max_ind = left; |
| 140 | if (right < i && array[right] > array[max_ind]) |
| 141 | max_ind = right; |
| 142 | if (max_ind != j) |
| 143 | Swap(array[j], array[max_ind]); |
| 144 | else |
| 145 | break; |
| 146 | } |
| 147 | } |
| Alexey Samsonov | e4a8898 | 2012-06-15 07:00:31 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 150 | } // namespace __sanitizer |
| Kostya Serebryany | 45d849c | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 151 | |
| Alexey Samsonov | fd67c83 | 2012-11-02 09:23:36 +0000 | [diff] [blame] | 152 | using namespace __sanitizer; // NOLINT |
| 153 | |
| 154 | extern "C" { |
| Kostya Serebryany | 45d849c | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 155 | void __sanitizer_set_report_path(const char *path) { |
| 156 | if (!path) return; |
| 157 | uptr len = internal_strlen(path); |
| Alexey Samsonov | 20ba98f | 2012-11-02 09:38:47 +0000 | [diff] [blame] | 158 | if (len > sizeof(report_path) - 100) { |
| Kostya Serebryany | 45d849c | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 159 | Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n", |
| 160 | path[0], path[1], path[2], path[3], |
| 161 | path[4], path[5], path[6], path[7]); |
| 162 | Die(); |
| 163 | } |
| Alexey Samsonov | 20ba98f | 2012-11-02 09:38:47 +0000 | [diff] [blame] | 164 | internal_snprintf(report_path, sizeof(report_path), "%s.%d", path, GetPid()); |
| 165 | report_fd = kInvalidFd; |
| Kostya Serebryany | 45d849c | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 166 | } |
| Alexey Samsonov | fd67c83 | 2012-11-02 09:23:36 +0000 | [diff] [blame] | 167 | |
| 168 | void __sanitizer_set_report_fd(int fd) { |
| Alexey Samsonov | 20ba98f | 2012-11-02 09:38:47 +0000 | [diff] [blame] | 169 | if (report_fd != kStdoutFd && |
| 170 | report_fd != kStderrFd && |
| 171 | report_fd != kInvalidFd) |
| 172 | internal_close(report_fd); |
| 173 | report_fd = fd; |
| Alexey Samsonov | fd67c83 | 2012-11-02 09:23:36 +0000 | [diff] [blame] | 174 | } |
| Alexey Samsonov | fd67c83 | 2012-11-02 09:23:36 +0000 | [diff] [blame] | 175 | } // extern "C" |