blob: 12bac2e8c064eb664c50ddb687e7de6bbc764ac2 [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
Evgeniy Stepanov412d9732014-03-18 13:45:19 +000037static void DescribeStackOrigin(const char *so, uptr pc) {
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000038 Decorator d;
Evgeniy Stepanov412d9732014-03-18 13:45:19 +000039 char *s = internal_strdup(so);
40 char *sep = internal_strchr(s, '@');
41 CHECK(sep);
42 *sep = '\0';
43 Printf("%s", d.Origin());
44 Printf(
45 " %sUninitialized value was created by an allocation of '%s%s%s'"
46 " in the stack frame of function '%s%s%s'%s\n",
47 d.Origin(), d.Name(), s, d.Origin(), d.Name(),
48 Symbolizer::Get()->Demangle(sep + 1), d.Origin(), d.End());
49 InternalFree(s);
50
51 if (pc) {
52 // For some reason function address in LLVM IR is 1 less then the address
53 // of the first instruction.
54 pc += 1;
55 StackTrace::PrintStack(&pc, 1);
56 }
57}
58
59static void DescribeOrigin(u32 origin) {
Sergey Matveev9be70fb2013-12-05 12:04:51 +000060 VPrintf(1, " raw origin id: %d\n", origin);
Evgeniy Stepanovac5ac342013-09-13 12:49:13 +000061 uptr pc;
Evgeniy Stepanov412d9732014-03-18 13:45:19 +000062 while (true) {
63 if (const char *so = GetOriginDescrIfStack(origin, &pc)) {
64 DescribeStackOrigin(so, pc);
65 break;
Evgeniy Stepanovac5ac342013-09-13 12:49:13 +000066 }
Evgeniy Stepanov412d9732014-03-18 13:45:19 +000067 Decorator d;
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000068 uptr size = 0;
69 const uptr *trace = StackDepotGet(origin, &size);
Evgeniy Stepanov412d9732014-03-18 13:45:19 +000070 CHECK_GT(size, 0);
71 if (TRACE_IS_CHAINED(trace[size - 1])) {
72 // Linked origin.
73 // FIXME: copied? modified? passed through? observed?
74 Printf(" %sUninitialized value was stored to memory at%s\n", d.Origin(),
75 d.End());
76 StackTrace::PrintStack(trace, size - 1);
77 origin = TRACE_TO_CHAINED_ID(trace[size - 1]);
78 } else {
79 Printf(" %sUninitialized value was created by a heap allocation%s\n",
80 d.Origin(), d.End());
81 StackTrace::PrintStack(trace, size);
82 break;
83 }
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000084 }
85}
86
87void ReportUMR(StackTrace *stack, u32 origin) {
88 if (!__msan::flags()->report_umrs) return;
89
Alexey Samsonov734aab42013-04-05 07:30:29 +000090 SpinMutexLock l(&CommonSanitizerReportMutex);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000091
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000092 Decorator d;
93 Printf("%s", d.Warning());
Evgeniy Stepanovdd0780f2013-05-28 14:27:30 +000094 Report(" WARNING: MemorySanitizer: use-of-uninitialized-value\n");
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000095 Printf("%s", d.End());
Alexey Samsonovf2c76592013-12-19 11:25:05 +000096 stack->Print();
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000097 if (origin) {
98 DescribeOrigin(origin);
99 }
Alexey Samsonov5dc6cff2013-11-01 17:02:14 +0000100 ReportErrorSummary("use-of-uninitialized-value", stack);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +0000101}
102
103void ReportExpectedUMRNotFound(StackTrace *stack) {
Alexey Samsonov734aab42013-04-05 07:30:29 +0000104 SpinMutexLock l(&CommonSanitizerReportMutex);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +0000105
106 Printf(" WARNING: Expected use of uninitialized value not found\n");
Alexey Samsonovf2c76592013-12-19 11:25:05 +0000107 stack->Print();
Evgeniy Stepanov367dc642012-12-26 09:32:05 +0000108}
109
Evgeniy Stepanov9b52ce92013-01-10 11:17:55 +0000110void ReportAtExitStatistics() {
Alexey Samsonov734aab42013-04-05 07:30:29 +0000111 SpinMutexLock l(&CommonSanitizerReportMutex);
112
Evgeniy Stepanov412d9732014-03-18 13:45:19 +0000113 if (msan_report_count > 0) {
114 Decorator d;
115 Printf("%s", d.Warning());
116 Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count);
117 Printf("%s", d.End());
118 }
119
120 StackDepotStats *stack_depot_stats = StackDepotGetStats();
121 // FIXME: we want this at normal exit, too!
122 // FIXME: but only with verbosity=1 or something
123 Printf("Unique heap origins: %zu\n", stack_depot_stats->n_uniq_ids);
124 Printf("Stack depot mapped bytes: %zu\n", stack_depot_stats->mapped);
Evgeniy Stepanov9b52ce92013-01-10 11:17:55 +0000125}
126
Evgeniy Stepanovfe250b02014-04-30 09:50:30 +0000127class OriginSet {
128 public:
129 OriginSet() : next_id_(0) {}
130 int insert(u32 o) {
131 // Scan from the end for better locality.
132 for (int i = next_id_ - 1; i >= 0; --i)
133 if (origins_[i] == o) return i;
134 if (next_id_ == kMaxSize_) return OVERFLOW;
135 int id = next_id_++;
136 origins_[id] = o;
137 return id;
138 }
139 int size() { return next_id_; }
140 u32 get(int id) { return origins_[id]; }
141 static char asChar(int id) {
142 switch (id) {
143 case MISSING:
144 return '.';
145 case OVERFLOW:
146 return '*';
147 default:
148 return 'A' + id;
149 }
150 }
151 static const int OVERFLOW = -1;
152 static const int MISSING = -2;
153
154 private:
155 static const int kMaxSize_ = 'Z' - 'A' + 1;
156 u32 origins_[kMaxSize_];
157 int next_id_;
158};
159
160void DescribeMemoryRange(const void *x, uptr size) {
161 // Real limits.
162 uptr start = MEM_TO_SHADOW(x);
163 uptr end = start + size;
164 // Scan limits: align start down to 4; align size up to 16.
165 uptr s = start & ~3UL;
166 size = end - s;
167 size = (size + 15) & ~15UL;
168 uptr e = s + size;
169
170 // Single letter names to origin id mapping.
171 OriginSet origin_set;
172
173 uptr pos = 0; // Offset from aligned start.
174 bool with_origins = __msan_get_track_origins();
175 // True if there is at least 1 poisoned bit in the last 4-byte group.
176 bool last_quad_poisoned;
177 int origin_ids[4]; // Single letter origin ids for the current line.
178
179 Decorator d;
180 Printf("%s", d.Warning());
181 Printf("Shadow map of [%p, %p), %zu bytes:\n", start, end, end - start);
182 Printf("%s", d.End());
183 while (s < e) {
184 // Line start.
185 if (pos % 16 == 0) {
186 for (int i = 0; i < 4; ++i) origin_ids[i] = -1;
187 Printf("%p:", s);
188 }
189 // Group start.
190 if (pos % 4 == 0) {
191 Printf(" ");
192 last_quad_poisoned = false;
193 }
194 // Print shadow byte.
195 if (s < start || s >= end) {
196 Printf("..");
197 } else {
198 unsigned char v = *(unsigned char *)s;
199 if (v) last_quad_poisoned = true;
200 Printf("%02x", v);
201 }
202 // Group end.
203 if (pos % 4 == 3 && with_origins) {
204 int id = OriginSet::MISSING;
205 if (last_quad_poisoned) {
206 u32 o = *(u32 *)SHADOW_TO_ORIGIN(s - 3);
207 id = origin_set.insert(o);
208 }
209 origin_ids[(pos % 16) / 4] = id;
210 }
211 // Line end.
212 if (pos % 16 == 15) {
213 if (with_origins) {
214 Printf(" |");
215 for (int i = 0; i < 4; ++i) {
216 char c = OriginSet::asChar(origin_ids[i]);
217 Printf("%c", c);
218 if (i != 3) Printf(" ");
219 }
220 Printf("|");
221 }
222 Printf("\n");
223 }
224 size--;
225 s++;
226 pos++;
227 }
228
229 Printf("\n");
230
231 for (int i = 0; i < origin_set.size(); ++i) {
232 u32 o = origin_set.get(i);
233 Printf("Origin %c (origin_id %x):\n", OriginSet::asChar(i), o);
234 DescribeOrigin(o);
235 }
236}
237
Alexey Samsonov49a32c12013-01-30 07:45:58 +0000238} // namespace __msan