Alexey Samsonov | 603c4be | 2012-06-04 13:55:19 +0000 | [diff] [blame] | 1 | //===-- tsan_suppressions.cc ----------------------------------------------===// |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of ThreadSanitizer (TSan), a race detector. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 14 | #include "sanitizer_common/sanitizer_common.h" |
Alexey Samsonov | 7f9c5a2 | 2012-06-05 07:46:31 +0000 | [diff] [blame] | 15 | #include "sanitizer_common/sanitizer_libc.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 16 | #include "tsan_suppressions.h" |
| 17 | #include "tsan_rtl.h" |
| 18 | #include "tsan_flags.h" |
| 19 | #include "tsan_mman.h" |
| 20 | #include "tsan_platform.h" |
| 21 | |
Dmitry Vyukov | c2b9f1c | 2013-01-24 13:50:10 +0000 | [diff] [blame] | 22 | // Can be overriden in frontend. |
| 23 | #ifndef TSAN_GO |
| 24 | extern "C" const char *WEAK __tsan_default_suppressions() { |
| 25 | return 0; |
| 26 | } |
| 27 | #endif |
| 28 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 29 | namespace __tsan { |
| 30 | |
| 31 | static Suppression *g_suppressions; |
| 32 | |
| 33 | static char *ReadFile(const char *filename) { |
| 34 | if (filename == 0 || filename[0] == 0) |
| 35 | return 0; |
Alexey Samsonov | 14c8bd7 | 2012-08-22 07:25:52 +0000 | [diff] [blame] | 36 | InternalScopedBuffer<char> tmp(4*1024); |
Dmitry Vyukov | 6422c32 | 2012-12-04 07:27:32 +0000 | [diff] [blame] | 37 | if (filename[0] == '/' || GetPwd() == 0) |
Alexey Samsonov | 1dc4cf7 | 2012-09-05 07:23:44 +0000 | [diff] [blame] | 38 | internal_snprintf(tmp.data(), tmp.size(), "%s", filename); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 39 | else |
Alexey Samsonov | 1dc4cf7 | 2012-09-05 07:23:44 +0000 | [diff] [blame] | 40 | internal_snprintf(tmp.data(), tmp.size(), "%s/%s", GetPwd(), filename); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 41 | uptr openrv = OpenFile(tmp.data(), false); |
| 42 | if (internal_iserror(openrv)) { |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 43 | Printf("ThreadSanitizer: failed to open suppressions file '%s'\n", |
Alexey Samsonov | 14c8bd7 | 2012-08-22 07:25:52 +0000 | [diff] [blame] | 44 | tmp.data()); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 45 | Die(); |
| 46 | } |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 47 | fd_t fd = openrv; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 48 | const uptr fsize = internal_filesize(fd); |
| 49 | if (fsize == (uptr)-1) { |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 50 | Printf("ThreadSanitizer: failed to stat suppressions file '%s'\n", |
Alexey Samsonov | 14c8bd7 | 2012-08-22 07:25:52 +0000 | [diff] [blame] | 51 | tmp.data()); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 52 | Die(); |
| 53 | } |
| 54 | char *buf = (char*)internal_alloc(MBlockSuppression, fsize + 1); |
| 55 | if (fsize != internal_read(fd, buf, fsize)) { |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 56 | Printf("ThreadSanitizer: failed to read suppressions file '%s'\n", |
Alexey Samsonov | 14c8bd7 | 2012-08-22 07:25:52 +0000 | [diff] [blame] | 57 | tmp.data()); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 58 | Die(); |
| 59 | } |
| 60 | internal_close(fd); |
| 61 | buf[fsize] = 0; |
| 62 | return buf; |
| 63 | } |
| 64 | |
| 65 | bool SuppressionMatch(char *templ, const char *str) { |
Dmitry Vyukov | 20f60c5 | 2012-05-31 13:18:11 +0000 | [diff] [blame] | 66 | if (str == 0 || str[0] == 0) |
| 67 | return false; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 68 | char *tpos; |
| 69 | const char *spos; |
| 70 | while (templ && templ[0]) { |
| 71 | if (templ[0] == '*') { |
| 72 | templ++; |
| 73 | continue; |
| 74 | } |
| 75 | if (str[0] == 0) |
| 76 | return false; |
| 77 | tpos = (char*)internal_strchr(templ, '*'); |
| 78 | if (tpos != 0) |
| 79 | tpos[0] = 0; |
Dmitry Vyukov | d51a1a1 | 2012-06-27 21:00:23 +0000 | [diff] [blame] | 80 | spos = internal_strstr(str, templ); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 81 | str = spos + internal_strlen(templ); |
| 82 | templ = tpos; |
| 83 | if (tpos) |
| 84 | tpos[0] = '*'; |
| 85 | if (spos == 0) |
| 86 | return false; |
| 87 | } |
| 88 | return true; |
| 89 | } |
| 90 | |
Dmitry Vyukov | c2b9f1c | 2013-01-24 13:50:10 +0000 | [diff] [blame] | 91 | Suppression *SuppressionParse(Suppression *head, const char* supp) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 92 | const char *line = supp; |
| 93 | while (line) { |
| 94 | while (line[0] == ' ' || line[0] == '\t') |
| 95 | line++; |
| 96 | const char *end = internal_strchr(line, '\n'); |
| 97 | if (end == 0) |
| 98 | end = line + internal_strlen(line); |
| 99 | if (line != end && line[0] != '#') { |
| 100 | const char *end2 = end; |
| 101 | while (line != end2 && (end2[-1] == ' ' || end2[-1] == '\t')) |
| 102 | end2--; |
| 103 | SuppressionType stype; |
Dmitry Vyukov | d51a1a1 | 2012-06-27 21:00:23 +0000 | [diff] [blame] | 104 | if (0 == internal_strncmp(line, "race:", sizeof("race:") - 1)) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 105 | stype = SuppressionRace; |
| 106 | line += sizeof("race:") - 1; |
Dmitry Vyukov | d51a1a1 | 2012-06-27 21:00:23 +0000 | [diff] [blame] | 107 | } else if (0 == internal_strncmp(line, "thread:", |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 108 | sizeof("thread:") - 1)) { |
| 109 | stype = SuppressionThread; |
| 110 | line += sizeof("thread:") - 1; |
Dmitry Vyukov | d51a1a1 | 2012-06-27 21:00:23 +0000 | [diff] [blame] | 111 | } else if (0 == internal_strncmp(line, "mutex:", |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 112 | sizeof("mutex:") - 1)) { |
| 113 | stype = SuppressionMutex; |
| 114 | line += sizeof("mutex:") - 1; |
Dmitry Vyukov | d51a1a1 | 2012-06-27 21:00:23 +0000 | [diff] [blame] | 115 | } else if (0 == internal_strncmp(line, "signal:", |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 116 | sizeof("signal:") - 1)) { |
| 117 | stype = SuppressionSignal; |
| 118 | line += sizeof("signal:") - 1; |
| 119 | } else { |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 120 | Printf("ThreadSanitizer: failed to parse suppressions file\n"); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 121 | Die(); |
| 122 | } |
| 123 | Suppression *s = (Suppression*)internal_alloc(MBlockSuppression, |
| 124 | sizeof(Suppression)); |
| 125 | s->next = head; |
| 126 | head = s; |
| 127 | s->type = stype; |
Dmitry Vyukov | 20f60c5 | 2012-05-31 13:18:11 +0000 | [diff] [blame] | 128 | s->templ = (char*)internal_alloc(MBlockSuppression, end2 - line + 1); |
Dmitry Vyukov | d51a1a1 | 2012-06-27 21:00:23 +0000 | [diff] [blame] | 129 | internal_memcpy(s->templ, line, end2 - line); |
Dmitry Vyukov | 20f60c5 | 2012-05-31 13:18:11 +0000 | [diff] [blame] | 130 | s->templ[end2 - line] = 0; |
Dmitry Vyukov | f754eb5 | 2013-03-27 17:59:57 +0000 | [diff] [blame] | 131 | s->hit_count = 0; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 132 | } |
| 133 | if (end[0] == 0) |
| 134 | break; |
| 135 | line = end + 1; |
| 136 | } |
| 137 | return head; |
| 138 | } |
| 139 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 140 | void InitializeSuppressions() { |
Dmitry Vyukov | c2b9f1c | 2013-01-24 13:50:10 +0000 | [diff] [blame] | 141 | const char *supp = ReadFile(flags()->suppressions); |
| 142 | g_suppressions = SuppressionParse(0, supp); |
| 143 | #ifndef TSAN_GO |
| 144 | supp = __tsan_default_suppressions(); |
Dmitry Vyukov | 15190a6 | 2013-02-14 11:03:45 +0000 | [diff] [blame] | 145 | g_suppressions = SuppressionParse(g_suppressions, supp); |
Dmitry Vyukov | c2b9f1c | 2013-01-24 13:50:10 +0000 | [diff] [blame] | 146 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Dmitry Vyukov | f754eb5 | 2013-03-27 17:59:57 +0000 | [diff] [blame] | 149 | uptr IsSuppressed(ReportType typ, const ReportStack *stack, Suppression **sp) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 150 | if (g_suppressions == 0 || stack == 0) |
Dmitry Vyukov | 158c6ac | 2012-10-05 15:51:32 +0000 | [diff] [blame] | 151 | return 0; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 152 | SuppressionType stype; |
| 153 | if (typ == ReportTypeRace) |
| 154 | stype = SuppressionRace; |
Dmitry Vyukov | 24cdfee | 2013-05-29 11:23:54 +0000 | [diff] [blame^] | 155 | else if (typ == ReportTypeVptrRace) |
| 156 | stype = SuppressionRace; |
| 157 | else if (typ == ReportTypeUseAfterFree) |
| 158 | return 0; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 159 | else if (typ == ReportTypeThreadLeak) |
| 160 | stype = SuppressionThread; |
| 161 | else if (typ == ReportTypeMutexDestroyLocked) |
| 162 | stype = SuppressionMutex; |
| 163 | else if (typ == ReportTypeSignalUnsafe) |
| 164 | stype = SuppressionSignal; |
Dmitry Vyukov | 24cdfee | 2013-05-29 11:23:54 +0000 | [diff] [blame^] | 165 | else if (typ == ReportTypeErrnoInSignal) |
Dmitry Vyukov | 158c6ac | 2012-10-05 15:51:32 +0000 | [diff] [blame] | 166 | return 0; |
Dmitry Vyukov | 24cdfee | 2013-05-29 11:23:54 +0000 | [diff] [blame^] | 167 | else |
| 168 | Printf("ThreadSanitizer: unknown report type %d\n", typ), Die(); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 169 | for (const ReportStack *frame = stack; frame; frame = frame->next) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 170 | for (Suppression *supp = g_suppressions; supp; supp = supp->next) { |
Dmitry Vyukov | a13749b | 2012-05-31 14:24:10 +0000 | [diff] [blame] | 171 | if (stype == supp->type && |
| 172 | (SuppressionMatch(supp->templ, frame->func) || |
Dmitry Vyukov | c2b9f1c | 2013-01-24 13:50:10 +0000 | [diff] [blame] | 173 | SuppressionMatch(supp->templ, frame->file) || |
| 174 | SuppressionMatch(supp->templ, frame->module))) { |
Dmitry Vyukov | 20f60c5 | 2012-05-31 13:18:11 +0000 | [diff] [blame] | 175 | DPrintf("ThreadSanitizer: matched suppression '%s'\n", supp->templ); |
Dmitry Vyukov | f754eb5 | 2013-03-27 17:59:57 +0000 | [diff] [blame] | 176 | supp->hit_count++; |
| 177 | *sp = supp; |
Dmitry Vyukov | 158c6ac | 2012-10-05 15:51:32 +0000 | [diff] [blame] | 178 | return frame->pc; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | } |
Dmitry Vyukov | 158c6ac | 2012-10-05 15:51:32 +0000 | [diff] [blame] | 182 | return 0; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 183 | } |
Dmitry Vyukov | f754eb5 | 2013-03-27 17:59:57 +0000 | [diff] [blame] | 184 | |
| 185 | static const char *SuppTypeStr(SuppressionType t) { |
| 186 | switch (t) { |
| 187 | case SuppressionRace: return "race"; |
| 188 | case SuppressionMutex: return "mutex"; |
| 189 | case SuppressionThread: return "thread"; |
| 190 | case SuppressionSignal: return "signal"; |
| 191 | } |
| 192 | CHECK(0); |
| 193 | return "unknown"; |
| 194 | } |
| 195 | |
| 196 | void PrintMatchedSuppressions() { |
| 197 | int hit_count = 0; |
| 198 | for (Suppression *supp = g_suppressions; supp; supp = supp->next) |
| 199 | hit_count += supp->hit_count; |
| 200 | if (hit_count == 0) |
| 201 | return; |
| 202 | Printf("ThreadSanitizer: Matched %d suppressions (pid=%d):\n", |
Peter Collingbourne | 0b694fc | 2013-05-17 16:56:53 +0000 | [diff] [blame] | 203 | hit_count, (int)internal_getpid()); |
Dmitry Vyukov | f754eb5 | 2013-03-27 17:59:57 +0000 | [diff] [blame] | 204 | for (Suppression *supp = g_suppressions; supp; supp = supp->next) { |
| 205 | if (supp->hit_count == 0) |
| 206 | continue; |
| 207 | Printf("%d %s:%s\n", supp->hit_count, SuppTypeStr(supp->type), supp->templ); |
| 208 | } |
| 209 | } |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 210 | } // namespace __tsan |