blob: a4b32df9e374f2ec50121084887dfb264d159261 [file] [log] [blame]
Alexey Samsonovcf055962013-08-08 10:11:02 +00001//===--- SanitizerArgs.cpp - Arguments for sanitizer tools ---------------===//
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//===----------------------------------------------------------------------===//
Alexey Samsonov609213f92013-08-19 09:14:21 +00009#include "clang/Driver/SanitizerArgs.h"
Alexey Samsonovcf055962013-08-08 10:11:02 +000010#include "clang/Driver/Driver.h"
11#include "clang/Driver/DriverDiagnostic.h"
12#include "clang/Driver/Options.h"
13#include "clang/Driver/ToolChain.h"
14#include "llvm/ADT/StringSwitch.h"
15#include "llvm/Support/FileSystem.h"
16#include "llvm/Support/Path.h"
Alexey Samsonov0c127d72013-08-19 13:59:22 +000017#include "llvm/Transforms/Utils/SpecialCaseList.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000018#include <memory>
Alexey Samsonovcf055962013-08-08 10:11:02 +000019
20using namespace clang::driver;
21using namespace llvm::opt;
22
Alexey Samsonovbb14f342013-08-08 11:32:17 +000023void SanitizerArgs::clear() {
24 Kind = 0;
25 BlacklistFile = "";
26 MsanTrackOrigins = false;
Peter Collingbourne32701642013-11-01 18:16:25 +000027 AsanZeroBaseShadow = false;
Alexey Samsonovbb14f342013-08-08 11:32:17 +000028 UbsanTrapOnError = false;
29}
30
31SanitizerArgs::SanitizerArgs() {
32 clear();
33}
Alexey Samsonovcf055962013-08-08 10:11:02 +000034
Peter Collingbourne32701642013-11-01 18:16:25 +000035SanitizerArgs::SanitizerArgs(const ToolChain &TC,
36 const llvm::opt::ArgList &Args) {
Alexey Samsonovbb14f342013-08-08 11:32:17 +000037 clear();
Peter Collingbourne32701642013-11-01 18:16:25 +000038 unsigned AllAdd = 0; // All kinds of sanitizers that were turned on
39 // at least once (possibly, disabled further).
40 unsigned AllRemove = 0; // During the loop below, the accumulated set of
41 // sanitizers disabled by the current sanitizer
42 // argument or any argument after it.
43 unsigned DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
44 // Used to deduplicate diagnostics.
45 const Driver &D = TC.getDriver();
46 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
47 I != E; ++I) {
Alexey Samsonovcf055962013-08-08 10:11:02 +000048 unsigned Add, Remove;
49 if (!parse(D, Args, *I, Add, Remove, true))
50 continue;
51 (*I)->claim();
Peter Collingbourne32701642013-11-01 18:16:25 +000052
53 AllAdd |= expandGroups(Add);
54 AllRemove |= expandGroups(Remove);
55
56 // Avoid diagnosing any sanitizer which is disabled later.
57 Add &= ~AllRemove;
58 // At this point we have not expanded groups, so any unsupported sanitizers
59 // in Add are those which have been explicitly enabled. Diagnose them.
60 Add = filterUnsupportedKinds(TC, Add, Args, *I, /*DiagnoseErrors=*/true,
61 DiagnosedKinds);
62 Add = expandGroups(Add);
63 // Group expansion may have enabled a sanitizer which is disabled later.
64 Add &= ~AllRemove;
65 // Silently discard any unsupported sanitizers implicitly enabled through
66 // group expansion.
67 Add = filterUnsupportedKinds(TC, Add, Args, *I, /*DiagnoseErrors=*/false,
68 DiagnosedKinds);
69
Alexey Samsonovcf055962013-08-08 10:11:02 +000070 Kind |= Add;
Alexey Samsonovcf055962013-08-08 10:11:02 +000071 }
72
73 UbsanTrapOnError =
Alexey Samsonovcf055962013-08-08 10:11:02 +000074 Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
75 options::OPT_fno_sanitize_undefined_trap_on_error, false);
76
Alexey Samsonovcf055962013-08-08 10:11:02 +000077 // Warn about undefined sanitizer options that require runtime support.
78 if (UbsanTrapOnError && notAllowedWithTrap()) {
Alexey Samsonovcb3f8122014-03-20 10:48:29 +000079 D.Diag(diag::err_drv_argument_not_allowed_with)
80 << lastArgumentForKind(D, Args, NotAllowedWithTrap)
81 << "-fsanitize-undefined-trap-on-error";
Alexey Samsonovcf055962013-08-08 10:11:02 +000082 }
83
84 // Only one runtime library can be used at once.
85 bool NeedsAsan = needsAsanRt();
86 bool NeedsTsan = needsTsanRt();
87 bool NeedsMsan = needsMsanRt();
88 bool NeedsLsan = needsLeakDetection();
89 if (NeedsAsan && NeedsTsan)
90 D.Diag(diag::err_drv_argument_not_allowed_with)
91 << lastArgumentForKind(D, Args, NeedsAsanRt)
92 << lastArgumentForKind(D, Args, NeedsTsanRt);
93 if (NeedsAsan && NeedsMsan)
94 D.Diag(diag::err_drv_argument_not_allowed_with)
95 << lastArgumentForKind(D, Args, NeedsAsanRt)
96 << lastArgumentForKind(D, Args, NeedsMsanRt);
97 if (NeedsTsan && NeedsMsan)
98 D.Diag(diag::err_drv_argument_not_allowed_with)
99 << lastArgumentForKind(D, Args, NeedsTsanRt)
100 << lastArgumentForKind(D, Args, NeedsMsanRt);
101 if (NeedsLsan && NeedsTsan)
102 D.Diag(diag::err_drv_argument_not_allowed_with)
103 << lastArgumentForKind(D, Args, NeedsLeakDetection)
104 << lastArgumentForKind(D, Args, NeedsTsanRt);
105 if (NeedsLsan && NeedsMsan)
106 D.Diag(diag::err_drv_argument_not_allowed_with)
107 << lastArgumentForKind(D, Args, NeedsLeakDetection)
108 << lastArgumentForKind(D, Args, NeedsMsanRt);
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000109 // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
Alexey Samsonovcf055962013-08-08 10:11:02 +0000110 // -fsanitize=address. Perhaps it should print an error, or perhaps
111 // -f(-no)sanitize=leak should change whether leak detection is enabled by
112 // default in ASan?
113
114 // If -fsanitize contains extra features of ASan, it should also
115 // explicitly contain -fsanitize=address (probably, turned off later in the
116 // command line).
Peter Collingbourne32701642013-11-01 18:16:25 +0000117 if ((Kind & AddressFull) != 0 && (AllAdd & Address) == 0)
Alexey Samsonovcf055962013-08-08 10:11:02 +0000118 D.Diag(diag::warn_drv_unused_sanitizer)
119 << lastArgumentForKind(D, Args, AddressFull)
120 << "-fsanitize=address";
121
122 // Parse -f(no-)sanitize-blacklist options.
123 if (Arg *BLArg = Args.getLastArg(options::OPT_fsanitize_blacklist,
124 options::OPT_fno_sanitize_blacklist)) {
125 if (BLArg->getOption().matches(options::OPT_fsanitize_blacklist)) {
126 std::string BLPath = BLArg->getValue();
Alexey Samsonov0c127d72013-08-19 13:59:22 +0000127 if (llvm::sys::fs::exists(BLPath)) {
128 // Validate the blacklist format.
129 std::string BLError;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000130 std::unique_ptr<llvm::SpecialCaseList> SCL(
Alexey Samsonov0c127d72013-08-19 13:59:22 +0000131 llvm::SpecialCaseList::create(BLPath, BLError));
132 if (!SCL.get())
133 D.Diag(diag::err_drv_malformed_sanitizer_blacklist) << BLError;
134 else
135 BlacklistFile = BLPath;
136 } else {
Alexey Samsonovcf055962013-08-08 10:11:02 +0000137 D.Diag(diag::err_drv_no_such_file) << BLPath;
Alexey Samsonov0c127d72013-08-19 13:59:22 +0000138 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000139 }
140 } else {
141 // If no -fsanitize-blacklist option is specified, try to look up for
142 // blacklist in the resource directory.
143 std::string BLPath;
144 if (getDefaultBlacklistForKind(D, Kind, BLPath) &&
145 llvm::sys::fs::exists(BLPath))
146 BlacklistFile = BLPath;
147 }
148
149 // Parse -f(no-)sanitize-memory-track-origins options.
150 if (NeedsMsan)
151 MsanTrackOrigins =
152 Args.hasFlag(options::OPT_fsanitize_memory_track_origins,
153 options::OPT_fno_sanitize_memory_track_origins,
154 /* Default */false);
Evgeniy Stepanovd04b8612014-01-16 10:19:31 +0000155 if (NeedsAsan)
Peter Collingbourne32701642013-11-01 18:16:25 +0000156 AsanZeroBaseShadow =
Evgeniy Stepanovd04b8612014-01-16 10:19:31 +0000157 (TC.getTriple().getEnvironment() == llvm::Triple::Android);
Alexey Samsonovcf055962013-08-08 10:11:02 +0000158}
159
Peter Collingbourne32701642013-11-01 18:16:25 +0000160void SanitizerArgs::addArgs(const llvm::opt::ArgList &Args,
Alexey Samsonovcf055962013-08-08 10:11:02 +0000161 llvm::opt::ArgStringList &CmdArgs) const {
162 if (!Kind)
163 return;
164 SmallString<256> SanitizeOpt("-fsanitize=");
165#define SANITIZER(NAME, ID) \
166 if (Kind & ID) \
167 SanitizeOpt += NAME ",";
168#include "clang/Basic/Sanitizers.def"
169 SanitizeOpt.pop_back();
170 CmdArgs.push_back(Args.MakeArgString(SanitizeOpt));
171 if (!BlacklistFile.empty()) {
172 SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
173 BlacklistOpt += BlacklistFile;
174 CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
175 }
176
177 if (MsanTrackOrigins)
178 CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins"));
179
Alexey Samsonovcf055962013-08-08 10:11:02 +0000180 // Workaround for PR16386.
181 if (needsMsanRt())
182 CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
183}
184
Alexey Samsonovcf055962013-08-08 10:11:02 +0000185unsigned SanitizerArgs::parse(const char *Value) {
186 unsigned ParsedKind = llvm::StringSwitch<SanitizeKind>(Value)
187#define SANITIZER(NAME, ID) .Case(NAME, ID)
Peter Collingbourne32701642013-11-01 18:16:25 +0000188#define SANITIZER_GROUP(NAME, ID, ALIAS) .Case(NAME, ID##Group)
Alexey Samsonovcf055962013-08-08 10:11:02 +0000189#include "clang/Basic/Sanitizers.def"
190 .Default(SanitizeKind());
Kostya Serebryanybedc6162013-09-23 09:52:37 +0000191 // Assume -fsanitize=address implies -fsanitize=init-order,use-after-return.
Alexey Samsonovcf055962013-08-08 10:11:02 +0000192 // FIXME: This should be either specified in Sanitizers.def, or go away when
Kostya Serebryanybedc6162013-09-23 09:52:37 +0000193 // we get rid of "-fsanitize=init-order,use-after-return" flags at all.
Alexey Samsonovcf055962013-08-08 10:11:02 +0000194 if (ParsedKind & Address)
Kostya Serebryanybedc6162013-09-23 09:52:37 +0000195 ParsedKind |= InitOrder | UseAfterReturn;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000196 return ParsedKind;
197}
198
Peter Collingbourne32701642013-11-01 18:16:25 +0000199unsigned SanitizerArgs::expandGroups(unsigned Kinds) {
200#define SANITIZER(NAME, ID)
201#define SANITIZER_GROUP(NAME, ID, ALIAS) if (Kinds & ID##Group) Kinds |= ID;
202#include "clang/Basic/Sanitizers.def"
203 return Kinds;
204}
205
206void SanitizerArgs::filterUnsupportedMask(const ToolChain &TC, unsigned &Kinds,
207 unsigned Mask,
208 const llvm::opt::ArgList &Args,
209 const llvm::opt::Arg *A,
210 bool DiagnoseErrors,
211 unsigned &DiagnosedKinds) {
212 unsigned MaskedKinds = Kinds & Mask;
213 if (!MaskedKinds)
214 return;
215 Kinds &= ~Mask;
216 // Do we have new kinds to diagnose?
217 if (DiagnoseErrors && (DiagnosedKinds & MaskedKinds) != MaskedKinds) {
218 // Only diagnose the new kinds.
219 std::string Desc =
220 describeSanitizeArg(Args, A, MaskedKinds & ~DiagnosedKinds);
221 TC.getDriver().Diag(diag::err_drv_unsupported_opt_for_target)
222 << Desc << TC.getTriple().str();
223 DiagnosedKinds |= MaskedKinds;
224 }
225}
226
227unsigned SanitizerArgs::filterUnsupportedKinds(const ToolChain &TC,
228 unsigned Kinds,
229 const llvm::opt::ArgList &Args,
230 const llvm::opt::Arg *A,
231 bool DiagnoseErrors,
232 unsigned &DiagnosedKinds) {
233 bool IsLinux = TC.getTriple().getOS() == llvm::Triple::Linux;
234 bool IsX86 = TC.getTriple().getArch() == llvm::Triple::x86;
235 bool IsX86_64 = TC.getTriple().getArch() == llvm::Triple::x86_64;
236 if (!(IsLinux && IsX86_64)) {
237 filterUnsupportedMask(TC, Kinds, Thread | Memory | DataFlow, Args, A,
238 DiagnoseErrors, DiagnosedKinds);
239 }
240 if (!(IsLinux && (IsX86 || IsX86_64))) {
241 filterUnsupportedMask(TC, Kinds, Function, Args, A, DiagnoseErrors,
242 DiagnosedKinds);
243 }
244 return Kinds;
245}
246
Alexey Samsonovcf055962013-08-08 10:11:02 +0000247unsigned SanitizerArgs::parse(const Driver &D, const llvm::opt::Arg *A,
248 bool DiagnoseErrors) {
249 unsigned Kind = 0;
250 for (unsigned I = 0, N = A->getNumValues(); I != N; ++I) {
251 if (unsigned K = parse(A->getValue(I)))
252 Kind |= K;
253 else if (DiagnoseErrors)
254 D.Diag(diag::err_drv_unsupported_option_argument)
255 << A->getOption().getName() << A->getValue(I);
256 }
257 return Kind;
258}
259
260bool SanitizerArgs::parse(const Driver &D, const llvm::opt::ArgList &Args,
261 const llvm::opt::Arg *A, unsigned &Add,
262 unsigned &Remove, bool DiagnoseErrors) {
263 Add = 0;
264 Remove = 0;
265 const char *DeprecatedReplacement = 0;
Alexey Samsonovcb3f8122014-03-20 10:48:29 +0000266 if (A->getOption().matches(options::OPT_fbounds_checking) ||
267 A->getOption().matches(options::OPT_fbounds_checking_EQ)) {
Richard Smith6b53e222013-10-22 22:51:04 +0000268 Add = LocalBounds;
269 DeprecatedReplacement = "-fsanitize=local-bounds";
Alexey Samsonovcf055962013-08-08 10:11:02 +0000270 } else if (A->getOption().matches(options::OPT_fsanitize_EQ)) {
271 Add = parse(D, A, DiagnoseErrors);
272 } else if (A->getOption().matches(options::OPT_fno_sanitize_EQ)) {
273 Remove = parse(D, A, DiagnoseErrors);
274 } else {
275 // Flag is not relevant to sanitizers.
276 return false;
277 }
278 // If this is a deprecated synonym, produce a warning directing users
279 // towards the new spelling.
280 if (DeprecatedReplacement && DiagnoseErrors)
281 D.Diag(diag::warn_drv_deprecated_arg)
282 << A->getAsString(Args) << DeprecatedReplacement;
283 return true;
284}
285
286std::string SanitizerArgs::lastArgumentForKind(const Driver &D,
287 const llvm::opt::ArgList &Args,
288 unsigned Kind) {
289 for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
290 E = Args.rend();
291 I != E; ++I) {
292 unsigned Add, Remove;
293 if (parse(D, Args, *I, Add, Remove, false) &&
Peter Collingbourne32701642013-11-01 18:16:25 +0000294 (expandGroups(Add) & Kind))
Alexey Samsonovcf055962013-08-08 10:11:02 +0000295 return describeSanitizeArg(Args, *I, Kind);
296 Kind &= ~Remove;
297 }
298 llvm_unreachable("arg list didn't provide expected value");
299}
300
301std::string SanitizerArgs::describeSanitizeArg(const llvm::opt::ArgList &Args,
302 const llvm::opt::Arg *A,
303 unsigned Mask) {
304 if (!A->getOption().matches(options::OPT_fsanitize_EQ))
305 return A->getAsString(Args);
306
Peter Collingbourne32701642013-11-01 18:16:25 +0000307 std::string Sanitizers;
308 for (unsigned I = 0, N = A->getNumValues(); I != N; ++I) {
309 if (expandGroups(parse(A->getValue(I))) & Mask) {
310 if (!Sanitizers.empty())
311 Sanitizers += ",";
312 Sanitizers += A->getValue(I);
313 }
314 }
Alexey Samsonovcf055962013-08-08 10:11:02 +0000315
Peter Collingbourne32701642013-11-01 18:16:25 +0000316 assert(!Sanitizers.empty() && "arg didn't provide expected value");
317 return "-fsanitize=" + Sanitizers;
Alexey Samsonovcf055962013-08-08 10:11:02 +0000318}
319
320bool SanitizerArgs::getDefaultBlacklistForKind(const Driver &D, unsigned Kind,
321 std::string &BLPath) {
322 const char *BlacklistFile = 0;
323 if (Kind & NeedsAsanRt)
324 BlacklistFile = "asan_blacklist.txt";
325 else if (Kind & NeedsMsanRt)
326 BlacklistFile = "msan_blacklist.txt";
327 else if (Kind & NeedsTsanRt)
328 BlacklistFile = "tsan_blacklist.txt";
Peter Collingbourne276be3c2013-08-14 18:54:18 +0000329 else if (Kind & NeedsDfsanRt)
330 BlacklistFile = "dfsan_abilist.txt";
331
Alexey Samsonovcf055962013-08-08 10:11:02 +0000332 if (BlacklistFile) {
333 SmallString<64> Path(D.ResourceDir);
334 llvm::sys::path::append(Path, BlacklistFile);
335 BLPath = Path.str();
336 return true;
337 }
338 return false;
339}