Alexey Samsonov | 230c3be | 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" |
Alexey Samsonov | 90b0f1e | 2013-10-04 08:55:03 +0000 | [diff] [blame] | 15 | #include "sanitizer_flags.h" |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 16 | #include "sanitizer_libc.h" |
Alexey Samsonov | 2fb0872 | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 17 | #include "sanitizer_stacktrace.h" |
| 18 | #include "sanitizer_symbolizer.h" |
Alexey Samsonov | 4c49666 | 2012-06-15 07:00:31 +0000 | [diff] [blame] | 19 | |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 20 | namespace __sanitizer { |
| 21 | |
Kostya Serebryany | 859778a | 2013-01-31 14:11:21 +0000 | [diff] [blame] | 22 | const char *SanitizerToolName = "SanitizerTool"; |
| 23 | |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 24 | uptr GetPageSizeCached() { |
| 25 | static uptr PageSize; |
| 26 | if (!PageSize) |
| 27 | PageSize = GetPageSize(); |
| 28 | return PageSize; |
| 29 | } |
| 30 | |
Alexander Potapenko | dedba5d | 2013-01-18 16:44:27 +0000 | [diff] [blame] | 31 | |
| 32 | // By default, dump to stderr. If |log_to_file| is true and |report_fd_pid| |
| 33 | // isn't equal to the current PID, try to obtain file descriptor by opening |
| 34 | // file "report_path_prefix.<PID>". |
Peter Collingbourne | 0c547de | 2013-05-17 16:17:19 +0000 | [diff] [blame] | 35 | fd_t report_fd = kStderrFd; |
Reid Kleckner | 923bac7 | 2013-09-05 03:19:57 +0000 | [diff] [blame] | 36 | |
| 37 | // Set via __sanitizer_set_report_path. |
| 38 | bool log_to_file = false; |
| 39 | char report_path_prefix[sizeof(report_path_prefix)]; |
| 40 | |
Alexander Potapenko | dedba5d | 2013-01-18 16:44:27 +0000 | [diff] [blame] | 41 | // PID of process that opened |report_fd|. If a fork() occurs, the PID of the |
| 42 | // child thread will be different from |report_fd_pid|. |
Reid Kleckner | 923bac7 | 2013-09-05 03:19:57 +0000 | [diff] [blame] | 43 | uptr report_fd_pid = 0; |
Kostya Serebryany | 81dfbb7 | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 44 | |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 45 | static DieCallbackType DieCallback; |
| 46 | void SetDieCallback(DieCallbackType callback) { |
Alexey Samsonov | 591616d | 2012-09-11 09:44:48 +0000 | [diff] [blame] | 47 | DieCallback = callback; |
| 48 | } |
| 49 | |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 50 | DieCallbackType GetDieCallback() { |
| 51 | return DieCallback; |
| 52 | } |
| 53 | |
Alexey Samsonov | 591616d | 2012-09-11 09:44:48 +0000 | [diff] [blame] | 54 | void NORETURN Die() { |
| 55 | if (DieCallback) { |
| 56 | DieCallback(); |
| 57 | } |
Alexey Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 58 | internal__exit(1); |
Alexey Samsonov | 591616d | 2012-09-11 09:44:48 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static CheckFailedCallbackType CheckFailedCallback; |
| 62 | void SetCheckFailedCallback(CheckFailedCallbackType callback) { |
| 63 | CheckFailedCallback = callback; |
| 64 | } |
| 65 | |
| 66 | void NORETURN CheckFailed(const char *file, int line, const char *cond, |
| 67 | u64 v1, u64 v2) { |
| 68 | if (CheckFailedCallback) { |
| 69 | CheckFailedCallback(file, line, cond, v1, v2); |
| 70 | } |
Dmitry Vyukov | e3c35c7 | 2013-01-11 15:07:49 +0000 | [diff] [blame] | 71 | Report("Sanitizer CHECK failed: %s:%d %s (%lld, %lld)\n", file, line, cond, |
| 72 | v1, v2); |
Alexey Samsonov | 591616d | 2012-09-11 09:44:48 +0000 | [diff] [blame] | 73 | Die(); |
| 74 | } |
| 75 | |
Alexey Samsonov | cffe2f5 | 2012-06-07 05:38:26 +0000 | [diff] [blame] | 76 | uptr ReadFileToBuffer(const char *file_name, char **buff, |
| 77 | uptr *buff_size, uptr max_len) { |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 78 | uptr PageSize = GetPageSizeCached(); |
| 79 | uptr kMinFileLen = PageSize; |
Alexey Samsonov | cffe2f5 | 2012-06-07 05:38:26 +0000 | [diff] [blame] | 80 | uptr read_len = 0; |
| 81 | *buff = 0; |
| 82 | *buff_size = 0; |
| 83 | // The files we usually open are not seekable, so try different buffer sizes. |
| 84 | for (uptr size = kMinFileLen; size <= max_len; size *= 2) { |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 85 | uptr openrv = OpenFile(file_name, /*write*/ false); |
| 86 | if (internal_iserror(openrv)) return 0; |
| 87 | fd_t fd = openrv; |
Alexey Samsonov | cffe2f5 | 2012-06-07 05:38:26 +0000 | [diff] [blame] | 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; |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 94 | while (read_len + PageSize <= size) { |
| 95 | uptr just_read = internal_read(fd, *buff + read_len, PageSize); |
Alexey Samsonov | cffe2f5 | 2012-06-07 05:38:26 +0000 | [diff] [blame] | 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 | |
Sergey Matveev | 15bb32b | 2013-05-13 11:58:48 +0000 | [diff] [blame] | 109 | typedef bool UptrComparisonFunction(const uptr &a, const uptr &b); |
| 110 | |
| 111 | template<class T> |
| 112 | static inline bool CompareLess(const T &a, const T &b) { |
| 113 | return a < b; |
| 114 | } |
| 115 | |
Alexey Samsonov | 4c49666 | 2012-06-15 07:00:31 +0000 | [diff] [blame] | 116 | void SortArray(uptr *array, uptr size) { |
Sergey Matveev | 15bb32b | 2013-05-13 11:58:48 +0000 | [diff] [blame] | 117 | InternalSort<uptr*, UptrComparisonFunction>(&array, size, CompareLess); |
Alexey Samsonov | 4c49666 | 2012-06-15 07:00:31 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Kostya Serebryany | cc75259 | 2012-12-06 06:10:31 +0000 | [diff] [blame] | 120 | // We want to map a chunk of address space aligned to 'alignment'. |
| 121 | // We do it by maping a bit more and then unmaping redundant pieces. |
| 122 | // We probably can do it with fewer syscalls in some OS-dependent way. |
| 123 | void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type) { |
Bill Wendling | 2dae63a | 2012-12-06 07:43:17 +0000 | [diff] [blame] | 124 | // uptr PageSize = GetPageSizeCached(); |
Kostya Serebryany | cc75259 | 2012-12-06 06:10:31 +0000 | [diff] [blame] | 125 | CHECK(IsPowerOfTwo(size)); |
| 126 | CHECK(IsPowerOfTwo(alignment)); |
| 127 | uptr map_size = size + alignment; |
| 128 | uptr map_res = (uptr)MmapOrDie(map_size, mem_type); |
| 129 | uptr map_end = map_res + map_size; |
| 130 | uptr res = map_res; |
| 131 | if (res & (alignment - 1)) // Not aligned. |
Dmitry Vyukov | 3617ad7 | 2012-12-06 15:42:54 +0000 | [diff] [blame] | 132 | res = (map_res + alignment) & ~(alignment - 1); |
Kostya Serebryany | cc75259 | 2012-12-06 06:10:31 +0000 | [diff] [blame] | 133 | uptr end = res + size; |
| 134 | if (res != map_res) |
| 135 | UnmapOrDie((void*)map_res, res - map_res); |
| 136 | if (end != map_end) |
| 137 | UnmapOrDie((void*)end, map_end - end); |
| 138 | return (void*)res; |
| 139 | } |
| 140 | |
Alexey Samsonov | 90b0f1e | 2013-10-04 08:55:03 +0000 | [diff] [blame] | 141 | const char *StripPathPrefix(const char *filepath, |
| 142 | const char *strip_path_prefix) { |
| 143 | if (filepath == 0) return 0; |
| 144 | if (strip_path_prefix == 0) return filepath; |
| 145 | const char *pos = internal_strstr(filepath, strip_path_prefix); |
| 146 | if (pos == 0) return filepath; |
| 147 | pos += internal_strlen(strip_path_prefix); |
| 148 | if (pos[0] == '.' && pos[1] == '/') |
| 149 | pos += 2; |
| 150 | return pos; |
| 151 | } |
| 152 | |
| 153 | void PrintSourceLocation(const char *file, int line, int column) { |
| 154 | CHECK(file); |
| 155 | Printf("%s", StripPathPrefix(file, common_flags()->strip_path_prefix)); |
| 156 | if (line > 0) { |
| 157 | Printf(":%d", line); |
| 158 | if (column > 0) |
| 159 | Printf(":%d", column); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void PrintModuleAndOffset(const char *module, uptr offset) { |
| 164 | Printf("(%s+0x%zx)", |
| 165 | StripPathPrefix(module, common_flags()->strip_path_prefix), offset); |
| 166 | } |
| 167 | |
Alexey Samsonov | 2fb0872 | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 168 | void ReportErrorSummary(const char *error_message) { |
| 169 | InternalScopedBuffer<char> buff(kMaxSummaryLength); |
| 170 | internal_snprintf(buff.data(), buff.size(), |
| 171 | "SUMMARY: %s: %s", SanitizerToolName, error_message); |
| 172 | __sanitizer_report_error_summary(buff.data()); |
| 173 | } |
| 174 | |
Kostya Serebryany | 2673fd8 | 2013-02-06 12:36:49 +0000 | [diff] [blame] | 175 | void ReportErrorSummary(const char *error_type, const char *file, |
| 176 | int line, const char *function) { |
Alexey Samsonov | 2fb0872 | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 177 | InternalScopedBuffer<char> buff(kMaxSummaryLength); |
Alexey Samsonov | 90b0f1e | 2013-10-04 08:55:03 +0000 | [diff] [blame] | 178 | internal_snprintf( |
Alexey Samsonov | 2fb0872 | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 179 | buff.data(), buff.size(), "%s %s:%d %s", error_type, |
Alexey Samsonov | 90b0f1e | 2013-10-04 08:55:03 +0000 | [diff] [blame] | 180 | file ? StripPathPrefix(file, common_flags()->strip_path_prefix) : "??", |
| 181 | line, function ? function : "??"); |
Alexey Samsonov | 2fb0872 | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 182 | ReportErrorSummary(buff.data()); |
| 183 | } |
| 184 | |
| 185 | void ReportErrorSummary(const char *error_type, StackTrace *stack) { |
| 186 | AddressInfo ai; |
Alexey Samsonov | 50cdc5a | 2013-11-01 18:00:22 +0000 | [diff] [blame^] | 187 | #if !SANITIZER_GO |
Alexey Samsonov | 2fb0872 | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 188 | if (stack->size > 0 && Symbolizer::Get()->IsAvailable()) { |
| 189 | // Currently, we include the first stack frame into the report summary. |
| 190 | // Maybe sometimes we need to choose another frame (e.g. skip memcpy/etc). |
| 191 | uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]); |
| 192 | Symbolizer::Get()->SymbolizeCode(pc, &ai, 1); |
| 193 | } |
Alexey Samsonov | 50cdc5a | 2013-11-01 18:00:22 +0000 | [diff] [blame^] | 194 | #endif |
Alexey Samsonov | 2fb0872 | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 195 | ReportErrorSummary(error_type, ai.file, ai.line, ai.function); |
Kostya Serebryany | 2673fd8 | 2013-02-06 12:36:49 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 198 | LoadedModule::LoadedModule(const char *module_name, uptr base_address) { |
| 199 | full_name_ = internal_strdup(module_name); |
| 200 | base_address_ = base_address; |
| 201 | n_ranges_ = 0; |
| 202 | } |
| 203 | |
| 204 | void LoadedModule::addAddressRange(uptr beg, uptr end) { |
| 205 | CHECK_LT(n_ranges_, kMaxNumberOfAddressRanges); |
| 206 | ranges_[n_ranges_].beg = beg; |
| 207 | ranges_[n_ranges_].end = end; |
| 208 | n_ranges_++; |
| 209 | } |
| 210 | |
| 211 | bool LoadedModule::containsAddress(uptr address) const { |
| 212 | for (uptr i = 0; i < n_ranges_; i++) { |
| 213 | if (ranges_[i].beg <= address && address < ranges_[i].end) |
| 214 | return true; |
| 215 | } |
| 216 | return false; |
| 217 | } |
| 218 | |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 219 | } // namespace __sanitizer |
Kostya Serebryany | 81dfbb7 | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 220 | |
Alexey Samsonov | ac8564e | 2012-11-02 09:23:36 +0000 | [diff] [blame] | 221 | using namespace __sanitizer; // NOLINT |
| 222 | |
| 223 | extern "C" { |
Kostya Serebryany | 81dfbb7 | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 224 | void __sanitizer_set_report_path(const char *path) { |
Dmitry Vyukov | b48c2b2 | 2013-10-15 12:25:29 +0000 | [diff] [blame] | 225 | if (!path) |
| 226 | return; |
Kostya Serebryany | 81dfbb7 | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 227 | uptr len = internal_strlen(path); |
Alexander Potapenko | dedba5d | 2013-01-18 16:44:27 +0000 | [diff] [blame] | 228 | if (len > sizeof(report_path_prefix) - 100) { |
Kostya Serebryany | 81dfbb7 | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 229 | Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n", |
| 230 | path[0], path[1], path[2], path[3], |
| 231 | path[4], path[5], path[6], path[7]); |
| 232 | Die(); |
| 233 | } |
Alexey Samsonov | f3457cb | 2012-11-02 09:38:47 +0000 | [diff] [blame] | 234 | if (report_fd != kStdoutFd && |
| 235 | report_fd != kStderrFd && |
| 236 | report_fd != kInvalidFd) |
| 237 | internal_close(report_fd); |
Dmitry Vyukov | b48c2b2 | 2013-10-15 12:25:29 +0000 | [diff] [blame] | 238 | report_fd = kInvalidFd; |
| 239 | log_to_file = false; |
| 240 | if (internal_strcmp(path, "stdout") == 0) { |
| 241 | report_fd = kStdoutFd; |
| 242 | } else if (internal_strcmp(path, "stderr") == 0) { |
| 243 | report_fd = kStderrFd; |
| 244 | } else { |
| 245 | internal_strncpy(report_path_prefix, path, sizeof(report_path_prefix)); |
| 246 | report_path_prefix[len] = '\0'; |
| 247 | log_to_file = true; |
| 248 | } |
Alexey Samsonov | ac8564e | 2012-11-02 09:23:36 +0000 | [diff] [blame] | 249 | } |
Alexander Potapenko | 2574257 | 2012-12-10 13:10:40 +0000 | [diff] [blame] | 250 | |
| 251 | void NOINLINE __sanitizer_sandbox_on_notify(void *reserved) { |
| 252 | (void)reserved; |
| 253 | PrepareForSandboxing(); |
| 254 | } |
Kostya Serebryany | 2673fd8 | 2013-02-06 12:36:49 +0000 | [diff] [blame] | 255 | |
| 256 | void __sanitizer_report_error_summary(const char *error_summary) { |
Nick Lewycky | 47fcd76 | 2013-10-23 07:45:53 +0000 | [diff] [blame] | 257 | Printf("%s\n", error_summary); |
Kostya Serebryany | 2673fd8 | 2013-02-06 12:36:49 +0000 | [diff] [blame] | 258 | } |
Alexey Samsonov | ac8564e | 2012-11-02 09:23:36 +0000 | [diff] [blame] | 259 | } // extern "C" |