| 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" |
| 16 | #include "sanitizer_common/sanitizer_common.h" |
| 17 | #include "sanitizer_common/sanitizer_mutex.h" |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_report_decorator.h" |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_stackdepot.h" |
| 20 | |
| 21 | using namespace __sanitizer; |
| 22 | |
| 23 | static StaticSpinMutex report_mu; |
| 24 | |
| 25 | namespace __msan { |
| 26 | |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 27 | static bool PrintsToTtyCached() { |
| 28 | static int cached = 0; |
| 29 | static bool prints_to_tty; |
| 30 | if (!cached) { // Ok wrt threads since we are printing only from one thread. |
| 31 | prints_to_tty = PrintsToTty(); |
| 32 | cached = 1; |
| 33 | } |
| 34 | return prints_to_tty; |
| 35 | } |
| 36 | |
| 37 | class Decorator: private __sanitizer::AnsiColorDecorator { |
| 38 | public: |
| 39 | Decorator() : __sanitizer::AnsiColorDecorator(PrintsToTtyCached()) { } |
| 40 | const char *Warning() { return Red(); } |
| 41 | const char *Origin() { return Magenta(); } |
| 42 | const char *Name() { return Green(); } |
| 43 | const char *End() { return Default(); } |
| 44 | }; |
| 45 | |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 46 | static void DescribeOrigin(u32 origin) { |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 47 | Decorator d; |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 48 | if (flags()->verbosity) |
| 49 | Printf(" raw origin id: %d\n", origin); |
| 50 | if (const char *so = __msan_get_origin_descr_if_stack(origin)) { |
| 51 | char* s = internal_strdup(so); |
| 52 | char* sep = internal_strchr(s, '@'); |
| 53 | CHECK(sep); |
| 54 | *sep = '\0'; |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 55 | Printf("%s", d.Origin()); |
| 56 | Printf(" %sUninitialised value was created by an allocation of '%s%s%s'" |
| 57 | " in the stack frame of function '%s%s%s'%s\n", |
| 58 | d.Origin(), d.Name(), s, d.Origin(), d.Name(), sep + 1, |
| 59 | d.Origin(), d.End()); |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 60 | InternalFree(s); |
| 61 | } else { |
| 62 | uptr size = 0; |
| 63 | const uptr *trace = StackDepotGet(origin, &size); |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 64 | Printf(" %sUninitialised value was created by a heap allocation%s\n", |
| 65 | d.Origin(), d.End()); |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 66 | StackTrace::PrintStack(trace, size, true, "", 0); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void ReportUMR(StackTrace *stack, u32 origin) { |
| 71 | if (!__msan::flags()->report_umrs) return; |
| 72 | |
| 73 | GenericScopedLock<StaticSpinMutex> lock(&report_mu); |
| 74 | |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 75 | Decorator d; |
| 76 | Printf("%s", d.Warning()); |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 77 | Report(" WARNING: Use of uninitialized value\n"); |
| Evgeniy Stepanov | fee82c6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 78 | Printf("%s", d.End()); |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 79 | StackTrace::PrintStack(stack->trace, stack->size, true, "", 0); |
| 80 | if (origin) { |
| 81 | DescribeOrigin(origin); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void ReportExpectedUMRNotFound(StackTrace *stack) { |
| 86 | GenericScopedLock<StaticSpinMutex> lock(&report_mu); |
| 87 | |
| 88 | Printf(" WARNING: Expected use of uninitialized value not found\n"); |
| 89 | StackTrace::PrintStack(stack->trace, stack->size, true, "", 0); |
| 90 | } |
| 91 | |
| Evgeniy Stepanov | 9b52ce9 | 2013-01-10 11:17:55 +0000 | [diff] [blame^] | 92 | void ReportAtExitStatistics() { |
| 93 | Decorator d; |
| 94 | Printf("%s", d.Warning()); |
| 95 | Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count); |
| 96 | Printf("%s", d.End()); |
| 97 | } |
| 98 | |
| 99 | |
| Evgeniy Stepanov | 367dc64 | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 100 | } // namespace msan |