tsan_suppressions.cc revision 7ac41484ea322e0ea5774df681660269f5dc321e
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) {
567ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  char *tpos;
577ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  const char *spos;
587ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  while (templ && templ[0]) {
597ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (templ[0] == '*') {
607ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      templ++;
617ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      continue;
627ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    }
637ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (str[0] == 0)
647ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      return false;
657ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    tpos = (char*)internal_strchr(templ, '*');
667ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (tpos != 0)
677ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      tpos[0] = 0;
687ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    spos = internal_strstr(str, templ);
697ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    str = spos + internal_strlen(templ);
707ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    templ = tpos;
717ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (tpos)
727ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      tpos[0] = '*';
737ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (spos == 0)
747ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      return false;
757ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
767ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  return true;
777ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}
787ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
797ac41484ea322e0ea5774df681660269f5dc321eKostya SerebryanySuppression *SuppressionParse(const char* supp) {
807ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  Suppression *head = 0;
817ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  const char *line = supp;
827ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  while (line) {
837ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    while (line[0] == ' ' || line[0] == '\t')
847ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      line++;
857ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    const char *end = internal_strchr(line, '\n');
867ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (end == 0)
877ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      end = line + internal_strlen(line);
887ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (line != end && line[0] != '#') {
897ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      const char *end2 = end;
907ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      while (line != end2 && (end2[-1] == ' ' || end2[-1] == '\t'))
917ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        end2--;
927ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      SuppressionType stype;
937ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      if (0 == internal_strncmp(line, "race:", sizeof("race:") - 1)) {
947ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        stype = SuppressionRace;
957ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        line += sizeof("race:") - 1;
967ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      } else if (0 == internal_strncmp(line, "thread:",
977ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany          sizeof("thread:") - 1)) {
987ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        stype = SuppressionThread;
997ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        line += sizeof("thread:") - 1;
1007ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      } else if (0 == internal_strncmp(line, "mutex:",
1017ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany          sizeof("mutex:") - 1)) {
1027ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        stype = SuppressionMutex;
1037ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        line += sizeof("mutex:") - 1;
1047ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      } else if (0 == internal_strncmp(line, "signal:",
1057ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany          sizeof("signal:") - 1)) {
1067ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        stype = SuppressionSignal;
1077ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        line += sizeof("signal:") - 1;
1087ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      } else {
1097ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        Printf("ThreadSanitizer: failed to parse suppressions file\n");
1107ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        Die();
1117ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      }
1127ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      Suppression *s = (Suppression*)internal_alloc(MBlockSuppression,
1137ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany          sizeof(Suppression));
1147ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      s->next = head;
1157ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      head = s;
1167ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      s->type = stype;
1177ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      s->func = (char*)internal_alloc(MBlockSuppression, end2 - line + 1);
1187ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      internal_memcpy(s->func, line, end2 - line);
1197ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      s->func[end2 - line] = 0;
1207ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    }
1217ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (end[0] == 0)
1227ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      break;
1237ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    line = end + 1;
1247ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
1257ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  return head;
1267ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}
1277ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1287ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanyvoid SuppressionFree(Suppression *supp) {
1297ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  while (supp) {
1307ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    Suppression *tmp = supp;
1317ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    supp = tmp->next;
1327ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    internal_free(tmp->func);
1337ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    internal_free(tmp);
1347ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
1357ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}
1367ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1377ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanyvoid InitializeSuppressions() {
1387ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  char *supp = ReadFile(flags()->suppressions);
1397ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  g_suppressions = SuppressionParse(supp);
1407ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}
1417ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1427ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanyvoid FinalizeSuppressions() {
1437ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  SuppressionFree(g_suppressions);
1447ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  g_suppressions = 0;
1457ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}
1467ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1477ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanybool IsSuppressed(ReportType typ, const ReportStack *stack) {
1487ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  if (g_suppressions == 0 || stack == 0)
1497ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    return false;
1507ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  SuppressionType stype;
1517ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  if (typ == ReportTypeRace)
1527ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    stype = SuppressionRace;
1537ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  else if (typ == ReportTypeThreadLeak)
1547ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    stype = SuppressionThread;
1557ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  else if (typ == ReportTypeMutexDestroyLocked)
1567ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    stype = SuppressionMutex;
1577ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  else if (typ == ReportTypeSignalUnsafe)
1587ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    stype = SuppressionSignal;
1597ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  else
1607ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    return false;
1617ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  for (const ReportStack *frame = stack; frame; frame = frame->next) {
1627ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    if (frame->func == 0)
1637ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      continue;
1647ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    for (Suppression *supp = g_suppressions; supp; supp = supp->next) {
1657ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      if (stype == supp->type && SuppressionMatch(supp->func, frame->func)) {
1667ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        DPrintf("ThreadSanitizer: matched suppression '%s'\n", supp->func);
1677ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany        return true;
1687ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      }
1697ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    }
1707ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
1717ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  return false;
1727ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}
1737ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}  // namespace __tsan
174