msan_report.cc revision 2fb08720b11b4c339e191b90d85477c6a2dd74db
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 DescribeOrigin(u32 origin) {
38  Decorator d;
39  if (common_flags()->verbosity)
40    Printf("  raw origin id: %d\n", origin);
41  uptr pc;
42  if (const char *so = GetOriginDescrIfStack(origin, &pc)) {
43    char* s = internal_strdup(so);
44    char* sep = internal_strchr(s, '@');
45    CHECK(sep);
46    *sep = '\0';
47    Printf("%s", d.Origin());
48    Printf("  %sUninitialized value was created by an allocation of '%s%s%s'"
49           " in the stack frame of function '%s%s%s'%s\n",
50           d.Origin(), d.Name(), s, d.Origin(), d.Name(),
51           Symbolizer::Get()->Demangle(sep + 1), d.Origin(), d.End());
52    InternalFree(s);
53
54    if (pc) {
55      // For some reason function address in LLVM IR is 1 less then the address
56      // of the first instruction.
57      pc += 1;
58      StackTrace::PrintStack(&pc, 1);
59    }
60  } else {
61    uptr size = 0;
62    const uptr *trace = StackDepotGet(origin, &size);
63    Printf("  %sUninitialized value was created by a heap allocation%s\n",
64           d.Origin(), d.End());
65    StackTrace::PrintStack(trace, size);
66  }
67}
68
69void ReportUMR(StackTrace *stack, u32 origin) {
70  if (!__msan::flags()->report_umrs) return;
71
72  SpinMutexLock l(&CommonSanitizerReportMutex);
73
74  Decorator d;
75  Printf("%s", d.Warning());
76  Report(" WARNING: MemorySanitizer: use-of-uninitialized-value\n");
77  Printf("%s", d.End());
78  StackTrace::PrintStack(stack->trace, stack->size);
79  if (origin) {
80    DescribeOrigin(origin);
81  }
82  ReportErrorSummary("use-of-uninitialized-value", stack);
83}
84
85void ReportExpectedUMRNotFound(StackTrace *stack) {
86  SpinMutexLock l(&CommonSanitizerReportMutex);
87
88  Printf(" WARNING: Expected use of uninitialized value not found\n");
89  StackTrace::PrintStack(stack->trace, stack->size);
90}
91
92void ReportAtExitStatistics() {
93  SpinMutexLock l(&CommonSanitizerReportMutex);
94
95  Decorator d;
96  Printf("%s", d.Warning());
97  Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count);
98  Printf("%s", d.End());
99}
100
101}  // namespace __msan
102