1 //===-- SpecialCaseList.cpp - special case list for sanitizers ------------===//
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 // This is a utility class for instrumentation passes (like AddressSanitizer
10 // or ThreadSanitizer) to avoid instrumenting some functions or global
11 // variables, or to instrument some functions or global variables in a specific
12 // way, based on a user-supplied list.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/Support/SpecialCaseList.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/Regex.h"
20 #include "llvm/Support/VirtualFileSystem.h"
21 #include <string>
22 #include <system_error>
23 #include <utility>
24 
25 #include <stdio.h>
26 namespace llvm {
27 
28 bool SpecialCaseList::Matcher::insert(std::string Regexp,
29                                       unsigned LineNumber,
30                                       std::string &REError) {
31   if (Regexp.empty()) {
32     REError = "Supplied regexp was blank";
33     return false;
34   }
35 
36   if (Regex::isLiteralERE(Regexp)) {
37     Strings[Regexp] = LineNumber;
38     return true;
39   }
40   Trigrams.insert(Regexp);
41 
42   // Replace * with .*
43   for (size_t pos = 0; (pos = Regexp.find('*', pos)) != std::string::npos;
44        pos += strlen(".*")) {
45     Regexp.replace(pos, strlen("*"), ".*");
46   }
47 
48   Regexp = (Twine("^(") + StringRef(Regexp) + ")$").str();
49 
50   // Check that the regexp is valid.
51   Regex CheckRE(Regexp);
52   if (!CheckRE.isValid(REError))
53     return false;
54 
55   RegExes.emplace_back(
56       std::make_pair(std::make_unique<Regex>(std::move(CheckRE)), LineNumber));
57   return true;
58 }
59 
60 unsigned SpecialCaseList::Matcher::match(StringRef Query) const {
61   auto It = Strings.find(Query);
62   if (It != Strings.end())
63     return It->second;
64   if (Trigrams.isDefinitelyOut(Query))
65     return false;
66   for (const auto &RegExKV : RegExes)
67     if (RegExKV.first->match(Query))
68       return RegExKV.second;
69   return 0;
70 }
71 
72 std::unique_ptr<SpecialCaseList>
73 SpecialCaseList::create(const std::vector<std::string> &Paths,
74                         llvm::vfs::FileSystem &FS, std::string &Error) {
75   std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
76   if (SCL->createInternal(Paths, FS, Error))
77     return SCL;
78   return nullptr;
79 }
80 
81 std::unique_ptr<SpecialCaseList> SpecialCaseList::create(const MemoryBuffer *MB,
82                                                          std::string &Error) {
83   std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
84   if (SCL->createInternal(MB, Error))
85     return SCL;
86   return nullptr;
87 }
88 
89 std::unique_ptr<SpecialCaseList>
90 SpecialCaseList::createOrDie(const std::vector<std::string> &Paths,
91                              llvm::vfs::FileSystem &FS) {
92   std::string Error;
93   if (auto SCL = create(Paths, FS, Error))
94     return SCL;
95   report_fatal_error(Twine(Error));
96 }
97 
98 bool SpecialCaseList::createInternal(const std::vector<std::string> &Paths,
99                                      vfs::FileSystem &VFS, std::string &Error) {
100   StringMap<size_t> Sections;
101   for (const auto &Path : Paths) {
102     ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
103         VFS.getBufferForFile(Path);
104     if (std::error_code EC = FileOrErr.getError()) {
105       Error = (Twine("can't open file '") + Path + "': " + EC.message()).str();
106       return false;
107     }
108     std::string ParseError;
109     if (!parse(FileOrErr.get().get(), Sections, ParseError)) {
110       Error = (Twine("error parsing file '") + Path + "': " + ParseError).str();
111       return false;
112     }
113   }
114   return true;
115 }
116 
117 bool SpecialCaseList::createInternal(const MemoryBuffer *MB,
118                                      std::string &Error) {
119   StringMap<size_t> Sections;
120   if (!parse(MB, Sections, Error))
121     return false;
122   return true;
123 }
124 
125 bool SpecialCaseList::parse(const MemoryBuffer *MB,
126                             StringMap<size_t> &SectionsMap,
127                             std::string &Error) {
128   // Iterate through each line in the exclusion list file.
129   SmallVector<StringRef, 16> Lines;
130   MB->getBuffer().split(Lines, '\n');
131 
132   unsigned LineNo = 1;
133   StringRef Section = "*";
134 
135   for (auto I = Lines.begin(), E = Lines.end(); I != E; ++I, ++LineNo) {
136     *I = I->trim();
137     // Ignore empty lines and lines starting with "#"
138     if (I->empty() || I->startswith("#"))
139       continue;
140 
141     // Save section names
142     if (I->startswith("[")) {
143       if (!I->endswith("]")) {
144         Error = (Twine("malformed section header on line ") + Twine(LineNo) +
145                  ": " + *I).str();
146         return false;
147       }
148 
149       Section = I->slice(1, I->size() - 1);
150 
151       std::string REError;
152       Regex CheckRE(Section);
153       if (!CheckRE.isValid(REError)) {
154         Error =
155             (Twine("malformed regex for section ") + Section + ": '" + REError)
156                 .str();
157         return false;
158       }
159 
160       continue;
161     }
162 
163     // Get our prefix and unparsed regexp.
164     std::pair<StringRef, StringRef> SplitLine = I->split(":");
165     StringRef Prefix = SplitLine.first;
166     if (SplitLine.second.empty()) {
167       // Missing ':' in the line.
168       Error = (Twine("malformed line ") + Twine(LineNo) + ": '" +
169                SplitLine.first + "'").str();
170       return false;
171     }
172 
173     std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
174     std::string Regexp = std::string(SplitRegexp.first);
175     StringRef Category = SplitRegexp.second;
176 
177     // Create this section if it has not been seen before.
178     if (SectionsMap.find(Section) == SectionsMap.end()) {
179       std::unique_ptr<Matcher> M = std::make_unique<Matcher>();
180       std::string REError;
181       if (!M->insert(std::string(Section), LineNo, REError)) {
182         Error = (Twine("malformed section ") + Section + ": '" + REError).str();
183         return false;
184       }
185 
186       SectionsMap[Section] = Sections.size();
187       Sections.emplace_back(std::move(M));
188     }
189 
190     auto &Entry = Sections[SectionsMap[Section]].Entries[Prefix][Category];
191     std::string REError;
192     if (!Entry.insert(std::move(Regexp), LineNo, REError)) {
193       Error = (Twine("malformed regex in line ") + Twine(LineNo) + ": '" +
194                SplitLine.second + "': " + REError).str();
195       return false;
196     }
197   }
198   return true;
199 }
200 
201 SpecialCaseList::~SpecialCaseList() {}
202 
203 bool SpecialCaseList::inSection(StringRef Section, StringRef Prefix,
204                                 StringRef Query, StringRef Category) const {
205   return inSectionBlame(Section, Prefix, Query, Category);
206 }
207 
208 unsigned SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
209                                          StringRef Query,
210                                          StringRef Category) const {
211   for (const auto &SectionIter : Sections)
212     if (SectionIter.SectionMatcher->match(Section)) {
213       unsigned Blame =
214           inSectionBlame(SectionIter.Entries, Prefix, Query, Category);
215       if (Blame)
216         return Blame;
217     }
218   return 0;
219 }
220 
221 unsigned SpecialCaseList::inSectionBlame(const SectionEntries &Entries,
222                                          StringRef Prefix, StringRef Query,
223                                          StringRef Category) const {
224   SectionEntries::const_iterator I = Entries.find(Prefix);
225   if (I == Entries.end()) return 0;
226   StringMap<Matcher>::const_iterator II = I->second.find(Category);
227   if (II == I->second.end()) return 0;
228 
229   return II->getValue().match(Query);
230 }
231 
232 }  // namespace llvm
233