1 //===-- sanitizer_suppressions.cpp ----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Suppression parsing/matching code.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "sanitizer_suppressions.h"
14 
15 #include "sanitizer_allocator_internal.h"
16 #include "sanitizer_common.h"
17 #include "sanitizer_flags.h"
18 #include "sanitizer_file.h"
19 #include "sanitizer_libc.h"
20 #include "sanitizer_placement_new.h"
21 
22 namespace __sanitizer {
23 
SuppressionContext(const char * suppression_types[],int suppression_types_num)24 SuppressionContext::SuppressionContext(const char *suppression_types[],
25                                        int suppression_types_num)
26     : suppression_types_(suppression_types),
27       suppression_types_num_(suppression_types_num),
28       can_parse_(true) {
29   CHECK_LE(suppression_types_num_, kMaxSuppressionTypes);
30   internal_memset(has_suppression_type_, 0, suppression_types_num_);
31 }
32 
33 #if !SANITIZER_FUCHSIA
GetPathAssumingFileIsRelativeToExec(const char * file_path,char * new_file_path,uptr new_file_path_size)34 static bool GetPathAssumingFileIsRelativeToExec(const char *file_path,
35                                                 /*out*/char *new_file_path,
36                                                 uptr new_file_path_size) {
37   InternalScopedString exec(kMaxPathLength);
38   if (ReadBinaryNameCached(exec.data(), exec.size())) {
39     const char *file_name_pos = StripModuleName(exec.data());
40     uptr path_to_exec_len = file_name_pos - exec.data();
41     internal_strncat(new_file_path, exec.data(),
42                      Min(path_to_exec_len, new_file_path_size - 1));
43     internal_strncat(new_file_path, file_path,
44                      new_file_path_size - internal_strlen(new_file_path) - 1);
45     return true;
46   }
47   return false;
48 }
49 
FindFile(const char * file_path,char * new_file_path,uptr new_file_path_size)50 static const char *FindFile(const char *file_path,
51                             /*out*/char *new_file_path,
52                             uptr new_file_path_size) {
53   // If we cannot find the file, check if its location is relative to
54   // the location of the executable.
55   if (!FileExists(file_path) && !IsAbsolutePath(file_path) &&
56       GetPathAssumingFileIsRelativeToExec(file_path, new_file_path,
57                                           new_file_path_size)) {
58     return new_file_path;
59   }
60   return file_path;
61 }
62 #else
FindFile(const char * file_path,char *,uptr)63 static const char *FindFile(const char *file_path, char *, uptr) {
64   return file_path;
65 }
66 #endif
67 
ParseFromFile(const char * filename)68 void SuppressionContext::ParseFromFile(const char *filename) {
69   if (filename[0] == '\0')
70     return;
71 
72   InternalScopedString new_file_path(kMaxPathLength);
73   filename = FindFile(filename, new_file_path.data(), new_file_path.size());
74 
75   // Read the file.
76   VPrintf(1, "%s: reading suppressions file at %s\n",
77           SanitizerToolName, filename);
78   char *file_contents;
79   uptr buffer_size;
80   uptr contents_size;
81   if (!ReadFileToBuffer(filename, &file_contents, &buffer_size,
82                         &contents_size)) {
83     Printf("%s: failed to read suppressions file '%s'\n", SanitizerToolName,
84            filename);
85     Die();
86   }
87 
88   Parse(file_contents);
89 }
90 
Match(const char * str,const char * type,Suppression ** s)91 bool SuppressionContext::Match(const char *str, const char *type,
92                                Suppression **s) {
93   can_parse_ = false;
94   if (!HasSuppressionType(type))
95     return false;
96   for (uptr i = 0; i < suppressions_.size(); i++) {
97     Suppression &cur = suppressions_[i];
98     if (0 == internal_strcmp(cur.type, type) && TemplateMatch(cur.templ, str)) {
99       *s = &cur;
100       return true;
101     }
102   }
103   return false;
104 }
105 
StripPrefix(const char * str,const char * prefix)106 static const char *StripPrefix(const char *str, const char *prefix) {
107   while (*str && *str == *prefix) {
108     str++;
109     prefix++;
110   }
111   if (!*prefix)
112     return str;
113   return 0;
114 }
115 
Parse(const char * str)116 void SuppressionContext::Parse(const char *str) {
117   // Context must not mutate once Match has been called.
118   CHECK(can_parse_);
119   const char *line = str;
120   while (line) {
121     while (line[0] == ' ' || line[0] == '\t')
122       line++;
123     const char *end = internal_strchr(line, '\n');
124     if (end == 0)
125       end = line + internal_strlen(line);
126     if (line != end && line[0] != '#') {
127       const char *end2 = end;
128       while (line != end2 &&
129              (end2[-1] == ' ' || end2[-1] == '\t' || end2[-1] == '\r'))
130         end2--;
131       int type;
132       for (type = 0; type < suppression_types_num_; type++) {
133         const char *next_char = StripPrefix(line, suppression_types_[type]);
134         if (next_char && *next_char == ':') {
135           line = ++next_char;
136           break;
137         }
138       }
139       if (type == suppression_types_num_) {
140         Printf("%s: failed to parse suppressions\n", SanitizerToolName);
141         Die();
142       }
143       Suppression s;
144       s.type = suppression_types_[type];
145       s.templ = (char*)InternalAlloc(end2 - line + 1);
146       internal_memcpy(s.templ, line, end2 - line);
147       s.templ[end2 - line] = 0;
148       suppressions_.push_back(s);
149       has_suppression_type_[type] = true;
150     }
151     if (end[0] == 0)
152       break;
153     line = end + 1;
154   }
155 }
156 
SuppressionCount() const157 uptr SuppressionContext::SuppressionCount() const {
158   return suppressions_.size();
159 }
160 
HasSuppressionType(const char * type) const161 bool SuppressionContext::HasSuppressionType(const char *type) const {
162   for (int i = 0; i < suppression_types_num_; i++) {
163     if (0 == internal_strcmp(type, suppression_types_[i]))
164       return has_suppression_type_[i];
165   }
166   return false;
167 }
168 
SuppressionAt(uptr i) const169 const Suppression *SuppressionContext::SuppressionAt(uptr i) const {
170   CHECK_LT(i, suppressions_.size());
171   return &suppressions_[i];
172 }
173 
GetMatched(InternalMmapVector<Suppression * > * matched)174 void SuppressionContext::GetMatched(
175     InternalMmapVector<Suppression *> *matched) {
176   for (uptr i = 0; i < suppressions_.size(); i++)
177     if (atomic_load_relaxed(&suppressions_[i].hit_count))
178       matched->push_back(&suppressions_[i]);
179 }
180 
181 }  // namespace __sanitizer
182