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 | |
Vitaly Buka | 21ddc62 | 2017-09-14 22:44:03 +0000 | [diff] [blame] | 25 | static void OnStackUnwind(const SignalContext &sig, |
| 26 | const void *callback_context, |
| 27 | BufferedStackTrace *stack) { |
| 28 | // Tests and maybe some users expect that scariness is going to be printed |
| 29 | // just before the stack. As only asan has scariness score we have no |
| 30 | // corresponding code in the sanitizer_common and we use this callback to |
| 31 | // print it. |
| 32 | static_cast<const ScarinessScoreBase *>(callback_context)->Print(); |
| 33 | GetStackTraceWithPcBpAndContext(stack, kStackTraceMax, sig.pc, sig.bp, |
| 34 | sig.context, |
| 35 | common_flags()->fast_unwind_on_fatal); |
| 36 | } |
| 37 | |
Filipe Cabecinhas | 1989be7 | 2016-09-08 12:58:15 +0000 | [diff] [blame] | 38 | void ErrorDeadlySignal::Print() { |
Vitaly Buka | 21ddc62 | 2017-09-14 22:44:03 +0000 | [diff] [blame] | 39 | ReportDeadlySignal(signal, tid, &OnStackUnwind, &scariness); |
Filipe Cabecinhas | 1989be7 | 2016-09-08 12:58:15 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Filipe Cabecinhas | b16672d | 2016-08-31 07:38:09 +0000 | [diff] [blame] | 42 | void ErrorDoubleFree::Print() { |
| 43 | Decorator d; |
| 44 | Printf("%s", d.Warning()); |
| 45 | char tname[128]; |
| 46 | Report( |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 47 | "ERROR: AddressSanitizer: attempting %s on %p in " |
Filipe Cabecinhas | b16672d | 2016-08-31 07:38:09 +0000 | [diff] [blame] | 48 | "thread T%d%s:\n", |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 49 | scariness.GetDescription(), addr_description.addr, tid, |
Filipe Cabecinhas | b16672d | 2016-08-31 07:38:09 +0000 | [diff] [blame] | 50 | ThreadNameWithParenthesis(tid, tname, sizeof(tname))); |
Vitaly Buka | 36266b6 | 2017-09-11 20:55:49 +0000 | [diff] [blame] | 51 | Printf("%s", d.Default()); |
Filipe Cabecinhas | 453b555 | 2016-08-31 09:39:47 +0000 | [diff] [blame] | 52 | scariness.Print(); |
Filipe Cabecinhas | b16672d | 2016-08-31 07:38:09 +0000 | [diff] [blame] | 53 | GET_STACK_TRACE_FATAL(second_free_stack->trace[0], |
| 54 | second_free_stack->top_frame_bp); |
| 55 | stack.Print(); |
| 56 | addr_description.Print(); |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 57 | ReportErrorSummary(scariness.GetDescription(), &stack); |
Filipe Cabecinhas | b16672d | 2016-08-31 07:38:09 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Filipe Cabecinhas | 25ad7b5 | 2016-09-07 14:20:54 +0000 | [diff] [blame] | 60 | void ErrorNewDeleteSizeMismatch::Print() { |
| 61 | Decorator d; |
| 62 | Printf("%s", d.Warning()); |
| 63 | char tname[128]; |
| 64 | Report( |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 65 | "ERROR: AddressSanitizer: %s on %p in thread " |
Filipe Cabecinhas | 25ad7b5 | 2016-09-07 14:20:54 +0000 | [diff] [blame] | 66 | "T%d%s:\n", |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 67 | scariness.GetDescription(), addr_description.addr, tid, |
Filipe Cabecinhas | 25ad7b5 | 2016-09-07 14:20:54 +0000 | [diff] [blame] | 68 | ThreadNameWithParenthesis(tid, tname, sizeof(tname))); |
Vitaly Buka | 36266b6 | 2017-09-11 20:55:49 +0000 | [diff] [blame] | 69 | Printf("%s object passed to delete has wrong type:\n", d.Default()); |
Filipe Cabecinhas | 25ad7b5 | 2016-09-07 14:20:54 +0000 | [diff] [blame] | 70 | Printf( |
| 71 | " size of the allocated type: %zd bytes;\n" |
| 72 | " size of the deallocated type: %zd bytes.\n", |
| 73 | addr_description.chunk_access.chunk_size, delete_size); |
| 74 | CHECK_GT(free_stack->size, 0); |
| 75 | scariness.Print(); |
| 76 | GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp); |
| 77 | stack.Print(); |
| 78 | addr_description.Print(); |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 79 | ReportErrorSummary(scariness.GetDescription(), &stack); |
Filipe Cabecinhas | 25ad7b5 | 2016-09-07 14:20:54 +0000 | [diff] [blame] | 80 | Report( |
| 81 | "HINT: if you don't care about these errors you may set " |
| 82 | "ASAN_OPTIONS=new_delete_type_mismatch=0\n"); |
| 83 | } |
| 84 | |
Filipe Cabecinhas | 6fb5462 | 2016-09-13 20:47:29 +0000 | [diff] [blame] | 85 | void ErrorFreeNotMalloced::Print() { |
| 86 | Decorator d; |
| 87 | Printf("%s", d.Warning()); |
| 88 | char tname[128]; |
| 89 | Report( |
| 90 | "ERROR: AddressSanitizer: attempting free on address " |
| 91 | "which was not malloc()-ed: %p in thread T%d%s\n", |
| 92 | addr_description.Address(), tid, |
| 93 | ThreadNameWithParenthesis(tid, tname, sizeof(tname))); |
Vitaly Buka | 36266b6 | 2017-09-11 20:55:49 +0000 | [diff] [blame] | 94 | Printf("%s", d.Default()); |
Filipe Cabecinhas | 6fb5462 | 2016-09-13 20:47:29 +0000 | [diff] [blame] | 95 | CHECK_GT(free_stack->size, 0); |
| 96 | scariness.Print(); |
| 97 | GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp); |
| 98 | stack.Print(); |
| 99 | addr_description.Print(); |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 100 | ReportErrorSummary(scariness.GetDescription(), &stack); |
Filipe Cabecinhas | 6fb5462 | 2016-09-13 20:47:29 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Filipe Cabecinhas | 92c5b5d | 2016-09-13 20:47:33 +0000 | [diff] [blame] | 103 | void ErrorAllocTypeMismatch::Print() { |
| 104 | static const char *alloc_names[] = {"INVALID", "malloc", "operator new", |
| 105 | "operator new []"}; |
| 106 | static const char *dealloc_names[] = {"INVALID", "free", "operator delete", |
| 107 | "operator delete []"}; |
| 108 | CHECK_NE(alloc_type, dealloc_type); |
| 109 | Decorator d; |
| 110 | Printf("%s", d.Warning()); |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 111 | Report("ERROR: AddressSanitizer: %s (%s vs %s) on %p\n", |
| 112 | scariness.GetDescription(), |
Filipe Cabecinhas | 92c5b5d | 2016-09-13 20:47:33 +0000 | [diff] [blame] | 113 | alloc_names[alloc_type], dealloc_names[dealloc_type], |
| 114 | addr_description.addr); |
Vitaly Buka | 36266b6 | 2017-09-11 20:55:49 +0000 | [diff] [blame] | 115 | Printf("%s", d.Default()); |
Filipe Cabecinhas | 92c5b5d | 2016-09-13 20:47:33 +0000 | [diff] [blame] | 116 | CHECK_GT(dealloc_stack->size, 0); |
| 117 | scariness.Print(); |
| 118 | GET_STACK_TRACE_FATAL(dealloc_stack->trace[0], dealloc_stack->top_frame_bp); |
| 119 | stack.Print(); |
| 120 | addr_description.Print(); |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 121 | ReportErrorSummary(scariness.GetDescription(), &stack); |
Filipe Cabecinhas | 92c5b5d | 2016-09-13 20:47:33 +0000 | [diff] [blame] | 122 | Report( |
| 123 | "HINT: if you don't care about these errors you may set " |
| 124 | "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n"); |
| 125 | } |
| 126 | |
Filipe Cabecinhas | 5f862c2 | 2016-09-13 20:47:37 +0000 | [diff] [blame] | 127 | void ErrorMallocUsableSizeNotOwned::Print() { |
| 128 | Decorator d; |
| 129 | Printf("%s", d.Warning()); |
| 130 | Report( |
| 131 | "ERROR: AddressSanitizer: attempting to call malloc_usable_size() for " |
| 132 | "pointer which is not owned: %p\n", |
| 133 | addr_description.Address()); |
Vitaly Buka | 36266b6 | 2017-09-11 20:55:49 +0000 | [diff] [blame] | 134 | Printf("%s", d.Default()); |
Filipe Cabecinhas | 5f862c2 | 2016-09-13 20:47:37 +0000 | [diff] [blame] | 135 | stack->Print(); |
| 136 | addr_description.Print(); |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 137 | ReportErrorSummary(scariness.GetDescription(), stack); |
Filipe Cabecinhas | 5f862c2 | 2016-09-13 20:47:37 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Filipe Cabecinhas | b0de43a | 2016-09-13 20:47:42 +0000 | [diff] [blame] | 140 | void ErrorSanitizerGetAllocatedSizeNotOwned::Print() { |
| 141 | Decorator d; |
| 142 | Printf("%s", d.Warning()); |
| 143 | Report( |
| 144 | "ERROR: AddressSanitizer: attempting to call " |
| 145 | "__sanitizer_get_allocated_size() for pointer which is not owned: %p\n", |
| 146 | addr_description.Address()); |
Vitaly Buka | 36266b6 | 2017-09-11 20:55:49 +0000 | [diff] [blame] | 147 | Printf("%s", d.Default()); |
Filipe Cabecinhas | b0de43a | 2016-09-13 20:47:42 +0000 | [diff] [blame] | 148 | stack->Print(); |
| 149 | addr_description.Print(); |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 150 | ReportErrorSummary(scariness.GetDescription(), stack); |
Filipe Cabecinhas | b0de43a | 2016-09-13 20:47:42 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Filipe Cabecinhas | 7a196b9 | 2016-09-14 07:37:14 +0000 | [diff] [blame] | 153 | void ErrorStringFunctionMemoryRangesOverlap::Print() { |
| 154 | Decorator d; |
| 155 | char bug_type[100]; |
| 156 | internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function); |
| 157 | Printf("%s", d.Warning()); |
| 158 | Report( |
| 159 | "ERROR: AddressSanitizer: %s: memory ranges [%p,%p) and [%p, %p) " |
| 160 | "overlap\n", |
| 161 | bug_type, addr1_description.Address(), |
| 162 | addr1_description.Address() + length1, addr2_description.Address(), |
| 163 | addr2_description.Address() + length2); |
Vitaly Buka | 36266b6 | 2017-09-11 20:55:49 +0000 | [diff] [blame] | 164 | Printf("%s", d.Default()); |
Filipe Cabecinhas | 7a196b9 | 2016-09-14 07:37:14 +0000 | [diff] [blame] | 165 | scariness.Print(); |
| 166 | stack->Print(); |
| 167 | addr1_description.Print(); |
| 168 | addr2_description.Print(); |
| 169 | ReportErrorSummary(bug_type, stack); |
| 170 | } |
| 171 | |
Filipe Cabecinhas | 36229e9 | 2016-09-14 07:37:20 +0000 | [diff] [blame] | 172 | void ErrorStringFunctionSizeOverflow::Print() { |
| 173 | Decorator d; |
| 174 | Printf("%s", d.Warning()); |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 175 | Report("ERROR: AddressSanitizer: %s: (size=%zd)\n", |
| 176 | scariness.GetDescription(), size); |
Vitaly Buka | 36266b6 | 2017-09-11 20:55:49 +0000 | [diff] [blame] | 177 | Printf("%s", d.Default()); |
Filipe Cabecinhas | 36229e9 | 2016-09-14 07:37:20 +0000 | [diff] [blame] | 178 | scariness.Print(); |
| 179 | stack->Print(); |
| 180 | addr_description.Print(); |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 181 | ReportErrorSummary(scariness.GetDescription(), stack); |
Filipe Cabecinhas | 36229e9 | 2016-09-14 07:37:20 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Filipe Cabecinhas | b50a5b3 | 2016-09-15 08:10:48 +0000 | [diff] [blame] | 184 | void ErrorBadParamsToAnnotateContiguousContainer::Print() { |
| 185 | Report( |
| 186 | "ERROR: AddressSanitizer: bad parameters to " |
| 187 | "__sanitizer_annotate_contiguous_container:\n" |
| 188 | " beg : %p\n" |
| 189 | " end : %p\n" |
| 190 | " old_mid : %p\n" |
| 191 | " new_mid : %p\n", |
| 192 | beg, end, old_mid, new_mid); |
| 193 | uptr granularity = SHADOW_GRANULARITY; |
| 194 | if (!IsAligned(beg, granularity)) |
| 195 | Report("ERROR: beg is not aligned by %d\n", granularity); |
| 196 | stack->Print(); |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 197 | ReportErrorSummary(scariness.GetDescription(), stack); |
Filipe Cabecinhas | b50a5b3 | 2016-09-15 08:10:48 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Filipe Cabecinhas | 719db0c | 2016-09-15 08:10:52 +0000 | [diff] [blame] | 200 | void ErrorODRViolation::Print() { |
| 201 | Decorator d; |
| 202 | Printf("%s", d.Warning()); |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 203 | Report("ERROR: AddressSanitizer: %s (%p):\n", scariness.GetDescription(), |
| 204 | global1.beg); |
Vitaly Buka | 36266b6 | 2017-09-11 20:55:49 +0000 | [diff] [blame] | 205 | Printf("%s", d.Default()); |
Filipe Cabecinhas | 719db0c | 2016-09-15 08:10:52 +0000 | [diff] [blame] | 206 | InternalScopedString g1_loc(256), g2_loc(256); |
| 207 | PrintGlobalLocation(&g1_loc, global1); |
| 208 | PrintGlobalLocation(&g2_loc, global2); |
| 209 | Printf(" [1] size=%zd '%s' %s\n", global1.size, |
| 210 | MaybeDemangleGlobalName(global1.name), g1_loc.data()); |
| 211 | Printf(" [2] size=%zd '%s' %s\n", global2.size, |
| 212 | MaybeDemangleGlobalName(global2.name), g2_loc.data()); |
| 213 | if (stack_id1 && stack_id2) { |
| 214 | Printf("These globals were registered at these points:\n"); |
| 215 | Printf(" [1]:\n"); |
| 216 | StackDepotGet(stack_id1).Print(); |
| 217 | Printf(" [2]:\n"); |
| 218 | StackDepotGet(stack_id2).Print(); |
| 219 | } |
| 220 | Report( |
| 221 | "HINT: if you don't care about these errors you may set " |
| 222 | "ASAN_OPTIONS=detect_odr_violation=0\n"); |
| 223 | InternalScopedString error_msg(256); |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 224 | error_msg.append("%s: global '%s' at %s", scariness.GetDescription(), |
Filipe Cabecinhas | 719db0c | 2016-09-15 08:10:52 +0000 | [diff] [blame] | 225 | MaybeDemangleGlobalName(global1.name), g1_loc.data()); |
| 226 | ReportErrorSummary(error_msg.data()); |
| 227 | } |
| 228 | |
Filipe Cabecinhas | 1b3742e | 2016-09-15 08:10:56 +0000 | [diff] [blame] | 229 | void ErrorInvalidPointerPair::Print() { |
Filipe Cabecinhas | 1b3742e | 2016-09-15 08:10:56 +0000 | [diff] [blame] | 230 | Decorator d; |
| 231 | Printf("%s", d.Warning()); |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 232 | Report("ERROR: AddressSanitizer: %s: %p %p\n", scariness.GetDescription(), |
Filipe Cabecinhas | 490f96c | 2016-09-21 19:21:01 +0000 | [diff] [blame] | 233 | addr1_description.Address(), addr2_description.Address()); |
Vitaly Buka | 36266b6 | 2017-09-11 20:55:49 +0000 | [diff] [blame] | 234 | Printf("%s", d.Default()); |
Filipe Cabecinhas | 1b3742e | 2016-09-15 08:10:56 +0000 | [diff] [blame] | 235 | GET_STACK_TRACE_FATAL(pc, bp); |
| 236 | stack.Print(); |
Filipe Cabecinhas | 490f96c | 2016-09-21 19:21:01 +0000 | [diff] [blame] | 237 | addr1_description.Print(); |
| 238 | addr2_description.Print(); |
Kuba Mracek | 48090f5 | 2016-11-28 21:18:15 +0000 | [diff] [blame] | 239 | ReportErrorSummary(scariness.GetDescription(), &stack); |
Filipe Cabecinhas | 1b3742e | 2016-09-15 08:10:56 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Filipe Cabecinhas | a8b5f5e | 2016-09-21 20:18:18 +0000 | [diff] [blame] | 242 | static bool AdjacentShadowValuesAreFullyPoisoned(u8 *s) { |
| 243 | return s[-1] > 127 && s[1] > 127; |
| 244 | } |
| 245 | |
| 246 | ErrorGeneric::ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr, |
| 247 | bool is_write_, uptr access_size_) |
| 248 | : ErrorBase(tid), |
| 249 | addr_description(addr, access_size_, /*shouldLockThreadRegistry=*/false), |
| 250 | pc(pc_), |
| 251 | bp(bp_), |
| 252 | sp(sp_), |
| 253 | access_size(access_size_), |
| 254 | is_write(is_write_), |
| 255 | shadow_val(0) { |
| 256 | scariness.Clear(); |
| 257 | if (access_size) { |
| 258 | if (access_size <= 9) { |
| 259 | char desr[] = "?-byte"; |
| 260 | desr[0] = '0' + access_size; |
| 261 | scariness.Scare(access_size + access_size / 2, desr); |
| 262 | } else if (access_size >= 10) { |
| 263 | scariness.Scare(15, "multi-byte"); |
| 264 | } |
| 265 | is_write ? scariness.Scare(20, "write") : scariness.Scare(1, "read"); |
| 266 | |
| 267 | // Determine the error type. |
| 268 | bug_descr = "unknown-crash"; |
| 269 | if (AddrIsInMem(addr)) { |
| 270 | u8 *shadow_addr = (u8 *)MemToShadow(addr); |
| 271 | // If we are accessing 16 bytes, look at the second shadow byte. |
| 272 | if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY) shadow_addr++; |
| 273 | // If we are in the partial right redzone, look at the next shadow byte. |
| 274 | if (*shadow_addr > 0 && *shadow_addr < 128) shadow_addr++; |
| 275 | bool far_from_bounds = false; |
| 276 | shadow_val = *shadow_addr; |
| 277 | int bug_type_score = 0; |
| 278 | // For use-after-frees reads are almost as bad as writes. |
| 279 | int read_after_free_bonus = 0; |
| 280 | switch (shadow_val) { |
| 281 | case kAsanHeapLeftRedzoneMagic: |
| 282 | case kAsanArrayCookieMagic: |
| 283 | bug_descr = "heap-buffer-overflow"; |
| 284 | bug_type_score = 10; |
| 285 | far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr); |
| 286 | break; |
| 287 | case kAsanHeapFreeMagic: |
| 288 | bug_descr = "heap-use-after-free"; |
| 289 | bug_type_score = 20; |
| 290 | if (!is_write) read_after_free_bonus = 18; |
| 291 | break; |
| 292 | case kAsanStackLeftRedzoneMagic: |
| 293 | bug_descr = "stack-buffer-underflow"; |
| 294 | bug_type_score = 25; |
| 295 | far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr); |
| 296 | break; |
| 297 | case kAsanInitializationOrderMagic: |
| 298 | bug_descr = "initialization-order-fiasco"; |
| 299 | bug_type_score = 1; |
| 300 | break; |
| 301 | case kAsanStackMidRedzoneMagic: |
| 302 | case kAsanStackRightRedzoneMagic: |
| 303 | bug_descr = "stack-buffer-overflow"; |
| 304 | bug_type_score = 25; |
| 305 | far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr); |
| 306 | break; |
| 307 | case kAsanStackAfterReturnMagic: |
| 308 | bug_descr = "stack-use-after-return"; |
| 309 | bug_type_score = 30; |
| 310 | if (!is_write) read_after_free_bonus = 18; |
| 311 | break; |
| 312 | case kAsanUserPoisonedMemoryMagic: |
| 313 | bug_descr = "use-after-poison"; |
| 314 | bug_type_score = 20; |
| 315 | break; |
| 316 | case kAsanContiguousContainerOOBMagic: |
| 317 | bug_descr = "container-overflow"; |
| 318 | bug_type_score = 10; |
| 319 | break; |
| 320 | case kAsanStackUseAfterScopeMagic: |
| 321 | bug_descr = "stack-use-after-scope"; |
| 322 | bug_type_score = 10; |
| 323 | break; |
| 324 | case kAsanGlobalRedzoneMagic: |
| 325 | bug_descr = "global-buffer-overflow"; |
| 326 | bug_type_score = 10; |
| 327 | far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr); |
| 328 | break; |
| 329 | case kAsanIntraObjectRedzone: |
| 330 | bug_descr = "intra-object-overflow"; |
| 331 | bug_type_score = 10; |
| 332 | break; |
| 333 | case kAsanAllocaLeftMagic: |
| 334 | case kAsanAllocaRightMagic: |
| 335 | bug_descr = "dynamic-stack-buffer-overflow"; |
| 336 | bug_type_score = 25; |
| 337 | far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr); |
| 338 | break; |
| 339 | } |
| 340 | scariness.Scare(bug_type_score + read_after_free_bonus, bug_descr); |
| 341 | if (far_from_bounds) scariness.Scare(10, "far-from-bounds"); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | static void PrintContainerOverflowHint() { |
| 347 | Printf("HINT: if you don't care about these errors you may set " |
| 348 | "ASAN_OPTIONS=detect_container_overflow=0.\n" |
| 349 | "If you suspect a false positive see also: " |
| 350 | "https://github.com/google/sanitizers/wiki/" |
| 351 | "AddressSanitizerContainerOverflow.\n"); |
| 352 | } |
| 353 | |
| 354 | static void PrintShadowByte(InternalScopedString *str, const char *before, |
| 355 | u8 byte, const char *after = "\n") { |
| 356 | PrintMemoryByte(str, before, byte, /*in_shadow*/true, after); |
| 357 | } |
| 358 | |
| 359 | static void PrintLegend(InternalScopedString *str) { |
| 360 | str->append( |
| 361 | "Shadow byte legend (one shadow byte represents %d " |
| 362 | "application bytes):\n", |
| 363 | (int)SHADOW_GRANULARITY); |
| 364 | PrintShadowByte(str, " Addressable: ", 0); |
| 365 | str->append(" Partially addressable: "); |
| 366 | for (u8 i = 1; i < SHADOW_GRANULARITY; i++) PrintShadowByte(str, "", i, " "); |
| 367 | str->append("\n"); |
| 368 | PrintShadowByte(str, " Heap left redzone: ", |
| 369 | kAsanHeapLeftRedzoneMagic); |
| 370 | PrintShadowByte(str, " Freed heap region: ", kAsanHeapFreeMagic); |
| 371 | PrintShadowByte(str, " Stack left redzone: ", |
| 372 | kAsanStackLeftRedzoneMagic); |
| 373 | PrintShadowByte(str, " Stack mid redzone: ", |
| 374 | kAsanStackMidRedzoneMagic); |
| 375 | PrintShadowByte(str, " Stack right redzone: ", |
| 376 | kAsanStackRightRedzoneMagic); |
| 377 | PrintShadowByte(str, " Stack after return: ", |
| 378 | kAsanStackAfterReturnMagic); |
| 379 | PrintShadowByte(str, " Stack use after scope: ", |
| 380 | kAsanStackUseAfterScopeMagic); |
| 381 | PrintShadowByte(str, " Global redzone: ", kAsanGlobalRedzoneMagic); |
| 382 | PrintShadowByte(str, " Global init order: ", |
| 383 | kAsanInitializationOrderMagic); |
| 384 | PrintShadowByte(str, " Poisoned by user: ", |
| 385 | kAsanUserPoisonedMemoryMagic); |
| 386 | PrintShadowByte(str, " Container overflow: ", |
| 387 | kAsanContiguousContainerOOBMagic); |
| 388 | PrintShadowByte(str, " Array cookie: ", |
| 389 | kAsanArrayCookieMagic); |
| 390 | PrintShadowByte(str, " Intra object redzone: ", |
| 391 | kAsanIntraObjectRedzone); |
| 392 | PrintShadowByte(str, " ASan internal: ", kAsanInternalHeapMagic); |
| 393 | PrintShadowByte(str, " Left alloca redzone: ", kAsanAllocaLeftMagic); |
| 394 | PrintShadowByte(str, " Right alloca redzone: ", kAsanAllocaRightMagic); |
| 395 | } |
| 396 | |
| 397 | static void PrintShadowBytes(InternalScopedString *str, const char *before, |
| 398 | u8 *bytes, u8 *guilty, uptr n) { |
| 399 | Decorator d; |
| 400 | if (before) str->append("%s%p:", before, bytes); |
| 401 | for (uptr i = 0; i < n; i++) { |
| 402 | u8 *p = bytes + i; |
| 403 | const char *before = |
| 404 | p == guilty ? "[" : (p - 1 == guilty && i != 0) ? "" : " "; |
| 405 | const char *after = p == guilty ? "]" : ""; |
| 406 | PrintShadowByte(str, before, *p, after); |
| 407 | } |
| 408 | str->append("\n"); |
| 409 | } |
| 410 | |
| 411 | static void PrintShadowMemoryForAddress(uptr addr) { |
| 412 | if (!AddrIsInMem(addr)) return; |
| 413 | uptr shadow_addr = MemToShadow(addr); |
| 414 | const uptr n_bytes_per_row = 16; |
| 415 | uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1); |
| 416 | InternalScopedString str(4096 * 8); |
| 417 | str.append("Shadow bytes around the buggy address:\n"); |
| 418 | for (int i = -5; i <= 5; i++) { |
| 419 | const char *prefix = (i == 0) ? "=>" : " "; |
| 420 | PrintShadowBytes(&str, prefix, (u8 *)(aligned_shadow + i * n_bytes_per_row), |
| 421 | (u8 *)shadow_addr, n_bytes_per_row); |
| 422 | } |
| 423 | if (flags()->print_legend) PrintLegend(&str); |
| 424 | Printf("%s", str.data()); |
| 425 | } |
| 426 | |
| 427 | void ErrorGeneric::Print() { |
| 428 | Decorator d; |
| 429 | Printf("%s", d.Warning()); |
| 430 | uptr addr = addr_description.Address(); |
| 431 | Report("ERROR: AddressSanitizer: %s on address %p at pc %p bp %p sp %p\n", |
| 432 | bug_descr, (void *)addr, pc, bp, sp); |
Vitaly Buka | 36266b6 | 2017-09-11 20:55:49 +0000 | [diff] [blame] | 433 | Printf("%s", d.Default()); |
Filipe Cabecinhas | a8b5f5e | 2016-09-21 20:18:18 +0000 | [diff] [blame] | 434 | |
| 435 | char tname[128]; |
| 436 | Printf("%s%s of size %zu at %p thread T%d%s%s\n", d.Access(), |
| 437 | access_size ? (is_write ? "WRITE" : "READ") : "ACCESS", access_size, |
| 438 | (void *)addr, tid, |
Vitaly Buka | 36266b6 | 2017-09-11 20:55:49 +0000 | [diff] [blame] | 439 | ThreadNameWithParenthesis(tid, tname, sizeof(tname)), d.Default()); |
Filipe Cabecinhas | a8b5f5e | 2016-09-21 20:18:18 +0000 | [diff] [blame] | 440 | |
| 441 | scariness.Print(); |
| 442 | GET_STACK_TRACE_FATAL(pc, bp); |
| 443 | stack.Print(); |
| 444 | |
| 445 | // Pass bug_descr because we have a special case for |
| 446 | // initialization-order-fiasco |
| 447 | addr_description.Print(bug_descr); |
| 448 | if (shadow_val == kAsanContiguousContainerOOBMagic) |
| 449 | PrintContainerOverflowHint(); |
| 450 | ReportErrorSummary(bug_descr, &stack); |
| 451 | PrintShadowMemoryForAddress(addr); |
| 452 | } |
| 453 | |
Filipe Cabecinhas | fddfdca | 2016-08-30 17:08:55 +0000 | [diff] [blame] | 454 | } // namespace __asan |