blob: 575cfc2217dd3bcbedea25a8e0c7397b531141e1 [file] [log] [blame]
Kostya Serebryany4b48f452012-12-27 14:09:19 +00001//===-- msan_report.cc ----------------------------------------------------===//
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +00002//
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 Samsonov1f3c2fe2013-05-29 09:15:39 +000016#include "sanitizer_common/sanitizer_allocator_internal.h"
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000017#include "sanitizer_common/sanitizer_common.h"
Sergey Matveev0b4bf4d2013-05-06 13:15:14 +000018#include "sanitizer_common/sanitizer_flags.h"
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000019#include "sanitizer_common/sanitizer_mutex.h"
Evgeniy Stepanovc209ba62012-12-26 10:16:45 +000020#include "sanitizer_common/sanitizer_report_decorator.h"
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000021#include "sanitizer_common/sanitizer_stackdepot.h"
Kostya Serebryany1d333c52013-02-07 08:04:56 +000022#include "sanitizer_common/sanitizer_symbolizer.h"
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000023
24using namespace __sanitizer;
25
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000026namespace __msan {
27
Evgeniy Stepanovc209ba62012-12-26 10:16:45 +000028class 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
Kostya Serebryany70c6e3f2013-02-13 07:19:47 +000037static void PrintStack(const uptr *trace, uptr size) {
38 SymbolizerScope sym_scope;
Alexey Samsonov90b0f1e2013-10-04 08:55:03 +000039 StackTrace::PrintStack(trace, size, true, 0);
Kostya Serebryany70c6e3f2013-02-13 07:19:47 +000040}
41
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000042static void DescribeOrigin(u32 origin) {
Evgeniy Stepanovc209ba62012-12-26 10:16:45 +000043 Decorator d;
Evgeniy Stepanov719df332013-10-25 11:17:54 +000044 Printf("\n");
Dmitry Vyukov6866dba2013-10-15 13:28:51 +000045 if (common_flags()->verbosity)
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000046 Printf(" raw origin id: %d\n", origin);
Evgeniy Stepanov6f346052013-09-13 12:49:13 +000047 uptr pc;
48 if (const char *so = GetOriginDescrIfStack(origin, &pc)) {
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000049 char* s = internal_strdup(so);
50 char* sep = internal_strchr(s, '@');
51 CHECK(sep);
52 *sep = '\0';
Evgeniy Stepanovc209ba62012-12-26 10:16:45 +000053 Printf("%s", d.Origin());
Evgeniy Stepanov9af86762013-02-11 11:34:26 +000054 Printf(" %sUninitialized value was created by an allocation of '%s%s%s'"
Evgeniy Stepanovc209ba62012-12-26 10:16:45 +000055 " in the stack frame of function '%s%s%s'%s\n",
Alexey Samsonov7847d772013-09-10 14:36:16 +000056 d.Origin(), d.Name(), s, d.Origin(), d.Name(),
57 getSymbolizer()->Demangle(sep + 1), d.Origin(), d.End());
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000058 InternalFree(s);
Evgeniy Stepanov6f346052013-09-13 12:49:13 +000059
60 if (pc) {
61 // For some reason function address in LLVM IR is 1 less then the address
62 // of the first instruction.
63 pc += 1;
64 PrintStack(&pc, 1);
65 }
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000066 } else {
67 uptr size = 0;
68 const uptr *trace = StackDepotGet(origin, &size);
Evgeniy Stepanov9af86762013-02-11 11:34:26 +000069 Printf(" %sUninitialized value was created by a heap allocation%s\n",
Evgeniy Stepanovc209ba62012-12-26 10:16:45 +000070 d.Origin(), d.End());
Kostya Serebryany70c6e3f2013-02-13 07:19:47 +000071 PrintStack(trace, size);
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000072 }
73}
74
Kostya Serebryany1d333c52013-02-07 08:04:56 +000075static void ReportSummary(const char *error_type, StackTrace *stack) {
Alexey Samsonov7847d772013-09-10 14:36:16 +000076 if (!stack->size || !getSymbolizer()->IsAvailable()) return;
Kostya Serebryany1d333c52013-02-07 08:04:56 +000077 AddressInfo ai;
Alexey Samsonov43fb7582013-02-12 10:46:39 +000078 uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]);
Kostya Serebryany70c6e3f2013-02-13 07:19:47 +000079 {
80 SymbolizerScope sym_scope;
Alexey Samsonov7847d772013-09-10 14:36:16 +000081 getSymbolizer()->SymbolizeCode(pc, &ai, 1);
Kostya Serebryany70c6e3f2013-02-13 07:19:47 +000082 }
Alexey Samsonov90b0f1e2013-10-04 08:55:03 +000083 ReportErrorSummary(error_type, ai.file, ai.line, ai.function);
Kostya Serebryany1d333c52013-02-07 08:04:56 +000084}
85
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000086void ReportUMR(StackTrace *stack, u32 origin) {
87 if (!__msan::flags()->report_umrs) return;
88
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +000089 SpinMutexLock l(&CommonSanitizerReportMutex);
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000090
Evgeniy Stepanovc209ba62012-12-26 10:16:45 +000091 Decorator d;
92 Printf("%s", d.Warning());
Evgeniy Stepanov6ac157d2013-05-28 14:27:30 +000093 Report(" WARNING: MemorySanitizer: use-of-uninitialized-value\n");
Evgeniy Stepanovc209ba62012-12-26 10:16:45 +000094 Printf("%s", d.End());
Kostya Serebryany70c6e3f2013-02-13 07:19:47 +000095 PrintStack(stack->trace, stack->size);
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000096 if (origin) {
97 DescribeOrigin(origin);
98 }
Kostya Serebryany1d333c52013-02-07 08:04:56 +000099 ReportSummary("use-of-uninitialized-value", stack);
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +0000100}
101
102void ReportExpectedUMRNotFound(StackTrace *stack) {
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +0000103 SpinMutexLock l(&CommonSanitizerReportMutex);
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +0000104
105 Printf(" WARNING: Expected use of uninitialized value not found\n");
Kostya Serebryany70c6e3f2013-02-13 07:19:47 +0000106 PrintStack(stack->trace, stack->size);
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +0000107}
108
Evgeniy Stepanov99bf1d72013-01-10 11:17:55 +0000109void ReportAtExitStatistics() {
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +0000110 SpinMutexLock l(&CommonSanitizerReportMutex);
111
Evgeniy Stepanov99bf1d72013-01-10 11:17:55 +0000112 Decorator d;
113 Printf("%s", d.Warning());
114 Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count);
115 Printf("%s", d.End());
116}
117
Alexey Samsonovba5e9962013-01-30 07:45:58 +0000118} // namespace __msan