msan_report.cpp 8.0 KB

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