* gcc-interface/trans.c (Subprogram_Body_to_gnu): Initialize locus.
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_suppressions.cc
blob4095a0942a327b949243af448b5f54e331e69dd5
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_file.h"
18 #include "sanitizer_libc.h"
19 #include "sanitizer_placement_new.h"
21 namespace __sanitizer {
23 SuppressionContext::SuppressionContext(const char *suppression_types[],
24 int suppression_types_num)
25 : suppression_types_(suppression_types),
26 suppression_types_num_(suppression_types_num), suppressions_(1),
27 can_parse_(true) {
28 CHECK_LE(suppression_types_num_, kMaxSuppressionTypes);
29 internal_memset(has_suppression_type_, 0, suppression_types_num_);
32 static bool GetPathAssumingFileIsRelativeToExec(const char *file_path,
33 /*out*/char *new_file_path,
34 uptr new_file_path_size) {
35 InternalScopedString exec(kMaxPathLength);
36 if (ReadBinaryNameCached(exec.data(), exec.size())) {
37 const char *file_name_pos = StripModuleName(exec.data());
38 uptr path_to_exec_len = file_name_pos - exec.data();
39 internal_strncat(new_file_path, exec.data(),
40 Min(path_to_exec_len, new_file_path_size - 1));
41 internal_strncat(new_file_path, file_path,
42 new_file_path_size - internal_strlen(new_file_path) - 1);
43 return true;
45 return false;
48 void SuppressionContext::ParseFromFile(const char *filename) {
49 if (filename[0] == '\0')
50 return;
52 #if !SANITIZER_FUCHSIA
53 // If we cannot find the file, check if its location is relative to
54 // the location of the executable.
55 InternalScopedString new_file_path(kMaxPathLength);
56 if (!FileExists(filename) && !IsAbsolutePath(filename) &&
57 GetPathAssumingFileIsRelativeToExec(filename, new_file_path.data(),
58 new_file_path.size())) {
59 filename = new_file_path.data();
61 #endif // !SANITIZER_FUCHSIA
63 // Read the file.
64 VPrintf(1, "%s: reading suppressions file at %s\n",
65 SanitizerToolName, filename);
66 char *file_contents;
67 uptr buffer_size;
68 uptr contents_size;
69 if (!ReadFileToBuffer(filename, &file_contents, &buffer_size,
70 &contents_size)) {
71 Printf("%s: failed to read suppressions file '%s'\n", SanitizerToolName,
72 filename);
73 Die();
76 Parse(file_contents);
79 bool SuppressionContext::Match(const char *str, const char *type,
80 Suppression **s) {
81 can_parse_ = false;
82 if (!HasSuppressionType(type))
83 return false;
84 for (uptr i = 0; i < suppressions_.size(); i++) {
85 Suppression &cur = suppressions_[i];
86 if (0 == internal_strcmp(cur.type, type) && TemplateMatch(cur.templ, str)) {
87 *s = &cur;
88 return true;
91 return false;
94 static const char *StripPrefix(const char *str, const char *prefix) {
95 while (str && *str == *prefix) {
96 str++;
97 prefix++;
99 if (!*prefix)
100 return str;
101 return 0;
104 void SuppressionContext::Parse(const char *str) {
105 // Context must not mutate once Match has been called.
106 CHECK(can_parse_);
107 const char *line = str;
108 while (line) {
109 while (line[0] == ' ' || line[0] == '\t')
110 line++;
111 const char *end = internal_strchr(line, '\n');
112 if (end == 0)
113 end = line + internal_strlen(line);
114 if (line != end && line[0] != '#') {
115 const char *end2 = end;
116 while (line != end2 &&
117 (end2[-1] == ' ' || end2[-1] == '\t' || end2[-1] == '\r'))
118 end2--;
119 int type;
120 for (type = 0; type < suppression_types_num_; type++) {
121 const char *next_char = StripPrefix(line, suppression_types_[type]);
122 if (next_char && *next_char == ':') {
123 line = ++next_char;
124 break;
127 if (type == suppression_types_num_) {
128 Printf("%s: failed to parse suppressions\n", SanitizerToolName);
129 Die();
131 Suppression s;
132 s.type = suppression_types_[type];
133 s.templ = (char*)InternalAlloc(end2 - line + 1);
134 internal_memcpy(s.templ, line, end2 - line);
135 s.templ[end2 - line] = 0;
136 suppressions_.push_back(s);
137 has_suppression_type_[type] = true;
139 if (end[0] == 0)
140 break;
141 line = end + 1;
145 uptr SuppressionContext::SuppressionCount() const {
146 return suppressions_.size();
149 bool SuppressionContext::HasSuppressionType(const char *type) const {
150 for (int i = 0; i < suppression_types_num_; i++) {
151 if (0 == internal_strcmp(type, suppression_types_[i]))
152 return has_suppression_type_[i];
154 return false;
157 const Suppression *SuppressionContext::SuppressionAt(uptr i) const {
158 CHECK_LT(i, suppressions_.size());
159 return &suppressions_[i];
162 void SuppressionContext::GetMatched(
163 InternalMmapVector<Suppression *> *matched) {
164 for (uptr i = 0; i < suppressions_.size(); i++)
165 if (atomic_load_relaxed(&suppressions_[i].hit_count))
166 matched->push_back(&suppressions_[i]);
169 } // namespace __sanitizer