1f4a2713aSLionel Sambuc //===--- SanitizerArgs.cpp - Arguments for sanitizer tools ---------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc #include "clang/Driver/SanitizerArgs.h"
10f4a2713aSLionel Sambuc #include "clang/Driver/Driver.h"
11f4a2713aSLionel Sambuc #include "clang/Driver/DriverDiagnostic.h"
12f4a2713aSLionel Sambuc #include "clang/Driver/Options.h"
13f4a2713aSLionel Sambuc #include "clang/Driver/ToolChain.h"
14*0a6a1f1dSLionel Sambuc #include "llvm/ADT/StringExtras.h"
15f4a2713aSLionel Sambuc #include "llvm/ADT/StringSwitch.h"
16f4a2713aSLionel Sambuc #include "llvm/Support/FileSystem.h"
17f4a2713aSLionel Sambuc #include "llvm/Support/Path.h"
18*0a6a1f1dSLionel Sambuc #include "llvm/Support/SpecialCaseList.h"
19*0a6a1f1dSLionel Sambuc #include <memory>
20f4a2713aSLionel Sambuc
21f4a2713aSLionel Sambuc using namespace clang::driver;
22f4a2713aSLionel Sambuc using namespace llvm::opt;
23f4a2713aSLionel Sambuc
24*0a6a1f1dSLionel Sambuc namespace {
25*0a6a1f1dSLionel Sambuc /// Assign ordinals to possible values of -fsanitize= flag.
26*0a6a1f1dSLionel Sambuc /// We use the ordinal values as bit positions within \c SanitizeKind.
27*0a6a1f1dSLionel Sambuc enum SanitizeOrdinal {
28*0a6a1f1dSLionel Sambuc #define SANITIZER(NAME, ID) SO_##ID,
29*0a6a1f1dSLionel Sambuc #define SANITIZER_GROUP(NAME, ID, ALIAS) SO_##ID##Group,
30*0a6a1f1dSLionel Sambuc #include "clang/Basic/Sanitizers.def"
31*0a6a1f1dSLionel Sambuc SO_Count
32*0a6a1f1dSLionel Sambuc };
33*0a6a1f1dSLionel Sambuc
34*0a6a1f1dSLionel Sambuc /// Represents a set of sanitizer kinds. It is also used to define:
35*0a6a1f1dSLionel Sambuc /// 1) set of sanitizers each sanitizer group expands into.
36*0a6a1f1dSLionel Sambuc /// 2) set of sanitizers sharing a specific property (e.g.
37*0a6a1f1dSLionel Sambuc /// all sanitizers with zero-base shadow).
38*0a6a1f1dSLionel Sambuc enum SanitizeKind {
39*0a6a1f1dSLionel Sambuc #define SANITIZER(NAME, ID) ID = 1 << SO_##ID,
40*0a6a1f1dSLionel Sambuc #define SANITIZER_GROUP(NAME, ID, ALIAS) \
41*0a6a1f1dSLionel Sambuc ID = ALIAS, ID##Group = 1 << SO_##ID##Group,
42*0a6a1f1dSLionel Sambuc #include "clang/Basic/Sanitizers.def"
43*0a6a1f1dSLionel Sambuc NeedsUbsanRt = Undefined | Integer,
44*0a6a1f1dSLionel Sambuc NotAllowedWithTrap = Vptr,
45*0a6a1f1dSLionel Sambuc RequiresPIE = Memory | DataFlow,
46*0a6a1f1dSLionel Sambuc NeedsUnwindTables = Address | Thread | Memory | DataFlow,
47*0a6a1f1dSLionel Sambuc SupportsCoverage = Address | Memory | Leak | Undefined | Integer,
48*0a6a1f1dSLionel Sambuc RecoverableByDefault = Undefined | Integer,
49*0a6a1f1dSLionel Sambuc Unrecoverable = Address | Unreachable | Return,
50*0a6a1f1dSLionel Sambuc LegacyFsanitizeRecoverMask = Undefined | Integer
51*0a6a1f1dSLionel Sambuc };
52f4a2713aSLionel Sambuc }
53f4a2713aSLionel Sambuc
54*0a6a1f1dSLionel Sambuc /// Returns true if set of \p Sanitizers contain at least one sanitizer from
55*0a6a1f1dSLionel Sambuc /// \p Kinds.
hasOneOf(const clang::SanitizerSet & Sanitizers,unsigned Kinds)56*0a6a1f1dSLionel Sambuc static bool hasOneOf(const clang::SanitizerSet &Sanitizers, unsigned Kinds) {
57*0a6a1f1dSLionel Sambuc #define SANITIZER(NAME, ID) \
58*0a6a1f1dSLionel Sambuc if (Sanitizers.has(clang::SanitizerKind::ID) && (Kinds & ID)) \
59*0a6a1f1dSLionel Sambuc return true;
60*0a6a1f1dSLionel Sambuc #include "clang/Basic/Sanitizers.def"
61*0a6a1f1dSLionel Sambuc return false;
62*0a6a1f1dSLionel Sambuc }
63*0a6a1f1dSLionel Sambuc
64*0a6a1f1dSLionel Sambuc /// Adds all sanitizers from \p Kinds to \p Sanitizers.
addAllOf(clang::SanitizerSet & Sanitizers,unsigned Kinds)65*0a6a1f1dSLionel Sambuc static void addAllOf(clang::SanitizerSet &Sanitizers, unsigned Kinds) {
66*0a6a1f1dSLionel Sambuc #define SANITIZER(NAME, ID) \
67*0a6a1f1dSLionel Sambuc if (Kinds & ID) \
68*0a6a1f1dSLionel Sambuc Sanitizers.set(clang::SanitizerKind::ID, true);
69*0a6a1f1dSLionel Sambuc #include "clang/Basic/Sanitizers.def"
70*0a6a1f1dSLionel Sambuc }
71*0a6a1f1dSLionel Sambuc
toSanitizeKind(clang::SanitizerKind K)72*0a6a1f1dSLionel Sambuc static unsigned toSanitizeKind(clang::SanitizerKind K) {
73*0a6a1f1dSLionel Sambuc #define SANITIZER(NAME, ID) \
74*0a6a1f1dSLionel Sambuc if (K == clang::SanitizerKind::ID) \
75*0a6a1f1dSLionel Sambuc return ID;
76*0a6a1f1dSLionel Sambuc #include "clang/Basic/Sanitizers.def"
77*0a6a1f1dSLionel Sambuc llvm_unreachable("Invalid SanitizerKind!");
78*0a6a1f1dSLionel Sambuc }
79*0a6a1f1dSLionel Sambuc
80*0a6a1f1dSLionel Sambuc /// Parse a single value from a -fsanitize= or -fno-sanitize= value list.
81*0a6a1f1dSLionel Sambuc /// Returns a member of the \c SanitizeKind enumeration, or \c 0
82*0a6a1f1dSLionel Sambuc /// if \p Value is not known.
83*0a6a1f1dSLionel Sambuc static unsigned parseValue(const char *Value);
84*0a6a1f1dSLionel Sambuc
85*0a6a1f1dSLionel Sambuc /// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any
86*0a6a1f1dSLionel Sambuc /// invalid components. Returns OR of members of \c SanitizeKind enumeration.
87*0a6a1f1dSLionel Sambuc static unsigned parseArgValues(const Driver &D, const llvm::opt::Arg *A,
88*0a6a1f1dSLionel Sambuc bool DiagnoseErrors);
89*0a6a1f1dSLionel Sambuc
90*0a6a1f1dSLionel Sambuc /// Produce an argument string from ArgList \p Args, which shows how it
91*0a6a1f1dSLionel Sambuc /// provides some sanitizer kind from \p Mask. For example, the argument list
92*0a6a1f1dSLionel Sambuc /// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt
93*0a6a1f1dSLionel Sambuc /// would produce "-fsanitize=vptr".
94*0a6a1f1dSLionel Sambuc static std::string lastArgumentForMask(const Driver &D,
95*0a6a1f1dSLionel Sambuc const llvm::opt::ArgList &Args,
96*0a6a1f1dSLionel Sambuc unsigned Mask);
97*0a6a1f1dSLionel Sambuc
lastArgumentForKind(const Driver & D,const llvm::opt::ArgList & Args,clang::SanitizerKind K)98*0a6a1f1dSLionel Sambuc static std::string lastArgumentForKind(const Driver &D,
99*0a6a1f1dSLionel Sambuc const llvm::opt::ArgList &Args,
100*0a6a1f1dSLionel Sambuc clang::SanitizerKind K) {
101*0a6a1f1dSLionel Sambuc return lastArgumentForMask(D, Args, toSanitizeKind(K));
102*0a6a1f1dSLionel Sambuc }
103*0a6a1f1dSLionel Sambuc
104*0a6a1f1dSLionel Sambuc /// Produce an argument string from argument \p A, which shows how it provides
105*0a6a1f1dSLionel Sambuc /// a value in \p Mask. For instance, the argument
106*0a6a1f1dSLionel Sambuc /// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce
107*0a6a1f1dSLionel Sambuc /// "-fsanitize=alignment".
108*0a6a1f1dSLionel Sambuc static std::string describeSanitizeArg(const llvm::opt::Arg *A, unsigned Mask);
109*0a6a1f1dSLionel Sambuc
110*0a6a1f1dSLionel Sambuc /// Produce a string containing comma-separated names of sanitizers in \p
111*0a6a1f1dSLionel Sambuc /// Sanitizers set.
112*0a6a1f1dSLionel Sambuc static std::string toString(const clang::SanitizerSet &Sanitizers);
113*0a6a1f1dSLionel Sambuc
114*0a6a1f1dSLionel Sambuc /// For each sanitizer group bit set in \p Kinds, set the bits for sanitizers
115*0a6a1f1dSLionel Sambuc /// this group enables.
116*0a6a1f1dSLionel Sambuc static unsigned expandGroups(unsigned Kinds);
117*0a6a1f1dSLionel Sambuc
getToolchainUnsupportedKinds(const ToolChain & TC)118*0a6a1f1dSLionel Sambuc static unsigned getToolchainUnsupportedKinds(const ToolChain &TC) {
119*0a6a1f1dSLionel Sambuc bool IsFreeBSD = TC.getTriple().getOS() == llvm::Triple::FreeBSD;
120*0a6a1f1dSLionel Sambuc bool IsLinux = TC.getTriple().getOS() == llvm::Triple::Linux;
121*0a6a1f1dSLionel Sambuc bool IsX86 = TC.getTriple().getArch() == llvm::Triple::x86;
122*0a6a1f1dSLionel Sambuc bool IsX86_64 = TC.getTriple().getArch() == llvm::Triple::x86_64;
123*0a6a1f1dSLionel Sambuc
124*0a6a1f1dSLionel Sambuc unsigned Unsupported = 0;
125*0a6a1f1dSLionel Sambuc if (!(IsLinux && IsX86_64)) {
126*0a6a1f1dSLionel Sambuc Unsupported |= Memory | DataFlow;
127*0a6a1f1dSLionel Sambuc }
128*0a6a1f1dSLionel Sambuc if (!((IsLinux || IsFreeBSD) && IsX86_64)) {
129*0a6a1f1dSLionel Sambuc Unsupported |= Thread;
130*0a6a1f1dSLionel Sambuc }
131*0a6a1f1dSLionel Sambuc if (!(IsLinux && (IsX86 || IsX86_64))) {
132*0a6a1f1dSLionel Sambuc Unsupported |= Function;
133*0a6a1f1dSLionel Sambuc }
134*0a6a1f1dSLionel Sambuc return Unsupported;
135*0a6a1f1dSLionel Sambuc }
136*0a6a1f1dSLionel Sambuc
needsUbsanRt() const137*0a6a1f1dSLionel Sambuc bool SanitizerArgs::needsUbsanRt() const {
138*0a6a1f1dSLionel Sambuc return !UbsanTrapOnError && hasOneOf(Sanitizers, NeedsUbsanRt);
139*0a6a1f1dSLionel Sambuc }
140*0a6a1f1dSLionel Sambuc
requiresPIE() const141*0a6a1f1dSLionel Sambuc bool SanitizerArgs::requiresPIE() const {
142*0a6a1f1dSLionel Sambuc return AsanZeroBaseShadow || hasOneOf(Sanitizers, RequiresPIE);
143*0a6a1f1dSLionel Sambuc }
144*0a6a1f1dSLionel Sambuc
needsUnwindTables() const145*0a6a1f1dSLionel Sambuc bool SanitizerArgs::needsUnwindTables() const {
146*0a6a1f1dSLionel Sambuc return hasOneOf(Sanitizers, NeedsUnwindTables);
147*0a6a1f1dSLionel Sambuc }
148*0a6a1f1dSLionel Sambuc
clear()149*0a6a1f1dSLionel Sambuc void SanitizerArgs::clear() {
150*0a6a1f1dSLionel Sambuc Sanitizers.clear();
151*0a6a1f1dSLionel Sambuc RecoverableSanitizers.clear();
152*0a6a1f1dSLionel Sambuc BlacklistFile = "";
153*0a6a1f1dSLionel Sambuc SanitizeCoverage = 0;
154*0a6a1f1dSLionel Sambuc MsanTrackOrigins = 0;
155*0a6a1f1dSLionel Sambuc AsanFieldPadding = 0;
156*0a6a1f1dSLionel Sambuc AsanZeroBaseShadow = false;
157*0a6a1f1dSLionel Sambuc UbsanTrapOnError = false;
158*0a6a1f1dSLionel Sambuc AsanSharedRuntime = false;
159*0a6a1f1dSLionel Sambuc LinkCXXRuntimes = false;
160f4a2713aSLionel Sambuc }
161f4a2713aSLionel Sambuc
SanitizerArgs(const ToolChain & TC,const llvm::opt::ArgList & Args)162f4a2713aSLionel Sambuc SanitizerArgs::SanitizerArgs(const ToolChain &TC,
163f4a2713aSLionel Sambuc const llvm::opt::ArgList &Args) {
164f4a2713aSLionel Sambuc clear();
165f4a2713aSLionel Sambuc unsigned AllRemove = 0; // During the loop below, the accumulated set of
166f4a2713aSLionel Sambuc // sanitizers disabled by the current sanitizer
167f4a2713aSLionel Sambuc // argument or any argument after it.
168f4a2713aSLionel Sambuc unsigned DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
169f4a2713aSLionel Sambuc // Used to deduplicate diagnostics.
170*0a6a1f1dSLionel Sambuc unsigned Kinds = 0;
171*0a6a1f1dSLionel Sambuc unsigned NotSupported = getToolchainUnsupportedKinds(TC);
172f4a2713aSLionel Sambuc const Driver &D = TC.getDriver();
173f4a2713aSLionel Sambuc for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
174f4a2713aSLionel Sambuc I != E; ++I) {
175*0a6a1f1dSLionel Sambuc const auto *Arg = *I;
176*0a6a1f1dSLionel Sambuc if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
177*0a6a1f1dSLionel Sambuc Arg->claim();
178*0a6a1f1dSLionel Sambuc unsigned Add = parseArgValues(D, Arg, true);
179f4a2713aSLionel Sambuc
180f4a2713aSLionel Sambuc // Avoid diagnosing any sanitizer which is disabled later.
181f4a2713aSLionel Sambuc Add &= ~AllRemove;
182*0a6a1f1dSLionel Sambuc // At this point we have not expanded groups, so any unsupported
183*0a6a1f1dSLionel Sambuc // sanitizers in Add are those which have been explicitly enabled.
184*0a6a1f1dSLionel Sambuc // Diagnose them.
185*0a6a1f1dSLionel Sambuc if (unsigned KindsToDiagnose = Add & NotSupported & ~DiagnosedKinds) {
186*0a6a1f1dSLionel Sambuc // Only diagnose the new kinds.
187*0a6a1f1dSLionel Sambuc std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
188*0a6a1f1dSLionel Sambuc D.Diag(diag::err_drv_unsupported_opt_for_target)
189*0a6a1f1dSLionel Sambuc << Desc << TC.getTriple().str();
190*0a6a1f1dSLionel Sambuc DiagnosedKinds |= KindsToDiagnose;
191*0a6a1f1dSLionel Sambuc }
192*0a6a1f1dSLionel Sambuc Add &= ~NotSupported;
193*0a6a1f1dSLionel Sambuc
194f4a2713aSLionel Sambuc Add = expandGroups(Add);
195f4a2713aSLionel Sambuc // Group expansion may have enabled a sanitizer which is disabled later.
196f4a2713aSLionel Sambuc Add &= ~AllRemove;
197f4a2713aSLionel Sambuc // Silently discard any unsupported sanitizers implicitly enabled through
198f4a2713aSLionel Sambuc // group expansion.
199*0a6a1f1dSLionel Sambuc Add &= ~NotSupported;
200f4a2713aSLionel Sambuc
201*0a6a1f1dSLionel Sambuc Kinds |= Add;
202*0a6a1f1dSLionel Sambuc } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
203*0a6a1f1dSLionel Sambuc Arg->claim();
204*0a6a1f1dSLionel Sambuc unsigned Remove = parseArgValues(D, Arg, true);
205*0a6a1f1dSLionel Sambuc AllRemove |= expandGroups(Remove);
206f4a2713aSLionel Sambuc }
207*0a6a1f1dSLionel Sambuc }
208*0a6a1f1dSLionel Sambuc addAllOf(Sanitizers, Kinds);
209*0a6a1f1dSLionel Sambuc
210*0a6a1f1dSLionel Sambuc // Parse -f(no-)?sanitize-recover flags.
211*0a6a1f1dSLionel Sambuc unsigned RecoverableKinds = RecoverableByDefault;
212*0a6a1f1dSLionel Sambuc unsigned DiagnosedUnrecoverableKinds = 0;
213*0a6a1f1dSLionel Sambuc for (const auto *Arg : Args) {
214*0a6a1f1dSLionel Sambuc if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
215*0a6a1f1dSLionel Sambuc // FIXME: Add deprecation notice, and then remove this flag.
216*0a6a1f1dSLionel Sambuc RecoverableKinds |= expandGroups(LegacyFsanitizeRecoverMask);
217*0a6a1f1dSLionel Sambuc Arg->claim();
218*0a6a1f1dSLionel Sambuc } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
219*0a6a1f1dSLionel Sambuc // FIXME: Add deprecation notice, and then remove this flag.
220*0a6a1f1dSLionel Sambuc RecoverableKinds &= ~expandGroups(LegacyFsanitizeRecoverMask);
221*0a6a1f1dSLionel Sambuc Arg->claim();
222*0a6a1f1dSLionel Sambuc } else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
223*0a6a1f1dSLionel Sambuc unsigned Add = parseArgValues(D, Arg, true);
224*0a6a1f1dSLionel Sambuc // Report error if user explicitly tries to recover from unrecoverable
225*0a6a1f1dSLionel Sambuc // sanitizer.
226*0a6a1f1dSLionel Sambuc if (unsigned KindsToDiagnose =
227*0a6a1f1dSLionel Sambuc Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
228*0a6a1f1dSLionel Sambuc SanitizerSet SetToDiagnose;
229*0a6a1f1dSLionel Sambuc addAllOf(SetToDiagnose, KindsToDiagnose);
230*0a6a1f1dSLionel Sambuc D.Diag(diag::err_drv_unsupported_option_argument)
231*0a6a1f1dSLionel Sambuc << Arg->getOption().getName() << toString(SetToDiagnose);
232*0a6a1f1dSLionel Sambuc DiagnosedUnrecoverableKinds |= KindsToDiagnose;
233*0a6a1f1dSLionel Sambuc }
234*0a6a1f1dSLionel Sambuc RecoverableKinds |= expandGroups(Add);
235*0a6a1f1dSLionel Sambuc Arg->claim();
236*0a6a1f1dSLionel Sambuc } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
237*0a6a1f1dSLionel Sambuc RecoverableKinds &= ~expandGroups(parseArgValues(D, Arg, true));
238*0a6a1f1dSLionel Sambuc Arg->claim();
239*0a6a1f1dSLionel Sambuc }
240*0a6a1f1dSLionel Sambuc }
241*0a6a1f1dSLionel Sambuc RecoverableKinds &= Kinds;
242*0a6a1f1dSLionel Sambuc RecoverableKinds &= ~Unrecoverable;
243*0a6a1f1dSLionel Sambuc addAllOf(RecoverableSanitizers, RecoverableKinds);
244f4a2713aSLionel Sambuc
245f4a2713aSLionel Sambuc UbsanTrapOnError =
246f4a2713aSLionel Sambuc Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
247f4a2713aSLionel Sambuc options::OPT_fno_sanitize_undefined_trap_on_error, false);
248f4a2713aSLionel Sambuc
249f4a2713aSLionel Sambuc // Warn about undefined sanitizer options that require runtime support.
250*0a6a1f1dSLionel Sambuc if (UbsanTrapOnError && hasOneOf(Sanitizers, NotAllowedWithTrap)) {
251*0a6a1f1dSLionel Sambuc D.Diag(clang::diag::err_drv_argument_not_allowed_with)
252*0a6a1f1dSLionel Sambuc << lastArgumentForMask(D, Args, NotAllowedWithTrap)
253f4a2713aSLionel Sambuc << "-fsanitize-undefined-trap-on-error";
254f4a2713aSLionel Sambuc }
255f4a2713aSLionel Sambuc
256*0a6a1f1dSLionel Sambuc // Check for incompatible sanitizers.
257*0a6a1f1dSLionel Sambuc bool NeedsAsan = Sanitizers.has(SanitizerKind::Address);
258*0a6a1f1dSLionel Sambuc bool NeedsTsan = Sanitizers.has(SanitizerKind::Thread);
259*0a6a1f1dSLionel Sambuc bool NeedsMsan = Sanitizers.has(SanitizerKind::Memory);
260*0a6a1f1dSLionel Sambuc bool NeedsLsan = Sanitizers.has(SanitizerKind::Leak);
261f4a2713aSLionel Sambuc if (NeedsAsan && NeedsTsan)
262*0a6a1f1dSLionel Sambuc D.Diag(clang::diag::err_drv_argument_not_allowed_with)
263*0a6a1f1dSLionel Sambuc << lastArgumentForKind(D, Args, SanitizerKind::Address)
264*0a6a1f1dSLionel Sambuc << lastArgumentForKind(D, Args, SanitizerKind::Thread);
265f4a2713aSLionel Sambuc if (NeedsAsan && NeedsMsan)
266*0a6a1f1dSLionel Sambuc D.Diag(clang::diag::err_drv_argument_not_allowed_with)
267*0a6a1f1dSLionel Sambuc << lastArgumentForKind(D, Args, SanitizerKind::Address)
268*0a6a1f1dSLionel Sambuc << lastArgumentForKind(D, Args, SanitizerKind::Memory);
269f4a2713aSLionel Sambuc if (NeedsTsan && NeedsMsan)
270*0a6a1f1dSLionel Sambuc D.Diag(clang::diag::err_drv_argument_not_allowed_with)
271*0a6a1f1dSLionel Sambuc << lastArgumentForKind(D, Args, SanitizerKind::Thread)
272*0a6a1f1dSLionel Sambuc << lastArgumentForKind(D, Args, SanitizerKind::Memory);
273f4a2713aSLionel Sambuc if (NeedsLsan && NeedsTsan)
274*0a6a1f1dSLionel Sambuc D.Diag(clang::diag::err_drv_argument_not_allowed_with)
275*0a6a1f1dSLionel Sambuc << lastArgumentForKind(D, Args, SanitizerKind::Leak)
276*0a6a1f1dSLionel Sambuc << lastArgumentForKind(D, Args, SanitizerKind::Thread);
277f4a2713aSLionel Sambuc if (NeedsLsan && NeedsMsan)
278*0a6a1f1dSLionel Sambuc D.Diag(clang::diag::err_drv_argument_not_allowed_with)
279*0a6a1f1dSLionel Sambuc << lastArgumentForKind(D, Args, SanitizerKind::Leak)
280*0a6a1f1dSLionel Sambuc << lastArgumentForKind(D, Args, SanitizerKind::Memory);
281*0a6a1f1dSLionel Sambuc // FIXME: Currently -fsanitize=leak is silently ignored in the presence of
282f4a2713aSLionel Sambuc // -fsanitize=address. Perhaps it should print an error, or perhaps
283f4a2713aSLionel Sambuc // -f(-no)sanitize=leak should change whether leak detection is enabled by
284f4a2713aSLionel Sambuc // default in ASan?
285f4a2713aSLionel Sambuc
286f4a2713aSLionel Sambuc // Parse -f(no-)sanitize-blacklist options.
287f4a2713aSLionel Sambuc if (Arg *BLArg = Args.getLastArg(options::OPT_fsanitize_blacklist,
288f4a2713aSLionel Sambuc options::OPT_fno_sanitize_blacklist)) {
289f4a2713aSLionel Sambuc if (BLArg->getOption().matches(options::OPT_fsanitize_blacklist)) {
290f4a2713aSLionel Sambuc std::string BLPath = BLArg->getValue();
291f4a2713aSLionel Sambuc if (llvm::sys::fs::exists(BLPath)) {
292f4a2713aSLionel Sambuc // Validate the blacklist format.
293f4a2713aSLionel Sambuc std::string BLError;
294*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::SpecialCaseList> SCL(
295f4a2713aSLionel Sambuc llvm::SpecialCaseList::create(BLPath, BLError));
296f4a2713aSLionel Sambuc if (!SCL.get())
297*0a6a1f1dSLionel Sambuc D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
298f4a2713aSLionel Sambuc else
299f4a2713aSLionel Sambuc BlacklistFile = BLPath;
300f4a2713aSLionel Sambuc } else {
301*0a6a1f1dSLionel Sambuc D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
302f4a2713aSLionel Sambuc }
303f4a2713aSLionel Sambuc }
304f4a2713aSLionel Sambuc } else {
305f4a2713aSLionel Sambuc // If no -fsanitize-blacklist option is specified, try to look up for
306f4a2713aSLionel Sambuc // blacklist in the resource directory.
307f4a2713aSLionel Sambuc std::string BLPath;
308*0a6a1f1dSLionel Sambuc if (getDefaultBlacklist(D, BLPath) && llvm::sys::fs::exists(BLPath))
309f4a2713aSLionel Sambuc BlacklistFile = BLPath;
310f4a2713aSLionel Sambuc }
311f4a2713aSLionel Sambuc
312*0a6a1f1dSLionel Sambuc // Parse -f[no-]sanitize-memory-track-origins[=level] options.
313*0a6a1f1dSLionel Sambuc if (NeedsMsan) {
314*0a6a1f1dSLionel Sambuc if (Arg *A =
315*0a6a1f1dSLionel Sambuc Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
316*0a6a1f1dSLionel Sambuc options::OPT_fsanitize_memory_track_origins,
317*0a6a1f1dSLionel Sambuc options::OPT_fno_sanitize_memory_track_origins)) {
318*0a6a1f1dSLionel Sambuc if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
319*0a6a1f1dSLionel Sambuc MsanTrackOrigins = 1;
320*0a6a1f1dSLionel Sambuc } else if (A->getOption().matches(
321*0a6a1f1dSLionel Sambuc options::OPT_fno_sanitize_memory_track_origins)) {
322*0a6a1f1dSLionel Sambuc MsanTrackOrigins = 0;
323*0a6a1f1dSLionel Sambuc } else {
324*0a6a1f1dSLionel Sambuc StringRef S = A->getValue();
325*0a6a1f1dSLionel Sambuc if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
326*0a6a1f1dSLionel Sambuc MsanTrackOrigins > 2) {
327*0a6a1f1dSLionel Sambuc D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
328*0a6a1f1dSLionel Sambuc }
329*0a6a1f1dSLionel Sambuc }
330*0a6a1f1dSLionel Sambuc }
331*0a6a1f1dSLionel Sambuc }
332f4a2713aSLionel Sambuc
333*0a6a1f1dSLionel Sambuc // Parse -fsanitize-coverage=N. Currently one of asan/msan/lsan is required.
334*0a6a1f1dSLionel Sambuc if (hasOneOf(Sanitizers, SupportsCoverage)) {
335*0a6a1f1dSLionel Sambuc if (Arg *A = Args.getLastArg(options::OPT_fsanitize_coverage)) {
336*0a6a1f1dSLionel Sambuc StringRef S = A->getValue();
337*0a6a1f1dSLionel Sambuc // Legal values are 0..4.
338*0a6a1f1dSLionel Sambuc if (S.getAsInteger(0, SanitizeCoverage) || SanitizeCoverage < 0 ||
339*0a6a1f1dSLionel Sambuc SanitizeCoverage > 4)
340*0a6a1f1dSLionel Sambuc D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
341*0a6a1f1dSLionel Sambuc }
342*0a6a1f1dSLionel Sambuc }
343*0a6a1f1dSLionel Sambuc
344f4a2713aSLionel Sambuc if (NeedsAsan) {
345*0a6a1f1dSLionel Sambuc AsanSharedRuntime =
346*0a6a1f1dSLionel Sambuc Args.hasArg(options::OPT_shared_libasan) ||
347*0a6a1f1dSLionel Sambuc (TC.getTriple().getEnvironment() == llvm::Triple::Android);
348f4a2713aSLionel Sambuc AsanZeroBaseShadow =
349*0a6a1f1dSLionel Sambuc (TC.getTriple().getEnvironment() == llvm::Triple::Android);
350*0a6a1f1dSLionel Sambuc if (Arg *A =
351*0a6a1f1dSLionel Sambuc Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
352*0a6a1f1dSLionel Sambuc StringRef S = A->getValue();
353*0a6a1f1dSLionel Sambuc // Legal values are 0 and 1, 2, but in future we may add more levels.
354*0a6a1f1dSLionel Sambuc if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
355*0a6a1f1dSLionel Sambuc AsanFieldPadding > 2) {
356*0a6a1f1dSLionel Sambuc D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
357f4a2713aSLionel Sambuc }
358f4a2713aSLionel Sambuc }
359*0a6a1f1dSLionel Sambuc
360*0a6a1f1dSLionel Sambuc if (Arg *WindowsDebugRTArg =
361*0a6a1f1dSLionel Sambuc Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
362*0a6a1f1dSLionel Sambuc options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
363*0a6a1f1dSLionel Sambuc options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
364*0a6a1f1dSLionel Sambuc switch (WindowsDebugRTArg->getOption().getID()) {
365*0a6a1f1dSLionel Sambuc case options::OPT__SLASH_MTd:
366*0a6a1f1dSLionel Sambuc case options::OPT__SLASH_MDd:
367*0a6a1f1dSLionel Sambuc case options::OPT__SLASH_LDd:
368*0a6a1f1dSLionel Sambuc D.Diag(clang::diag::err_drv_argument_not_allowed_with)
369*0a6a1f1dSLionel Sambuc << WindowsDebugRTArg->getAsString(Args)
370*0a6a1f1dSLionel Sambuc << lastArgumentForKind(D, Args, SanitizerKind::Address);
371*0a6a1f1dSLionel Sambuc D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
372*0a6a1f1dSLionel Sambuc }
373*0a6a1f1dSLionel Sambuc }
374*0a6a1f1dSLionel Sambuc }
375*0a6a1f1dSLionel Sambuc
376*0a6a1f1dSLionel Sambuc // Parse -link-cxx-sanitizer flag.
377*0a6a1f1dSLionel Sambuc LinkCXXRuntimes =
378*0a6a1f1dSLionel Sambuc Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
379*0a6a1f1dSLionel Sambuc }
380*0a6a1f1dSLionel Sambuc
toString(const clang::SanitizerSet & Sanitizers)381*0a6a1f1dSLionel Sambuc static std::string toString(const clang::SanitizerSet &Sanitizers) {
382*0a6a1f1dSLionel Sambuc std::string Res;
383*0a6a1f1dSLionel Sambuc #define SANITIZER(NAME, ID) \
384*0a6a1f1dSLionel Sambuc if (Sanitizers.has(clang::SanitizerKind::ID)) { \
385*0a6a1f1dSLionel Sambuc if (!Res.empty()) \
386*0a6a1f1dSLionel Sambuc Res += ","; \
387*0a6a1f1dSLionel Sambuc Res += NAME; \
388*0a6a1f1dSLionel Sambuc }
389*0a6a1f1dSLionel Sambuc #include "clang/Basic/Sanitizers.def"
390*0a6a1f1dSLionel Sambuc return Res;
391f4a2713aSLionel Sambuc }
392f4a2713aSLionel Sambuc
addArgs(const llvm::opt::ArgList & Args,llvm::opt::ArgStringList & CmdArgs) const393f4a2713aSLionel Sambuc void SanitizerArgs::addArgs(const llvm::opt::ArgList &Args,
394f4a2713aSLionel Sambuc llvm::opt::ArgStringList &CmdArgs) const {
395*0a6a1f1dSLionel Sambuc if (Sanitizers.empty())
396f4a2713aSLionel Sambuc return;
397*0a6a1f1dSLionel Sambuc CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
398*0a6a1f1dSLionel Sambuc
399*0a6a1f1dSLionel Sambuc if (!RecoverableSanitizers.empty())
400*0a6a1f1dSLionel Sambuc CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
401*0a6a1f1dSLionel Sambuc toString(RecoverableSanitizers)));
402*0a6a1f1dSLionel Sambuc
403*0a6a1f1dSLionel Sambuc if (UbsanTrapOnError)
404*0a6a1f1dSLionel Sambuc CmdArgs.push_back("-fsanitize-undefined-trap-on-error");
405*0a6a1f1dSLionel Sambuc
406f4a2713aSLionel Sambuc if (!BlacklistFile.empty()) {
407f4a2713aSLionel Sambuc SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
408f4a2713aSLionel Sambuc BlacklistOpt += BlacklistFile;
409f4a2713aSLionel Sambuc CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
410f4a2713aSLionel Sambuc }
411f4a2713aSLionel Sambuc
412f4a2713aSLionel Sambuc if (MsanTrackOrigins)
413*0a6a1f1dSLionel Sambuc CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
414*0a6a1f1dSLionel Sambuc llvm::utostr(MsanTrackOrigins)));
415*0a6a1f1dSLionel Sambuc if (AsanFieldPadding)
416*0a6a1f1dSLionel Sambuc CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
417*0a6a1f1dSLionel Sambuc llvm::utostr(AsanFieldPadding)));
418*0a6a1f1dSLionel Sambuc if (SanitizeCoverage)
419*0a6a1f1dSLionel Sambuc CmdArgs.push_back(Args.MakeArgString("-fsanitize-coverage=" +
420*0a6a1f1dSLionel Sambuc llvm::utostr(SanitizeCoverage)));
421f4a2713aSLionel Sambuc // Workaround for PR16386.
422*0a6a1f1dSLionel Sambuc if (Sanitizers.has(SanitizerKind::Memory))
423f4a2713aSLionel Sambuc CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
424f4a2713aSLionel Sambuc }
425f4a2713aSLionel Sambuc
getDefaultBlacklist(const Driver & D,std::string & BLPath)426*0a6a1f1dSLionel Sambuc bool SanitizerArgs::getDefaultBlacklist(const Driver &D, std::string &BLPath) {
427*0a6a1f1dSLionel Sambuc const char *BlacklistFile = nullptr;
428*0a6a1f1dSLionel Sambuc if (Sanitizers.has(SanitizerKind::Address))
429f4a2713aSLionel Sambuc BlacklistFile = "asan_blacklist.txt";
430*0a6a1f1dSLionel Sambuc else if (Sanitizers.has(SanitizerKind::Memory))
431f4a2713aSLionel Sambuc BlacklistFile = "msan_blacklist.txt";
432*0a6a1f1dSLionel Sambuc else if (Sanitizers.has(SanitizerKind::Thread))
433f4a2713aSLionel Sambuc BlacklistFile = "tsan_blacklist.txt";
434*0a6a1f1dSLionel Sambuc else if (Sanitizers.has(SanitizerKind::DataFlow))
435f4a2713aSLionel Sambuc BlacklistFile = "dfsan_abilist.txt";
436f4a2713aSLionel Sambuc
437f4a2713aSLionel Sambuc if (BlacklistFile) {
438f4a2713aSLionel Sambuc SmallString<64> Path(D.ResourceDir);
439f4a2713aSLionel Sambuc llvm::sys::path::append(Path, BlacklistFile);
440f4a2713aSLionel Sambuc BLPath = Path.str();
441f4a2713aSLionel Sambuc return true;
442f4a2713aSLionel Sambuc }
443f4a2713aSLionel Sambuc return false;
444f4a2713aSLionel Sambuc }
445*0a6a1f1dSLionel Sambuc
parseValue(const char * Value)446*0a6a1f1dSLionel Sambuc unsigned parseValue(const char *Value) {
447*0a6a1f1dSLionel Sambuc unsigned ParsedKind = llvm::StringSwitch<SanitizeKind>(Value)
448*0a6a1f1dSLionel Sambuc #define SANITIZER(NAME, ID) .Case(NAME, ID)
449*0a6a1f1dSLionel Sambuc #define SANITIZER_GROUP(NAME, ID, ALIAS) .Case(NAME, ID##Group)
450*0a6a1f1dSLionel Sambuc #include "clang/Basic/Sanitizers.def"
451*0a6a1f1dSLionel Sambuc .Default(SanitizeKind());
452*0a6a1f1dSLionel Sambuc return ParsedKind;
453*0a6a1f1dSLionel Sambuc }
454*0a6a1f1dSLionel Sambuc
expandGroups(unsigned Kinds)455*0a6a1f1dSLionel Sambuc unsigned expandGroups(unsigned Kinds) {
456*0a6a1f1dSLionel Sambuc #define SANITIZER(NAME, ID)
457*0a6a1f1dSLionel Sambuc #define SANITIZER_GROUP(NAME, ID, ALIAS) if (Kinds & ID##Group) Kinds |= ID;
458*0a6a1f1dSLionel Sambuc #include "clang/Basic/Sanitizers.def"
459*0a6a1f1dSLionel Sambuc return Kinds;
460*0a6a1f1dSLionel Sambuc }
461*0a6a1f1dSLionel Sambuc
parseArgValues(const Driver & D,const llvm::opt::Arg * A,bool DiagnoseErrors)462*0a6a1f1dSLionel Sambuc unsigned parseArgValues(const Driver &D, const llvm::opt::Arg *A,
463*0a6a1f1dSLionel Sambuc bool DiagnoseErrors) {
464*0a6a1f1dSLionel Sambuc assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
465*0a6a1f1dSLionel Sambuc A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
466*0a6a1f1dSLionel Sambuc A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
467*0a6a1f1dSLionel Sambuc A->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) &&
468*0a6a1f1dSLionel Sambuc "Invalid argument in parseArgValues!");
469*0a6a1f1dSLionel Sambuc unsigned Kinds = 0;
470*0a6a1f1dSLionel Sambuc for (unsigned I = 0, N = A->getNumValues(); I != N; ++I) {
471*0a6a1f1dSLionel Sambuc const char *Value = A->getValue(I);
472*0a6a1f1dSLionel Sambuc unsigned Kind;
473*0a6a1f1dSLionel Sambuc // Special case: don't accept -fsanitize=all.
474*0a6a1f1dSLionel Sambuc if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
475*0a6a1f1dSLionel Sambuc 0 == strcmp("all", Value))
476*0a6a1f1dSLionel Sambuc Kind = 0;
477*0a6a1f1dSLionel Sambuc else
478*0a6a1f1dSLionel Sambuc Kind = parseValue(Value);
479*0a6a1f1dSLionel Sambuc
480*0a6a1f1dSLionel Sambuc if (Kind)
481*0a6a1f1dSLionel Sambuc Kinds |= Kind;
482*0a6a1f1dSLionel Sambuc else if (DiagnoseErrors)
483*0a6a1f1dSLionel Sambuc D.Diag(clang::diag::err_drv_unsupported_option_argument)
484*0a6a1f1dSLionel Sambuc << A->getOption().getName() << Value;
485*0a6a1f1dSLionel Sambuc }
486*0a6a1f1dSLionel Sambuc return Kinds;
487*0a6a1f1dSLionel Sambuc }
488*0a6a1f1dSLionel Sambuc
lastArgumentForMask(const Driver & D,const llvm::opt::ArgList & Args,unsigned Mask)489*0a6a1f1dSLionel Sambuc std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
490*0a6a1f1dSLionel Sambuc unsigned Mask) {
491*0a6a1f1dSLionel Sambuc for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
492*0a6a1f1dSLionel Sambuc E = Args.rend();
493*0a6a1f1dSLionel Sambuc I != E; ++I) {
494*0a6a1f1dSLionel Sambuc const auto *Arg = *I;
495*0a6a1f1dSLionel Sambuc if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
496*0a6a1f1dSLionel Sambuc unsigned AddKinds = expandGroups(parseArgValues(D, Arg, false));
497*0a6a1f1dSLionel Sambuc if (AddKinds & Mask)
498*0a6a1f1dSLionel Sambuc return describeSanitizeArg(Arg, Mask);
499*0a6a1f1dSLionel Sambuc } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
500*0a6a1f1dSLionel Sambuc unsigned RemoveKinds = expandGroups(parseArgValues(D, Arg, false));
501*0a6a1f1dSLionel Sambuc Mask &= ~RemoveKinds;
502*0a6a1f1dSLionel Sambuc }
503*0a6a1f1dSLionel Sambuc }
504*0a6a1f1dSLionel Sambuc llvm_unreachable("arg list didn't provide expected value");
505*0a6a1f1dSLionel Sambuc }
506*0a6a1f1dSLionel Sambuc
describeSanitizeArg(const llvm::opt::Arg * A,unsigned Mask)507*0a6a1f1dSLionel Sambuc std::string describeSanitizeArg(const llvm::opt::Arg *A, unsigned Mask) {
508*0a6a1f1dSLionel Sambuc assert(A->getOption().matches(options::OPT_fsanitize_EQ)
509*0a6a1f1dSLionel Sambuc && "Invalid argument in describeSanitizerArg!");
510*0a6a1f1dSLionel Sambuc
511*0a6a1f1dSLionel Sambuc std::string Sanitizers;
512*0a6a1f1dSLionel Sambuc for (unsigned I = 0, N = A->getNumValues(); I != N; ++I) {
513*0a6a1f1dSLionel Sambuc if (expandGroups(parseValue(A->getValue(I))) & Mask) {
514*0a6a1f1dSLionel Sambuc if (!Sanitizers.empty())
515*0a6a1f1dSLionel Sambuc Sanitizers += ",";
516*0a6a1f1dSLionel Sambuc Sanitizers += A->getValue(I);
517*0a6a1f1dSLionel Sambuc }
518*0a6a1f1dSLionel Sambuc }
519*0a6a1f1dSLionel Sambuc
520*0a6a1f1dSLionel Sambuc assert(!Sanitizers.empty() && "arg didn't provide expected value");
521*0a6a1f1dSLionel Sambuc return "-fsanitize=" + Sanitizers;
522*0a6a1f1dSLionel Sambuc }
523