| 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 | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 37 | static void DescribeOrigin(u32 origin) { |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 38 | Decorator d; |
| Sergey Matveev | 9be70fb | 2013-12-05 12:04:51 +0000 | [diff] [blame] | 39 | VPrintf(1, " raw origin id: %d\n", origin); |
| Evgeniy Stepanov | ac5ac34 | 2013-09-13 12:49:13 +0000 | [diff] [blame] | 40 | uptr pc; |
| 41 | if (const char *so = GetOriginDescrIfStack(origin, &pc)) { |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 42 | char* s = internal_strdup(so); |
| 43 | char* sep = internal_strchr(s, '@'); |
| 44 | CHECK(sep); |
| 45 | *sep = '\0'; |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 46 | Printf("%s", d.Origin()); |
| Evgeniy Stepanov | 257274e | 2013-02-11 11:34:26 +0000 | [diff] [blame] | 47 | Printf(" %sUninitialized value was created by an allocation of '%s%s%s'" |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 48 | " in the stack frame of function '%s%s%s'%s\n", |
| Alexey Samsonov | 7a36e61 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 49 | d.Origin(), d.Name(), s, d.Origin(), d.Name(), |
| Peter Collingbourne | 791e65d | 2013-10-25 23:03:29 +0000 | [diff] [blame] | 50 | Symbolizer::Get()->Demangle(sep + 1), d.Origin(), d.End()); |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 51 | InternalFree(s); |
| Evgeniy Stepanov | ac5ac34 | 2013-09-13 12:49:13 +0000 | [diff] [blame] | 52 | |
| 53 | if (pc) { |
| 54 | // For some reason function address in LLVM IR is 1 less then the address |
| 55 | // of the first instruction. |
| 56 | pc += 1; |
| Alexey Samsonov | 4708c59 | 2013-11-01 00:19:46 +0000 | [diff] [blame] | 57 | StackTrace::PrintStack(&pc, 1); |
| Evgeniy Stepanov | ac5ac34 | 2013-09-13 12:49:13 +0000 | [diff] [blame] | 58 | } |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 59 | } else { |
| 60 | uptr size = 0; |
| 61 | const uptr *trace = StackDepotGet(origin, &size); |
| Evgeniy Stepanov | 257274e | 2013-02-11 11:34:26 +0000 | [diff] [blame] | 62 | Printf(" %sUninitialized value was created by a heap allocation%s\n", |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 63 | d.Origin(), d.End()); |
| Alexey Samsonov | 4708c59 | 2013-11-01 00:19:46 +0000 | [diff] [blame] | 64 | StackTrace::PrintStack(trace, size); |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | void ReportUMR(StackTrace *stack, u32 origin) { |
| 69 | if (!__msan::flags()->report_umrs) return; |
| 70 | |
| Alexey Samsonov | 734aab4 | 2013-04-05 07:30:29 +0000 | [diff] [blame] | 71 | SpinMutexLock l(&CommonSanitizerReportMutex); |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 72 | |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 73 | Decorator d; |
| 74 | Printf("%s", d.Warning()); |
| Evgeniy Stepanov | dd0780f | 2013-05-28 14:27:30 +0000 | [diff] [blame] | 75 | Report(" WARNING: MemorySanitizer: use-of-uninitialized-value\n"); |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 76 | Printf("%s", d.End()); |
| Alexey Samsonov | f2c7659 | 2013-12-19 11:25:05 +0000 | [diff] [blame] | 77 | stack->Print(); |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 78 | if (origin) { |
| 79 | DescribeOrigin(origin); |
| 80 | } |
| Alexey Samsonov | 5dc6cff | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 81 | ReportErrorSummary("use-of-uninitialized-value", stack); |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void ReportExpectedUMRNotFound(StackTrace *stack) { |
| Alexey Samsonov | 734aab4 | 2013-04-05 07:30:29 +0000 | [diff] [blame] | 85 | SpinMutexLock l(&CommonSanitizerReportMutex); |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 86 | |
| 87 | Printf(" WARNING: Expected use of uninitialized value not found\n"); |
| Alexey Samsonov | f2c7659 | 2013-12-19 11:25:05 +0000 | [diff] [blame] | 88 | stack->Print(); |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| Evgeniy Stepanov | 9b52ce9 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 91 | void ReportAtExitStatistics() { |
| Alexey Samsonov | 734aab4 | 2013-04-05 07:30:29 +0000 | [diff] [blame] | 92 | SpinMutexLock l(&CommonSanitizerReportMutex); |
| 93 | |
| Evgeniy Stepanov | 9b52ce9 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 94 | Decorator d; |
| 95 | Printf("%s", d.Warning()); |
| 96 | Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count); |
| 97 | Printf("%s", d.End()); |
| 98 | } |
| 99 | |
| Alexey Samsonov | 49a32c1 | 2013-01-30 07:45:58 +0000 | [diff] [blame] | 100 | } // namespace __msan |