tsan_suppressions.cc revision 4af0f21c0c98950df1136dbec8824a029ed5bb8e
1//===-- tsan_suppressions.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 ThreadSanitizer (TSan), a race detector. 11// 12//===----------------------------------------------------------------------===// 13 14#include "sanitizer_common/sanitizer_common.h" 15#include "sanitizer_common/sanitizer_libc.h" 16#include "sanitizer_common/sanitizer_placement_new.h" 17#include "sanitizer_common/sanitizer_suppressions.h" 18#include "tsan_suppressions.h" 19#include "tsan_rtl.h" 20#include "tsan_flags.h" 21#include "tsan_mman.h" 22#include "tsan_platform.h" 23 24// Suppressions for true/false positives in standard libraries. 25static const char *const std_suppressions = 26// Libstdc++ 4.4 has data races in std::string. 27// See http://crbug.com/181502 for an example. 28"race:^_M_rep$\n" 29"race:^_M_is_leaked$\n" 30// False positive when using std <thread>. 31// Happens because we miss atomic synchronization in libstdc++. 32// See http://llvm.org/bugs/show_bug.cgi?id=17066 for details. 33"race:std::_Sp_counted_ptr_inplace<std::thread::_Impl\n"; 34 35// Can be overriden in frontend. 36#ifndef TSAN_GO 37extern "C" const char *WEAK __tsan_default_suppressions() { 38 return 0; 39} 40#endif 41 42namespace __tsan { 43 44static SuppressionContext* g_ctx; 45 46static char *ReadFile(const char *filename) { 47 if (filename == 0 || filename[0] == 0) 48 return 0; 49 InternalScopedBuffer<char> tmp(4*1024); 50 if (filename[0] == '/' || GetPwd() == 0) 51 internal_snprintf(tmp.data(), tmp.size(), "%s", filename); 52 else 53 internal_snprintf(tmp.data(), tmp.size(), "%s/%s", GetPwd(), filename); 54 uptr openrv = OpenFile(tmp.data(), false); 55 if (internal_iserror(openrv)) { 56 Printf("ThreadSanitizer: failed to open suppressions file '%s'\n", 57 tmp.data()); 58 Die(); 59 } 60 fd_t fd = openrv; 61 const uptr fsize = internal_filesize(fd); 62 if (fsize == (uptr)-1) { 63 Printf("ThreadSanitizer: failed to stat suppressions file '%s'\n", 64 tmp.data()); 65 Die(); 66 } 67 char *buf = (char*)internal_alloc(MBlockSuppression, fsize + 1); 68 if (fsize != internal_read(fd, buf, fsize)) { 69 Printf("ThreadSanitizer: failed to read suppressions file '%s'\n", 70 tmp.data()); 71 Die(); 72 } 73 internal_close(fd); 74 buf[fsize] = 0; 75 return buf; 76} 77 78void InitializeSuppressions() { 79 ALIGNED(64) static char placeholder_[sizeof(SuppressionContext)]; 80 g_ctx = new(placeholder_) SuppressionContext; 81 const char *supp = ReadFile(flags()->suppressions); 82 g_ctx->Parse(supp); 83#ifndef TSAN_GO 84 supp = __tsan_default_suppressions(); 85 g_ctx->Parse(supp); 86 g_ctx->Parse(std_suppressions); 87#endif 88} 89 90SuppressionContext *GetSuppressionContext() { 91 CHECK_NE(g_ctx, 0); 92 return g_ctx; 93} 94 95SuppressionType conv(ReportType typ) { 96 if (typ == ReportTypeRace) 97 return SuppressionRace; 98 else if (typ == ReportTypeVptrRace) 99 return SuppressionRace; 100 else if (typ == ReportTypeUseAfterFree) 101 return SuppressionRace; 102 else if (typ == ReportTypeThreadLeak) 103 return SuppressionThread; 104 else if (typ == ReportTypeMutexDestroyLocked) 105 return SuppressionMutex; 106 else if (typ == ReportTypeSignalUnsafe) 107 return SuppressionSignal; 108 else if (typ == ReportTypeErrnoInSignal) 109 return SuppressionNone; 110 Printf("ThreadSanitizer: unknown report type %d\n", typ), 111 Die(); 112} 113 114uptr IsSuppressed(ReportType typ, const ReportStack *stack, Suppression **sp) { 115 CHECK(g_ctx); 116 if (!g_ctx->SuppressionCount() || stack == 0) return 0; 117 SuppressionType stype = conv(typ); 118 if (stype == SuppressionNone) 119 return 0; 120 Suppression *s; 121 for (const ReportStack *frame = stack; frame; frame = frame->next) { 122 if (g_ctx->Match(frame->func, stype, &s) || 123 g_ctx->Match(frame->file, stype, &s) || 124 g_ctx->Match(frame->module, stype, &s)) { 125 DPrintf("ThreadSanitizer: matched suppression '%s'\n", s->templ); 126 s->hit_count++; 127 *sp = s; 128 return frame->pc; 129 } 130 } 131 return 0; 132} 133 134uptr IsSuppressed(ReportType typ, const ReportLocation *loc, Suppression **sp) { 135 CHECK(g_ctx); 136 if (!g_ctx->SuppressionCount() || loc == 0 || 137 loc->type != ReportLocationGlobal) 138 return 0; 139 SuppressionType stype = conv(typ); 140 if (stype == SuppressionNone) 141 return 0; 142 Suppression *s; 143 if (g_ctx->Match(loc->name, stype, &s) || 144 g_ctx->Match(loc->file, stype, &s) || 145 g_ctx->Match(loc->module, stype, &s)) { 146 DPrintf("ThreadSanitizer: matched suppression '%s'\n", s->templ); 147 s->hit_count++; 148 *sp = s; 149 return loc->addr; 150 } 151 return 0; 152} 153 154void PrintMatchedSuppressions() { 155 CHECK(g_ctx); 156 InternalMmapVector<Suppression *> matched(1); 157 g_ctx->GetMatched(&matched); 158 if (!matched.size()) 159 return; 160 int hit_count = 0; 161 for (uptr i = 0; i < matched.size(); i++) 162 hit_count += matched[i]->hit_count; 163 Printf("ThreadSanitizer: Matched %d suppressions (pid=%d):\n", hit_count, 164 (int)internal_getpid()); 165 for (uptr i = 0; i < matched.size(); i++) { 166 Printf("%d %s:%s\n", matched[i]->hit_count, 167 SuppressionTypeString(matched[i]->type), matched[i]->templ); 168 } 169} 170} // namespace __tsan 171