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