LLVM API Documentation

SpecialCaseList.cpp
Go to the documentation of this file.
00001 //===-- SpecialCaseList.cpp - special case list for sanitizers ------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This is a utility class for instrumentation passes (like AddressSanitizer
00011 // or ThreadSanitizer) to avoid instrumenting some functions or global
00012 // variables, or to instrument some functions or global variables in a specific
00013 // way, based on a user-supplied list.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #include "llvm/Support/SpecialCaseList.h"
00018 #include "llvm/ADT/STLExtras.h"
00019 #include "llvm/ADT/SmallVector.h"
00020 #include "llvm/ADT/StringExtras.h"
00021 #include "llvm/ADT/StringSet.h"
00022 #include "llvm/Support/MemoryBuffer.h"
00023 #include "llvm/Support/Regex.h"
00024 #include "llvm/Support/raw_ostream.h"
00025 #include <string>
00026 #include <system_error>
00027 #include <utility>
00028 
00029 namespace llvm {
00030 
00031 /// Represents a set of regular expressions.  Regular expressions which are
00032 /// "literal" (i.e. no regex metacharacters) are stored in Strings, while all
00033 /// others are represented as a single pipe-separated regex in RegEx.  The
00034 /// reason for doing so is efficiency; StringSet is much faster at matching
00035 /// literal strings than Regex.
00036 struct SpecialCaseList::Entry {
00037   Entry() {}
00038   Entry(Entry &&Other)
00039       : Strings(std::move(Other.Strings)), RegEx(std::move(Other.RegEx)) {}
00040 
00041   StringSet<> Strings;
00042   std::unique_ptr<Regex> RegEx;
00043 
00044   bool match(StringRef Query) const {
00045     return Strings.count(Query) || (RegEx && RegEx->match(Query));
00046   }
00047 };
00048 
00049 SpecialCaseList::SpecialCaseList() : Entries() {}
00050 
00051 std::unique_ptr<SpecialCaseList> SpecialCaseList::create(StringRef Path,
00052                                                          std::string &Error) {
00053   if (Path.empty())
00054     return std::unique_ptr<SpecialCaseList>(new SpecialCaseList());
00055   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
00056       MemoryBuffer::getFile(Path);
00057   if (std::error_code EC = FileOrErr.getError()) {
00058     Error = (Twine("Can't open file '") + Path + "': " + EC.message()).str();
00059     return nullptr;
00060   }
00061   return create(FileOrErr.get().get(), Error);
00062 }
00063 
00064 std::unique_ptr<SpecialCaseList> SpecialCaseList::create(const MemoryBuffer *MB,
00065                                                          std::string &Error) {
00066   std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
00067   if (!SCL->parse(MB, Error))
00068     return nullptr;
00069   return SCL;
00070 }
00071 
00072 std::unique_ptr<SpecialCaseList> SpecialCaseList::createOrDie(StringRef Path) {
00073   std::string Error;
00074   if (auto SCL = create(Path, Error))
00075     return SCL;
00076   report_fatal_error(Error);
00077 }
00078 
00079 bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
00080   // Iterate through each line in the blacklist file.
00081   SmallVector<StringRef, 16> Lines;
00082   SplitString(MB->getBuffer(), Lines, "\n\r");
00083   StringMap<StringMap<std::string> > Regexps;
00084   assert(Entries.empty() &&
00085          "parse() should be called on an empty SpecialCaseList");
00086   int LineNo = 1;
00087   for (SmallVectorImpl<StringRef>::iterator I = Lines.begin(), E = Lines.end();
00088        I != E; ++I, ++LineNo) {
00089     // Ignore empty lines and lines starting with "#"
00090     if (I->empty() || I->startswith("#"))
00091       continue;
00092     // Get our prefix and unparsed regexp.
00093     std::pair<StringRef, StringRef> SplitLine = I->split(":");
00094     StringRef Prefix = SplitLine.first;
00095     if (SplitLine.second.empty()) {
00096       // Missing ':' in the line.
00097       Error = (Twine("Malformed line ") + Twine(LineNo) + ": '" +
00098                SplitLine.first + "'").str();
00099       return false;
00100     }
00101 
00102     std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
00103     std::string Regexp = SplitRegexp.first;
00104     StringRef Category = SplitRegexp.second;
00105 
00106     // Backwards compatibility.
00107     if (Prefix == "global-init") {
00108       Prefix = "global";
00109       Category = "init";
00110     } else if (Prefix == "global-init-type") {
00111       Prefix = "type";
00112       Category = "init";
00113     } else if (Prefix == "global-init-src") {
00114       Prefix = "src";
00115       Category = "init";
00116     }
00117 
00118     // See if we can store Regexp in Strings.
00119     if (Regex::isLiteralERE(Regexp)) {
00120       Entries[Prefix][Category].Strings.insert(Regexp);
00121       continue;
00122     }
00123 
00124     // Replace * with .*
00125     for (size_t pos = 0; (pos = Regexp.find("*", pos)) != std::string::npos;
00126          pos += strlen(".*")) {
00127       Regexp.replace(pos, strlen("*"), ".*");
00128     }
00129 
00130     // Check that the regexp is valid.
00131     Regex CheckRE(Regexp);
00132     std::string REError;
00133     if (!CheckRE.isValid(REError)) {
00134       Error = (Twine("Malformed regex in line ") + Twine(LineNo) + ": '" +
00135                SplitLine.second + "': " + REError).str();
00136       return false;
00137     }
00138 
00139     // Add this regexp into the proper group by its prefix.
00140     if (!Regexps[Prefix][Category].empty())
00141       Regexps[Prefix][Category] += "|";
00142     Regexps[Prefix][Category] += "^" + Regexp + "$";
00143   }
00144 
00145   // Iterate through each of the prefixes, and create Regexs for them.
00146   for (StringMap<StringMap<std::string> >::const_iterator I = Regexps.begin(),
00147                                                           E = Regexps.end();
00148        I != E; ++I) {
00149     for (StringMap<std::string>::const_iterator II = I->second.begin(),
00150                                                 IE = I->second.end();
00151          II != IE; ++II) {
00152       Entries[I->getKey()][II->getKey()].RegEx.reset(new Regex(II->getValue()));
00153     }
00154   }
00155   return true;
00156 }
00157 
00158 SpecialCaseList::~SpecialCaseList() {}
00159 
00160 bool SpecialCaseList::inSection(StringRef Section, StringRef Query,
00161                                 StringRef Category) const {
00162   StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
00163   if (I == Entries.end()) return false;
00164   StringMap<Entry>::const_iterator II = I->second.find(Category);
00165   if (II == I->second.end()) return false;
00166 
00167   return II->getValue().match(Query);
00168 }
00169 
00170 }  // namespace llvm