msan_report.cc revision 7996a2e2a1c461743c9216f13429c04d75050230
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);
40  Printf("\n");
41}
42
43static void DescribeOrigin(u32 origin) {
44  Decorator d;
45  if (common_flags()->verbosity)
46    Printf("  raw origin id: %d\n", origin);
47  uptr pc;
48  if (const char *so = GetOriginDescrIfStack(origin, &pc)) {
49    char* s = internal_strdup(so);
50    char* sep = internal_strchr(s, '@');
51    CHECK(sep);
52    *sep = '\0';
53    Printf("%s", d.Origin());
54    Printf("  %sUninitialized value was created by an allocation of '%s%s%s'"
55           " in the stack frame of function '%s%s%s'%s\n",
56           d.Origin(), d.Name(), s, d.Origin(), d.Name(),
57           Symbolizer::Get()->Demangle(sep + 1), d.Origin(), d.End());
58    InternalFree(s);
59
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    }
66  } else {
67    uptr size = 0;
68    const uptr *trace = StackDepotGet(origin, &size);
69    Printf("  %sUninitialized value was created by a heap allocation%s\n",
70           d.Origin(), d.End());
71    PrintStack(trace, size);
72  }
73}
74
75static void ReportSummary(const char *error_type, StackTrace *stack) {
76  if (!stack->size || !Symbolizer::Get()->IsAvailable()) return;
77  AddressInfo ai;
78  uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]);
79  {
80    SymbolizerScope sym_scope;
81    Symbolizer::Get()->SymbolizeCode(pc, &ai, 1);
82  }
83  ReportErrorSummary(error_type, ai.file, ai.line, ai.function);
84}
85
86void ReportUMR(StackTrace *stack, u32 origin) {
87  if (!__msan::flags()->report_umrs) return;
88
89  SpinMutexLock l(&CommonSanitizerReportMutex);
90
91  Decorator d;
92  Printf("%s", d.Warning());
93  Report(" WARNING: MemorySanitizer: use-of-uninitialized-value\n");
94  Printf("%s", d.End());
95  PrintStack(stack->trace, stack->size);
96  if (origin) {
97    DescribeOrigin(origin);
98  }
99  ReportSummary("use-of-uninitialized-value", stack);
100}
101
102void ReportExpectedUMRNotFound(StackTrace *stack) {
103  SpinMutexLock l(&CommonSanitizerReportMutex);
104
105  Printf(" WARNING: Expected use of uninitialized value not found\n");
106  PrintStack(stack->trace, stack->size);
107}
108
109void ReportAtExitStatistics() {
110  SpinMutexLock l(&CommonSanitizerReportMutex);
111
112  Decorator d;
113  Printf("%s", d.Warning());
114  Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count);
115  Printf("%s", d.End());
116}
117
118}  // namespace __msan
119