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"
16a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev#include "sanitizer_common/sanitizer_placement_new.h"
17a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev#include "sanitizer_common/sanitizer_suppressions.h"
187ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_suppressions.h"
197ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_rtl.h"
207ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_flags.h"
217ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_mman.h"
227ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_platform.h"
237ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
24c2b9f1c1d54e857e56462e65155381faddf398dfDmitry Vyukov// Can be overriden in frontend.
25c2b9f1c1d54e857e56462e65155381faddf398dfDmitry Vyukov#ifndef TSAN_GO
26c2b9f1c1d54e857e56462e65155381faddf398dfDmitry Vyukovextern "C" const char *WEAK __tsan_default_suppressions() {
27c2b9f1c1d54e857e56462e65155381faddf398dfDmitry Vyukov  return 0;
28c2b9f1c1d54e857e56462e65155381faddf398dfDmitry Vyukov}
29c2b9f1c1d54e857e56462e65155381faddf398dfDmitry Vyukov#endif
30c2b9f1c1d54e857e56462e65155381faddf398dfDmitry Vyukov
317ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanynamespace __tsan {
327ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
33a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveevstatic SuppressionContext* g_ctx;
347ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
357ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanystatic char *ReadFile(const char *filename) {
367ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  if (filename == 0 || filename[0] == 0)
377ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    return 0;
3814c8bd7250742749e44e306c02a56cf47ad1db82Alexey Samsonov  InternalScopedBuffer<char> tmp(4*1024);
396422c32996ec2d65b2ebc77dda36b613dbd1571aDmitry Vyukov  if (filename[0] == '/' || GetPwd() == 0)
401dc4cf7e253aefa3ce3bd4a1d349a13647e8b2eaAlexey Samsonov    internal_snprintf(tmp.data(), tmp.size(), "%s", filename);
417ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  else
421dc4cf7e253aefa3ce3bd4a1d349a13647e8b2eaAlexey Samsonov    internal_snprintf(tmp.data(), tmp.size(), "%s/%s", GetPwd(), filename);
439578a3ecfc35a264ede1135033398e2a77a6cad6Peter Collingbourne  uptr openrv = OpenFile(tmp.data(), false);
449578a3ecfc35a264ede1135033398e2a77a6cad6Peter Collingbourne  if (internal_iserror(openrv)) {
45b1fe3021eca0843e37878d224ee7f32e32f40d99Alexey Samsonov    Printf("ThreadSanitizer: failed to open suppressions file '%s'\n",
4614c8bd7250742749e44e306c02a56cf47ad1db82Alexey Samsonov               tmp.data());
477ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    Die();
487ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
499578a3ecfc35a264ede1135033398e2a77a6cad6Peter Collingbourne  fd_t fd = openrv;
507ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  const uptr fsize = internal_filesize(fd);
517ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  if (fsize == (uptr)-1) {
52b1fe3021eca0843e37878d224ee7f32e32f40d99Alexey Samsonov    Printf("ThreadSanitizer: failed to stat suppressions file '%s'\n",
5314c8bd7250742749e44e306c02a56cf47ad1db82Alexey Samsonov               tmp.data());
547ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    Die();
557ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
567ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  char *buf = (char*)internal_alloc(MBlockSuppression, fsize + 1);
577ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  if (fsize != internal_read(fd, buf, fsize)) {
58b1fe3021eca0843e37878d224ee7f32e32f40d99Alexey Samsonov    Printf("ThreadSanitizer: failed to read suppressions file '%s'\n",
5914c8bd7250742749e44e306c02a56cf47ad1db82Alexey Samsonov               tmp.data());
607ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    Die();
617ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
627ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  internal_close(fd);
637ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  buf[fsize] = 0;
647ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  return buf;
657ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}
667ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
677ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanyvoid InitializeSuppressions() {
68a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  ALIGNED(64) static char placeholder_[sizeof(SuppressionContext)];
69a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  g_ctx = new(placeholder_) SuppressionContext;
70c2b9f1c1d54e857e56462e65155381faddf398dfDmitry Vyukov  const char *supp = ReadFile(flags()->suppressions);
71a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  g_ctx->Parse(supp);
72c2b9f1c1d54e857e56462e65155381faddf398dfDmitry Vyukov#ifndef TSAN_GO
73c2b9f1c1d54e857e56462e65155381faddf398dfDmitry Vyukov  supp = __tsan_default_suppressions();
74a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  g_ctx->Parse(supp);
75c2b9f1c1d54e857e56462e65155381faddf398dfDmitry Vyukov#endif
767ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}
777ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
7839968339a07d790aadcf27534f92a0de8c0c90fbDmitry VyukovSuppressionType conv(ReportType typ) {
797ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  if (typ == ReportTypeRace)
8039968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov    return SuppressionRace;
8124cdfee9776abddcd6c96240e398706667029e0dDmitry Vyukov  else if (typ == ReportTypeVptrRace)
8239968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov    return SuppressionRace;
8324cdfee9776abddcd6c96240e398706667029e0dDmitry Vyukov  else if (typ == ReportTypeUseAfterFree)
8445c652e3b3068f5a0626977e4506c71cfdba8ad3Alexander Potapenko    return SuppressionRace;
857ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  else if (typ == ReportTypeThreadLeak)
8639968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov    return SuppressionThread;
877ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  else if (typ == ReportTypeMutexDestroyLocked)
8839968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov    return SuppressionMutex;
897ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  else if (typ == ReportTypeSignalUnsafe)
9039968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov    return SuppressionSignal;
9124cdfee9776abddcd6c96240e398706667029e0dDmitry Vyukov  else if (typ == ReportTypeErrnoInSignal)
9239968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov    return SuppressionNone;
9339968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov  Printf("ThreadSanitizer: unknown report type %d\n", typ),
9439968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov  Die();
9539968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov}
9639968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov
9739968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukovuptr IsSuppressed(ReportType typ, const ReportStack *stack, Suppression **sp) {
98a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  CHECK(g_ctx);
99a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  if (!g_ctx->SuppressionCount() || stack == 0) return 0;
10039968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov  SuppressionType stype = conv(typ);
10139968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov  if (stype == SuppressionNone)
102158c6ac3bb46753db217f9c2c73485811a3a1890Dmitry Vyukov    return 0;
103a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  Suppression *s;
1047ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  for (const ReportStack *frame = stack; frame; frame = frame->next) {
105a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev    if (g_ctx->Match(frame->func, stype, &s) ||
106a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev        g_ctx->Match(frame->file, stype, &s) ||
107a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev        g_ctx->Match(frame->module, stype, &s)) {
108a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev      DPrintf("ThreadSanitizer: matched suppression '%s'\n", s->templ);
109a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev      s->hit_count++;
110a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev      *sp = s;
111a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev      return frame->pc;
1127ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    }
1137ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
114158c6ac3bb46753db217f9c2c73485811a3a1890Dmitry Vyukov  return 0;
1157ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}
116f754eb501d6bd163fff6747716b7703fe45be4b8Dmitry Vyukov
11739968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukovuptr IsSuppressed(ReportType typ, const ReportLocation *loc, Suppression **sp) {
118a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  CHECK(g_ctx);
119a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  if (!g_ctx->SuppressionCount() || loc == 0 ||
120a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev      loc->type != ReportLocationGlobal)
12139968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov    return 0;
12239968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov  SuppressionType stype = conv(typ);
12339968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov  if (stype == SuppressionNone)
12439968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov    return 0;
125a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  Suppression *s;
126a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  if (g_ctx->Match(loc->name, stype, &s) ||
127a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev      g_ctx->Match(loc->file, stype, &s) ||
128a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev      g_ctx->Match(loc->module, stype, &s)) {
129f2c844564c14e5438494083bf6db304a33673df2Sergey Matveev      DPrintf("ThreadSanitizer: matched suppression '%s'\n", s->templ);
130a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev      s->hit_count++;
131a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev      *sp = s;
13239968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov      return loc->addr;
13339968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov  }
13439968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov  return 0;
13539968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov}
13639968339a07d790aadcf27534f92a0de8c0c90fbDmitry Vyukov
137f754eb501d6bd163fff6747716b7703fe45be4b8Dmitry Vyukovvoid PrintMatchedSuppressions() {
138a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  CHECK(g_ctx);
139a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  InternalMmapVector<Suppression *> matched(1);
140a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  g_ctx->GetMatched(&matched);
141a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  if (!matched.size())
142f754eb501d6bd163fff6747716b7703fe45be4b8Dmitry Vyukov    return;
143a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  int hit_count = 0;
144a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  for (uptr i = 0; i < matched.size(); i++)
145a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev    hit_count += matched[i]->hit_count;
146a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  Printf("ThreadSanitizer: Matched %d suppressions (pid=%d):\n", hit_count,
147a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev         (int)internal_getpid());
148a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev  for (uptr i = 0; i < matched.size(); i++) {
149a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev    Printf("%d %s:%s\n", matched[i]->hit_count,
150a52e5c6f371bcc66e89792db1219a557664aab8dSergey Matveev           SuppressionTypeString(matched[i]->type), matched[i]->templ);
151f754eb501d6bd163fff6747716b7703fe45be4b8Dmitry Vyukov  }
152f754eb501d6bd163fff6747716b7703fe45be4b8Dmitry Vyukov}
1537ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}  // namespace __tsan
154