Kostya Serebryany | 4b48f45 | 2012-12-27 14:09:19 +0000 | [diff] [blame] | 1 | //===-- msan_report.cc ----------------------------------------------------===// |
Evgeniy Stepanov | db010da | 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 | 1f3c2fe | 2013-05-29 09:15:39 +0000 | [diff] [blame] | 16 | #include "sanitizer_common/sanitizer_allocator_internal.h" |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 17 | #include "sanitizer_common/sanitizer_common.h" |
Sergey Matveev | 0b4bf4d | 2013-05-06 13:15:14 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_flags.h" |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_mutex.h" |
Evgeniy Stepanov | c209ba6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_report_decorator.h" |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_stackdepot.h" |
Kostya Serebryany | 1d333c5 | 2013-02-07 08:04:56 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_symbolizer.h" |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace __sanitizer; |
| 25 | |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 26 | namespace __msan { |
| 27 | |
Evgeniy Stepanov | c209ba6 | 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 | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 37 | static void DescribeOrigin(u32 origin) { |
Evgeniy Stepanov | c209ba6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 38 | Decorator d; |
Dmitry Vyukov | 6866dba | 2013-10-15 13:28:51 +0000 | [diff] [blame] | 39 | if (common_flags()->verbosity) |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 40 | Printf(" raw origin id: %d\n", origin); |
Evgeniy Stepanov | 6f34605 | 2013-09-13 12:49:13 +0000 | [diff] [blame] | 41 | uptr pc; |
| 42 | if (const char *so = GetOriginDescrIfStack(origin, &pc)) { |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 43 | char* s = internal_strdup(so); |
| 44 | char* sep = internal_strchr(s, '@'); |
| 45 | CHECK(sep); |
| 46 | *sep = '\0'; |
Evgeniy Stepanov | c209ba6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 47 | Printf("%s", d.Origin()); |
Evgeniy Stepanov | 9af8676 | 2013-02-11 11:34:26 +0000 | [diff] [blame] | 48 | Printf(" %sUninitialized value was created by an allocation of '%s%s%s'" |
Evgeniy Stepanov | c209ba6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 49 | " in the stack frame of function '%s%s%s'%s\n", |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 50 | d.Origin(), d.Name(), s, d.Origin(), d.Name(), |
Peter Collingbourne | c1a1ed6 | 2013-10-25 23:03:29 +0000 | [diff] [blame] | 51 | Symbolizer::Get()->Demangle(sep + 1), d.Origin(), d.End()); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 52 | InternalFree(s); |
Evgeniy Stepanov | 6f34605 | 2013-09-13 12:49:13 +0000 | [diff] [blame] | 53 | |
| 54 | if (pc) { |
| 55 | // For some reason function address in LLVM IR is 1 less then the address |
| 56 | // of the first instruction. |
| 57 | pc += 1; |
Alexey Samsonov | a96c4dc | 2013-11-01 00:19:46 +0000 | [diff] [blame] | 58 | StackTrace::PrintStack(&pc, 1); |
Evgeniy Stepanov | 6f34605 | 2013-09-13 12:49:13 +0000 | [diff] [blame] | 59 | } |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 60 | } else { |
| 61 | uptr size = 0; |
| 62 | const uptr *trace = StackDepotGet(origin, &size); |
Evgeniy Stepanov | 9af8676 | 2013-02-11 11:34:26 +0000 | [diff] [blame] | 63 | Printf(" %sUninitialized value was created by a heap allocation%s\n", |
Evgeniy Stepanov | c209ba6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 64 | d.Origin(), d.End()); |
Alexey Samsonov | a96c4dc | 2013-11-01 00:19:46 +0000 | [diff] [blame] | 65 | StackTrace::PrintStack(trace, size); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | void ReportUMR(StackTrace *stack, u32 origin) { |
| 70 | if (!__msan::flags()->report_umrs) return; |
| 71 | |
Alexey Samsonov | 7ed46ff | 2013-04-05 07:30:29 +0000 | [diff] [blame] | 72 | SpinMutexLock l(&CommonSanitizerReportMutex); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 73 | |
Evgeniy Stepanov | c209ba6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 74 | Decorator d; |
| 75 | Printf("%s", d.Warning()); |
Evgeniy Stepanov | 6ac157d | 2013-05-28 14:27:30 +0000 | [diff] [blame] | 76 | Report(" WARNING: MemorySanitizer: use-of-uninitialized-value\n"); |
Evgeniy Stepanov | c209ba6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 77 | Printf("%s", d.End()); |
Alexey Samsonov | a96c4dc | 2013-11-01 00:19:46 +0000 | [diff] [blame] | 78 | StackTrace::PrintStack(stack->trace, stack->size); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 79 | if (origin) { |
| 80 | DescribeOrigin(origin); |
| 81 | } |
Alexey Samsonov | 2fb0872 | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 82 | ReportErrorSummary("use-of-uninitialized-value", stack); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void ReportExpectedUMRNotFound(StackTrace *stack) { |
Alexey Samsonov | 7ed46ff | 2013-04-05 07:30:29 +0000 | [diff] [blame] | 86 | SpinMutexLock l(&CommonSanitizerReportMutex); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 87 | |
| 88 | Printf(" WARNING: Expected use of uninitialized value not found\n"); |
Alexey Samsonov | a96c4dc | 2013-11-01 00:19:46 +0000 | [diff] [blame] | 89 | StackTrace::PrintStack(stack->trace, stack->size); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Evgeniy Stepanov | 99bf1d7 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 92 | void ReportAtExitStatistics() { |
Alexey Samsonov | 7ed46ff | 2013-04-05 07:30:29 +0000 | [diff] [blame] | 93 | SpinMutexLock l(&CommonSanitizerReportMutex); |
| 94 | |
Evgeniy Stepanov | 99bf1d7 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 95 | Decorator d; |
| 96 | Printf("%s", d.Warning()); |
| 97 | Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count); |
| 98 | Printf("%s", d.End()); |
| 99 | } |
| 100 | |
Alexey Samsonov | ba5e996 | 2013-01-30 07:45:58 +0000 | [diff] [blame] | 101 | } // namespace __msan |