Alexey Samsonov | bb1071c | 2012-11-06 15:09:03 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 12 | #include "clang/Driver/Arg.h" |
Alexey Samsonov | bb1071c | 2012-11-06 15:09:03 +0000 | [diff] [blame] | 13 | #include "clang/Driver/ArgList.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/Driver/Driver.h" |
| 15 | #include "clang/Driver/DriverDiagnostic.h" |
| 16 | #include "clang/Driver/Options.h" |
| 17 | #include "llvm/ADT/StringSwitch.h" |
Alexey Samsonov | 24697b0 | 2013-02-19 11:25:29 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Path.h" |
Alexey Samsonov | bb1071c | 2012-11-06 15:09:03 +0000 | [diff] [blame] | 19 | |
| 20 | namespace clang { |
| 21 | namespace driver { |
| 22 | |
| 23 | class 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 | #include "clang/Basic/Sanitizers.def" |
| 29 | SO_Count |
| 30 | }; |
| 31 | |
| 32 | /// Bugs to catch at runtime. |
| 33 | enum SanitizeKind { |
| 34 | #define SANITIZER(NAME, ID) ID = 1 << SO_##ID, |
| 35 | #define SANITIZER_GROUP(NAME, ID, ALIAS) ID = ALIAS, |
| 36 | #include "clang/Basic/Sanitizers.def" |
Alexey Samsonov | 3e335c1 | 2013-01-28 07:20:44 +0000 | [diff] [blame] | 37 | NeedsAsanRt = Address, |
Alexey Samsonov | bb1071c | 2012-11-06 15:09:03 +0000 | [diff] [blame] | 38 | NeedsTsanRt = Thread, |
Evgeniy Stepanov | 09ccf39 | 2012-12-03 13:20:43 +0000 | [diff] [blame] | 39 | NeedsMsanRt = Memory, |
Richard Smith | a0a628f | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 40 | NeedsUbsanRt = Undefined | Integer, |
Chad Rosier | 78d85b1 | 2013-01-29 23:31:22 +0000 | [diff] [blame] | 41 | NotAllowedWithTrap = Vptr |
Alexey Samsonov | bb1071c | 2012-11-06 15:09:03 +0000 | [diff] [blame] | 42 | }; |
| 43 | unsigned Kind; |
Alexey Samsonov | 91ecfa6 | 2012-12-03 19:12:58 +0000 | [diff] [blame] | 44 | std::string BlacklistFile; |
Evgeniy Stepanov | 34ef11b | 2012-12-24 08:42:34 +0000 | [diff] [blame] | 45 | bool MsanTrackOrigins; |
Alexey Samsonov | 4bdc604 | 2013-01-20 13:12:12 +0000 | [diff] [blame] | 46 | bool AsanZeroBaseShadow; |
Chad Rosier | 78d85b1 | 2013-01-29 23:31:22 +0000 | [diff] [blame] | 47 | bool UbsanTrapOnError; |
Alexey Samsonov | bb1071c | 2012-11-06 15:09:03 +0000 | [diff] [blame] | 48 | |
| 49 | public: |
Alexey Samsonov | 4bdc604 | 2013-01-20 13:12:12 +0000 | [diff] [blame] | 50 | SanitizerArgs() : Kind(0), BlacklistFile(""), MsanTrackOrigins(false), |
Chad Rosier | 78d85b1 | 2013-01-29 23:31:22 +0000 | [diff] [blame] | 51 | AsanZeroBaseShadow(false), UbsanTrapOnError(false) {} |
Alexey Samsonov | bb1071c | 2012-11-06 15:09:03 +0000 | [diff] [blame] | 52 | /// Parses the sanitizer arguments from an argument list. |
| 53 | SanitizerArgs(const Driver &D, const ArgList &Args); |
| 54 | |
| 55 | bool needsAsanRt() const { return Kind & NeedsAsanRt; } |
| 56 | bool needsTsanRt() const { return Kind & NeedsTsanRt; } |
Evgeniy Stepanov | 09ccf39 | 2012-12-03 13:20:43 +0000 | [diff] [blame] | 57 | bool needsMsanRt() const { return Kind & NeedsMsanRt; } |
Chad Rosier | 78d85b1 | 2013-01-29 23:31:22 +0000 | [diff] [blame] | 58 | bool needsUbsanRt() const { |
| 59 | if (UbsanTrapOnError) |
| 60 | return false; |
| 61 | return Kind & NeedsUbsanRt; |
| 62 | } |
Alexey Samsonov | bb1071c | 2012-11-06 15:09:03 +0000 | [diff] [blame] | 63 | |
| 64 | bool sanitizesVptr() const { return Kind & Vptr; } |
Chad Rosier | 78d85b1 | 2013-01-29 23:31:22 +0000 | [diff] [blame] | 65 | bool notAllowedWithTrap() const { return Kind & NotAllowedWithTrap; } |
Alexey Samsonov | 4d1a6e4 | 2012-11-29 22:36:21 +0000 | [diff] [blame] | 66 | |
Alexey Samsonov | bb1071c | 2012-11-06 15:09:03 +0000 | [diff] [blame] | 67 | void addArgs(const ArgList &Args, ArgStringList &CmdArgs) const { |
| 68 | if (!Kind) |
| 69 | return; |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 70 | SmallString<256> SanitizeOpt("-fsanitize="); |
Alexey Samsonov | bb1071c | 2012-11-06 15:09:03 +0000 | [diff] [blame] | 71 | #define SANITIZER(NAME, ID) \ |
| 72 | if (Kind & ID) \ |
| 73 | SanitizeOpt += NAME ","; |
| 74 | #include "clang/Basic/Sanitizers.def" |
| 75 | SanitizeOpt.pop_back(); |
| 76 | CmdArgs.push_back(Args.MakeArgString(SanitizeOpt)); |
Alexey Samsonov | 91ecfa6 | 2012-12-03 19:12:58 +0000 | [diff] [blame] | 77 | if (!BlacklistFile.empty()) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 78 | SmallString<64> BlacklistOpt("-fsanitize-blacklist="); |
Alexey Samsonov | 91ecfa6 | 2012-12-03 19:12:58 +0000 | [diff] [blame] | 79 | BlacklistOpt += BlacklistFile; |
| 80 | CmdArgs.push_back(Args.MakeArgString(BlacklistOpt)); |
| 81 | } |
Evgeniy Stepanov | 34ef11b | 2012-12-24 08:42:34 +0000 | [diff] [blame] | 82 | |
| 83 | if (MsanTrackOrigins) |
| 84 | CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins")); |
Alexey Samsonov | 4bdc604 | 2013-01-20 13:12:12 +0000 | [diff] [blame] | 85 | |
| 86 | if (AsanZeroBaseShadow) |
| 87 | CmdArgs.push_back(Args.MakeArgString( |
| 88 | "-fsanitize-address-zero-base-shadow")); |
Alexey Samsonov | bb1071c | 2012-11-06 15:09:03 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | private: |
| 92 | /// Parse a single value from a -fsanitize= or -fno-sanitize= value list. |
| 93 | /// Returns a member of the \c SanitizeKind enumeration, or \c 0 if \p Value |
| 94 | /// is not known. |
| 95 | static unsigned parse(const char *Value) { |
| 96 | return llvm::StringSwitch<SanitizeKind>(Value) |
| 97 | #define SANITIZER(NAME, ID) .Case(NAME, ID) |
| 98 | #define SANITIZER_GROUP(NAME, ID, ALIAS) .Case(NAME, ID) |
| 99 | #include "clang/Basic/Sanitizers.def" |
| 100 | .Default(SanitizeKind()); |
| 101 | } |
| 102 | |
| 103 | /// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any |
| 104 | /// invalid components. |
Alexey Samsonov | 3325b16 | 2012-11-28 17:34:24 +0000 | [diff] [blame] | 105 | static unsigned parse(const Driver &D, const Arg *A, bool DiagnoseErrors) { |
Alexey Samsonov | bb1071c | 2012-11-06 15:09:03 +0000 | [diff] [blame] | 106 | unsigned Kind = 0; |
| 107 | for (unsigned I = 0, N = A->getNumValues(); I != N; ++I) { |
| 108 | if (unsigned K = parse(A->getValue(I))) |
| 109 | Kind |= K; |
Alexey Samsonov | 3325b16 | 2012-11-28 17:34:24 +0000 | [diff] [blame] | 110 | else if (DiagnoseErrors) |
Alexey Samsonov | bb1071c | 2012-11-06 15:09:03 +0000 | [diff] [blame] | 111 | D.Diag(diag::err_drv_unsupported_option_argument) |
| 112 | << A->getOption().getName() << A->getValue(I); |
| 113 | } |
| 114 | return Kind; |
| 115 | } |
| 116 | |
Alexey Samsonov | 3325b16 | 2012-11-28 17:34:24 +0000 | [diff] [blame] | 117 | /// Parse a single flag of the form -f[no]sanitize=, or |
| 118 | /// -f*-sanitizer. Sets the masks defining required change of Kind value. |
| 119 | /// Returns true if the flag was parsed successfully. |
| 120 | static bool parse(const Driver &D, const ArgList &Args, const Arg *A, |
| 121 | unsigned &Add, unsigned &Remove, bool DiagnoseErrors) { |
| 122 | Add = 0; |
| 123 | Remove = 0; |
| 124 | const char *DeprecatedReplacement = 0; |
| 125 | if (A->getOption().matches(options::OPT_faddress_sanitizer)) { |
| 126 | Add = Address; |
| 127 | DeprecatedReplacement = "-fsanitize=address"; |
| 128 | } else if (A->getOption().matches(options::OPT_fno_address_sanitizer)) { |
| 129 | Remove = Address; |
| 130 | DeprecatedReplacement = "-fno-sanitize=address"; |
| 131 | } else if (A->getOption().matches(options::OPT_fthread_sanitizer)) { |
| 132 | Add = Thread; |
| 133 | DeprecatedReplacement = "-fsanitize=thread"; |
| 134 | } else if (A->getOption().matches(options::OPT_fno_thread_sanitizer)) { |
| 135 | Remove = Thread; |
| 136 | DeprecatedReplacement = "-fno-sanitize=thread"; |
| 137 | } else if (A->getOption().matches(options::OPT_fcatch_undefined_behavior)) { |
Chad Rosier | 78d85b1 | 2013-01-29 23:31:22 +0000 | [diff] [blame] | 138 | Add = UndefinedTrap; |
| 139 | DeprecatedReplacement = |
| 140 | "-fsanitize=undefined-trap -fsanitize-undefined-trap-on-error"; |
Alexey Samsonov | 3325b16 | 2012-11-28 17:34:24 +0000 | [diff] [blame] | 141 | } else if (A->getOption().matches(options::OPT_fbounds_checking) || |
| 142 | A->getOption().matches(options::OPT_fbounds_checking_EQ)) { |
| 143 | Add = Bounds; |
| 144 | DeprecatedReplacement = "-fsanitize=bounds"; |
| 145 | } else if (A->getOption().matches(options::OPT_fsanitize_EQ)) { |
| 146 | Add = parse(D, A, DiagnoseErrors); |
| 147 | } else if (A->getOption().matches(options::OPT_fno_sanitize_EQ)) { |
| 148 | Remove = parse(D, A, DiagnoseErrors); |
| 149 | } else { |
| 150 | // Flag is not relevant to sanitizers. |
| 151 | return false; |
| 152 | } |
| 153 | // If this is a deprecated synonym, produce a warning directing users |
| 154 | // towards the new spelling. |
| 155 | if (DeprecatedReplacement && DiagnoseErrors) |
| 156 | D.Diag(diag::warn_drv_deprecated_arg) |
| 157 | << A->getAsString(Args) << DeprecatedReplacement; |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | /// Produce an argument string from ArgList \p Args, which shows how it |
| 162 | /// provides a sanitizer kind in \p Mask. For example, the argument list |
| 163 | /// "-fsanitize=thread,vptr -faddress-sanitizer" with mask \c NeedsUbsanRt |
| 164 | /// would produce "-fsanitize=vptr". |
| 165 | static std::string lastArgumentForKind(const Driver &D, const ArgList &Args, |
| 166 | unsigned Kind) { |
| 167 | for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend(); |
| 168 | I != E; ++I) { |
| 169 | unsigned Add, Remove; |
| 170 | if (parse(D, Args, *I, Add, Remove, false) && |
| 171 | (Add & Kind)) |
| 172 | return describeSanitizeArg(Args, *I, Kind); |
| 173 | Kind &= ~Remove; |
| 174 | } |
| 175 | llvm_unreachable("arg list didn't provide expected value"); |
| 176 | } |
| 177 | |
Alexey Samsonov | bb1071c | 2012-11-06 15:09:03 +0000 | [diff] [blame] | 178 | /// Produce an argument string from argument \p A, which shows how it provides |
| 179 | /// a value in \p Mask. For instance, the argument |
| 180 | /// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce |
| 181 | /// "-fsanitize=alignment". |
| 182 | static std::string describeSanitizeArg(const ArgList &Args, const Arg *A, |
| 183 | unsigned Mask) { |
| 184 | if (!A->getOption().matches(options::OPT_fsanitize_EQ)) |
| 185 | return A->getAsString(Args); |
| 186 | |
| 187 | for (unsigned I = 0, N = A->getNumValues(); I != N; ++I) |
| 188 | if (parse(A->getValue(I)) & Mask) |
| 189 | return std::string("-fsanitize=") + A->getValue(I); |
| 190 | |
| 191 | llvm_unreachable("arg didn't provide expected value"); |
| 192 | } |
Alexey Samsonov | 24697b0 | 2013-02-19 11:25:29 +0000 | [diff] [blame] | 193 | |
| 194 | static bool getDefaultBlacklistForKind(const Driver &D, unsigned Kind, |
| 195 | std::string &BLPath) { |
| 196 | // For now, specify the default blacklist location for ASan only. |
| 197 | if (Kind & NeedsAsanRt) { |
| 198 | SmallString<64> Path(D.ResourceDir); |
| 199 | llvm::sys::path::append(Path, "asan_blacklist.txt"); |
| 200 | BLPath = Path.str(); |
| 201 | return true; |
| 202 | } |
| 203 | return false; |
| 204 | } |
Alexey Samsonov | bb1071c | 2012-11-06 15:09:03 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | } // namespace driver |
| 208 | } // namespace clang |
| 209 | |
| 210 | #endif // CLANG_LIB_DRIVER_SANITIZERARGS_H_ |