blob: 2f80d68f6ecd3083ad3b11606723534fa8685325 [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);
47 if (const char *so = __msan_get_origin_descr_if_stack(origin)) {
48 char* s = internal_strdup(so);
49 char* sep = internal_strchr(s, '@');
50 CHECK(sep);
51 *sep = '\0';
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000052 Printf("%s", d.Origin());
Evgeniy Stepanov257274e2013-02-11 11:34:26 +000053 Printf(" %sUninitialized value was created by an allocation of '%s%s%s'"
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000054 " in the stack frame of function '%s%s%s'%s\n",
Alexey Samsonov7a36e612013-09-10 14:36:16 +000055 d.Origin(), d.Name(), s, d.Origin(), d.Name(),
56 getSymbolizer()->Demangle(sep + 1), d.Origin(), d.End());
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000057 InternalFree(s);
58 } else {
59 uptr size = 0;
60 const uptr *trace = StackDepotGet(origin, &size);
Evgeniy Stepanov257274e2013-02-11 11:34:26 +000061 Printf(" %sUninitialized value was created by a heap allocation%s\n",
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000062 d.Origin(), d.End());
Kostya Serebryanyec87e782013-02-13 07:19:47 +000063 PrintStack(trace, size);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000064 }
65}
66
Kostya Serebryany7b0b9b32013-02-07 08:04:56 +000067static void ReportSummary(const char *error_type, StackTrace *stack) {
Alexey Samsonov7a36e612013-09-10 14:36:16 +000068 if (!stack->size || !getSymbolizer()->IsAvailable()) return;
Kostya Serebryany7b0b9b32013-02-07 08:04:56 +000069 AddressInfo ai;
Alexey Samsonov93686fc2013-02-12 10:46:39 +000070 uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]);
Kostya Serebryanyec87e782013-02-13 07:19:47 +000071 {
72 SymbolizerScope sym_scope;
Alexey Samsonov7a36e612013-09-10 14:36:16 +000073 getSymbolizer()->SymbolizeCode(pc, &ai, 1);
Kostya Serebryanyec87e782013-02-13 07:19:47 +000074 }
Kostya Serebryany7b0b9b32013-02-07 08:04:56 +000075 ReportErrorSummary(error_type,
Sergey Matveev6eff11e2013-05-06 13:15:14 +000076 StripPathPrefix(ai.file,
77 common_flags()->strip_path_prefix),
Kostya Serebryany7b0b9b32013-02-07 08:04:56 +000078 ai.line, ai.function);
79}
80
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000081void ReportUMR(StackTrace *stack, u32 origin) {
82 if (!__msan::flags()->report_umrs) return;
83
Alexey Samsonov734aab42013-04-05 07:30:29 +000084 SpinMutexLock l(&CommonSanitizerReportMutex);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000085
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000086 Decorator d;
87 Printf("%s", d.Warning());
Evgeniy Stepanovdd0780f2013-05-28 14:27:30 +000088 Report(" WARNING: MemorySanitizer: use-of-uninitialized-value\n");
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000089 Printf("%s", d.End());
Kostya Serebryanyec87e782013-02-13 07:19:47 +000090 PrintStack(stack->trace, stack->size);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000091 if (origin) {
92 DescribeOrigin(origin);
93 }
Kostya Serebryany7b0b9b32013-02-07 08:04:56 +000094 ReportSummary("use-of-uninitialized-value", stack);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000095}
96
97void ReportExpectedUMRNotFound(StackTrace *stack) {
Alexey Samsonov734aab42013-04-05 07:30:29 +000098 SpinMutexLock l(&CommonSanitizerReportMutex);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000099
100 Printf(" WARNING: Expected use of uninitialized value not found\n");
Kostya Serebryanyec87e782013-02-13 07:19:47 +0000101 PrintStack(stack->trace, stack->size);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +0000102}
103
Evgeniy Stepanov9b52ce92013-01-10 11:17:55 +0000104void ReportAtExitStatistics() {
Alexey Samsonov734aab42013-04-05 07:30:29 +0000105 SpinMutexLock l(&CommonSanitizerReportMutex);
106
Evgeniy Stepanov9b52ce92013-01-10 11:17:55 +0000107 Decorator d;
108 Printf("%s", d.Warning());
109 Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count);
110 Printf("%s", d.End());
111}
112
Alexey Samsonov49a32c12013-01-30 07:45:58 +0000113} // namespace __msan