msan_report.cpp 8.1 KB

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