2013-06-26 23:37:14 +08:00
|
|
|
//===-- sanitizer_suppressions.cc -----------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Suppression parsing/matching code shared between TSan and LSan.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "sanitizer_suppressions.h"
|
|
|
|
|
|
|
|
#include "sanitizer_allocator_internal.h"
|
|
|
|
#include "sanitizer_common.h"
|
|
|
|
#include "sanitizer_libc.h"
|
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
|
|
|
static const char *const kTypeStrings[SuppressionTypeCount] = {
|
2013-10-03 21:37:17 +08:00
|
|
|
"none", "race", "mutex", "thread", "signal", "leak", "called_from_lib"
|
2013-06-26 23:37:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
bool TemplateMatch(char *templ, const char *str) {
|
|
|
|
if (str == 0 || str[0] == 0)
|
|
|
|
return false;
|
2013-07-17 00:44:15 +08:00
|
|
|
bool start = false;
|
|
|
|
if (templ && templ[0] == '^') {
|
|
|
|
start = true;
|
|
|
|
templ++;
|
|
|
|
}
|
|
|
|
bool asterisk = false;
|
2013-06-26 23:37:14 +08:00
|
|
|
while (templ && templ[0]) {
|
|
|
|
if (templ[0] == '*') {
|
|
|
|
templ++;
|
2013-07-17 00:44:15 +08:00
|
|
|
start = false;
|
|
|
|
asterisk = true;
|
2013-06-26 23:37:14 +08:00
|
|
|
continue;
|
|
|
|
}
|
2013-07-17 00:44:15 +08:00
|
|
|
if (templ[0] == '$')
|
|
|
|
return str[0] == 0 || asterisk;
|
2013-06-26 23:37:14 +08:00
|
|
|
if (str[0] == 0)
|
|
|
|
return false;
|
2013-07-17 00:44:15 +08:00
|
|
|
char *tpos = (char*)internal_strchr(templ, '*');
|
|
|
|
char *tpos1 = (char*)internal_strchr(templ, '$');
|
|
|
|
if (tpos == 0 || (tpos1 && tpos1 < tpos))
|
|
|
|
tpos = tpos1;
|
2013-06-26 23:37:14 +08:00
|
|
|
if (tpos != 0)
|
|
|
|
tpos[0] = 0;
|
2013-07-17 00:44:15 +08:00
|
|
|
const char *str0 = str;
|
|
|
|
const char *spos = internal_strstr(str, templ);
|
2013-06-26 23:37:14 +08:00
|
|
|
str = spos + internal_strlen(templ);
|
|
|
|
templ = tpos;
|
|
|
|
if (tpos)
|
2013-07-17 00:44:15 +08:00
|
|
|
tpos[0] = tpos == tpos1 ? '$' : '*';
|
2013-06-26 23:37:14 +08:00
|
|
|
if (spos == 0)
|
|
|
|
return false;
|
2013-07-17 00:44:15 +08:00
|
|
|
if (start && spos != str0)
|
|
|
|
return false;
|
|
|
|
start = false;
|
|
|
|
asterisk = false;
|
2013-06-26 23:37:14 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SuppressionContext::Match(const char *str, SuppressionType type,
|
|
|
|
Suppression **s) {
|
|
|
|
can_parse_ = false;
|
|
|
|
uptr i;
|
|
|
|
for (i = 0; i < suppressions_.size(); i++)
|
|
|
|
if (type == suppressions_[i].type &&
|
|
|
|
TemplateMatch(suppressions_[i].templ, str))
|
|
|
|
break;
|
|
|
|
if (i == suppressions_.size()) return false;
|
|
|
|
*s = &suppressions_[i];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *StripPrefix(const char *str, const char *prefix) {
|
|
|
|
while (str && *str == *prefix) {
|
|
|
|
str++;
|
|
|
|
prefix++;
|
|
|
|
}
|
|
|
|
if (!*prefix)
|
|
|
|
return str;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SuppressionContext::Parse(const char *str) {
|
|
|
|
// Context must not mutate once Match has been called.
|
|
|
|
CHECK(can_parse_);
|
|
|
|
const char *line = str;
|
|
|
|
while (line) {
|
|
|
|
while (line[0] == ' ' || line[0] == '\t')
|
|
|
|
line++;
|
|
|
|
const char *end = internal_strchr(line, '\n');
|
|
|
|
if (end == 0)
|
|
|
|
end = line + internal_strlen(line);
|
|
|
|
if (line != end && line[0] != '#') {
|
|
|
|
const char *end2 = end;
|
|
|
|
while (line != end2 && (end2[-1] == ' ' || end2[-1] == '\t'))
|
|
|
|
end2--;
|
|
|
|
int type;
|
|
|
|
for (type = 0; type < SuppressionTypeCount; type++) {
|
|
|
|
const char *next_char = StripPrefix(line, kTypeStrings[type]);
|
|
|
|
if (next_char && *next_char == ':') {
|
|
|
|
line = ++next_char;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type == SuppressionTypeCount) {
|
2013-06-28 22:38:31 +08:00
|
|
|
Printf("%s: failed to parse suppressions\n", SanitizerToolName);
|
2013-06-26 23:37:14 +08:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
Suppression s;
|
|
|
|
s.type = static_cast<SuppressionType>(type);
|
|
|
|
s.templ = (char*)InternalAlloc(end2 - line + 1);
|
|
|
|
internal_memcpy(s.templ, line, end2 - line);
|
|
|
|
s.templ[end2 - line] = 0;
|
|
|
|
s.hit_count = 0;
|
2013-06-28 22:38:31 +08:00
|
|
|
s.weight = 0;
|
2013-06-26 23:37:14 +08:00
|
|
|
suppressions_.push_back(s);
|
|
|
|
}
|
|
|
|
if (end[0] == 0)
|
|
|
|
break;
|
|
|
|
line = end + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-03 21:37:17 +08:00
|
|
|
uptr SuppressionContext::SuppressionCount() const {
|
2013-06-26 23:37:14 +08:00
|
|
|
return suppressions_.size();
|
|
|
|
}
|
|
|
|
|
2013-10-03 21:37:17 +08:00
|
|
|
const Suppression *SuppressionContext::SuppressionAt(uptr i) const {
|
|
|
|
CHECK_LT(i, suppressions_.size());
|
|
|
|
return &suppressions_[i];
|
|
|
|
}
|
|
|
|
|
2013-06-26 23:37:14 +08:00
|
|
|
void SuppressionContext::GetMatched(
|
|
|
|
InternalMmapVector<Suppression *> *matched) {
|
|
|
|
for (uptr i = 0; i < suppressions_.size(); i++)
|
|
|
|
if (suppressions_[i].hit_count)
|
|
|
|
matched->push_back(&suppressions_[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *SuppressionTypeString(SuppressionType t) {
|
|
|
|
CHECK(t < SuppressionTypeCount);
|
|
|
|
return kTypeStrings[t];
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __sanitizer
|