msan_report.cc revision 6866dba92ac842fc513ba339ba849a953ffb7507
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, 0);
40}
41
42static void DescribeOrigin(u32 origin) {
43  Decorator d;
44  if (common_flags()->verbosity)
45    Printf("  raw origin id: %d\n", origin);
46  uptr pc;
47  if (const char *so = GetOriginDescrIfStack(origin, &pc)) {
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
59    if (pc) {
60      // For some reason function address in LLVM IR is 1 less then the address
61      // of the first instruction.
62      pc += 1;
63      PrintStack(&pc, 1);
64    }
65  } else {
66    uptr size = 0;
67    const uptr *trace = StackDepotGet(origin, &size);
68    Printf("  %sUninitialized value was created by a heap allocation%s\n",
69           d.Origin(), d.End());
70    PrintStack(trace, size);
71  }
72}
73
74static void ReportSummary(const char *error_type, StackTrace *stack) {
75  if (!stack->size || !getSymbolizer()->IsAvailable()) return;
76  AddressInfo ai;
77  uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]);
78  {
79    SymbolizerScope sym_scope;
80    getSymbolizer()->SymbolizeCode(pc, &ai, 1);
81  }
82  ReportErrorSummary(error_type, ai.file, ai.line, ai.function);
83}
84
85void ReportUMR(StackTrace *stack, u32 origin) {
86  if (!__msan::flags()->report_umrs) return;
87
88  SpinMutexLock l(&CommonSanitizerReportMutex);
89
90  Decorator d;
91  Printf("%s", d.Warning());
92  Report(" WARNING: MemorySanitizer: use-of-uninitialized-value\n");
93  Printf("%s", d.End());
94  PrintStack(stack->trace, stack->size);
95  if (origin) {
96    DescribeOrigin(origin);
97  }
98  ReportSummary("use-of-uninitialized-value", stack);
99}
100
101void ReportExpectedUMRNotFound(StackTrace *stack) {
102  SpinMutexLock l(&CommonSanitizerReportMutex);
103
104  Printf(" WARNING: Expected use of uninitialized value not found\n");
105  PrintStack(stack->trace, stack->size);
106}
107
108void ReportAtExitStatistics() {
109  SpinMutexLock l(&CommonSanitizerReportMutex);
110
111  Decorator d;
112  Printf("%s", d.Warning());
113  Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count);
114  Printf("%s", d.End());
115}
116
117}  // namespace __msan
118