| Kostya Serebryany | 4a42cf6 | 2012-12-27 14:09:19 +0000 | [diff] [blame] | 1 | //===-- msan_report.cc ----------------------------------------------------===// |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 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 MemorySanitizer. |
| 11 | // |
| 12 | // Error reporting. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "msan.h" |
| Alexey Samsonov | c30e2d6 | 2013-05-29 09:15:39 +0000 | [diff] [blame] | 16 | #include "sanitizer_common/sanitizer_allocator_internal.h" |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 17 | #include "sanitizer_common/sanitizer_common.h" |
| Sergey Matveev | 6eff11e | 2013-05-06 13:15:14 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_flags.h" |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_mutex.h" |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_report_decorator.h" |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_stackdepot.h" |
| Kostya Serebryany | 7b0b9b3 | 2013-02-07 08:04:56 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_symbolizer.h" |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace __sanitizer; |
| 25 | |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 26 | namespace __msan { |
| 27 | |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 28 | class Decorator: private __sanitizer::AnsiColorDecorator { |
| 29 | public: |
| 30 | Decorator() : __sanitizer::AnsiColorDecorator(PrintsToTtyCached()) { } |
| 31 | const char *Warning() { return Red(); } |
| 32 | const char *Origin() { return Magenta(); } |
| 33 | const char *Name() { return Green(); } |
| 34 | const char *End() { return Default(); } |
| 35 | }; |
| 36 | |
| Evgeniy Stepanov | 412d973 | 2014-03-18 13:45:19 +0000 | [diff] [blame] | 37 | static void DescribeStackOrigin(const char *so, uptr pc) { |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 38 | Decorator d; |
| Evgeniy Stepanov | 412d973 | 2014-03-18 13:45:19 +0000 | [diff] [blame] | 39 | char *s = internal_strdup(so); |
| 40 | char *sep = internal_strchr(s, '@'); |
| 41 | CHECK(sep); |
| 42 | *sep = '\0'; |
| 43 | Printf("%s", d.Origin()); |
| 44 | Printf( |
| 45 | " %sUninitialized value was created by an allocation of '%s%s%s'" |
| 46 | " in the stack frame of function '%s%s%s'%s\n", |
| 47 | d.Origin(), d.Name(), s, d.Origin(), d.Name(), |
| 48 | Symbolizer::Get()->Demangle(sep + 1), d.Origin(), d.End()); |
| 49 | InternalFree(s); |
| 50 | |
| 51 | if (pc) { |
| 52 | // For some reason function address in LLVM IR is 1 less then the address |
| 53 | // of the first instruction. |
| 54 | pc += 1; |
| 55 | StackTrace::PrintStack(&pc, 1); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | static void DescribeOrigin(u32 origin) { |
| Sergey Matveev | 9be70fb | 2013-12-05 12:04:51 +0000 | [diff] [blame] | 60 | VPrintf(1, " raw origin id: %d\n", origin); |
| Evgeniy Stepanov | ac5ac34 | 2013-09-13 12:49:13 +0000 | [diff] [blame] | 61 | uptr pc; |
| Evgeniy Stepanov | 412d973 | 2014-03-18 13:45:19 +0000 | [diff] [blame] | 62 | while (true) { |
| 63 | if (const char *so = GetOriginDescrIfStack(origin, &pc)) { |
| 64 | DescribeStackOrigin(so, pc); |
| 65 | break; |
| Evgeniy Stepanov | ac5ac34 | 2013-09-13 12:49:13 +0000 | [diff] [blame] | 66 | } |
| Evgeniy Stepanov | 412d973 | 2014-03-18 13:45:19 +0000 | [diff] [blame] | 67 | Decorator d; |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 68 | uptr size = 0; |
| 69 | const uptr *trace = StackDepotGet(origin, &size); |
| Evgeniy Stepanov | 412d973 | 2014-03-18 13:45:19 +0000 | [diff] [blame] | 70 | CHECK_GT(size, 0); |
| 71 | if (TRACE_IS_CHAINED(trace[size - 1])) { |
| 72 | // Linked origin. |
| 73 | // FIXME: copied? modified? passed through? observed? |
| 74 | Printf(" %sUninitialized value was stored to memory at%s\n", d.Origin(), |
| 75 | d.End()); |
| 76 | StackTrace::PrintStack(trace, size - 1); |
| 77 | origin = TRACE_TO_CHAINED_ID(trace[size - 1]); |
| 78 | } else { |
| 79 | Printf(" %sUninitialized value was created by a heap allocation%s\n", |
| 80 | d.Origin(), d.End()); |
| 81 | StackTrace::PrintStack(trace, size); |
| 82 | break; |
| 83 | } |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | void ReportUMR(StackTrace *stack, u32 origin) { |
| 88 | if (!__msan::flags()->report_umrs) return; |
| 89 | |
| Alexey Samsonov | 734aab4 | 2013-04-05 07:30:29 +0000 | [diff] [blame] | 90 | SpinMutexLock l(&CommonSanitizerReportMutex); |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 91 | |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 92 | Decorator d; |
| 93 | Printf("%s", d.Warning()); |
| Evgeniy Stepanov | dd0780f | 2013-05-28 14:27:30 +0000 | [diff] [blame] | 94 | Report(" WARNING: MemorySanitizer: use-of-uninitialized-value\n"); |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 95 | Printf("%s", d.End()); |
| Alexey Samsonov | f2c7659 | 2013-12-19 11:25:05 +0000 | [diff] [blame] | 96 | stack->Print(); |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 97 | if (origin) { |
| 98 | DescribeOrigin(origin); |
| 99 | } |
| Alexey Samsonov | 5dc6cff | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 100 | ReportErrorSummary("use-of-uninitialized-value", stack); |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void ReportExpectedUMRNotFound(StackTrace *stack) { |
| Alexey Samsonov | 734aab4 | 2013-04-05 07:30:29 +0000 | [diff] [blame] | 104 | SpinMutexLock l(&CommonSanitizerReportMutex); |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 105 | |
| 106 | Printf(" WARNING: Expected use of uninitialized value not found\n"); |
| Alexey Samsonov | f2c7659 | 2013-12-19 11:25:05 +0000 | [diff] [blame] | 107 | stack->Print(); |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| Evgeniy Stepanov | 9b52ce9 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 110 | void ReportAtExitStatistics() { |
| Alexey Samsonov | 734aab4 | 2013-04-05 07:30:29 +0000 | [diff] [blame] | 111 | SpinMutexLock l(&CommonSanitizerReportMutex); |
| 112 | |
| Evgeniy Stepanov | 412d973 | 2014-03-18 13:45:19 +0000 | [diff] [blame] | 113 | if (msan_report_count > 0) { |
| 114 | Decorator d; |
| 115 | Printf("%s", d.Warning()); |
| 116 | Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count); |
| 117 | Printf("%s", d.End()); |
| 118 | } |
| 119 | |
| 120 | StackDepotStats *stack_depot_stats = StackDepotGetStats(); |
| 121 | // FIXME: we want this at normal exit, too! |
| 122 | // FIXME: but only with verbosity=1 or something |
| 123 | Printf("Unique heap origins: %zu\n", stack_depot_stats->n_uniq_ids); |
| 124 | Printf("Stack depot mapped bytes: %zu\n", stack_depot_stats->mapped); |
| Evgeniy Stepanov | 9b52ce9 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| Evgeniy Stepanov | fe250b0 | 2014-04-30 09:50:30 +0000 | [diff] [blame^] | 127 | class OriginSet { |
| 128 | public: |
| 129 | OriginSet() : next_id_(0) {} |
| 130 | int insert(u32 o) { |
| 131 | // Scan from the end for better locality. |
| 132 | for (int i = next_id_ - 1; i >= 0; --i) |
| 133 | if (origins_[i] == o) return i; |
| 134 | if (next_id_ == kMaxSize_) return OVERFLOW; |
| 135 | int id = next_id_++; |
| 136 | origins_[id] = o; |
| 137 | return id; |
| 138 | } |
| 139 | int size() { return next_id_; } |
| 140 | u32 get(int id) { return origins_[id]; } |
| 141 | static char asChar(int id) { |
| 142 | switch (id) { |
| 143 | case MISSING: |
| 144 | return '.'; |
| 145 | case OVERFLOW: |
| 146 | return '*'; |
| 147 | default: |
| 148 | return 'A' + id; |
| 149 | } |
| 150 | } |
| 151 | static const int OVERFLOW = -1; |
| 152 | static const int MISSING = -2; |
| 153 | |
| 154 | private: |
| 155 | static const int kMaxSize_ = 'Z' - 'A' + 1; |
| 156 | u32 origins_[kMaxSize_]; |
| 157 | int next_id_; |
| 158 | }; |
| 159 | |
| 160 | void DescribeMemoryRange(const void *x, uptr size) { |
| 161 | // Real limits. |
| 162 | uptr start = MEM_TO_SHADOW(x); |
| 163 | uptr end = start + size; |
| 164 | // Scan limits: align start down to 4; align size up to 16. |
| 165 | uptr s = start & ~3UL; |
| 166 | size = end - s; |
| 167 | size = (size + 15) & ~15UL; |
| 168 | uptr e = s + size; |
| 169 | |
| 170 | // Single letter names to origin id mapping. |
| 171 | OriginSet origin_set; |
| 172 | |
| 173 | uptr pos = 0; // Offset from aligned start. |
| 174 | bool with_origins = __msan_get_track_origins(); |
| 175 | // True if there is at least 1 poisoned bit in the last 4-byte group. |
| 176 | bool last_quad_poisoned; |
| 177 | int origin_ids[4]; // Single letter origin ids for the current line. |
| 178 | |
| 179 | Decorator d; |
| 180 | Printf("%s", d.Warning()); |
| 181 | Printf("Shadow map of [%p, %p), %zu bytes:\n", start, end, end - start); |
| 182 | Printf("%s", d.End()); |
| 183 | while (s < e) { |
| 184 | // Line start. |
| 185 | if (pos % 16 == 0) { |
| 186 | for (int i = 0; i < 4; ++i) origin_ids[i] = -1; |
| 187 | Printf("%p:", s); |
| 188 | } |
| 189 | // Group start. |
| 190 | if (pos % 4 == 0) { |
| 191 | Printf(" "); |
| 192 | last_quad_poisoned = false; |
| 193 | } |
| 194 | // Print shadow byte. |
| 195 | if (s < start || s >= end) { |
| 196 | Printf(".."); |
| 197 | } else { |
| 198 | unsigned char v = *(unsigned char *)s; |
| 199 | if (v) last_quad_poisoned = true; |
| 200 | Printf("%02x", v); |
| 201 | } |
| 202 | // Group end. |
| 203 | if (pos % 4 == 3 && with_origins) { |
| 204 | int id = OriginSet::MISSING; |
| 205 | if (last_quad_poisoned) { |
| 206 | u32 o = *(u32 *)SHADOW_TO_ORIGIN(s - 3); |
| 207 | id = origin_set.insert(o); |
| 208 | } |
| 209 | origin_ids[(pos % 16) / 4] = id; |
| 210 | } |
| 211 | // Line end. |
| 212 | if (pos % 16 == 15) { |
| 213 | if (with_origins) { |
| 214 | Printf(" |"); |
| 215 | for (int i = 0; i < 4; ++i) { |
| 216 | char c = OriginSet::asChar(origin_ids[i]); |
| 217 | Printf("%c", c); |
| 218 | if (i != 3) Printf(" "); |
| 219 | } |
| 220 | Printf("|"); |
| 221 | } |
| 222 | Printf("\n"); |
| 223 | } |
| 224 | size--; |
| 225 | s++; |
| 226 | pos++; |
| 227 | } |
| 228 | |
| 229 | Printf("\n"); |
| 230 | |
| 231 | for (int i = 0; i < origin_set.size(); ++i) { |
| 232 | u32 o = origin_set.get(i); |
| 233 | Printf("Origin %c (origin_id %x):\n", OriginSet::asChar(i), o); |
| 234 | DescribeOrigin(o); |
| 235 | } |
| 236 | } |
| 237 | |
| Alexey Samsonov | 49a32c1 | 2013-01-30 07:45:58 +0000 | [diff] [blame] | 238 | } // namespace __msan |