tsan_suppressions.cc revision 0969bcf2c936126b1f6e636b978aade8fc207437
1603c4be006d8c53905d736bf1f19a49f5ce98276Alexey Samsonov//===-- tsan_suppressions.cc ----------------------------------------------===// 27ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// 37ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// The LLVM Compiler Infrastructure 47ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// 57ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// This file is distributed under the University of Illinois Open Source 67ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// License. See LICENSE.TXT for details. 77ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// 87ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//===----------------------------------------------------------------------===// 97ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// 107ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// This file is a part of ThreadSanitizer (TSan), a race detector. 117ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// 127ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//===----------------------------------------------------------------------===// 137ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany 140969bcf2c936126b1f6e636b978aade8fc207437Alexey Samsonov#include "sanitizer_common/sanitizer_common.h" 157f9c5a220b2768a450696bbd157a0e6f2e9ceea3Alexey Samsonov#include "sanitizer_common/sanitizer_libc.h" 167ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_suppressions.h" 177ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_rtl.h" 187ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_flags.h" 197ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_mman.h" 207ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_platform.h" 217ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany 227ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanynamespace __tsan { 237ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany 247ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanystatic Suppression *g_suppressions; 257ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany 267ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanystatic char *ReadFile(const char *filename) { 277ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany if (filename == 0 || filename[0] == 0) 287ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany return 0; 297ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany InternalScopedBuf<char> tmp(4*1024); 307ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany if (filename[0] == '/') 3167a64dd8259fdbd867633b27f54d584f435f1ce6Alexey Samsonov SNPrintf(tmp, tmp.Size(), "%s", filename); 327ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany else 330969bcf2c936126b1f6e636b978aade8fc207437Alexey Samsonov SNPrintf(tmp, tmp.Size(), "%s/%s", GetPwd(), filename); 347ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany fd_t fd = internal_open(tmp, false); 357ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany if (fd == kInvalidFd) { 3667a64dd8259fdbd867633b27f54d584f435f1ce6Alexey Samsonov TsanPrintf("ThreadSanitizer: failed to open suppressions file '%s'\n", 377ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany tmp.Ptr()); 387ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany Die(); 397ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany } 407ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany const uptr fsize = internal_filesize(fd); 417ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany if (fsize == (uptr)-1) { 4267a64dd8259fdbd867633b27f54d584f435f1ce6Alexey Samsonov TsanPrintf("ThreadSanitizer: failed to stat suppressions file '%s'\n", 437ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany tmp.Ptr()); 447ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany Die(); 457ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany } 467ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany char *buf = (char*)internal_alloc(MBlockSuppression, fsize + 1); 477ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany if (fsize != internal_read(fd, buf, fsize)) { 4867a64dd8259fdbd867633b27f54d584f435f1ce6Alexey Samsonov TsanPrintf("ThreadSanitizer: failed to read suppressions file '%s'\n", 497ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany tmp.Ptr()); 507ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany Die(); 517ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany } 527ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany internal_close(fd); 537ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany buf[fsize] = 0; 547ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany return buf; 557ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany} 567ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany 577ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanybool SuppressionMatch(char *templ, const char *str) { 5820f60c53c446f42af2b4ebc3b85f6a5042be6904Dmitry Vyukov if (str == 0 || str[0] == 0) 5920f60c53c446f42af2b4ebc3b85f6a5042be6904Dmitry Vyukov return false; 607ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany char *tpos; 617ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany const char *spos; 627ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany while (templ && templ[0]) { 637ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany if (templ[0] == '*') { 647ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany templ++; 657ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany continue; 667ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany } 677ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany if (str[0] == 0) 687ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany return false; 697ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany tpos = (char*)internal_strchr(templ, '*'); 707ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany if (tpos != 0) 717ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany tpos[0] = 0; 7288207ab15125e2f1e9b3d541b735b2b8aba9b6d9Alexey Samsonov spos = REAL(strstr)(str, templ); 737ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany str = spos + internal_strlen(templ); 747ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany templ = tpos; 757ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany if (tpos) 767ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany tpos[0] = '*'; 777ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany if (spos == 0) 787ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany return false; 797ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany } 807ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany return true; 817ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany} 827ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany 837ac41484ea322e0ea5774df681660269f5dc321eKostya SerebryanySuppression *SuppressionParse(const char* supp) { 847ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany Suppression *head = 0; 857ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany const char *line = supp; 867ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany while (line) { 877ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany while (line[0] == ' ' || line[0] == '\t') 887ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany line++; 897ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany const char *end = internal_strchr(line, '\n'); 907ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany if (end == 0) 917ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany end = line + internal_strlen(line); 927ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany if (line != end && line[0] != '#') { 937ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany const char *end2 = end; 947ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany while (line != end2 && (end2[-1] == ' ' || end2[-1] == '\t')) 957ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany end2--; 967ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany SuppressionType stype; 9788207ab15125e2f1e9b3d541b735b2b8aba9b6d9Alexey Samsonov if (0 == REAL(strncmp)(line, "race:", sizeof("race:") - 1)) { 987ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany stype = SuppressionRace; 997ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany line += sizeof("race:") - 1; 10088207ab15125e2f1e9b3d541b735b2b8aba9b6d9Alexey Samsonov } else if (0 == REAL(strncmp)(line, "thread:", 1017ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany sizeof("thread:") - 1)) { 1027ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany stype = SuppressionThread; 1037ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany line += sizeof("thread:") - 1; 10488207ab15125e2f1e9b3d541b735b2b8aba9b6d9Alexey Samsonov } else if (0 == REAL(strncmp)(line, "mutex:", 1057ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany sizeof("mutex:") - 1)) { 1067ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany stype = SuppressionMutex; 1077ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany line += sizeof("mutex:") - 1; 10888207ab15125e2f1e9b3d541b735b2b8aba9b6d9Alexey Samsonov } else if (0 == REAL(strncmp)(line, "signal:", 1097ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany sizeof("signal:") - 1)) { 1107ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany stype = SuppressionSignal; 1117ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany line += sizeof("signal:") - 1; 1127ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany } else { 11367a64dd8259fdbd867633b27f54d584f435f1ce6Alexey Samsonov TsanPrintf("ThreadSanitizer: failed to parse suppressions file\n"); 1147ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany Die(); 1157ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany } 1167ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany Suppression *s = (Suppression*)internal_alloc(MBlockSuppression, 1177ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany sizeof(Suppression)); 1187ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany s->next = head; 1197ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany head = s; 1207ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany s->type = stype; 12120f60c53c446f42af2b4ebc3b85f6a5042be6904Dmitry Vyukov s->templ = (char*)internal_alloc(MBlockSuppression, end2 - line + 1); 12288207ab15125e2f1e9b3d541b735b2b8aba9b6d9Alexey Samsonov REAL(memcpy)(s->templ, line, end2 - line); 12320f60c53c446f42af2b4ebc3b85f6a5042be6904Dmitry Vyukov s->templ[end2 - line] = 0; 1247ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany } 1257ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany if (end[0] == 0) 1267ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany break; 1277ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany line = end + 1; 1287ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany } 1297ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany return head; 1307ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany} 1317ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany 1327ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanyvoid InitializeSuppressions() { 1337ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany char *supp = ReadFile(flags()->suppressions); 1347ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany g_suppressions = SuppressionParse(supp); 1357ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany} 1367ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany 1377ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanybool IsSuppressed(ReportType typ, const ReportStack *stack) { 1387ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany if (g_suppressions == 0 || stack == 0) 1397ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany return false; 1407ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany SuppressionType stype; 1417ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany if (typ == ReportTypeRace) 1427ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany stype = SuppressionRace; 1437ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany else if (typ == ReportTypeThreadLeak) 1447ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany stype = SuppressionThread; 1457ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany else if (typ == ReportTypeMutexDestroyLocked) 1467ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany stype = SuppressionMutex; 1477ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany else if (typ == ReportTypeSignalUnsafe) 1487ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany stype = SuppressionSignal; 1497ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany else 1507ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany return false; 1517ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany for (const ReportStack *frame = stack; frame; frame = frame->next) { 1527ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany for (Suppression *supp = g_suppressions; supp; supp = supp->next) { 153a13749bd7b3629a3498e765960729a8ade5db7cfDmitry Vyukov if (stype == supp->type && 154a13749bd7b3629a3498e765960729a8ade5db7cfDmitry Vyukov (SuppressionMatch(supp->templ, frame->func) || 155a13749bd7b3629a3498e765960729a8ade5db7cfDmitry Vyukov SuppressionMatch(supp->templ, frame->file))) { 15620f60c53c446f42af2b4ebc3b85f6a5042be6904Dmitry Vyukov DPrintf("ThreadSanitizer: matched suppression '%s'\n", supp->templ); 1577ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany return true; 1587ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany } 1597ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany } 1607ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany } 1617ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany return false; 1627ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany} 1637ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany} // namespace __tsan 164