blob: e3ef99307940a0a0503768edeab7c7cdf2516924 [file] [log] [blame]
Kostya Serebryany4b48f452012-12-27 14:09:19 +00001//===-- msan_report.cc ----------------------------------------------------===//
Evgeniy Stepanovdb010da2012-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 Samsonov1f3c2fe2013-05-29 09:15:39 +000016#include "sanitizer_common/sanitizer_allocator_internal.h"
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000017#include "sanitizer_common/sanitizer_common.h"
Sergey Matveev0b4bf4d2013-05-06 13:15:14 +000018#include "sanitizer_common/sanitizer_flags.h"
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000019#include "sanitizer_common/sanitizer_mutex.h"
Evgeniy Stepanovc209ba62012-12-26 10:16:45 +000020#include "sanitizer_common/sanitizer_report_decorator.h"
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000021#include "sanitizer_common/sanitizer_stackdepot.h"
Kostya Serebryany1d333c52013-02-07 08:04:56 +000022#include "sanitizer_common/sanitizer_symbolizer.h"
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000023
24using namespace __sanitizer;
25
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000026namespace __msan {
27
Evgeniy Stepanovc209ba62012-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 Stepanovdb010da2012-12-26 09:32:05 +000037static void DescribeOrigin(u32 origin) {
Evgeniy Stepanovc209ba62012-12-26 10:16:45 +000038 Decorator d;
Dmitry Vyukov6866dba2013-10-15 13:28:51 +000039 if (common_flags()->verbosity)
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000040 Printf(" raw origin id: %d\n", origin);
Evgeniy Stepanov6f346052013-09-13 12:49:13 +000041 uptr pc;
42 if (const char *so = GetOriginDescrIfStack(origin, &pc)) {
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000043 char* s = internal_strdup(so);
44 char* sep = internal_strchr(s, '@');
45 CHECK(sep);
46 *sep = '\0';
Evgeniy Stepanovc209ba62012-12-26 10:16:45 +000047 Printf("%s", d.Origin());
Evgeniy Stepanov9af86762013-02-11 11:34:26 +000048 Printf(" %sUninitialized value was created by an allocation of '%s%s%s'"
Evgeniy Stepanovc209ba62012-12-26 10:16:45 +000049 " in the stack frame of function '%s%s%s'%s\n",
Alexey Samsonov7847d772013-09-10 14:36:16 +000050 d.Origin(), d.Name(), s, d.Origin(), d.Name(),
Peter Collingbournec1a1ed62013-10-25 23:03:29 +000051 Symbolizer::Get()->Demangle(sep + 1), d.Origin(), d.End());
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000052 InternalFree(s);
Evgeniy Stepanov6f346052013-09-13 12:49:13 +000053
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;
Alexey Samsonova96c4dc2013-11-01 00:19:46 +000058 StackTrace::PrintStack(&pc, 1);
Evgeniy Stepanov6f346052013-09-13 12:49:13 +000059 }
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000060 } else {
61 uptr size = 0;
62 const uptr *trace = StackDepotGet(origin, &size);
Evgeniy Stepanov9af86762013-02-11 11:34:26 +000063 Printf(" %sUninitialized value was created by a heap allocation%s\n",
Evgeniy Stepanovc209ba62012-12-26 10:16:45 +000064 d.Origin(), d.End());
Alexey Samsonova96c4dc2013-11-01 00:19:46 +000065 StackTrace::PrintStack(trace, size);
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000066 }
67}
68
69void ReportUMR(StackTrace *stack, u32 origin) {
70 if (!__msan::flags()->report_umrs) return;
71
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +000072 SpinMutexLock l(&CommonSanitizerReportMutex);
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000073
Evgeniy Stepanovc209ba62012-12-26 10:16:45 +000074 Decorator d;
75 Printf("%s", d.Warning());
Evgeniy Stepanov6ac157d2013-05-28 14:27:30 +000076 Report(" WARNING: MemorySanitizer: use-of-uninitialized-value\n");
Evgeniy Stepanovc209ba62012-12-26 10:16:45 +000077 Printf("%s", d.End());
Alexey Samsonova96c4dc2013-11-01 00:19:46 +000078 StackTrace::PrintStack(stack->trace, stack->size);
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000079 if (origin) {
80 DescribeOrigin(origin);
81 }
Alexey Samsonov2fb08722013-11-01 17:02:14 +000082 ReportErrorSummary("use-of-uninitialized-value", stack);
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000083}
84
85void ReportExpectedUMRNotFound(StackTrace *stack) {
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +000086 SpinMutexLock l(&CommonSanitizerReportMutex);
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000087
88 Printf(" WARNING: Expected use of uninitialized value not found\n");
Alexey Samsonova96c4dc2013-11-01 00:19:46 +000089 StackTrace::PrintStack(stack->trace, stack->size);
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +000090}
91
Evgeniy Stepanov99bf1d72013-01-10 11:17:55 +000092void ReportAtExitStatistics() {
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +000093 SpinMutexLock l(&CommonSanitizerReportMutex);
94
Evgeniy Stepanov99bf1d72013-01-10 11:17:55 +000095 Decorator d;
96 Printf("%s", d.Warning());
97 Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count);
98 Printf("%s", d.End());
99}
100
Alexey Samsonovba5e9962013-01-30 07:45:58 +0000101} // namespace __msan