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" |
| 16 | #include "sanitizer_common/sanitizer_common.h" |
| 17 | #include "sanitizer_common/sanitizer_mutex.h" |
Evgeniy Stepanov | c209ba6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_report_decorator.h" |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_stackdepot.h" |
Kostya Serebryany | 1d333c5 | 2013-02-07 08:04:56 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_symbolizer.h" |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace __sanitizer; |
| 23 | |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 24 | namespace __msan { |
| 25 | |
Evgeniy Stepanov | c209ba6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 26 | static bool PrintsToTtyCached() { |
| 27 | static int cached = 0; |
| 28 | static bool prints_to_tty; |
| 29 | if (!cached) { // Ok wrt threads since we are printing only from one thread. |
| 30 | prints_to_tty = PrintsToTty(); |
| 31 | cached = 1; |
| 32 | } |
| 33 | return prints_to_tty; |
| 34 | } |
| 35 | |
| 36 | class Decorator: private __sanitizer::AnsiColorDecorator { |
| 37 | public: |
| 38 | Decorator() : __sanitizer::AnsiColorDecorator(PrintsToTtyCached()) { } |
| 39 | const char *Warning() { return Red(); } |
| 40 | const char *Origin() { return Magenta(); } |
| 41 | const char *Name() { return Green(); } |
| 42 | const char *End() { return Default(); } |
| 43 | }; |
| 44 | |
Kostya Serebryany | 70c6e3f | 2013-02-13 07:19:47 +0000 | [diff] [blame] | 45 | static void PrintStack(const uptr *trace, uptr size) { |
| 46 | SymbolizerScope sym_scope; |
| 47 | StackTrace::PrintStack(trace, size, true, flags()->strip_path_prefix, 0); |
| 48 | } |
| 49 | |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 50 | static void DescribeOrigin(u32 origin) { |
Evgeniy Stepanov | c209ba6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 51 | Decorator d; |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 52 | if (flags()->verbosity) |
| 53 | Printf(" raw origin id: %d\n", origin); |
| 54 | if (const char *so = __msan_get_origin_descr_if_stack(origin)) { |
| 55 | char* s = internal_strdup(so); |
| 56 | char* sep = internal_strchr(s, '@'); |
| 57 | CHECK(sep); |
| 58 | *sep = '\0'; |
Evgeniy Stepanov | c209ba6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 59 | Printf("%s", d.Origin()); |
Evgeniy Stepanov | 9af8676 | 2013-02-11 11:34:26 +0000 | [diff] [blame] | 60 | Printf(" %sUninitialized value was created by an allocation of '%s%s%s'" |
Evgeniy Stepanov | c209ba6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 61 | " in the stack frame of function '%s%s%s'%s\n", |
| 62 | d.Origin(), d.Name(), s, d.Origin(), d.Name(), sep + 1, |
| 63 | d.Origin(), d.End()); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 64 | InternalFree(s); |
| 65 | } else { |
| 66 | uptr size = 0; |
| 67 | const uptr *trace = StackDepotGet(origin, &size); |
Evgeniy Stepanov | 9af8676 | 2013-02-11 11:34:26 +0000 | [diff] [blame] | 68 | Printf(" %sUninitialized value was created by a heap allocation%s\n", |
Evgeniy Stepanov | c209ba6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 69 | d.Origin(), d.End()); |
Kostya Serebryany | 70c6e3f | 2013-02-13 07:19:47 +0000 | [diff] [blame] | 70 | PrintStack(trace, size); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Kostya Serebryany | 1d333c5 | 2013-02-07 08:04:56 +0000 | [diff] [blame] | 74 | static void ReportSummary(const char *error_type, StackTrace *stack) { |
| 75 | if (!stack->size || !IsSymbolizerAvailable()) return; |
| 76 | AddressInfo ai; |
Alexey Samsonov | 43fb758 | 2013-02-12 10:46:39 +0000 | [diff] [blame] | 77 | uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]); |
Kostya Serebryany | 70c6e3f | 2013-02-13 07:19:47 +0000 | [diff] [blame] | 78 | { |
| 79 | SymbolizerScope sym_scope; |
| 80 | SymbolizeCode(pc, &ai, 1); |
| 81 | } |
Kostya Serebryany | 1d333c5 | 2013-02-07 08:04:56 +0000 | [diff] [blame] | 82 | ReportErrorSummary(error_type, |
| 83 | StripPathPrefix(ai.file, flags()->strip_path_prefix), |
| 84 | ai.line, ai.function); |
| 85 | } |
| 86 | |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 87 | void ReportUMR(StackTrace *stack, u32 origin) { |
| 88 | if (!__msan::flags()->report_umrs) return; |
| 89 | |
Alexey Samsonov | 7ed46ff | 2013-04-05 07:30:29 +0000 | [diff] [blame^] | 90 | SpinMutexLock l(&CommonSanitizerReportMutex); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 91 | |
Evgeniy Stepanov | c209ba6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 92 | Decorator d; |
| 93 | Printf("%s", d.Warning()); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 94 | Report(" WARNING: Use of uninitialized value\n"); |
Evgeniy Stepanov | c209ba6 | 2012-12-26 10:16:45 +0000 | [diff] [blame] | 95 | Printf("%s", d.End()); |
Kostya Serebryany | 70c6e3f | 2013-02-13 07:19:47 +0000 | [diff] [blame] | 96 | PrintStack(stack->trace, stack->size); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 97 | if (origin) { |
| 98 | DescribeOrigin(origin); |
| 99 | } |
Kostya Serebryany | 1d333c5 | 2013-02-07 08:04:56 +0000 | [diff] [blame] | 100 | ReportSummary("use-of-uninitialized-value", stack); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void ReportExpectedUMRNotFound(StackTrace *stack) { |
Alexey Samsonov | 7ed46ff | 2013-04-05 07:30:29 +0000 | [diff] [blame^] | 104 | SpinMutexLock l(&CommonSanitizerReportMutex); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 105 | |
| 106 | Printf(" WARNING: Expected use of uninitialized value not found\n"); |
Kostya Serebryany | 70c6e3f | 2013-02-13 07:19:47 +0000 | [diff] [blame] | 107 | PrintStack(stack->trace, stack->size); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Evgeniy Stepanov | 99bf1d7 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 110 | void ReportAtExitStatistics() { |
Alexey Samsonov | 7ed46ff | 2013-04-05 07:30:29 +0000 | [diff] [blame^] | 111 | SpinMutexLock l(&CommonSanitizerReportMutex); |
| 112 | |
Evgeniy Stepanov | 99bf1d7 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 113 | Decorator d; |
| 114 | Printf("%s", d.Warning()); |
| 115 | Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count); |
| 116 | Printf("%s", d.End()); |
| 117 | } |
| 118 | |
| 119 | |
Alexey Samsonov | ba5e996 | 2013-01-30 07:45:58 +0000 | [diff] [blame] | 120 | } // namespace __msan |