2017-05-11 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_suppressions.cc
blobbfdff59a35cd21740237c174e49b84dfb4404ad8
1 //===-- sanitizer_suppressions.cc -----------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // Suppression parsing/matching code.
9 //
10 //===----------------------------------------------------------------------===//
12 #include "sanitizer_suppressions.h"
14 #include "sanitizer_allocator_internal.h"
15 #include "sanitizer_common.h"
16 #include "sanitizer_flags.h"
17 #include "sanitizer_libc.h"
18 #include "sanitizer_placement_new.h"
20 namespace __sanitizer {
22 SuppressionContext::SuppressionContext(const char *suppression_types[],
23 int suppression_types_num)
24 : suppression_types_(suppression_types),
25 suppression_types_num_(suppression_types_num), suppressions_(1),
26 can_parse_(true) {
27 CHECK_LE(suppression_types_num_, kMaxSuppressionTypes);
28 internal_memset(has_suppression_type_, 0, suppression_types_num_);
31 static bool GetPathAssumingFileIsRelativeToExec(const char *file_path,
32 /*out*/char *new_file_path,
33 uptr new_file_path_size) {
34 InternalScopedString exec(kMaxPathLength);
35 if (ReadBinaryNameCached(exec.data(), exec.size())) {
36 const char *file_name_pos = StripModuleName(exec.data());
37 uptr path_to_exec_len = file_name_pos - exec.data();
38 internal_strncat(new_file_path, exec.data(),
39 Min(path_to_exec_len, new_file_path_size - 1));
40 internal_strncat(new_file_path, file_path,
41 new_file_path_size - internal_strlen(new_file_path) - 1);
42 return true;
44 return false;
47 void SuppressionContext::ParseFromFile(const char *filename) {
48 if (filename[0] == '\0')
49 return;
51 // If we cannot find the file, check if its location is relative to
52 // the location of the executable.
53 InternalScopedString new_file_path(kMaxPathLength);
54 if (!FileExists(filename) && !IsAbsolutePath(filename) &&
55 GetPathAssumingFileIsRelativeToExec(filename, new_file_path.data(),
56 new_file_path.size())) {
57 filename = new_file_path.data();
60 // Read the file.
61 VPrintf(1, "%s: reading suppressions file at %s\n",
62 SanitizerToolName, filename);
63 char *file_contents;
64 uptr buffer_size;
65 uptr contents_size;
66 if (!ReadFileToBuffer(filename, &file_contents, &buffer_size,
67 &contents_size)) {
68 Printf("%s: failed to read suppressions file '%s'\n", SanitizerToolName,
69 filename);
70 Die();
73 Parse(file_contents);
76 bool SuppressionContext::Match(const char *str, const char *type,
77 Suppression **s) {
78 can_parse_ = false;
79 if (!HasSuppressionType(type))
80 return false;
81 for (uptr i = 0; i < suppressions_.size(); i++) {
82 Suppression &cur = suppressions_[i];
83 if (0 == internal_strcmp(cur.type, type) && TemplateMatch(cur.templ, str)) {
84 *s = &cur;
85 return true;
88 return false;
91 static const char *StripPrefix(const char *str, const char *prefix) {
92 while (str && *str == *prefix) {
93 str++;
94 prefix++;
96 if (!*prefix)
97 return str;
98 return 0;
101 void SuppressionContext::Parse(const char *str) {
102 // Context must not mutate once Match has been called.
103 CHECK(can_parse_);
104 const char *line = str;
105 while (line) {
106 while (line[0] == ' ' || line[0] == '\t')
107 line++;
108 const char *end = internal_strchr(line, '\n');
109 if (end == 0)
110 end = line + internal_strlen(line);
111 if (line != end && line[0] != '#') {
112 const char *end2 = end;
113 while (line != end2 &&
114 (end2[-1] == ' ' || end2[-1] == '\t' || end2[-1] == '\r'))
115 end2--;
116 int type;
117 for (type = 0; type < suppression_types_num_; type++) {
118 const char *next_char = StripPrefix(line, suppression_types_[type]);
119 if (next_char && *next_char == ':') {
120 line = ++next_char;
121 break;
124 if (type == suppression_types_num_) {
125 Printf("%s: failed to parse suppressions\n", SanitizerToolName);
126 Die();
128 Suppression s;
129 s.type = suppression_types_[type];
130 s.templ = (char*)InternalAlloc(end2 - line + 1);
131 internal_memcpy(s.templ, line, end2 - line);
132 s.templ[end2 - line] = 0;
133 suppressions_.push_back(s);
134 has_suppression_type_[type] = true;
136 if (end[0] == 0)
137 break;
138 line = end + 1;
142 uptr SuppressionContext::SuppressionCount() const {
143 return suppressions_.size();
146 bool SuppressionContext::HasSuppressionType(const char *type) const {
147 for (int i = 0; i < suppression_types_num_; i++) {
148 if (0 == internal_strcmp(type, suppression_types_[i]))
149 return has_suppression_type_[i];
151 return false;
154 const Suppression *SuppressionContext::SuppressionAt(uptr i) const {
155 CHECK_LT(i, suppressions_.size());
156 return &suppressions_[i];
159 void SuppressionContext::GetMatched(
160 InternalMmapVector<Suppression *> *matched) {
161 for (uptr i = 0; i < suppressions_.size(); i++)
162 if (atomic_load_relaxed(&suppressions_[i].hit_count))
163 matched->push_back(&suppressions_[i]);
166 } // namespace __sanitizer