blob: 598a13e8f79b6c3fcfa10369264aacdb5be94c0d [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 Stepanov367dc642012-12-26 09:32:05 +000037static void DescribeOrigin(u32 origin) {
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000038 Decorator d;
Sergey Matveev9be70fb2013-12-05 12:04:51 +000039 VPrintf(1, " raw origin id: %d\n", origin);
Evgeniy Stepanovac5ac342013-09-13 12:49:13 +000040 uptr pc;
41 if (const char *so = GetOriginDescrIfStack(origin, &pc)) {
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000042 char* s = internal_strdup(so);
43 char* sep = internal_strchr(s, '@');
44 CHECK(sep);
45 *sep = '\0';
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000046 Printf("%s", d.Origin());
Evgeniy Stepanov257274e2013-02-11 11:34:26 +000047 Printf(" %sUninitialized value was created by an allocation of '%s%s%s'"
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000048 " in the stack frame of function '%s%s%s'%s\n",
Alexey Samsonov7a36e612013-09-10 14:36:16 +000049 d.Origin(), d.Name(), s, d.Origin(), d.Name(),
Peter Collingbourne791e65d2013-10-25 23:03:29 +000050 Symbolizer::Get()->Demangle(sep + 1), d.Origin(), d.End());
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000051 InternalFree(s);
Evgeniy Stepanovac5ac342013-09-13 12:49:13 +000052
53 if (pc) {
54 // For some reason function address in LLVM IR is 1 less then the address
55 // of the first instruction.
56 pc += 1;
Alexey Samsonov4708c592013-11-01 00:19:46 +000057 StackTrace::PrintStack(&pc, 1);
Evgeniy Stepanovac5ac342013-09-13 12:49:13 +000058 }
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000059 } else {
60 uptr size = 0;
61 const uptr *trace = StackDepotGet(origin, &size);
Evgeniy Stepanov257274e2013-02-11 11:34:26 +000062 Printf(" %sUninitialized value was created by a heap allocation%s\n",
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000063 d.Origin(), d.End());
Alexey Samsonov4708c592013-11-01 00:19:46 +000064 StackTrace::PrintStack(trace, size);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000065 }
66}
67
68void ReportUMR(StackTrace *stack, u32 origin) {
69 if (!__msan::flags()->report_umrs) return;
70
Alexey Samsonov734aab42013-04-05 07:30:29 +000071 SpinMutexLock l(&CommonSanitizerReportMutex);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000072
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000073 Decorator d;
74 Printf("%s", d.Warning());
Evgeniy Stepanovdd0780f2013-05-28 14:27:30 +000075 Report(" WARNING: MemorySanitizer: use-of-uninitialized-value\n");
Evgeniy Stepanovfee82c62012-12-26 10:16:45 +000076 Printf("%s", d.End());
Alexey Samsonovf2c76592013-12-19 11:25:05 +000077 stack->Print();
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000078 if (origin) {
79 DescribeOrigin(origin);
80 }
Alexey Samsonov5dc6cff2013-11-01 17:02:14 +000081 ReportErrorSummary("use-of-uninitialized-value", stack);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000082}
83
84void ReportExpectedUMRNotFound(StackTrace *stack) {
Alexey Samsonov734aab42013-04-05 07:30:29 +000085 SpinMutexLock l(&CommonSanitizerReportMutex);
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000086
87 Printf(" WARNING: Expected use of uninitialized value not found\n");
Alexey Samsonovf2c76592013-12-19 11:25:05 +000088 stack->Print();
Evgeniy Stepanov367dc642012-12-26 09:32:05 +000089}
90
Evgeniy Stepanov9b52ce92013-01-10 11:17:55 +000091void ReportAtExitStatistics() {
Alexey Samsonov734aab42013-04-05 07:30:29 +000092 SpinMutexLock l(&CommonSanitizerReportMutex);
93
Evgeniy Stepanov9b52ce92013-01-10 11:17:55 +000094 Decorator d;
95 Printf("%s", d.Warning());
96 Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count);
97 Printf("%s", d.End());
98}
99
Alexey Samsonov49a32c12013-01-30 07:45:58 +0000100} // namespace __msan