1//===--- SanitizerArgs.h - Arguments for sanitizer tools -------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9#ifndef CLANG_LIB_DRIVER_SANITIZERARGS_H_ 10#define CLANG_LIB_DRIVER_SANITIZERARGS_H_ 11 12#include <string> 13 14#include "llvm/Option/Arg.h" 15#include "llvm/Option/ArgList.h" 16 17namespace clang { 18namespace driver { 19 20class Driver; 21class ToolChain; 22 23class SanitizerArgs { 24 /// Assign ordinals to sanitizer flags. We'll use the ordinal values as 25 /// bit positions within \c Kind. 26 enum SanitizeOrdinal { 27#define SANITIZER(NAME, ID) SO_##ID, 28#define SANITIZER_GROUP(NAME, ID, ALIAS) SO_##ID##Group, 29#include "clang/Basic/Sanitizers.def" 30 SO_Count 31 }; 32 33 /// Bugs to catch at runtime. 34 enum SanitizeKind { 35#define SANITIZER(NAME, ID) ID = 1 << SO_##ID, 36#define SANITIZER_GROUP(NAME, ID, ALIAS) \ 37 ID = ALIAS, ID##Group = 1 << SO_##ID##Group, 38#include "clang/Basic/Sanitizers.def" 39 NeedsAsanRt = Address, 40 NeedsTsanRt = Thread, 41 NeedsMsanRt = Memory, 42 NeedsDfsanRt = DataFlow, 43 NeedsLeakDetection = Leak, 44 NeedsUbsanRt = Undefined | Integer, 45 NotAllowedWithTrap = Vptr, 46 HasZeroBaseShadow = Thread | Memory | DataFlow 47 }; 48 unsigned Kind; 49 50 std::string BlacklistFile; 51 bool MsanTrackOrigins; 52 bool AsanZeroBaseShadow; 53 bool UbsanTrapOnError; 54 55 public: 56 SanitizerArgs(); 57 /// Parses the sanitizer arguments from an argument list. 58 SanitizerArgs(const ToolChain &TC, const llvm::opt::ArgList &Args); 59 60 bool needsAsanRt() const { return Kind & NeedsAsanRt; } 61 bool needsTsanRt() const { return Kind & NeedsTsanRt; } 62 bool needsMsanRt() const { return Kind & NeedsMsanRt; } 63 bool needsLeakDetection() const { return Kind & NeedsLeakDetection; } 64 bool needsLsanRt() const { 65 return needsLeakDetection() && !needsAsanRt(); 66 } 67 bool needsUbsanRt() const { 68 return !UbsanTrapOnError && (Kind & NeedsUbsanRt); 69 } 70 bool needsDfsanRt() const { return Kind & NeedsDfsanRt; } 71 72 bool sanitizesVptr() const { return Kind & Vptr; } 73 bool notAllowedWithTrap() const { return Kind & NotAllowedWithTrap; } 74 bool hasZeroBaseShadow() const { 75 return (Kind & HasZeroBaseShadow) || AsanZeroBaseShadow; 76 } 77 void addArgs(const llvm::opt::ArgList &Args, 78 llvm::opt::ArgStringList &CmdArgs) const; 79 80 private: 81 void clear(); 82 83 /// Parse a single value from a -fsanitize= or -fno-sanitize= value list. 84 /// Returns OR of members of the \c SanitizeKind enumeration, or \c 0 85 /// if \p Value is not known. 86 static unsigned parse(const char *Value); 87 88 /// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any 89 /// invalid components. 90 static unsigned parse(const Driver &D, const llvm::opt::Arg *A, 91 bool DiagnoseErrors); 92 93 /// Parse a single flag of the form -f[no]sanitize=, or 94 /// -f*-sanitizer. Sets the masks defining required change of Kind value. 95 /// Returns true if the flag was parsed successfully. 96 static bool parse(const Driver &D, const llvm::opt::ArgList &Args, 97 const llvm::opt::Arg *A, unsigned &Add, unsigned &Remove, 98 bool DiagnoseErrors); 99 100 /// Produce an argument string from ArgList \p Args, which shows how it 101 /// provides a sanitizer kind in \p Mask. For example, the argument list 102 /// "-fsanitize=thread,vptr -faddress-sanitizer" with mask \c NeedsUbsanRt 103 /// would produce "-fsanitize=vptr". 104 static std::string lastArgumentForKind(const Driver &D, 105 const llvm::opt::ArgList &Args, 106 unsigned Kind); 107 108 /// Produce an argument string from argument \p A, which shows how it provides 109 /// a value in \p Mask. For instance, the argument 110 /// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce 111 /// "-fsanitize=alignment". 112 static std::string describeSanitizeArg(const llvm::opt::ArgList &Args, 113 const llvm::opt::Arg *A, 114 unsigned Mask); 115 116 static bool getDefaultBlacklistForKind(const Driver &D, unsigned Kind, 117 std::string &BLPath); 118 119 /// Return the smallest superset of sanitizer set \p Kinds such that each 120 /// member of each group whose flag is set in \p Kinds has its flag set in the 121 /// result. 122 static unsigned expandGroups(unsigned Kinds); 123 124 /// Return the subset of \p Kinds supported by toolchain \p TC. If 125 /// \p DiagnoseErrors is true, produce an error diagnostic for each sanitizer 126 /// removed from \p Kinds. 127 static unsigned filterUnsupportedKinds(const ToolChain &TC, unsigned Kinds, 128 const llvm::opt::ArgList &Args, 129 const llvm::opt::Arg *A, 130 bool DiagnoseErrors, 131 unsigned &DiagnosedKinds); 132 133 /// The flags in \p Mask are unsupported by \p TC. If present in \p Kinds, 134 /// remove them and produce an error diagnostic referring to \p A if 135 /// \p DiagnoseErrors is true. 136 static void filterUnsupportedMask(const ToolChain &TC, unsigned &Kinds, 137 unsigned Mask, 138 const llvm::opt::ArgList &Args, 139 const llvm::opt::Arg *A, 140 bool DiagnoseErrors, 141 unsigned &DiagnosedKinds); 142}; 143 144} // namespace driver 145} // namespace clang 146 147#endif // CLANG_LIB_DRIVER_SANITIZERARGS_H_ 148