1 //===-- sanitizer_suppressions.cc -----------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Suppression parsing/matching code.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_suppressions.h"
16 #include "sanitizer_allocator_internal.h"
17 #include "sanitizer_common.h"
18 #include "sanitizer_flags.h"
19 #include "sanitizer_libc.h"
20 #include "sanitizer_placement_new.h"
22 namespace __sanitizer {
24 SuppressionContext::SuppressionContext(const char *suppression_types[],
25 int suppression_types_num)
26 : suppression_types_(suppression_types),
27 suppression_types_num_(suppression_types_num), suppressions_(1),
29 CHECK_LE(suppression_types_num_, kMaxSuppressionTypes);
30 internal_memset(has_suppression_type_, 0, suppression_types_num_);
33 static bool GetPathAssumingFileIsRelativeToExec(const char *file_path,
34 /*out*/char *new_file_path,
35 uptr new_file_path_size) {
36 InternalScopedString exec(kMaxPathLength);
37 if (ReadBinaryNameCached(exec.data(), exec.size())) {
38 const char *file_name_pos = StripModuleName(exec.data());
39 uptr path_to_exec_len = file_name_pos - exec.data();
40 internal_strncat(new_file_path, exec.data(),
41 Min(path_to_exec_len, new_file_path_size - 1));
42 internal_strncat(new_file_path, file_path,
43 new_file_path_size - internal_strlen(new_file_path) - 1);
49 void SuppressionContext::ParseFromFile(const char *filename) {
50 if (filename[0] == '\0')
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();
65 const uptr max_len = 1 << 26;
67 ReadFileToBuffer(filename, &file_contents, &buffer_size, max_len);
68 VPrintf(1, "%s: reading suppressions file at %s\n",
69 SanitizerToolName, filename);
71 if (contents_size == 0) {
72 Printf("%s: failed to read suppressions file '%s'\n", SanitizerToolName,
80 bool SuppressionContext::Match(const char *str, const char *type,
83 if (!HasSuppressionType(type))
85 for (uptr i = 0; i < suppressions_.size(); i++) {
86 Suppression &cur = suppressions_[i];
87 if (0 == internal_strcmp(cur.type, type) && TemplateMatch(cur.templ, str)) {
95 static const char *StripPrefix(const char *str, const char *prefix) {
96 while (str && *str == *prefix) {
105 void SuppressionContext::Parse(const char *str) {
106 // Context must not mutate once Match has been called.
108 const char *line = str;
110 while (line[0] == ' ' || line[0] == '\t')
112 const char *end = internal_strchr(line, '\n');
114 end = line + internal_strlen(line);
115 if (line != end && line[0] != '#') {
116 const char *end2 = end;
117 while (line != end2 && (end2[-1] == ' ' || end2[-1] == '\t'))
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 == ':') {
127 if (type == suppression_types_num_) {
128 Printf("%s: failed to parse suppressions\n", SanitizerToolName);
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;
138 suppressions_.push_back(s);
139 has_suppression_type_[type] = true;
147 uptr SuppressionContext::SuppressionCount() const {
148 return suppressions_.size();
151 bool SuppressionContext::HasSuppressionType(const char *type) const {
152 for (int i = 0; i < suppression_types_num_; i++) {
153 if (0 == internal_strcmp(type, suppression_types_[i]))
154 return has_suppression_type_[i];
159 const Suppression *SuppressionContext::SuppressionAt(uptr i) const {
160 CHECK_LT(i, suppressions_.size());
161 return &suppressions_[i];
164 void SuppressionContext::GetMatched(
165 InternalMmapVector<Suppression *> *matched) {
166 for (uptr i = 0; i < suppressions_.size(); i++)
167 if (suppressions_[i].hit_count)
168 matched->push_back(&suppressions_[i]);
171 } // namespace __sanitizer