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