| 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 | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 51 | void RawWrite(const char *buffer) { | 
|  | 52 | static const char *kRawWriteError = "RawWrite can't output requested buffer!"; | 
|  | 53 | uptr length = (uptr)internal_strlen(buffer); | 
| Kostya Serebryany | 45d849c | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 54 | if (report_fd == kInvalidFd) { | 
|  | 55 | fd_t fd = internal_open(report_path, true); | 
|  | 56 | if (fd == kInvalidFd) { | 
| Alexey Samsonov | 20ba98f | 2012-11-02 09:38:47 +0000 | [diff] [blame] | 57 | report_fd = kStderrFd; | 
| Kostya Serebryany | 45d849c | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 58 | Report("ERROR: Can't open file: %s\n", report_path); | 
|  | 59 | Die(); | 
|  | 60 | } | 
|  | 61 | report_fd = fd; | 
|  | 62 | } | 
|  | 63 | if (length != internal_write(report_fd, buffer, length)) { | 
|  | 64 | internal_write(report_fd, kRawWriteError, internal_strlen(kRawWriteError)); | 
| Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 65 | Die(); | 
|  | 66 | } | 
|  | 67 | } | 
|  | 68 |  | 
| Alexey Samsonov | fe44fbd | 2012-06-07 05:38:26 +0000 | [diff] [blame] | 69 | uptr ReadFileToBuffer(const char *file_name, char **buff, | 
|  | 70 | uptr *buff_size, uptr max_len) { | 
|  | 71 | const uptr kMinFileLen = kPageSize; | 
|  | 72 | uptr read_len = 0; | 
|  | 73 | *buff = 0; | 
|  | 74 | *buff_size = 0; | 
|  | 75 | // The files we usually open are not seekable, so try different buffer sizes. | 
|  | 76 | for (uptr size = kMinFileLen; size <= max_len; size *= 2) { | 
|  | 77 | fd_t fd = internal_open(file_name, /*write*/ false); | 
|  | 78 | if (fd == kInvalidFd) return 0; | 
|  | 79 | UnmapOrDie(*buff, *buff_size); | 
|  | 80 | *buff = (char*)MmapOrDie(size, __FUNCTION__); | 
|  | 81 | *buff_size = size; | 
|  | 82 | // Read up to one page at a time. | 
|  | 83 | read_len = 0; | 
|  | 84 | bool reached_eof = false; | 
|  | 85 | while (read_len + kPageSize <= size) { | 
|  | 86 | uptr just_read = internal_read(fd, *buff + read_len, kPageSize); | 
|  | 87 | if (just_read == 0) { | 
|  | 88 | reached_eof = true; | 
|  | 89 | break; | 
|  | 90 | } | 
|  | 91 | read_len += just_read; | 
|  | 92 | } | 
|  | 93 | internal_close(fd); | 
|  | 94 | if (reached_eof)  // We've read the whole file. | 
|  | 95 | break; | 
|  | 96 | } | 
|  | 97 | return read_len; | 
|  | 98 | } | 
|  | 99 |  | 
| Alexey Samsonov | d77fbba | 2012-07-16 11:27:17 +0000 | [diff] [blame] | 100 | // We don't want to use std::sort to avoid including <algorithm>, as | 
|  | 101 | // we may end up with two implementation of std::sort - one in instrumented | 
|  | 102 | // code, and the other in runtime. | 
|  | 103 | // qsort() from stdlib won't work as it calls malloc(), which results | 
|  | 104 | // in deadlock in ASan allocator. | 
|  | 105 | // We re-implement in-place sorting w/o recursion as straightforward heapsort. | 
| Alexey Samsonov | e4a8898 | 2012-06-15 07:00:31 +0000 | [diff] [blame] | 106 | void SortArray(uptr *array, uptr size) { | 
| Alexey Samsonov | d77fbba | 2012-07-16 11:27:17 +0000 | [diff] [blame] | 107 | if (size < 2) | 
|  | 108 | return; | 
|  | 109 | // Stage 1: insert elements to the heap. | 
|  | 110 | for (uptr i = 1; i < size; i++) { | 
|  | 111 | uptr j, p; | 
|  | 112 | for (j = i; j > 0; j = p) { | 
|  | 113 | p = (j - 1) / 2; | 
|  | 114 | if (array[j] > array[p]) | 
|  | 115 | Swap(array[j], array[p]); | 
|  | 116 | else | 
|  | 117 | break; | 
|  | 118 | } | 
|  | 119 | } | 
|  | 120 | // Stage 2: swap largest element with the last one, | 
|  | 121 | // and sink the new top. | 
|  | 122 | for (uptr i = size - 1; i > 0; i--) { | 
|  | 123 | Swap(array[0], array[i]); | 
|  | 124 | uptr j, max_ind; | 
|  | 125 | for (j = 0; j < i; j = max_ind) { | 
|  | 126 | uptr left = 2 * j + 1; | 
|  | 127 | uptr right = 2 * j + 2; | 
|  | 128 | max_ind = j; | 
|  | 129 | if (left < i && array[left] > array[max_ind]) | 
|  | 130 | max_ind = left; | 
|  | 131 | if (right < i && array[right] > array[max_ind]) | 
|  | 132 | max_ind = right; | 
|  | 133 | if (max_ind != j) | 
|  | 134 | Swap(array[j], array[max_ind]); | 
|  | 135 | else | 
|  | 136 | break; | 
|  | 137 | } | 
|  | 138 | } | 
| Alexey Samsonov | e4a8898 | 2012-06-15 07:00:31 +0000 | [diff] [blame] | 139 | } | 
|  | 140 |  | 
| Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 141 | }  // namespace __sanitizer | 
| Kostya Serebryany | 45d849c | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 142 |  | 
| Alexey Samsonov | fd67c83 | 2012-11-02 09:23:36 +0000 | [diff] [blame] | 143 | using namespace __sanitizer;  // NOLINT | 
|  | 144 |  | 
|  | 145 | extern "C" { | 
| Kostya Serebryany | 45d849c | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 146 | void __sanitizer_set_report_path(const char *path) { | 
|  | 147 | if (!path) return; | 
|  | 148 | uptr len = internal_strlen(path); | 
| Alexey Samsonov | 20ba98f | 2012-11-02 09:38:47 +0000 | [diff] [blame] | 149 | if (len > sizeof(report_path) - 100) { | 
| Kostya Serebryany | 45d849c | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 150 | Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n", | 
|  | 151 | path[0], path[1], path[2], path[3], | 
|  | 152 | path[4], path[5], path[6], path[7]); | 
|  | 153 | Die(); | 
|  | 154 | } | 
| Alexey Samsonov | 20ba98f | 2012-11-02 09:38:47 +0000 | [diff] [blame] | 155 | internal_snprintf(report_path, sizeof(report_path), "%s.%d", path, GetPid()); | 
|  | 156 | report_fd = kInvalidFd; | 
| Kostya Serebryany | 45d849c | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 157 | } | 
| Alexey Samsonov | fd67c83 | 2012-11-02 09:23:36 +0000 | [diff] [blame] | 158 |  | 
|  | 159 | void __sanitizer_set_report_fd(int fd) { | 
| Alexey Samsonov | 20ba98f | 2012-11-02 09:38:47 +0000 | [diff] [blame] | 160 | if (report_fd != kStdoutFd && | 
|  | 161 | report_fd != kStderrFd && | 
|  | 162 | report_fd != kInvalidFd) | 
|  | 163 | internal_close(report_fd); | 
|  | 164 | report_fd = fd; | 
| Alexey Samsonov | fd67c83 | 2012-11-02 09:23:36 +0000 | [diff] [blame] | 165 | } | 
| Alexey Samsonov | fd67c83 | 2012-11-02 09:23:36 +0000 | [diff] [blame] | 166 | }  // extern "C" |