xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/msan/msan_report.cc (revision a7c257b03e4462df2b1020128fb82716512d7856)
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 "msan_chained_origin_depot.h"
17 #include "msan_origin.h"
18 #include "msan_report.h"
19 #include "sanitizer_common/sanitizer_allocator_internal.h"
20 #include "sanitizer_common/sanitizer_common.h"
21 #include "sanitizer_common/sanitizer_flags.h"
22 #include "sanitizer_common/sanitizer_mutex.h"
23 #include "sanitizer_common/sanitizer_report_decorator.h"
24 #include "sanitizer_common/sanitizer_stackdepot.h"
25 #include "sanitizer_common/sanitizer_symbolizer.h"
26 
27 using namespace __sanitizer;
28 
29 namespace __msan {
30 
31 class Decorator: public __sanitizer::SanitizerCommonDecorator {
32  public:
Decorator()33   Decorator() : SanitizerCommonDecorator() { }
Origin() const34   const char *Origin() const { return Magenta(); }
Name() const35   const char *Name() const { return Green(); }
36 };
37 
DescribeStackOrigin(const char * so,uptr pc)38 static void DescribeStackOrigin(const char *so, uptr pc) {
39   Decorator d;
40   char *s = internal_strdup(so);
41   char *sep = internal_strchr(s, '@');
42   CHECK(sep);
43   *sep = '\0';
44   Printf("%s", d.Origin());
45   Printf(
46       "  %sUninitialized value was created by an allocation of '%s%s%s'"
47       " in the stack frame of function '%s%s%s'%s\n",
48       d.Origin(), d.Name(), s, d.Origin(), d.Name(), sep + 1, d.Origin(),
49       d.Default());
50   InternalFree(s);
51 
52   if (pc) {
53     // For some reason function address in LLVM IR is 1 less then the address
54     // of the first instruction.
55     pc = StackTrace::GetNextInstructionPc(pc);
56     StackTrace(&pc, 1).Print();
57   }
58 }
59 
DescribeOrigin(u32 id)60 static void DescribeOrigin(u32 id) {
61   VPrintf(1, "  raw origin id: %d\n", id);
62   Decorator d;
63   Origin o = Origin::FromRawId(id);
64   while (o.isChainedOrigin()) {
65     StackTrace stack;
66     o = o.getNextChainedOrigin(&stack);
67     Printf("  %sUninitialized value was stored to memory at%s\n", d.Origin(),
68            d.Default());
69     stack.Print();
70   }
71   if (o.isStackOrigin()) {
72     uptr pc;
73     const char *so = GetStackOriginDescr(o.getStackId(), &pc);
74     DescribeStackOrigin(so, pc);
75   } else {
76     StackTrace stack = o.getStackTraceForHeapOrigin();
77     switch (stack.tag) {
78       case StackTrace::TAG_ALLOC:
79         Printf("  %sUninitialized value was created by a heap allocation%s\n",
80                d.Origin(), d.Default());
81         break;
82       case StackTrace::TAG_DEALLOC:
83         Printf("  %sUninitialized value was created by a heap deallocation%s\n",
84                d.Origin(), d.Default());
85         break;
86       case STACK_TRACE_TAG_POISON:
87         Printf("  %sMemory was marked as uninitialized%s\n", d.Origin(),
88                d.Default());
89         break;
90       default:
91         Printf("  %sUninitialized value was created%s\n", d.Origin(),
92                d.Default());
93         break;
94     }
95     stack.Print();
96   }
97 }
98 
ReportUMR(StackTrace * stack,u32 origin)99 void ReportUMR(StackTrace *stack, u32 origin) {
100   if (!__msan::flags()->report_umrs) return;
101 
102   ScopedErrorReportLock l;
103 
104   Decorator d;
105   Printf("%s", d.Warning());
106   Report("WARNING: MemorySanitizer: use-of-uninitialized-value\n");
107   Printf("%s", d.Default());
108   stack->Print();
109   if (origin) {
110     DescribeOrigin(origin);
111   }
112   ReportErrorSummary("use-of-uninitialized-value", stack);
113 }
114 
ReportExpectedUMRNotFound(StackTrace * stack)115 void ReportExpectedUMRNotFound(StackTrace *stack) {
116   ScopedErrorReportLock l;
117 
118   Printf("WARNING: Expected use of uninitialized value not found\n");
119   stack->Print();
120 }
121 
ReportStats()122 void ReportStats() {
123   ScopedErrorReportLock l;
124 
125   if (__msan_get_track_origins() > 0) {
126     StackDepotStats *stack_depot_stats = StackDepotGetStats();
127     // FIXME: we want this at normal exit, too!
128     // FIXME: but only with verbosity=1 or something
129     Printf("Unique heap origins: %zu\n", stack_depot_stats->n_uniq_ids);
130     Printf("Stack depot allocated bytes: %zu\n", stack_depot_stats->allocated);
131 
132     StackDepotStats *chained_origin_depot_stats = ChainedOriginDepotGetStats();
133     Printf("Unique origin histories: %zu\n",
134            chained_origin_depot_stats->n_uniq_ids);
135     Printf("History depot allocated bytes: %zu\n",
136            chained_origin_depot_stats->allocated);
137   }
138 }
139 
ReportAtExitStatistics()140 void ReportAtExitStatistics() {
141   ScopedErrorReportLock l;
142 
143   if (msan_report_count > 0) {
144     Decorator d;
145     Printf("%s", d.Warning());
146     Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count);
147     Printf("%s", d.Default());
148   }
149 }
150 
151 class OriginSet {
152  public:
OriginSet()153   OriginSet() : next_id_(0) {}
insert(u32 o)154   int insert(u32 o) {
155     // Scan from the end for better locality.
156     for (int i = next_id_ - 1; i >= 0; --i)
157       if (origins_[i] == o) return i;
158     if (next_id_ == kMaxSize_) return OVERFLOW;
159     int id = next_id_++;
160     origins_[id] = o;
161     return id;
162   }
size()163   int size() { return next_id_; }
get(int id)164   u32 get(int id) { return origins_[id]; }
asChar(int id)165   static char asChar(int id) {
166     switch (id) {
167       case MISSING:
168         return '.';
169       case OVERFLOW:
170         return '*';
171       default:
172         return 'A' + id;
173     }
174   }
175   static const int OVERFLOW = -1;
176   static const int MISSING = -2;
177 
178  private:
179   static const int kMaxSize_ = 'Z' - 'A' + 1;
180   u32 origins_[kMaxSize_];
181   int next_id_;
182 };
183 
DescribeMemoryRange(const void * x,uptr size)184 void DescribeMemoryRange(const void *x, uptr size) {
185   // Real limits.
186   uptr start = MEM_TO_SHADOW(x);
187   uptr end = start + size;
188   // Scan limits: align start down to 4; align size up to 16.
189   uptr s = start & ~3UL;
190   size = end - s;
191   size = (size + 15) & ~15UL;
192   uptr e = s + size;
193 
194   // Single letter names to origin id mapping.
195   OriginSet origin_set;
196 
197   uptr pos = 0;  // Offset from aligned start.
198   bool with_origins = __msan_get_track_origins();
199   // True if there is at least 1 poisoned bit in the last 4-byte group.
200   bool last_quad_poisoned;
201   int origin_ids[4];  // Single letter origin ids for the current line.
202 
203   Decorator d;
204   Printf("%s", d.Warning());
205   Printf("Shadow map of [%p, %p), %zu bytes:\n", start, end, end - start);
206   Printf("%s", d.Default());
207   while (s < e) {
208     // Line start.
209     if (pos % 16 == 0) {
210       for (int i = 0; i < 4; ++i) origin_ids[i] = -1;
211       Printf("%p:", s);
212     }
213     // Group start.
214     if (pos % 4 == 0) {
215       Printf(" ");
216       last_quad_poisoned = false;
217     }
218     // Print shadow byte.
219     if (s < start || s >= end) {
220       Printf("..");
221     } else {
222       unsigned char v = *(unsigned char *)s;
223       if (v) last_quad_poisoned = true;
224       Printf("%x%x", v >> 4, v & 0xf);
225     }
226     // Group end.
227     if (pos % 4 == 3 && with_origins) {
228       int id = OriginSet::MISSING;
229       if (last_quad_poisoned) {
230         u32 o = *(u32 *)SHADOW_TO_ORIGIN(s - 3);
231         id = origin_set.insert(o);
232       }
233       origin_ids[(pos % 16) / 4] = id;
234     }
235     // Line end.
236     if (pos % 16 == 15) {
237       if (with_origins) {
238         Printf("  |");
239         for (int i = 0; i < 4; ++i) {
240           char c = OriginSet::asChar(origin_ids[i]);
241           Printf("%c", c);
242           if (i != 3) Printf(" ");
243         }
244         Printf("|");
245       }
246       Printf("\n");
247     }
248     size--;
249     s++;
250     pos++;
251   }
252 
253   Printf("\n");
254 
255   for (int i = 0; i < origin_set.size(); ++i) {
256     u32 o = origin_set.get(i);
257     Printf("Origin %c (origin_id %x):\n", OriginSet::asChar(i), o);
258     DescribeOrigin(o);
259   }
260 }
261 
ReportUMRInsideAddressRange(const char * what,const void * start,uptr size,uptr offset)262 void ReportUMRInsideAddressRange(const char *what, const void *start, uptr size,
263                                  uptr offset) {
264   Decorator d;
265   Printf("%s", d.Warning());
266   Printf("%sUninitialized bytes in %s%s%s at offset %zu inside [%p, %zu)%s\n",
267          d.Warning(), d.Name(), what, d.Warning(), offset, start, size,
268          d.Default());
269   if (__sanitizer::Verbosity())
270     DescribeMemoryRange(start, size);
271 }
272 
273 }  // namespace __msan
274