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 #ifndef SANITIZER_GO 25 // Suppressions for true/false positives in standard libraries. 26 static const char *const std_suppressions = 27 // Libstdc++ 4.4 has data races in std::string. 28 // See http://crbug.com/181502 for an example. 29 "race:^_M_rep$\n" 30 "race:^_M_is_leaked$\n" 31 // False positive when using std <thread>. 32 // Happens because we miss atomic synchronization in libstdc++. 33 // See http://llvm.org/bugs/show_bug.cgi?id=17066 for details. 34 "race:std::_Sp_counted_ptr_inplace<std::thread::_Impl\n"; 35 36 // Can be overriden in frontend. 37 SANITIZER_WEAK_DEFAULT_IMPL 38 const char *__tsan_default_suppressions() { 39 return 0; 40 } 41 #endif 42 43 namespace __tsan { 44 45 ALIGNED(64) static char suppression_placeholder[sizeof(SuppressionContext)]; 46 static SuppressionContext *suppression_ctx = nullptr; 47 static const char *kSuppressionTypes[] = { 48 kSuppressionRace, kSuppressionRaceTop, kSuppressionMutex, 49 kSuppressionThread, kSuppressionSignal, kSuppressionLib, 50 kSuppressionDeadlock}; 51 52 void InitializeSuppressions() { 53 CHECK_EQ(nullptr, suppression_ctx); 54 suppression_ctx = new (suppression_placeholder) // NOLINT 55 SuppressionContext(kSuppressionTypes, ARRAY_SIZE(kSuppressionTypes)); 56 suppression_ctx->ParseFromFile(flags()->suppressions); 57 #ifndef SANITIZER_GO 58 suppression_ctx->Parse(__tsan_default_suppressions()); 59 suppression_ctx->Parse(std_suppressions); 60 #endif 61 } 62 63 SuppressionContext *Suppressions() { 64 CHECK(suppression_ctx); 65 return suppression_ctx; 66 } 67 68 static const char *conv(ReportType typ) { 69 if (typ == ReportTypeRace) 70 return kSuppressionRace; 71 else if (typ == ReportTypeVptrRace) 72 return kSuppressionRace; 73 else if (typ == ReportTypeUseAfterFree) 74 return kSuppressionRace; 75 else if (typ == ReportTypeVptrUseAfterFree) 76 return kSuppressionRace; 77 else if (typ == ReportTypeThreadLeak) 78 return kSuppressionThread; 79 else if (typ == ReportTypeMutexDestroyLocked) 80 return kSuppressionMutex; 81 else if (typ == ReportTypeMutexDoubleLock) 82 return kSuppressionMutex; 83 else if (typ == ReportTypeMutexBadUnlock) 84 return kSuppressionMutex; 85 else if (typ == ReportTypeMutexBadReadLock) 86 return kSuppressionMutex; 87 else if (typ == ReportTypeMutexBadReadUnlock) 88 return kSuppressionMutex; 89 else if (typ == ReportTypeSignalUnsafe) 90 return kSuppressionSignal; 91 else if (typ == ReportTypeErrnoInSignal) 92 return kSuppressionNone; 93 else if (typ == ReportTypeDeadlock) 94 return kSuppressionDeadlock; 95 Printf("ThreadSanitizer: unknown report type %d\n", typ), 96 Die(); 97 } 98 99 static uptr IsSuppressed(const char *stype, const AddressInfo &info, 100 Suppression **sp) { 101 if (suppression_ctx->Match(info.function, stype, sp) || 102 suppression_ctx->Match(info.file, stype, sp) || 103 suppression_ctx->Match(info.module, stype, sp)) { 104 VPrintf(2, "ThreadSanitizer: matched suppression '%s'\n", (*sp)->templ); 105 atomic_fetch_add(&(*sp)->hit_count, 1, memory_order_relaxed); 106 return info.address; 107 } 108 return 0; 109 } 110 111 uptr IsSuppressed(ReportType typ, const ReportStack *stack, Suppression **sp) { 112 CHECK(suppression_ctx); 113 if (!suppression_ctx->SuppressionCount() || stack == 0 || 114 !stack->suppressable) 115 return 0; 116 const char *stype = conv(typ); 117 if (0 == internal_strcmp(stype, kSuppressionNone)) 118 return 0; 119 for (const SymbolizedStack *frame = stack->frames; frame; 120 frame = frame->next) { 121 uptr pc = IsSuppressed(stype, frame->info, sp); 122 if (pc != 0) 123 return pc; 124 } 125 if (0 == internal_strcmp(stype, kSuppressionRace) && stack->frames != nullptr) 126 return IsSuppressed(kSuppressionRaceTop, stack->frames->info, sp); 127 return 0; 128 } 129 130 uptr IsSuppressed(ReportType typ, const ReportLocation *loc, Suppression **sp) { 131 CHECK(suppression_ctx); 132 if (!suppression_ctx->SuppressionCount() || loc == 0 || 133 loc->type != ReportLocationGlobal || !loc->suppressable) 134 return 0; 135 const char *stype = conv(typ); 136 if (0 == internal_strcmp(stype, kSuppressionNone)) 137 return 0; 138 Suppression *s; 139 const DataInfo &global = loc->global; 140 if (suppression_ctx->Match(global.name, stype, &s) || 141 suppression_ctx->Match(global.module, stype, &s)) { 142 VPrintf(2, "ThreadSanitizer: matched suppression '%s'\n", s->templ); 143 atomic_fetch_add(&s->hit_count, 1, memory_order_relaxed); 144 *sp = s; 145 return global.start; 146 } 147 return 0; 148 } 149 150 void PrintMatchedSuppressions() { 151 InternalMmapVector<Suppression *> matched(1); 152 CHECK(suppression_ctx); 153 suppression_ctx->GetMatched(&matched); 154 if (!matched.size()) 155 return; 156 int hit_count = 0; 157 for (uptr i = 0; i < matched.size(); i++) 158 hit_count += atomic_load_relaxed(&matched[i]->hit_count); 159 Printf("ThreadSanitizer: Matched %d suppressions (pid=%d):\n", hit_count, 160 (int)internal_getpid()); 161 for (uptr i = 0; i < matched.size(); i++) { 162 Printf("%d %s:%s\n", matched[i]->hit_count, matched[i]->type, 163 matched[i]->templ); 164 } 165 } 166 } // namespace __tsan 167