Filipe Cabecinhas | fddfdca | 2016-08-30 17:08:55 +0000 | [diff] [blame] | 1 | //===-- asan_errors.cc ------------------------------------------*- C++ -*-===// |
| 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 a part of AddressSanitizer, an address sanity checker. |
| 11 | // |
| 12 | // ASan implementation for error structures. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "asan_errors.h" |
Filipe Cabecinhas | 1989be7 | 2016-09-08 12:58:15 +0000 | [diff] [blame] | 16 | #include <signal.h> |
Filipe Cabecinhas | b16672d | 2016-08-31 07:38:09 +0000 | [diff] [blame] | 17 | #include "asan_descriptions.h" |
Filipe Cabecinhas | b50a5b3 | 2016-09-15 08:10:48 +0000 | [diff] [blame] | 18 | #include "asan_mapping.h" |
Filipe Cabecinhas | 1989be7 | 2016-09-08 12:58:15 +0000 | [diff] [blame] | 19 | #include "asan_report.h" |
Filipe Cabecinhas | fddfdca | 2016-08-30 17:08:55 +0000 | [diff] [blame] | 20 | #include "asan_stack.h" |
Filipe Cabecinhas | 719db0c | 2016-09-15 08:10:52 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_stackdepot.h" |
Filipe Cabecinhas | fddfdca | 2016-08-30 17:08:55 +0000 | [diff] [blame] | 22 | |
| 23 | namespace __asan { |
| 24 | |
| 25 | void ErrorStackOverflow::Print() { |
| 26 | Decorator d; |
| 27 | Printf("%s", d.Warning()); |
| 28 | Report( |
| 29 | "ERROR: AddressSanitizer: stack-overflow on address %p" |
| 30 | " (pc %p bp %p sp %p T%d)\n", |
| 31 | (void *)addr, (void *)pc, (void *)bp, (void *)sp, tid); |
| 32 | Printf("%s", d.EndWarning()); |
| 33 | scariness.Print(); |
| 34 | BufferedStackTrace stack; |
| 35 | GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, context, |
| 36 | common_flags()->fast_unwind_on_fatal); |
| 37 | stack.Print(); |
| 38 | ReportErrorSummary("stack-overflow", &stack); |
| 39 | } |
| 40 | |
Filipe Cabecinhas | 1989be7 | 2016-09-08 12:58:15 +0000 | [diff] [blame] | 41 | static void MaybeDumpInstructionBytes(uptr pc) { |
| 42 | if (!flags()->dump_instruction_bytes || (pc < GetPageSizeCached())) return; |
| 43 | InternalScopedString str(1024); |
| 44 | str.append("First 16 instruction bytes at pc: "); |
| 45 | if (IsAccessibleMemoryRange(pc, 16)) { |
| 46 | for (int i = 0; i < 16; ++i) { |
| 47 | PrintMemoryByte(&str, "", ((u8 *)pc)[i], /*in_shadow*/ false, " "); |
| 48 | } |
| 49 | str.append("\n"); |
| 50 | } else { |
| 51 | str.append("unaccessible\n"); |
| 52 | } |
| 53 | Report("%s", str.data()); |
| 54 | } |
| 55 | |
| 56 | void ErrorDeadlySignal::Print() { |
| 57 | Decorator d; |
| 58 | Printf("%s", d.Warning()); |
| 59 | const char *description = DescribeSignalOrException(signo); |
| 60 | Report( |
| 61 | "ERROR: AddressSanitizer: %s on unknown address %p (pc %p bp %p sp %p " |
| 62 | "T%d)\n", |
| 63 | description, (void *)addr, (void *)pc, (void *)bp, (void *)sp, tid); |
| 64 | Printf("%s", d.EndWarning()); |
| 65 | if (pc < GetPageSizeCached()) Report("Hint: pc points to the zero page.\n"); |
| 66 | if (is_memory_access) { |
| 67 | const char *access_type = |
| 68 | write_flag == SignalContext::WRITE |
| 69 | ? "WRITE" |
| 70 | : (write_flag == SignalContext::READ ? "READ" : "UNKNOWN"); |
| 71 | Report("The signal is caused by a %s memory access.\n", access_type); |
| 72 | if (addr < GetPageSizeCached()) |
| 73 | Report("Hint: address points to the zero page.\n"); |
| 74 | } |
| 75 | scariness.Print(); |
| 76 | BufferedStackTrace stack; |
| 77 | GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, context, |
| 78 | common_flags()->fast_unwind_on_fatal); |
| 79 | stack.Print(); |
| 80 | MaybeDumpInstructionBytes(pc); |
| 81 | Printf("AddressSanitizer can not provide additional info.\n"); |
| 82 | ReportErrorSummary(description, &stack); |
| 83 | } |
| 84 | |
Filipe Cabecinhas | b16672d | 2016-08-31 07:38:09 +0000 | [diff] [blame] | 85 | void ErrorDoubleFree::Print() { |
| 86 | Decorator d; |
| 87 | Printf("%s", d.Warning()); |
| 88 | char tname[128]; |
| 89 | Report( |
| 90 | "ERROR: AddressSanitizer: attempting double-free on %p in " |
| 91 | "thread T%d%s:\n", |
| 92 | addr_description.addr, tid, |
| 93 | ThreadNameWithParenthesis(tid, tname, sizeof(tname))); |
| 94 | Printf("%s", d.EndWarning()); |
Filipe Cabecinhas | 453b555 | 2016-08-31 09:39:47 +0000 | [diff] [blame] | 95 | scariness.Print(); |
Filipe Cabecinhas | b16672d | 2016-08-31 07:38:09 +0000 | [diff] [blame] | 96 | GET_STACK_TRACE_FATAL(second_free_stack->trace[0], |
| 97 | second_free_stack->top_frame_bp); |
| 98 | stack.Print(); |
| 99 | addr_description.Print(); |
| 100 | ReportErrorSummary("double-free", &stack); |
| 101 | } |
| 102 | |
Filipe Cabecinhas | 25ad7b5 | 2016-09-07 14:20:54 +0000 | [diff] [blame] | 103 | void ErrorNewDeleteSizeMismatch::Print() { |
| 104 | Decorator d; |
| 105 | Printf("%s", d.Warning()); |
| 106 | char tname[128]; |
| 107 | Report( |
| 108 | "ERROR: AddressSanitizer: new-delete-type-mismatch on %p in thread " |
| 109 | "T%d%s:\n", |
| 110 | addr_description.addr, tid, |
| 111 | ThreadNameWithParenthesis(tid, tname, sizeof(tname))); |
| 112 | Printf("%s object passed to delete has wrong type:\n", d.EndWarning()); |
| 113 | Printf( |
| 114 | " size of the allocated type: %zd bytes;\n" |
| 115 | " size of the deallocated type: %zd bytes.\n", |
| 116 | addr_description.chunk_access.chunk_size, delete_size); |
| 117 | CHECK_GT(free_stack->size, 0); |
| 118 | scariness.Print(); |
| 119 | GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp); |
| 120 | stack.Print(); |
| 121 | addr_description.Print(); |
| 122 | ReportErrorSummary("new-delete-type-mismatch", &stack); |
| 123 | Report( |
| 124 | "HINT: if you don't care about these errors you may set " |
| 125 | "ASAN_OPTIONS=new_delete_type_mismatch=0\n"); |
| 126 | } |
| 127 | |
Filipe Cabecinhas | 6fb5462 | 2016-09-13 20:47:29 +0000 | [diff] [blame] | 128 | void ErrorFreeNotMalloced::Print() { |
| 129 | Decorator d; |
| 130 | Printf("%s", d.Warning()); |
| 131 | char tname[128]; |
| 132 | Report( |
| 133 | "ERROR: AddressSanitizer: attempting free on address " |
| 134 | "which was not malloc()-ed: %p in thread T%d%s\n", |
| 135 | addr_description.Address(), tid, |
| 136 | ThreadNameWithParenthesis(tid, tname, sizeof(tname))); |
| 137 | Printf("%s", d.EndWarning()); |
| 138 | CHECK_GT(free_stack->size, 0); |
| 139 | scariness.Print(); |
| 140 | GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp); |
| 141 | stack.Print(); |
| 142 | addr_description.Print(); |
| 143 | ReportErrorSummary("bad-free", &stack); |
| 144 | } |
| 145 | |
Filipe Cabecinhas | 92c5b5d | 2016-09-13 20:47:33 +0000 | [diff] [blame] | 146 | void ErrorAllocTypeMismatch::Print() { |
| 147 | static const char *alloc_names[] = {"INVALID", "malloc", "operator new", |
| 148 | "operator new []"}; |
| 149 | static const char *dealloc_names[] = {"INVALID", "free", "operator delete", |
| 150 | "operator delete []"}; |
| 151 | CHECK_NE(alloc_type, dealloc_type); |
| 152 | Decorator d; |
| 153 | Printf("%s", d.Warning()); |
| 154 | Report("ERROR: AddressSanitizer: alloc-dealloc-mismatch (%s vs %s) on %p\n", |
| 155 | alloc_names[alloc_type], dealloc_names[dealloc_type], |
| 156 | addr_description.addr); |
| 157 | Printf("%s", d.EndWarning()); |
| 158 | CHECK_GT(dealloc_stack->size, 0); |
| 159 | scariness.Print(); |
| 160 | GET_STACK_TRACE_FATAL(dealloc_stack->trace[0], dealloc_stack->top_frame_bp); |
| 161 | stack.Print(); |
| 162 | addr_description.Print(); |
| 163 | ReportErrorSummary("alloc-dealloc-mismatch", &stack); |
| 164 | Report( |
| 165 | "HINT: if you don't care about these errors you may set " |
| 166 | "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n"); |
| 167 | } |
| 168 | |
Filipe Cabecinhas | 5f862c2 | 2016-09-13 20:47:37 +0000 | [diff] [blame] | 169 | void ErrorMallocUsableSizeNotOwned::Print() { |
| 170 | Decorator d; |
| 171 | Printf("%s", d.Warning()); |
| 172 | Report( |
| 173 | "ERROR: AddressSanitizer: attempting to call malloc_usable_size() for " |
| 174 | "pointer which is not owned: %p\n", |
| 175 | addr_description.Address()); |
| 176 | Printf("%s", d.EndWarning()); |
| 177 | stack->Print(); |
| 178 | addr_description.Print(); |
| 179 | ReportErrorSummary("bad-malloc_usable_size", stack); |
| 180 | } |
| 181 | |
Filipe Cabecinhas | b0de43a | 2016-09-13 20:47:42 +0000 | [diff] [blame] | 182 | void ErrorSanitizerGetAllocatedSizeNotOwned::Print() { |
| 183 | Decorator d; |
| 184 | Printf("%s", d.Warning()); |
| 185 | Report( |
| 186 | "ERROR: AddressSanitizer: attempting to call " |
| 187 | "__sanitizer_get_allocated_size() for pointer which is not owned: %p\n", |
| 188 | addr_description.Address()); |
| 189 | Printf("%s", d.EndWarning()); |
| 190 | stack->Print(); |
| 191 | addr_description.Print(); |
| 192 | ReportErrorSummary("bad-__sanitizer_get_allocated_size", stack); |
| 193 | } |
| 194 | |
Filipe Cabecinhas | 7a196b9 | 2016-09-14 07:37:14 +0000 | [diff] [blame] | 195 | void ErrorStringFunctionMemoryRangesOverlap::Print() { |
| 196 | Decorator d; |
| 197 | char bug_type[100]; |
| 198 | internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function); |
| 199 | Printf("%s", d.Warning()); |
| 200 | Report( |
| 201 | "ERROR: AddressSanitizer: %s: memory ranges [%p,%p) and [%p, %p) " |
| 202 | "overlap\n", |
| 203 | bug_type, addr1_description.Address(), |
| 204 | addr1_description.Address() + length1, addr2_description.Address(), |
| 205 | addr2_description.Address() + length2); |
| 206 | Printf("%s", d.EndWarning()); |
| 207 | scariness.Print(); |
| 208 | stack->Print(); |
| 209 | addr1_description.Print(); |
| 210 | addr2_description.Print(); |
| 211 | ReportErrorSummary(bug_type, stack); |
| 212 | } |
| 213 | |
Filipe Cabecinhas | 36229e9 | 2016-09-14 07:37:20 +0000 | [diff] [blame] | 214 | void ErrorStringFunctionSizeOverflow::Print() { |
| 215 | Decorator d; |
| 216 | Printf("%s", d.Warning()); |
| 217 | const char *bug_type = "negative-size-param"; |
| 218 | Report("ERROR: AddressSanitizer: %s: (size=%zd)\n", bug_type, size); |
| 219 | Printf("%s", d.EndWarning()); |
| 220 | scariness.Print(); |
| 221 | stack->Print(); |
| 222 | addr_description.Print(); |
| 223 | ReportErrorSummary(bug_type, stack); |
| 224 | } |
| 225 | |
Filipe Cabecinhas | b50a5b3 | 2016-09-15 08:10:48 +0000 | [diff] [blame] | 226 | void ErrorBadParamsToAnnotateContiguousContainer::Print() { |
| 227 | Report( |
| 228 | "ERROR: AddressSanitizer: bad parameters to " |
| 229 | "__sanitizer_annotate_contiguous_container:\n" |
| 230 | " beg : %p\n" |
| 231 | " end : %p\n" |
| 232 | " old_mid : %p\n" |
| 233 | " new_mid : %p\n", |
| 234 | beg, end, old_mid, new_mid); |
| 235 | uptr granularity = SHADOW_GRANULARITY; |
| 236 | if (!IsAligned(beg, granularity)) |
| 237 | Report("ERROR: beg is not aligned by %d\n", granularity); |
| 238 | stack->Print(); |
| 239 | ReportErrorSummary("bad-__sanitizer_annotate_contiguous_container", stack); |
| 240 | } |
| 241 | |
Filipe Cabecinhas | 719db0c | 2016-09-15 08:10:52 +0000 | [diff] [blame] | 242 | void ErrorODRViolation::Print() { |
| 243 | Decorator d; |
| 244 | Printf("%s", d.Warning()); |
| 245 | Report("ERROR: AddressSanitizer: odr-violation (%p):\n", global1.beg); |
| 246 | Printf("%s", d.EndWarning()); |
| 247 | InternalScopedString g1_loc(256), g2_loc(256); |
| 248 | PrintGlobalLocation(&g1_loc, global1); |
| 249 | PrintGlobalLocation(&g2_loc, global2); |
| 250 | Printf(" [1] size=%zd '%s' %s\n", global1.size, |
| 251 | MaybeDemangleGlobalName(global1.name), g1_loc.data()); |
| 252 | Printf(" [2] size=%zd '%s' %s\n", global2.size, |
| 253 | MaybeDemangleGlobalName(global2.name), g2_loc.data()); |
| 254 | if (stack_id1 && stack_id2) { |
| 255 | Printf("These globals were registered at these points:\n"); |
| 256 | Printf(" [1]:\n"); |
| 257 | StackDepotGet(stack_id1).Print(); |
| 258 | Printf(" [2]:\n"); |
| 259 | StackDepotGet(stack_id2).Print(); |
| 260 | } |
| 261 | Report( |
| 262 | "HINT: if you don't care about these errors you may set " |
| 263 | "ASAN_OPTIONS=detect_odr_violation=0\n"); |
| 264 | InternalScopedString error_msg(256); |
| 265 | error_msg.append("odr-violation: global '%s' at %s", |
| 266 | MaybeDemangleGlobalName(global1.name), g1_loc.data()); |
| 267 | ReportErrorSummary(error_msg.data()); |
| 268 | } |
| 269 | |
Filipe Cabecinhas | 1b3742e | 2016-09-15 08:10:56 +0000 | [diff] [blame] | 270 | void ErrorInvalidPointerPair::Print() { |
| 271 | const char *bug_type = "invalid-pointer-pair"; |
| 272 | Decorator d; |
| 273 | Printf("%s", d.Warning()); |
Filipe Cabecinhas | 490f96c | 2016-09-21 19:21:01 +0000 | [diff] [blame] | 274 | Report("ERROR: AddressSanitizer: invalid-pointer-pair: %p %p\n", |
| 275 | addr1_description.Address(), addr2_description.Address()); |
Filipe Cabecinhas | 1b3742e | 2016-09-15 08:10:56 +0000 | [diff] [blame] | 276 | Printf("%s", d.EndWarning()); |
| 277 | GET_STACK_TRACE_FATAL(pc, bp); |
| 278 | stack.Print(); |
Filipe Cabecinhas | 490f96c | 2016-09-21 19:21:01 +0000 | [diff] [blame] | 279 | addr1_description.Print(); |
| 280 | addr2_description.Print(); |
Filipe Cabecinhas | 1b3742e | 2016-09-15 08:10:56 +0000 | [diff] [blame] | 281 | ReportErrorSummary(bug_type, &stack); |
| 282 | } |
| 283 | |
Filipe Cabecinhas | fddfdca | 2016-08-30 17:08:55 +0000 | [diff] [blame] | 284 | } // namespace __asan |