blob: 00890347ec36166a25fe96a94b90edc92b0fd426 [file] [log] [blame]
Kostya Serebryany4a42cf62012-12-27 14:09:19 +00001//===-- msan_report.cc ----------------------------------------------------===//
Evgeniy Stepanov367dc642012-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 Samsonovc30e2d62013-05-29 09:15:39 +000016#include "sanitizer_common/sanitizer_allocator_internal.h"
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000017#include "sanitizer_common/sanitizer_common.h"
Sergey Matveev6eff11e2013-05-06 13:15:14 +000018#include "sanitizer_common/sanitizer_flags.h"
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000019#include "sanitizer_common/sanitizer_mutex.h"
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000020#include "sanitizer_common/sanitizer_report_decorator.h"
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000021#include "sanitizer_common/sanitizer_stackdepot.h"
Kostya Serebryany7b0b9b32013-02-07 08:04:56 +000022#include "sanitizer_common/sanitizer_symbolizer.h"
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000023
24using namespace __sanitizer;
25
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000026namespace __msan {
27
Evgeniy Stepanovfee82c62012-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 Serebryanyec87e782013-02-13 07:19:47 +000037static void PrintStack(const uptr *trace, uptr size) {
38 SymbolizerScope sym_scope;
Sergey Matveev6eff11e2013-05-06 13:15:14 +000039 StackTrace::PrintStack(trace, size, true,
40 common_flags()->strip_path_prefix, 0);
Kostya Serebryanyec87e782013-02-13 07:19:47 +000041}
42
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000043static void DescribeOrigin(u32 origin) {
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000044 Decorator d;
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000045 if (flags()->verbosity)
46 Printf(" raw origin id: %d\n", origin);
Evgeniy Stepanovac5ac342013-09-13 12:49:13 +000047 uptr pc;
48 if (const char *so = GetOriginDescrIfStack(origin, &pc)) {
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000049 char* s = internal_strdup(so);
50 char* sep = internal_strchr(s, '@');
51 CHECK(sep);
52 *sep = '\0';
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000053 Printf("%s", d.Origin());
Evgeniy Stepanov257274e2013-02-11 11:34:26 +000054 Printf(" %sUninitialized value was created by an allocation of '%s%s%s'"
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000055 " in the stack frame of function '%s%s%s'%s\n",
Alexey Samsonov7a36e612013-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 Stepanov367dc642012-12-26 09:32:05 +000058 InternalFree(s);
Evgeniy Stepanovac5ac342013-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 Stepanov367dc642012-12-26 09:32:05 +000066 } else {
67 uptr size = 0;
68 const uptr *trace = StackDepotGet(origin, &size);
Evgeniy Stepanov257274e2013-02-11 11:34:26 +000069 Printf(" %sUninitialized value was created by a heap allocation%s\n",
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000070 d.Origin(), d.End());
Kostya Serebryanyec87e782013-02-13 07:19:47 +000071 PrintStack(trace, size);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000072 }
73}
74
Kostya Serebryany7b0b9b32013-02-07 08:04:56 +000075static void ReportSummary(const char *error_type, StackTrace *stack) {
Alexey Samsonov7a36e612013-09-10 14:36:16 +000076 if (!stack->size || !getSymbolizer()->IsAvailable()) return;
Kostya Serebryany7b0b9b32013-02-07 08:04:56 +000077 AddressInfo ai;
Alexey Samsonov93686fc2013-02-12 10:46:39 +000078 uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]);
Kostya Serebryanyec87e782013-02-13 07:19:47 +000079 {
80 SymbolizerScope sym_scope;
Alexey Samsonov7a36e612013-09-10 14:36:16 +000081 getSymbolizer()->SymbolizeCode(pc, &ai, 1);
Kostya Serebryanyec87e782013-02-13 07:19:47 +000082 }
Kostya Serebryany7b0b9b32013-02-07 08:04:56 +000083 ReportErrorSummary(error_type,
Sergey Matveev6eff11e2013-05-06 13:15:14 +000084 StripPathPrefix(ai.file,
85 common_flags()->strip_path_prefix),
Kostya Serebryany7b0b9b32013-02-07 08:04:56 +000086 ai.line, ai.function);
87}
88
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000089void ReportUMR(StackTrace *stack, u32 origin) {
90 if (!__msan::flags()->report_umrs) return;
91
Alexey Samsonov734aab42013-04-05 07:30:29 +000092 SpinMutexLock l(&CommonSanitizerReportMutex);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000093
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000094 Decorator d;
95 Printf("%s", d.Warning());
Evgeniy Stepanovdd0780f2013-05-28 14:27:30 +000096 Report(" WARNING: MemorySanitizer: use-of-uninitialized-value\n");
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000097 Printf("%s", d.End());
Kostya Serebryanyec87e782013-02-13 07:19:47 +000098 PrintStack(stack->trace, stack->size);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000099 if (origin) {
100 DescribeOrigin(origin);
101 }
Kostya Serebryany7b0b9b32013-02-07 08:04:56 +0000102 ReportSummary("use-of-uninitialized-value", stack);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +0000103}
104
105void ReportExpectedUMRNotFound(StackTrace *stack) {
Alexey Samsonov734aab42013-04-05 07:30:29 +0000106 SpinMutexLock l(&CommonSanitizerReportMutex);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +0000107
108 Printf(" WARNING: Expected use of uninitialized value not found\n");
Kostya Serebryanyec87e782013-02-13 07:19:47 +0000109 PrintStack(stack->trace, stack->size);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +0000110}
111
Evgeniy Stepanov9b52ce92013-01-10 11:17:55 +0000112void ReportAtExitStatistics() {
Alexey Samsonov734aab42013-04-05 07:30:29 +0000113 SpinMutexLock l(&CommonSanitizerReportMutex);
114
Evgeniy Stepanov9b52ce92013-01-10 11:17:55 +0000115 Decorator d;
116 Printf("%s", d.Warning());
117 Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count);
118 Printf("%s", d.End());
119}
120
Alexey Samsonov49a32c12013-01-30 07:45:58 +0000121} // namespace __msan