tsan_suppressions.cc revision 7f9c5a220b2768a450696bbd157a0e6f2e9ceea3
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
147f9c5a220b2768a450696bbd157a0e6f2e9ceea3Alexey Samsonov#include "sanitizer_common/sanitizer_libc.h"
157ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_suppressions.h"
167ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_rtl.h"
177ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_flags.h"
187ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_mman.h"
197ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_platform.h"
207ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
217f9c5a220b2768a450696bbd157a0e6f2e9ceea3Alexey Samsonovusing namespace __sanitizer;  // NOLINT
227f9c5a220b2768a450696bbd157a0e6f2e9ceea3Alexey Samsonov
237ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanynamespace __tsan {
247ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
257ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanystatic Suppression *g_suppressions;
267ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
277ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanystatic char *ReadFile(const char *filename) {
287ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  if (filename == 0 || filename[0] == 0)
297ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    return 0;
307ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  InternalScopedBuf<char> tmp(4*1024);
317ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  if (filename[0] == '/')
327ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    Snprintf(tmp, tmp.Size(), "%s", filename);
337ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  else
347ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    Snprintf(tmp, tmp.Size(), "%s/%s", internal_getpwd(), filename);
357ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  fd_t fd = internal_open(tmp, false);
367ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  if (fd == kInvalidFd) {
377ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    Printf("ThreadSanitizer: failed to open suppressions file '%s'\n",
387ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        tmp.Ptr());
397ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    Die();
407ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
417ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  const uptr fsize = internal_filesize(fd);
427ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  if (fsize == (uptr)-1) {
437ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    Printf("ThreadSanitizer: failed to stat suppressions file '%s'\n",
447ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        tmp.Ptr());
457ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    Die();
467ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
477ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  char *buf = (char*)internal_alloc(MBlockSuppression, fsize + 1);
487ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  if (fsize != internal_read(fd, buf, fsize)) {
497ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    Printf("ThreadSanitizer: failed to read suppressions file '%s'\n",
507ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        tmp.Ptr());
517ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    Die();
527ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
537ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  internal_close(fd);
547ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  buf[fsize] = 0;
557ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  return buf;
567ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}
577ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
587ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanybool SuppressionMatch(char *templ, const char *str) {
5920f60c53c446f42af2b4ebc3b85f6a5042be6904Dmitry Vyukov  if (str == 0 || str[0] == 0)
6020f60c53c446f42af2b4ebc3b85f6a5042be6904Dmitry Vyukov    return false;
617ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  char *tpos;
627ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  const char *spos;
637ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  while (templ && templ[0]) {
647ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (templ[0] == '*') {
657ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      templ++;
667ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      continue;
677ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    }
687ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (str[0] == 0)
697ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      return false;
707ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    tpos = (char*)internal_strchr(templ, '*');
717ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (tpos != 0)
727ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      tpos[0] = 0;
737ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    spos = internal_strstr(str, templ);
747ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    str = spos + internal_strlen(templ);
757ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    templ = tpos;
767ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (tpos)
777ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      tpos[0] = '*';
787ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (spos == 0)
797ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      return false;
807ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
817ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  return true;
827ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}
837ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
847ac41484ea322e0ea5774df681660269f5dc321eKostya SerebryanySuppression *SuppressionParse(const char* supp) {
857ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  Suppression *head = 0;
867ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  const char *line = supp;
877ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  while (line) {
887ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    while (line[0] == ' ' || line[0] == '\t')
897ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      line++;
907ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    const char *end = internal_strchr(line, '\n');
917ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (end == 0)
927ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      end = line + internal_strlen(line);
937ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (line != end && line[0] != '#') {
947ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      const char *end2 = end;
957ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      while (line != end2 && (end2[-1] == ' ' || end2[-1] == '\t'))
967ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        end2--;
977ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      SuppressionType stype;
987ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      if (0 == internal_strncmp(line, "race:", sizeof("race:") - 1)) {
997ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        stype = SuppressionRace;
1007ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        line += sizeof("race:") - 1;
1017ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      } else if (0 == internal_strncmp(line, "thread:",
1027ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany          sizeof("thread:") - 1)) {
1037ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        stype = SuppressionThread;
1047ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        line += sizeof("thread:") - 1;
1057ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      } else if (0 == internal_strncmp(line, "mutex:",
1067ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany          sizeof("mutex:") - 1)) {
1077ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        stype = SuppressionMutex;
1087ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        line += sizeof("mutex:") - 1;
1097ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      } else if (0 == internal_strncmp(line, "signal:",
1107ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany          sizeof("signal:") - 1)) {
1117ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        stype = SuppressionSignal;
1127ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        line += sizeof("signal:") - 1;
1137ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      } else {
1147ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        Printf("ThreadSanitizer: failed to parse suppressions file\n");
1157ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        Die();
1167ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      }
1177ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      Suppression *s = (Suppression*)internal_alloc(MBlockSuppression,
1187ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany          sizeof(Suppression));
1197ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      s->next = head;
1207ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      head = s;
1217ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      s->type = stype;
12220f60c53c446f42af2b4ebc3b85f6a5042be6904Dmitry Vyukov      s->templ = (char*)internal_alloc(MBlockSuppression, end2 - line + 1);
12320f60c53c446f42af2b4ebc3b85f6a5042be6904Dmitry Vyukov      internal_memcpy(s->templ, line, end2 - line);
12420f60c53c446f42af2b4ebc3b85f6a5042be6904Dmitry Vyukov      s->templ[end2 - line] = 0;
1257ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    }
1267ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (end[0] == 0)
1277ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      break;
1287ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    line = end + 1;
1297ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
1307ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  return head;
1317ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}
1327ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1337ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanyvoid InitializeSuppressions() {
1347ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  char *supp = ReadFile(flags()->suppressions);
1357ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  g_suppressions = SuppressionParse(supp);
1367ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}
1377ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1387ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanybool IsSuppressed(ReportType typ, const ReportStack *stack) {
1397ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  if (g_suppressions == 0 || stack == 0)
1407ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    return false;
1417ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  SuppressionType stype;
1427ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  if (typ == ReportTypeRace)
1437ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    stype = SuppressionRace;
1447ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  else if (typ == ReportTypeThreadLeak)
1457ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    stype = SuppressionThread;
1467ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  else if (typ == ReportTypeMutexDestroyLocked)
1477ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    stype = SuppressionMutex;
1487ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  else if (typ == ReportTypeSignalUnsafe)
1497ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    stype = SuppressionSignal;
1507ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  else
1517ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    return false;
1527ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  for (const ReportStack *frame = stack; frame; frame = frame->next) {
1537ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    for (Suppression *supp = g_suppressions; supp; supp = supp->next) {
154a13749bd7b3629a3498e765960729a8ade5db7cfDmitry Vyukov      if (stype == supp->type &&
155a13749bd7b3629a3498e765960729a8ade5db7cfDmitry Vyukov          (SuppressionMatch(supp->templ, frame->func) ||
156a13749bd7b3629a3498e765960729a8ade5db7cfDmitry Vyukov          SuppressionMatch(supp->templ, frame->file))) {
15720f60c53c446f42af2b4ebc3b85f6a5042be6904Dmitry Vyukov        DPrintf("ThreadSanitizer: matched suppression '%s'\n", supp->templ);
1587ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        return true;
1597ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      }
1607ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    }
1617ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
1627ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  return false;
1637ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}
1647ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}  // namespace __tsan
165