blob: c3d84f9a4f6b6218b1eedbc9da1a493e740b5737 [file] [log] [blame]
Alexey Samsonovbb1071c2012-11-06 15:09:03 +00001//===--- 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 Carruth55fc8732012-12-04 09:13:33 +000012#include "clang/Driver/Arg.h"
Alexey Samsonovbb1071c2012-11-06 15:09:03 +000013#include "clang/Driver/ArgList.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000014#include "clang/Driver/Driver.h"
15#include "clang/Driver/DriverDiagnostic.h"
16#include "clang/Driver/Options.h"
17#include "llvm/ADT/StringSwitch.h"
Alexey Samsonov24697b02013-02-19 11:25:29 +000018#include "llvm/Support/Path.h"
Alexey Samsonovbb1071c2012-11-06 15:09:03 +000019
20namespace clang {
21namespace driver {
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#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 Samsonov3e335c12013-01-28 07:20:44 +000037 NeedsAsanRt = Address,
Alexey Samsonovbb1071c2012-11-06 15:09:03 +000038 NeedsTsanRt = Thread,
Evgeniy Stepanov09ccf392012-12-03 13:20:43 +000039 NeedsMsanRt = Memory,
Richard Smitha0a628f2013-02-23 02:53:19 +000040 NeedsUbsanRt = Undefined | Integer,
Chad Rosier78d85b12013-01-29 23:31:22 +000041 NotAllowedWithTrap = Vptr
Alexey Samsonovbb1071c2012-11-06 15:09:03 +000042 };
43 unsigned Kind;
Alexey Samsonov91ecfa62012-12-03 19:12:58 +000044 std::string BlacklistFile;
Evgeniy Stepanov34ef11b2012-12-24 08:42:34 +000045 bool MsanTrackOrigins;
Alexey Samsonov4bdc6042013-01-20 13:12:12 +000046 bool AsanZeroBaseShadow;
Chad Rosier78d85b12013-01-29 23:31:22 +000047 bool UbsanTrapOnError;
Alexey Samsonovbb1071c2012-11-06 15:09:03 +000048
49 public:
Alexey Samsonov4bdc6042013-01-20 13:12:12 +000050 SanitizerArgs() : Kind(0), BlacklistFile(""), MsanTrackOrigins(false),
Chad Rosier78d85b12013-01-29 23:31:22 +000051 AsanZeroBaseShadow(false), UbsanTrapOnError(false) {}
Alexey Samsonovbb1071c2012-11-06 15:09:03 +000052 /// 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 Stepanov09ccf392012-12-03 13:20:43 +000057 bool needsMsanRt() const { return Kind & NeedsMsanRt; }
Chad Rosier78d85b12013-01-29 23:31:22 +000058 bool needsUbsanRt() const {
59 if (UbsanTrapOnError)
60 return false;
61 return Kind & NeedsUbsanRt;
62 }
Alexey Samsonovbb1071c2012-11-06 15:09:03 +000063
64 bool sanitizesVptr() const { return Kind & Vptr; }
Chad Rosier78d85b12013-01-29 23:31:22 +000065 bool notAllowedWithTrap() const { return Kind & NotAllowedWithTrap; }
Alexey Samsonov4d1a6e42012-11-29 22:36:21 +000066
Alexey Samsonovbb1071c2012-11-06 15:09:03 +000067 void addArgs(const ArgList &Args, ArgStringList &CmdArgs) const {
68 if (!Kind)
69 return;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +000070 SmallString<256> SanitizeOpt("-fsanitize=");
Alexey Samsonovbb1071c2012-11-06 15:09:03 +000071#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 Samsonov91ecfa62012-12-03 19:12:58 +000077 if (!BlacklistFile.empty()) {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +000078 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
Alexey Samsonov91ecfa62012-12-03 19:12:58 +000079 BlacklistOpt += BlacklistFile;
80 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
81 }
Evgeniy Stepanov34ef11b2012-12-24 08:42:34 +000082
83 if (MsanTrackOrigins)
84 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins"));
Alexey Samsonov4bdc6042013-01-20 13:12:12 +000085
86 if (AsanZeroBaseShadow)
87 CmdArgs.push_back(Args.MakeArgString(
88 "-fsanitize-address-zero-base-shadow"));
Alexey Samsonovbb1071c2012-11-06 15:09:03 +000089 }
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 Samsonov3325b162012-11-28 17:34:24 +0000105 static unsigned parse(const Driver &D, const Arg *A, bool DiagnoseErrors) {
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000106 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 Samsonov3325b162012-11-28 17:34:24 +0000110 else if (DiagnoseErrors)
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000111 D.Diag(diag::err_drv_unsupported_option_argument)
112 << A->getOption().getName() << A->getValue(I);
113 }
114 return Kind;
115 }
116
Alexey Samsonov3325b162012-11-28 17:34:24 +0000117 /// 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 Rosier78d85b12013-01-29 23:31:22 +0000138 Add = UndefinedTrap;
139 DeprecatedReplacement =
140 "-fsanitize=undefined-trap -fsanitize-undefined-trap-on-error";
Alexey Samsonov3325b162012-11-28 17:34:24 +0000141 } 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 Samsonovbb1071c2012-11-06 15:09:03 +0000178 /// 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 Samsonov24697b02013-02-19 11:25:29 +0000193
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 Samsonovbb1071c2012-11-06 15:09:03 +0000205};
206
207} // namespace driver
208} // namespace clang
209
210#endif // CLANG_LIB_DRIVER_SANITIZERARGS_H_