msan_report.cc revision 7847d77b246635211c3bf465421d49d7af5226c1
1//===-- msan_report.cc ----------------------------------------------------===//
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_allocator_internal.h"
17#include "sanitizer_common/sanitizer_common.h"
18#include "sanitizer_common/sanitizer_flags.h"
19#include "sanitizer_common/sanitizer_mutex.h"
20#include "sanitizer_common/sanitizer_report_decorator.h"
21#include "sanitizer_common/sanitizer_stackdepot.h"
22#include "sanitizer_common/sanitizer_symbolizer.h"
23
24using namespace __sanitizer;
25
26namespace __msan {
27
28class 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
37static void PrintStack(const uptr *trace, uptr size) {
38  SymbolizerScope sym_scope;
39  StackTrace::PrintStack(trace, size, true,
40                         common_flags()->strip_path_prefix, 0);
41}
42
43static void DescribeOrigin(u32 origin) {
44  Decorator d;
45  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';
52    Printf("%s", d.Origin());
53    Printf("  %sUninitialized value was created by an allocation of '%s%s%s'"
54           " in the stack frame of function '%s%s%s'%s\n",
55           d.Origin(), d.Name(), s, d.Origin(), d.Name(),
56           getSymbolizer()->Demangle(sep + 1), d.Origin(), d.End());
57    InternalFree(s);
58  } else {
59    uptr size = 0;
60    const uptr *trace = StackDepotGet(origin, &size);
61    Printf("  %sUninitialized value was created by a heap allocation%s\n",
62           d.Origin(), d.End());
63    PrintStack(trace, size);
64  }
65}
66
67static void ReportSummary(const char *error_type, StackTrace *stack) {
68  if (!stack->size || !getSymbolizer()->IsAvailable()) return;
69  AddressInfo ai;
70  uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]);
71  {
72    SymbolizerScope sym_scope;
73    getSymbolizer()->SymbolizeCode(pc, &ai, 1);
74  }
75  ReportErrorSummary(error_type,
76                     StripPathPrefix(ai.file,
77                                     common_flags()->strip_path_prefix),
78                     ai.line, ai.function);
79}
80
81void ReportUMR(StackTrace *stack, u32 origin) {
82  if (!__msan::flags()->report_umrs) return;
83
84  SpinMutexLock l(&CommonSanitizerReportMutex);
85
86  Decorator d;
87  Printf("%s", d.Warning());
88  Report(" WARNING: MemorySanitizer: use-of-uninitialized-value\n");
89  Printf("%s", d.End());
90  PrintStack(stack->trace, stack->size);
91  if (origin) {
92    DescribeOrigin(origin);
93  }
94  ReportSummary("use-of-uninitialized-value", stack);
95}
96
97void ReportExpectedUMRNotFound(StackTrace *stack) {
98  SpinMutexLock l(&CommonSanitizerReportMutex);
99
100  Printf(" WARNING: Expected use of uninitialized value not found\n");
101  PrintStack(stack->trace, stack->size);
102}
103
104void ReportAtExitStatistics() {
105  SpinMutexLock l(&CommonSanitizerReportMutex);
106
107  Decorator d;
108  Printf("%s", d.Warning());
109  Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count);
110  Printf("%s", d.End());
111}
112
113}  // namespace __msan
114