/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/SanitizerArgs.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- SanitizerArgs.cpp - Arguments for sanitizer tools ---------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | #include "clang/Driver/SanitizerArgs.h" |
9 | | #include "ToolChains/CommonArgs.h" |
10 | | #include "clang/Basic/Sanitizers.h" |
11 | | #include "clang/Driver/Driver.h" |
12 | | #include "clang/Driver/DriverDiagnostic.h" |
13 | | #include "clang/Driver/Options.h" |
14 | | #include "clang/Driver/ToolChain.h" |
15 | | #include "llvm/ADT/StringExtras.h" |
16 | | #include "llvm/ADT/StringRef.h" |
17 | | #include "llvm/ADT/StringSwitch.h" |
18 | | #include "llvm/Support/Path.h" |
19 | | #include "llvm/Support/SpecialCaseList.h" |
20 | | #include "llvm/Support/VirtualFileSystem.h" |
21 | | #include "llvm/TargetParser/AArch64TargetParser.h" |
22 | | #include "llvm/TargetParser/RISCVTargetParser.h" |
23 | | #include "llvm/TargetParser/TargetParser.h" |
24 | | #include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h" |
25 | | #include <memory> |
26 | | |
27 | | using namespace clang; |
28 | | using namespace clang::driver; |
29 | | using namespace llvm::opt; |
30 | | |
31 | | static const SanitizerMask NeedsUbsanRt = |
32 | | SanitizerKind::Undefined | SanitizerKind::Integer | |
33 | | SanitizerKind::ImplicitConversion | SanitizerKind::Nullability | |
34 | | SanitizerKind::CFI | SanitizerKind::FloatDivideByZero | |
35 | | SanitizerKind::ObjCCast; |
36 | | static const SanitizerMask NeedsUbsanCxxRt = |
37 | | SanitizerKind::Vptr | SanitizerKind::CFI; |
38 | | static const SanitizerMask NotAllowedWithTrap = SanitizerKind::Vptr; |
39 | | static const SanitizerMask NotAllowedWithMinimalRuntime = SanitizerKind::Vptr; |
40 | | static const SanitizerMask NotAllowedWithExecuteOnly = |
41 | | SanitizerKind::Function | SanitizerKind::KCFI; |
42 | | static const SanitizerMask RequiresPIE = |
43 | | SanitizerKind::DataFlow | SanitizerKind::Scudo; |
44 | | static const SanitizerMask NeedsUnwindTables = |
45 | | SanitizerKind::Address | SanitizerKind::HWAddress | SanitizerKind::Thread | |
46 | | SanitizerKind::Memory | SanitizerKind::DataFlow; |
47 | | static const SanitizerMask SupportsCoverage = |
48 | | SanitizerKind::Address | SanitizerKind::HWAddress | |
49 | | SanitizerKind::KernelAddress | SanitizerKind::KernelHWAddress | |
50 | | SanitizerKind::MemtagStack | SanitizerKind::MemtagHeap | |
51 | | SanitizerKind::MemtagGlobals | SanitizerKind::Memory | |
52 | | SanitizerKind::KernelMemory | SanitizerKind::Leak | |
53 | | SanitizerKind::Undefined | SanitizerKind::Integer | SanitizerKind::Bounds | |
54 | | SanitizerKind::ImplicitConversion | SanitizerKind::Nullability | |
55 | | SanitizerKind::DataFlow | SanitizerKind::Fuzzer | |
56 | | SanitizerKind::FuzzerNoLink | SanitizerKind::FloatDivideByZero | |
57 | | SanitizerKind::SafeStack | SanitizerKind::ShadowCallStack | |
58 | | SanitizerKind::Thread | SanitizerKind::ObjCCast | SanitizerKind::KCFI; |
59 | | static const SanitizerMask RecoverableByDefault = |
60 | | SanitizerKind::Undefined | SanitizerKind::Integer | |
61 | | SanitizerKind::ImplicitConversion | SanitizerKind::Nullability | |
62 | | SanitizerKind::FloatDivideByZero | SanitizerKind::ObjCCast; |
63 | | static const SanitizerMask Unrecoverable = |
64 | | SanitizerKind::Unreachable | SanitizerKind::Return; |
65 | | static const SanitizerMask AlwaysRecoverable = SanitizerKind::KernelAddress | |
66 | | SanitizerKind::KernelHWAddress | |
67 | | SanitizerKind::KCFI; |
68 | | static const SanitizerMask NeedsLTO = SanitizerKind::CFI; |
69 | | static const SanitizerMask TrappingSupported = |
70 | | (SanitizerKind::Undefined & ~SanitizerKind::Vptr) | SanitizerKind::Integer | |
71 | | SanitizerKind::Nullability | SanitizerKind::LocalBounds | |
72 | | SanitizerKind::CFI | SanitizerKind::FloatDivideByZero | |
73 | | SanitizerKind::ObjCCast; |
74 | | static const SanitizerMask TrappingDefault = SanitizerKind::CFI; |
75 | | static const SanitizerMask CFIClasses = |
76 | | SanitizerKind::CFIVCall | SanitizerKind::CFINVCall | |
77 | | SanitizerKind::CFIMFCall | SanitizerKind::CFIDerivedCast | |
78 | | SanitizerKind::CFIUnrelatedCast; |
79 | | static const SanitizerMask CompatibleWithMinimalRuntime = |
80 | | TrappingSupported | SanitizerKind::Scudo | SanitizerKind::ShadowCallStack | |
81 | | SanitizerKind::MemtagStack | SanitizerKind::MemtagHeap | |
82 | | SanitizerKind::MemtagGlobals | SanitizerKind::KCFI; |
83 | | |
84 | | enum CoverageFeature { |
85 | | CoverageFunc = 1 << 0, |
86 | | CoverageBB = 1 << 1, |
87 | | CoverageEdge = 1 << 2, |
88 | | CoverageIndirCall = 1 << 3, |
89 | | CoverageTraceBB = 1 << 4, // Deprecated. |
90 | | CoverageTraceCmp = 1 << 5, |
91 | | CoverageTraceDiv = 1 << 6, |
92 | | CoverageTraceGep = 1 << 7, |
93 | | Coverage8bitCounters = 1 << 8, // Deprecated. |
94 | | CoverageTracePC = 1 << 9, |
95 | | CoverageTracePCGuard = 1 << 10, |
96 | | CoverageNoPrune = 1 << 11, |
97 | | CoverageInline8bitCounters = 1 << 12, |
98 | | CoveragePCTable = 1 << 13, |
99 | | CoverageStackDepth = 1 << 14, |
100 | | CoverageInlineBoolFlag = 1 << 15, |
101 | | CoverageTraceLoads = 1 << 16, |
102 | | CoverageTraceStores = 1 << 17, |
103 | | CoverageControlFlow = 1 << 18, |
104 | | }; |
105 | | |
106 | | enum BinaryMetadataFeature { |
107 | | BinaryMetadataCovered = 1 << 0, |
108 | | BinaryMetadataAtomics = 1 << 1, |
109 | | BinaryMetadataUAR = 1 << 2, |
110 | | }; |
111 | | |
112 | | /// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any |
113 | | /// invalid components. Returns a SanitizerMask. |
114 | | static SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A, |
115 | | bool DiagnoseErrors); |
116 | | |
117 | | /// Parse -f(no-)?sanitize-coverage= flag values, diagnosing any invalid |
118 | | /// components. Returns OR of members of \c CoverageFeature enumeration. |
119 | | static int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A, |
120 | | bool DiagnoseErrors); |
121 | | |
122 | | /// Parse -f(no-)?sanitize-metadata= flag values, diagnosing any invalid |
123 | | /// components. Returns OR of members of \c BinaryMetadataFeature enumeration. |
124 | | static int parseBinaryMetadataFeatures(const Driver &D, const llvm::opt::Arg *A, |
125 | | bool DiagnoseErrors); |
126 | | |
127 | | /// Produce an argument string from ArgList \p Args, which shows how it |
128 | | /// provides some sanitizer kind from \p Mask. For example, the argument list |
129 | | /// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt |
130 | | /// would produce "-fsanitize=vptr". |
131 | | static std::string lastArgumentForMask(const Driver &D, |
132 | | const llvm::opt::ArgList &Args, |
133 | | SanitizerMask Mask); |
134 | | |
135 | | /// Produce an argument string from argument \p A, which shows how it provides |
136 | | /// a value in \p Mask. For instance, the argument |
137 | | /// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce |
138 | | /// "-fsanitize=alignment". |
139 | | static std::string describeSanitizeArg(const llvm::opt::Arg *A, |
140 | | SanitizerMask Mask); |
141 | | |
142 | | /// Produce a string containing comma-separated names of sanitizers in \p |
143 | | /// Sanitizers set. |
144 | | static std::string toString(const clang::SanitizerSet &Sanitizers); |
145 | | |
146 | | /// Return true if an execute-only target disallows data access to code |
147 | | /// sections. |
148 | | static bool isExecuteOnlyTarget(const llvm::Triple &Triple, |
149 | 4.47k | const llvm::opt::ArgList &Args) { |
150 | 4.47k | if (Triple.isPS5()) |
151 | 96 | return true; |
152 | 4.37k | return Args.hasFlagNoClaim(options::OPT_mexecute_only, |
153 | 4.37k | options::OPT_mno_execute_only, false); |
154 | 4.47k | } |
155 | | |
156 | | static void validateSpecialCaseListFormat(const Driver &D, |
157 | | std::vector<std::string> &SCLFiles, |
158 | | unsigned MalformedSCLErrorDiagID, |
159 | 116k | bool DiagnoseErrors) { |
160 | 116k | if (SCLFiles.empty()) |
161 | 116k | return; |
162 | | |
163 | 234 | std::string BLError; |
164 | 234 | std::unique_ptr<llvm::SpecialCaseList> SCL( |
165 | 234 | llvm::SpecialCaseList::create(SCLFiles, D.getVFS(), BLError)); |
166 | 234 | if (!SCL.get() && DiagnoseErrors3 ) |
167 | 1 | D.Diag(MalformedSCLErrorDiagID) << BLError; |
168 | 234 | } |
169 | | |
170 | | static void addDefaultIgnorelists(const Driver &D, SanitizerMask Kinds, |
171 | | std::vector<std::string> &IgnorelistFiles, |
172 | 58.1k | bool DiagnoseErrors) { |
173 | 58.1k | struct Ignorelist { |
174 | 58.1k | const char *File; |
175 | 58.1k | SanitizerMask Mask; |
176 | 58.1k | } Ignorelists[] = {{"asan_ignorelist.txt", SanitizerKind::Address}, |
177 | 58.1k | {"hwasan_ignorelist.txt", SanitizerKind::HWAddress}, |
178 | 58.1k | {"memtag_ignorelist.txt", SanitizerKind::MemTag}, |
179 | 58.1k | {"msan_ignorelist.txt", SanitizerKind::Memory}, |
180 | 58.1k | {"tsan_ignorelist.txt", SanitizerKind::Thread}, |
181 | 58.1k | {"dfsan_abilist.txt", SanitizerKind::DataFlow}, |
182 | 58.1k | {"cfi_ignorelist.txt", SanitizerKind::CFI}, |
183 | 58.1k | {"ubsan_ignorelist.txt", |
184 | 58.1k | SanitizerKind::Undefined | SanitizerKind::Integer | |
185 | 58.1k | SanitizerKind::Nullability | |
186 | 58.1k | SanitizerKind::FloatDivideByZero}}; |
187 | | |
188 | 465k | for (auto BL : Ignorelists) { |
189 | 465k | if (!(Kinds & BL.Mask)) |
190 | 463k | continue; |
191 | | |
192 | 1.54k | clang::SmallString<64> Path(D.ResourceDir); |
193 | 1.54k | llvm::sys::path::append(Path, "share", BL.File); |
194 | 1.54k | if (D.getVFS().exists(Path)) |
195 | 216 | IgnorelistFiles.push_back(std::string(Path.str())); |
196 | 1.32k | else if (BL.Mask == SanitizerKind::CFI && DiagnoseErrors25 ) |
197 | | // If cfi_ignorelist.txt cannot be found in the resource dir, driver |
198 | | // should fail. |
199 | 10 | D.Diag(clang::diag::err_drv_missing_sanitizer_ignorelist) << Path; |
200 | 1.54k | } |
201 | 58.1k | validateSpecialCaseListFormat( |
202 | 58.1k | D, IgnorelistFiles, clang::diag::err_drv_malformed_sanitizer_ignorelist, |
203 | 58.1k | DiagnoseErrors); |
204 | 58.1k | } |
205 | | |
206 | | /// Parse -f(no-)?sanitize-(coverage-)?(allow|ignore)list argument's values, |
207 | | /// diagnosing any invalid file paths and validating special case list format. |
208 | | static void parseSpecialCaseListArg(const Driver &D, |
209 | | const llvm::opt::ArgList &Args, |
210 | | std::vector<std::string> &SCLFiles, |
211 | | llvm::opt::OptSpecifier SCLOptionID, |
212 | | llvm::opt::OptSpecifier NoSCLOptionID, |
213 | | unsigned MalformedSCLErrorDiagID, |
214 | 58.6k | bool DiagnoseErrors) { |
215 | 891k | for (const auto *Arg : Args) { |
216 | | // Match -fsanitize-(coverage-)?(allow|ignore)list. |
217 | 891k | if (Arg->getOption().matches(SCLOptionID)) { |
218 | 55 | Arg->claim(); |
219 | 55 | std::string SCLPath = Arg->getValue(); |
220 | 55 | if (D.getVFS().exists(SCLPath)) { |
221 | 52 | SCLFiles.push_back(SCLPath); |
222 | 52 | } else if (3 DiagnoseErrors3 ) { |
223 | 1 | D.Diag(clang::diag::err_drv_no_such_file) << SCLPath; |
224 | 1 | } |
225 | | // Match -fno-sanitize-ignorelist. |
226 | 891k | } else if (Arg->getOption().matches(NoSCLOptionID)) { |
227 | 15 | Arg->claim(); |
228 | 15 | SCLFiles.clear(); |
229 | 15 | } |
230 | 891k | } |
231 | 58.6k | validateSpecialCaseListFormat(D, SCLFiles, MalformedSCLErrorDiagID, |
232 | 58.6k | DiagnoseErrors); |
233 | 58.6k | } |
234 | | |
235 | | /// Sets group bits for every group that has at least one representative already |
236 | | /// enabled in \p Kinds. |
237 | 116k | static SanitizerMask setGroupBits(SanitizerMask Kinds) { |
238 | 116k | #define SANITIZER(NAME, ID) |
239 | 116k | #define SANITIZER_GROUP(NAME, ID, ALIAS) \ |
240 | 1.39M | if (Kinds & SanitizerKind::ID) \ |
241 | 1.39M | Kinds |= SanitizerKind::ID1.28M ##Group; |
242 | 116k | #include "clang/Basic/Sanitizers.def" |
243 | 116k | return Kinds; |
244 | 116k | } |
245 | | |
246 | | static SanitizerMask parseSanitizeTrapArgs(const Driver &D, |
247 | | const llvm::opt::ArgList &Args, |
248 | 58.1k | bool DiagnoseErrors) { |
249 | 58.1k | SanitizerMask TrapRemove; // During the loop below, the accumulated set of |
250 | | // sanitizers disabled by the current sanitizer |
251 | | // argument or any argument after it. |
252 | 58.1k | SanitizerMask TrappingKinds; |
253 | 58.1k | SanitizerMask TrappingSupportedWithGroups = setGroupBits(TrappingSupported); |
254 | | |
255 | 888k | for (const llvm::opt::Arg *Arg : llvm::reverse(Args)) { |
256 | 888k | if (Arg->getOption().matches(options::OPT_fsanitize_trap_EQ)) { |
257 | 50 | Arg->claim(); |
258 | 50 | SanitizerMask Add = parseArgValues(D, Arg, true); |
259 | 50 | Add &= ~TrapRemove; |
260 | 50 | SanitizerMask InvalidValues = Add & ~TrappingSupportedWithGroups; |
261 | 50 | if (InvalidValues && DiagnoseErrors5 ) { |
262 | 3 | SanitizerSet S; |
263 | 3 | S.Mask = InvalidValues; |
264 | 3 | D.Diag(diag::err_drv_unsupported_option_argument) |
265 | 3 | << Arg->getSpelling() << toString(S); |
266 | 3 | } |
267 | 50 | TrappingKinds |= expandSanitizerGroups(Add) & ~TrapRemove; |
268 | 888k | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) { |
269 | 21 | Arg->claim(); |
270 | 21 | TrapRemove |= |
271 | 21 | expandSanitizerGroups(parseArgValues(D, Arg, DiagnoseErrors)); |
272 | 21 | } |
273 | 888k | } |
274 | | |
275 | | // Apply default trapping behavior. |
276 | 58.1k | TrappingKinds |= TrappingDefault & ~TrapRemove; |
277 | | |
278 | 58.1k | return TrappingKinds; |
279 | 58.1k | } |
280 | | |
281 | 8 | bool SanitizerArgs::needsFuzzerInterceptors() const { |
282 | 8 | return needsFuzzer() && !needsAsanRt() && !needsTsanRt() && !needsMsanRt(); |
283 | 8 | } |
284 | | |
285 | 14.6k | bool SanitizerArgs::needsUbsanRt() const { |
286 | | // All of these include ubsan. |
287 | 14.6k | if (needsAsanRt() || needsMsanRt()14.4k || needsHwasanRt()14.4k || needsTsanRt()14.3k || |
288 | 14.6k | needsDfsanRt()14.3k || needsLsanRt()14.3k || needsCfiDiagRt()14.2k || |
289 | 14.6k | (14.2k needsScudoRt()14.2k && !requiresMinimalRuntime()38 )) |
290 | 402 | return false; |
291 | | |
292 | 14.2k | return (Sanitizers.Mask & NeedsUbsanRt & ~TrapSanitizers.Mask) || |
293 | 14.2k | CoverageFeatures14.1k ; |
294 | 14.6k | } |
295 | | |
296 | 1.94k | bool SanitizerArgs::needsCfiRt() const { |
297 | 1.94k | return !(Sanitizers.Mask & SanitizerKind::CFI & ~TrapSanitizers.Mask) && |
298 | 1.94k | CfiCrossDso1.94k && !ImplicitCfiRuntime2 ; |
299 | 1.94k | } |
300 | | |
301 | 16.2k | bool SanitizerArgs::needsCfiDiagRt() const { |
302 | 16.2k | return (Sanitizers.Mask & SanitizerKind::CFI & ~TrapSanitizers.Mask) && |
303 | 16.2k | CfiCrossDso9 && !ImplicitCfiRuntime4 ; |
304 | 16.2k | } |
305 | | |
306 | 297 | bool SanitizerArgs::requiresPIE() const { |
307 | 297 | return NeedPIE || (Sanitizers.Mask & RequiresPIE); |
308 | 297 | } |
309 | | |
310 | 5.51k | bool SanitizerArgs::needsUnwindTables() const { |
311 | 5.51k | return static_cast<bool>(Sanitizers.Mask & NeedsUnwindTables); |
312 | 5.51k | } |
313 | | |
314 | 95.7k | bool SanitizerArgs::needsLTO() const { |
315 | 95.7k | return static_cast<bool>(Sanitizers.Mask & NeedsLTO); |
316 | 95.7k | } |
317 | | |
318 | | SanitizerArgs::SanitizerArgs(const ToolChain &TC, |
319 | | const llvm::opt::ArgList &Args, |
320 | 58.1k | bool DiagnoseErrors) { |
321 | 58.1k | SanitizerMask AllRemove; // During the loop below, the accumulated set of |
322 | | // sanitizers disabled by the current sanitizer |
323 | | // argument or any argument after it. |
324 | 58.1k | SanitizerMask AllAddedKinds; // Mask of all sanitizers ever enabled by |
325 | | // -fsanitize= flags (directly or via group |
326 | | // expansion), some of which may be disabled |
327 | | // later. Used to carefully prune |
328 | | // unused-argument diagnostics. |
329 | 58.1k | SanitizerMask DiagnosedKinds; // All Kinds we have diagnosed up to now. |
330 | | // Used to deduplicate diagnostics. |
331 | 58.1k | SanitizerMask Kinds; |
332 | 58.1k | const SanitizerMask Supported = setGroupBits(TC.getSupportedSanitizers()); |
333 | | |
334 | 58.1k | CfiCrossDso = Args.hasFlag(options::OPT_fsanitize_cfi_cross_dso, |
335 | 58.1k | options::OPT_fno_sanitize_cfi_cross_dso, false); |
336 | | |
337 | 58.1k | ToolChain::RTTIMode RTTIMode = TC.getRTTIMode(); |
338 | | |
339 | 58.1k | const Driver &D = TC.getDriver(); |
340 | 58.1k | SanitizerMask TrappingKinds = parseSanitizeTrapArgs(D, Args, DiagnoseErrors); |
341 | 58.1k | SanitizerMask InvalidTrappingKinds = TrappingKinds & NotAllowedWithTrap; |
342 | | |
343 | 58.1k | MinimalRuntime = |
344 | 58.1k | Args.hasFlag(options::OPT_fsanitize_minimal_runtime, |
345 | 58.1k | options::OPT_fno_sanitize_minimal_runtime, MinimalRuntime); |
346 | | |
347 | | // The object size sanitizer should not be enabled at -O0. |
348 | 58.1k | Arg *OptLevel = Args.getLastArg(options::OPT_O_Group); |
349 | 58.1k | bool RemoveObjectSizeAtO0 = |
350 | 58.1k | !OptLevel || OptLevel->getOption().matches(options::OPT_O0)6.03k ; |
351 | | |
352 | 888k | for (const llvm::opt::Arg *Arg : llvm::reverse(Args)) { |
353 | 888k | if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) { |
354 | 2.23k | Arg->claim(); |
355 | 2.23k | SanitizerMask Add = parseArgValues(D, Arg, DiagnoseErrors); |
356 | | |
357 | 2.23k | if (RemoveObjectSizeAtO0) { |
358 | 2.15k | AllRemove |= SanitizerKind::ObjectSize; |
359 | | |
360 | | // The user explicitly enabled the object size sanitizer. Warn |
361 | | // that this does nothing at -O0. |
362 | 2.15k | if ((Add & SanitizerKind::ObjectSize) && DiagnoseErrors12 ) |
363 | 4 | D.Diag(diag::warn_drv_object_size_disabled_O0) |
364 | 4 | << Arg->getAsString(Args); |
365 | 2.15k | } |
366 | | |
367 | 2.23k | AllAddedKinds |= expandSanitizerGroups(Add); |
368 | | |
369 | | // Avoid diagnosing any sanitizer which is disabled later. |
370 | 2.23k | Add &= ~AllRemove; |
371 | | // At this point we have not expanded groups, so any unsupported |
372 | | // sanitizers in Add are those which have been explicitly enabled. |
373 | | // Diagnose them. |
374 | 2.23k | if (SanitizerMask KindsToDiagnose = |
375 | 2.23k | Add & InvalidTrappingKinds & ~DiagnosedKinds) { |
376 | 6 | if (DiagnoseErrors) { |
377 | 2 | std::string Desc = describeSanitizeArg(Arg, KindsToDiagnose); |
378 | 2 | D.Diag(diag::err_drv_argument_not_allowed_with) |
379 | 2 | << Desc << "-fsanitize-trap=undefined"; |
380 | 2 | } |
381 | 6 | DiagnosedKinds |= KindsToDiagnose; |
382 | 6 | } |
383 | 2.23k | Add &= ~InvalidTrappingKinds; |
384 | | |
385 | 2.23k | if (MinimalRuntime) { |
386 | 62 | if (SanitizerMask KindsToDiagnose = |
387 | 62 | Add & NotAllowedWithMinimalRuntime & ~DiagnosedKinds) { |
388 | 3 | if (DiagnoseErrors) { |
389 | 1 | std::string Desc = describeSanitizeArg(Arg, KindsToDiagnose); |
390 | 1 | D.Diag(diag::err_drv_argument_not_allowed_with) |
391 | 1 | << Desc << "-fsanitize-minimal-runtime"; |
392 | 1 | } |
393 | 3 | DiagnosedKinds |= KindsToDiagnose; |
394 | 3 | } |
395 | 62 | Add &= ~NotAllowedWithMinimalRuntime; |
396 | 62 | } |
397 | | |
398 | 2.23k | if (llvm::opt::Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) { |
399 | 3 | StringRef CM = A->getValue(); |
400 | 3 | if (CM != "small" && |
401 | 3 | (Add & SanitizerKind::Function & ~DiagnosedKinds)) { |
402 | 3 | if (DiagnoseErrors) |
403 | 1 | D.Diag(diag::err_drv_argument_only_allowed_with) |
404 | 1 | << "-fsanitize=function" |
405 | 1 | << "-mcmodel=small"; |
406 | 3 | Add &= ~SanitizerKind::Function; |
407 | 3 | DiagnosedKinds |= SanitizerKind::Function; |
408 | 3 | } |
409 | 3 | } |
410 | | // -fsanitize=function and -fsanitize=kcfi instrument indirect function |
411 | | // calls to load a type hash before the function label. Therefore, an |
412 | | // execute-only target doesn't support the function and kcfi sanitizers. |
413 | 2.23k | const llvm::Triple &Triple = TC.getTriple(); |
414 | 2.23k | if (isExecuteOnlyTarget(Triple, Args)) { |
415 | 51 | if (SanitizerMask KindsToDiagnose = |
416 | 51 | Add & NotAllowedWithExecuteOnly & ~DiagnosedKinds) { |
417 | 17 | if (DiagnoseErrors) { |
418 | 7 | std::string Desc = describeSanitizeArg(Arg, KindsToDiagnose); |
419 | 7 | D.Diag(diag::err_drv_argument_not_allowed_with) |
420 | 7 | << Desc << Triple.str(); |
421 | 7 | } |
422 | 17 | DiagnosedKinds |= KindsToDiagnose; |
423 | 17 | } |
424 | 51 | Add &= ~NotAllowedWithExecuteOnly; |
425 | 51 | } |
426 | | |
427 | | // FIXME: Make CFI on member function calls compatible with cross-DSO CFI. |
428 | | // There are currently two problems: |
429 | | // - Virtual function call checks need to pass a pointer to the function |
430 | | // address to llvm.type.test and a pointer to the address point to the |
431 | | // diagnostic function. Currently we pass the same pointer to both |
432 | | // places. |
433 | | // - Non-virtual function call checks may need to check multiple type |
434 | | // identifiers. |
435 | | // Fixing both of those may require changes to the cross-DSO CFI |
436 | | // interface. |
437 | 2.23k | if (CfiCrossDso && (Add & SanitizerKind::CFIMFCall & ~DiagnosedKinds)19 ) { |
438 | 1 | if (DiagnoseErrors) |
439 | 1 | D.Diag(diag::err_drv_argument_not_allowed_with) |
440 | 1 | << "-fsanitize=cfi-mfcall" |
441 | 1 | << "-fsanitize-cfi-cross-dso"; |
442 | 1 | Add &= ~SanitizerKind::CFIMFCall; |
443 | 1 | DiagnosedKinds |= SanitizerKind::CFIMFCall; |
444 | 1 | } |
445 | | |
446 | 2.23k | if (SanitizerMask KindsToDiagnose = Add & ~Supported & ~DiagnosedKinds) { |
447 | 72 | if (DiagnoseErrors) { |
448 | 27 | std::string Desc = describeSanitizeArg(Arg, KindsToDiagnose); |
449 | 27 | D.Diag(diag::err_drv_unsupported_opt_for_target) |
450 | 27 | << Desc << TC.getTriple().str(); |
451 | 27 | } |
452 | 72 | DiagnosedKinds |= KindsToDiagnose; |
453 | 72 | } |
454 | 2.23k | Add &= Supported; |
455 | | |
456 | | // Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups |
457 | | // so we don't error out if -fno-rtti and -fsanitize=undefined were |
458 | | // passed. |
459 | 2.23k | if ((Add & SanitizerKind::Vptr) && (RTTIMode == ToolChain::RM_Disabled)29 ) { |
460 | 9 | if (const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg()) { |
461 | 5 | assert(NoRTTIArg->getOption().matches(options::OPT_fno_rtti) && |
462 | 5 | "RTTI disabled without -fno-rtti option?"); |
463 | | // The user explicitly passed -fno-rtti with -fsanitize=vptr, but |
464 | | // the vptr sanitizer requires RTTI, so this is a user error. |
465 | 5 | if (DiagnoseErrors) |
466 | 3 | D.Diag(diag::err_drv_argument_not_allowed_with) |
467 | 3 | << "-fsanitize=vptr" << NoRTTIArg->getAsString(Args); |
468 | 5 | } else { |
469 | | // The vptr sanitizer requires RTTI, but RTTI is disabled (by |
470 | | // default). Warn that the vptr sanitizer is being disabled. |
471 | 4 | if (DiagnoseErrors) |
472 | 2 | D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default); |
473 | 4 | } |
474 | | |
475 | | // Take out the Vptr sanitizer from the enabled sanitizers |
476 | 9 | AllRemove |= SanitizerKind::Vptr; |
477 | 9 | } |
478 | | |
479 | 2.23k | Add = expandSanitizerGroups(Add); |
480 | | // Group expansion may have enabled a sanitizer which is disabled later. |
481 | 2.23k | Add &= ~AllRemove; |
482 | | // Silently discard any unsupported sanitizers implicitly enabled through |
483 | | // group expansion. |
484 | 2.23k | Add &= ~InvalidTrappingKinds; |
485 | 2.23k | if (MinimalRuntime) { |
486 | 62 | Add &= ~NotAllowedWithMinimalRuntime; |
487 | 62 | } |
488 | | // NotAllowedWithExecuteOnly is silently discarded on an execute-only |
489 | | // target if implicitly enabled through group expansion. |
490 | 2.23k | if (isExecuteOnlyTarget(Triple, Args)) |
491 | 51 | Add &= ~NotAllowedWithExecuteOnly; |
492 | 2.23k | if (CfiCrossDso) |
493 | 19 | Add &= ~SanitizerKind::CFIMFCall; |
494 | 2.23k | Add &= Supported; |
495 | | |
496 | 2.23k | if (Add & SanitizerKind::Fuzzer) |
497 | 41 | Add |= SanitizerKind::FuzzerNoLink; |
498 | | |
499 | | // Enable coverage if the fuzzing flag is set. |
500 | 2.23k | if (Add & SanitizerKind::FuzzerNoLink) { |
501 | 43 | CoverageFeatures |= CoverageInline8bitCounters | CoverageIndirCall | |
502 | 43 | CoverageTraceCmp | CoveragePCTable; |
503 | | // Due to TLS differences, stack depth tracking is only enabled on Linux |
504 | 43 | if (TC.getTriple().isOSLinux()) |
505 | 20 | CoverageFeatures |= CoverageStackDepth; |
506 | 43 | } |
507 | | |
508 | 2.23k | Kinds |= Add; |
509 | 885k | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) { |
510 | 70 | Arg->claim(); |
511 | 70 | SanitizerMask Remove = parseArgValues(D, Arg, DiagnoseErrors); |
512 | 70 | AllRemove |= expandSanitizerGroups(Remove); |
513 | 70 | } |
514 | 888k | } |
515 | | |
516 | 58.1k | std::pair<SanitizerMask, SanitizerMask> IncompatibleGroups[] = { |
517 | 58.1k | std::make_pair(SanitizerKind::Address, |
518 | 58.1k | SanitizerKind::Thread | SanitizerKind::Memory), |
519 | 58.1k | std::make_pair(SanitizerKind::Thread, SanitizerKind::Memory), |
520 | 58.1k | std::make_pair(SanitizerKind::Leak, |
521 | 58.1k | SanitizerKind::Thread | SanitizerKind::Memory), |
522 | 58.1k | std::make_pair(SanitizerKind::KernelAddress, |
523 | 58.1k | SanitizerKind::Address | SanitizerKind::Leak | |
524 | 58.1k | SanitizerKind::Thread | SanitizerKind::Memory), |
525 | 58.1k | std::make_pair(SanitizerKind::HWAddress, |
526 | 58.1k | SanitizerKind::Address | SanitizerKind::Thread | |
527 | 58.1k | SanitizerKind::Memory | SanitizerKind::KernelAddress), |
528 | 58.1k | std::make_pair(SanitizerKind::Scudo, |
529 | 58.1k | SanitizerKind::Address | SanitizerKind::HWAddress | |
530 | 58.1k | SanitizerKind::Leak | SanitizerKind::Thread | |
531 | 58.1k | SanitizerKind::Memory | SanitizerKind::KernelAddress), |
532 | 58.1k | std::make_pair(SanitizerKind::SafeStack, |
533 | 58.1k | (TC.getTriple().isOSFuchsia() ? SanitizerMask()380 |
534 | 58.1k | : SanitizerKind::Leak57.7k ) | |
535 | 58.1k | SanitizerKind::Address | SanitizerKind::HWAddress | |
536 | 58.1k | SanitizerKind::Thread | SanitizerKind::Memory | |
537 | 58.1k | SanitizerKind::KernelAddress), |
538 | 58.1k | std::make_pair(SanitizerKind::KernelHWAddress, |
539 | 58.1k | SanitizerKind::Address | SanitizerKind::HWAddress | |
540 | 58.1k | SanitizerKind::Leak | SanitizerKind::Thread | |
541 | 58.1k | SanitizerKind::Memory | SanitizerKind::KernelAddress | |
542 | 58.1k | SanitizerKind::SafeStack), |
543 | 58.1k | std::make_pair(SanitizerKind::KernelMemory, |
544 | 58.1k | SanitizerKind::Address | SanitizerKind::HWAddress | |
545 | 58.1k | SanitizerKind::Leak | SanitizerKind::Thread | |
546 | 58.1k | SanitizerKind::Memory | SanitizerKind::KernelAddress | |
547 | 58.1k | SanitizerKind::Scudo | SanitizerKind::SafeStack), |
548 | 58.1k | std::make_pair(SanitizerKind::MemTag, |
549 | 58.1k | SanitizerKind::Address | SanitizerKind::KernelAddress | |
550 | 58.1k | SanitizerKind::HWAddress | |
551 | 58.1k | SanitizerKind::KernelHWAddress), |
552 | 58.1k | std::make_pair(SanitizerKind::KCFI, SanitizerKind::Function)}; |
553 | | // Enable toolchain specific default sanitizers if not explicitly disabled. |
554 | 58.1k | SanitizerMask Default = TC.getDefaultSanitizers() & ~AllRemove; |
555 | | |
556 | | // Disable default sanitizers that are incompatible with explicitly requested |
557 | | // ones. |
558 | 639k | for (auto G : IncompatibleGroups) { |
559 | 639k | SanitizerMask Group = G.first; |
560 | 639k | if ((Default & Group) && (Kinds & G.second)243 ) |
561 | 38 | Default &= ~Group; |
562 | 639k | } |
563 | | |
564 | 58.1k | Kinds |= Default; |
565 | | |
566 | | // We disable the vptr sanitizer if it was enabled by group expansion but RTTI |
567 | | // is disabled. |
568 | 58.1k | if ((Kinds & SanitizerKind::Vptr) && (RTTIMode == ToolChain::RM_Disabled)221 ) { |
569 | 22 | Kinds &= ~SanitizerKind::Vptr; |
570 | 22 | } |
571 | | |
572 | | // Check that LTO is enabled if we need it. |
573 | 58.1k | if ((Kinds & NeedsLTO) && !D.isUsingLTO()91 && DiagnoseErrors34 ) { |
574 | 11 | D.Diag(diag::err_drv_argument_only_allowed_with) |
575 | 11 | << lastArgumentForMask(D, Args, Kinds & NeedsLTO) << "-flto"; |
576 | 11 | } |
577 | | |
578 | 58.1k | if ((Kinds & SanitizerKind::ShadowCallStack) && TC.getTriple().isAArch64()176 && |
579 | 58.1k | !llvm::AArch64::isX18ReservedByDefault(TC.getTriple())121 && |
580 | 58.1k | !Args.hasArg(options::OPT_ffixed_x18)8 && DiagnoseErrors3 ) { |
581 | 1 | D.Diag(diag::err_drv_argument_only_allowed_with) |
582 | 1 | << lastArgumentForMask(D, Args, Kinds & SanitizerKind::ShadowCallStack) |
583 | 1 | << "-ffixed-x18"; |
584 | 1 | } |
585 | | |
586 | | // Report error if there are non-trapping sanitizers that require |
587 | | // c++abi-specific parts of UBSan runtime, and they are not provided by the |
588 | | // toolchain. We don't have a good way to check the latter, so we just |
589 | | // check if the toolchan supports vptr. |
590 | 58.1k | if (~Supported & SanitizerKind::Vptr) { |
591 | 26.1k | SanitizerMask KindsToDiagnose = Kinds & ~TrappingKinds & NeedsUbsanCxxRt; |
592 | | // The runtime library supports the Microsoft C++ ABI, but only well enough |
593 | | // for CFI. FIXME: Remove this once we support vptr on Windows. |
594 | 26.1k | if (TC.getTriple().isOSWindows()) |
595 | 9.98k | KindsToDiagnose &= ~SanitizerKind::CFI; |
596 | 26.1k | if (KindsToDiagnose) { |
597 | 1 | SanitizerSet S; |
598 | 1 | S.Mask = KindsToDiagnose; |
599 | 1 | if (DiagnoseErrors) |
600 | 1 | D.Diag(diag::err_drv_unsupported_opt_for_target) |
601 | 1 | << ("-fno-sanitize-trap=" + toString(S)) << TC.getTriple().str(); |
602 | 1 | Kinds &= ~KindsToDiagnose; |
603 | 1 | } |
604 | 26.1k | } |
605 | | |
606 | | // Warn about incompatible groups of sanitizers. |
607 | 639k | for (auto G : IncompatibleGroups) { |
608 | 639k | SanitizerMask Group = G.first; |
609 | 639k | if (Kinds & Group) { |
610 | 1.64k | if (SanitizerMask Incompatible = Kinds & G.second) { |
611 | 110 | if (DiagnoseErrors) |
612 | 36 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
613 | 36 | << lastArgumentForMask(D, Args, Group) |
614 | 36 | << lastArgumentForMask(D, Args, Incompatible); |
615 | 110 | Kinds &= ~Incompatible; |
616 | 110 | } |
617 | 1.64k | } |
618 | 639k | } |
619 | | // FIXME: Currently -fsanitize=leak is silently ignored in the presence of |
620 | | // -fsanitize=address. Perhaps it should print an error, or perhaps |
621 | | // -f(-no)sanitize=leak should change whether leak detection is enabled by |
622 | | // default in ASan? |
623 | | |
624 | | // Parse -f(no-)?sanitize-recover flags. |
625 | 58.1k | SanitizerMask RecoverableKinds = RecoverableByDefault | AlwaysRecoverable; |
626 | 58.1k | SanitizerMask DiagnosedUnrecoverableKinds; |
627 | 58.1k | SanitizerMask DiagnosedAlwaysRecoverableKinds; |
628 | 888k | for (const auto *Arg : Args) { |
629 | 888k | if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) { |
630 | 46 | SanitizerMask Add = parseArgValues(D, Arg, DiagnoseErrors); |
631 | | // Report error if user explicitly tries to recover from unrecoverable |
632 | | // sanitizer. |
633 | 46 | if (SanitizerMask KindsToDiagnose = |
634 | 46 | Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) { |
635 | 3 | SanitizerSet SetToDiagnose; |
636 | 3 | SetToDiagnose.Mask |= KindsToDiagnose; |
637 | 3 | if (DiagnoseErrors) |
638 | 1 | D.Diag(diag::err_drv_unsupported_option_argument) |
639 | 1 | << Arg->getSpelling() << toString(SetToDiagnose); |
640 | 3 | DiagnosedUnrecoverableKinds |= KindsToDiagnose; |
641 | 3 | } |
642 | 46 | RecoverableKinds |= expandSanitizerGroups(Add); |
643 | 46 | Arg->claim(); |
644 | 888k | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) { |
645 | 35 | SanitizerMask Remove = parseArgValues(D, Arg, DiagnoseErrors); |
646 | | // Report error if user explicitly tries to disable recovery from |
647 | | // always recoverable sanitizer. |
648 | 35 | if (SanitizerMask KindsToDiagnose = |
649 | 35 | Remove & AlwaysRecoverable & ~DiagnosedAlwaysRecoverableKinds) { |
650 | 9 | SanitizerSet SetToDiagnose; |
651 | 9 | SetToDiagnose.Mask |= KindsToDiagnose; |
652 | 9 | if (DiagnoseErrors) |
653 | 3 | D.Diag(diag::err_drv_unsupported_option_argument) |
654 | 3 | << Arg->getSpelling() << toString(SetToDiagnose); |
655 | 9 | DiagnosedAlwaysRecoverableKinds |= KindsToDiagnose; |
656 | 9 | } |
657 | 35 | RecoverableKinds &= ~expandSanitizerGroups(Remove); |
658 | 35 | Arg->claim(); |
659 | 35 | } |
660 | 888k | } |
661 | 58.1k | RecoverableKinds &= Kinds; |
662 | 58.1k | RecoverableKinds &= ~Unrecoverable; |
663 | | |
664 | 58.1k | TrappingKinds &= Kinds; |
665 | 58.1k | RecoverableKinds &= ~TrappingKinds; |
666 | | |
667 | | // Setup ignorelist files. |
668 | | // Add default ignorelist from resource directory for activated sanitizers, |
669 | | // and validate special case lists format. |
670 | 58.1k | if (!Args.hasArgNoClaim(options::OPT_fno_sanitize_ignorelist)) |
671 | 58.1k | addDefaultIgnorelists(D, Kinds, SystemIgnorelistFiles, DiagnoseErrors); |
672 | | |
673 | | // Parse -f(no-)?sanitize-ignorelist options. |
674 | | // This also validates special case lists format. |
675 | 58.1k | parseSpecialCaseListArg( |
676 | 58.1k | D, Args, UserIgnorelistFiles, options::OPT_fsanitize_ignorelist_EQ, |
677 | 58.1k | options::OPT_fno_sanitize_ignorelist, |
678 | 58.1k | clang::diag::err_drv_malformed_sanitizer_ignorelist, DiagnoseErrors); |
679 | | |
680 | | // Parse -f[no-]sanitize-memory-track-origins[=level] options. |
681 | 58.1k | if (AllAddedKinds & SanitizerKind::Memory) { |
682 | 163 | if (Arg *A = |
683 | 163 | Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ, |
684 | 163 | options::OPT_fno_sanitize_memory_track_origins)) { |
685 | 48 | if (!A->getOption().matches( |
686 | 48 | options::OPT_fno_sanitize_memory_track_origins)) { |
687 | 39 | StringRef S = A->getValue(); |
688 | 39 | if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 || |
689 | 39 | MsanTrackOrigins > 2) { |
690 | 3 | if (DiagnoseErrors) |
691 | 1 | D.Diag(clang::diag::err_drv_invalid_value) |
692 | 1 | << A->getAsString(Args) << S; |
693 | 3 | } |
694 | 39 | } |
695 | 48 | } |
696 | 163 | MsanUseAfterDtor = Args.hasFlag( |
697 | 163 | options::OPT_fsanitize_memory_use_after_dtor, |
698 | 163 | options::OPT_fno_sanitize_memory_use_after_dtor, MsanUseAfterDtor); |
699 | 163 | MsanParamRetval = Args.hasFlag( |
700 | 163 | options::OPT_fsanitize_memory_param_retval, |
701 | 163 | options::OPT_fno_sanitize_memory_param_retval, MsanParamRetval); |
702 | 163 | NeedPIE |= !(TC.getTriple().isOSLinux() && |
703 | 163 | TC.getTriple().getArch() == llvm::Triple::x86_64144 ); |
704 | 57.9k | } else if (AllAddedKinds & SanitizerKind::KernelMemory) { |
705 | 49 | MsanUseAfterDtor = false; |
706 | 49 | MsanParamRetval = Args.hasFlag( |
707 | 49 | options::OPT_fsanitize_memory_param_retval, |
708 | 49 | options::OPT_fno_sanitize_memory_param_retval, MsanParamRetval); |
709 | 57.9k | } else { |
710 | 57.9k | MsanUseAfterDtor = false; |
711 | 57.9k | MsanParamRetval = false; |
712 | 57.9k | } |
713 | | |
714 | 58.1k | if (AllAddedKinds & SanitizerKind::MemTag) { |
715 | 74 | StringRef S = |
716 | 74 | Args.getLastArgValue(options::OPT_fsanitize_memtag_mode_EQ, "sync"); |
717 | 74 | if (S == "async" || S == "sync"62 ) { |
718 | 66 | MemtagMode = S.str(); |
719 | 66 | } else { |
720 | 8 | D.Diag(clang::diag::err_drv_invalid_value_with_suggestion) |
721 | 8 | << "-fsanitize-memtag-mode=" << S << "{async, sync}"; |
722 | 8 | MemtagMode = "sync"; |
723 | 8 | } |
724 | 74 | } |
725 | | |
726 | 58.1k | if (AllAddedKinds & SanitizerKind::Thread) { |
727 | 171 | TsanMemoryAccess = Args.hasFlag( |
728 | 171 | options::OPT_fsanitize_thread_memory_access, |
729 | 171 | options::OPT_fno_sanitize_thread_memory_access, TsanMemoryAccess); |
730 | 171 | TsanFuncEntryExit = Args.hasFlag( |
731 | 171 | options::OPT_fsanitize_thread_func_entry_exit, |
732 | 171 | options::OPT_fno_sanitize_thread_func_entry_exit, TsanFuncEntryExit); |
733 | 171 | TsanAtomics = |
734 | 171 | Args.hasFlag(options::OPT_fsanitize_thread_atomics, |
735 | 171 | options::OPT_fno_sanitize_thread_atomics, TsanAtomics); |
736 | 171 | } |
737 | | |
738 | 58.1k | if (AllAddedKinds & SanitizerKind::CFI) { |
739 | | // Without PIE, external function address may resolve to a PLT record, which |
740 | | // can not be verified by the target module. |
741 | 95 | NeedPIE |= CfiCrossDso; |
742 | 95 | CfiICallGeneralizePointers = |
743 | 95 | Args.hasArg(options::OPT_fsanitize_cfi_icall_generalize_pointers); |
744 | | |
745 | 95 | CfiICallNormalizeIntegers = |
746 | 95 | Args.hasArg(options::OPT_fsanitize_cfi_icall_normalize_integers); |
747 | | |
748 | 95 | if (CfiCrossDso && CfiICallGeneralizePointers19 && DiagnoseErrors1 ) |
749 | 1 | D.Diag(diag::err_drv_argument_not_allowed_with) |
750 | 1 | << "-fsanitize-cfi-cross-dso" |
751 | 1 | << "-fsanitize-cfi-icall-generalize-pointers"; |
752 | | |
753 | 95 | CfiCanonicalJumpTables = |
754 | 95 | Args.hasFlag(options::OPT_fsanitize_cfi_canonical_jump_tables, |
755 | 95 | options::OPT_fno_sanitize_cfi_canonical_jump_tables, true); |
756 | 95 | } |
757 | | |
758 | 58.1k | if (AllAddedKinds & SanitizerKind::KCFI) { |
759 | 26 | CfiICallNormalizeIntegers = |
760 | 26 | Args.hasArg(options::OPT_fsanitize_cfi_icall_normalize_integers); |
761 | | |
762 | 26 | if (AllAddedKinds & SanitizerKind::CFI && DiagnoseErrors3 ) |
763 | 1 | D.Diag(diag::err_drv_argument_not_allowed_with) |
764 | 1 | << "-fsanitize=kcfi" |
765 | 1 | << lastArgumentForMask(D, Args, SanitizerKind::CFI); |
766 | 26 | } |
767 | | |
768 | 58.1k | Stats = Args.hasFlag(options::OPT_fsanitize_stats, |
769 | 58.1k | options::OPT_fno_sanitize_stats, false); |
770 | | |
771 | 58.1k | if (MinimalRuntime) { |
772 | 53 | SanitizerMask IncompatibleMask = |
773 | 53 | Kinds & ~setGroupBits(CompatibleWithMinimalRuntime); |
774 | 53 | if (IncompatibleMask && DiagnoseErrors12 ) |
775 | 4 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
776 | 4 | << "-fsanitize-minimal-runtime" |
777 | 4 | << lastArgumentForMask(D, Args, IncompatibleMask); |
778 | | |
779 | 53 | SanitizerMask NonTrappingCfi = Kinds & SanitizerKind::CFI & ~TrappingKinds; |
780 | 53 | if (NonTrappingCfi && DiagnoseErrors3 ) |
781 | 1 | D.Diag(clang::diag::err_drv_argument_only_allowed_with) |
782 | 1 | << "fsanitize-minimal-runtime" |
783 | 1 | << "fsanitize-trap=cfi"; |
784 | 53 | } |
785 | | |
786 | | // Parse -f(no-)?sanitize-coverage flags if coverage is supported by the |
787 | | // enabled sanitizers. |
788 | 888k | for (const auto *Arg : Args) { |
789 | 888k | if (Arg->getOption().matches(options::OPT_fsanitize_coverage)) { |
790 | 217 | int LegacySanitizeCoverage; |
791 | 217 | if (Arg->getNumValues() == 1 && |
792 | 217 | !StringRef(Arg->getValue(0)) |
793 | 114 | .getAsInteger(0, LegacySanitizeCoverage)) { |
794 | 9 | CoverageFeatures = 0; |
795 | 9 | Arg->claim(); |
796 | 9 | if (LegacySanitizeCoverage != 0 && DiagnoseErrors3 ) { |
797 | 1 | D.Diag(diag::warn_drv_deprecated_arg) |
798 | 1 | << Arg->getAsString(Args) << "-fsanitize-coverage=trace-pc-guard"; |
799 | 1 | } |
800 | 9 | continue; |
801 | 9 | } |
802 | 208 | CoverageFeatures |= parseCoverageFeatures(D, Arg, DiagnoseErrors); |
803 | | |
804 | | // Disable coverage and not claim the flags if there is at least one |
805 | | // non-supporting sanitizer. |
806 | 208 | if (!(AllAddedKinds & ~AllRemove & ~setGroupBits(SupportsCoverage))) { |
807 | 208 | Arg->claim(); |
808 | 208 | } else { |
809 | 0 | CoverageFeatures = 0; |
810 | 0 | } |
811 | 887k | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_coverage)) { |
812 | 3 | Arg->claim(); |
813 | 3 | CoverageFeatures &= ~parseCoverageFeatures(D, Arg, DiagnoseErrors); |
814 | 3 | } |
815 | 888k | } |
816 | | // Choose at most one coverage type: function, bb, or edge. |
817 | 58.1k | if (DiagnoseErrors) { |
818 | 50.1k | if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageBB)29 ) |
819 | 0 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
820 | 0 | << "-fsanitize-coverage=func" |
821 | 0 | << "-fsanitize-coverage=bb"; |
822 | 50.1k | if ((CoverageFeatures & CoverageFunc) && (CoverageFeatures & CoverageEdge)29 ) |
823 | 1 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
824 | 1 | << "-fsanitize-coverage=func" |
825 | 1 | << "-fsanitize-coverage=edge"; |
826 | 50.1k | if ((CoverageFeatures & CoverageBB) && (CoverageFeatures & CoverageEdge)6 ) |
827 | 0 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
828 | 0 | << "-fsanitize-coverage=bb" |
829 | 0 | << "-fsanitize-coverage=edge"; |
830 | | // Basic block tracing and 8-bit counters require some type of coverage |
831 | | // enabled. |
832 | 50.1k | if (CoverageFeatures & CoverageTraceBB) |
833 | 1 | D.Diag(clang::diag::warn_drv_deprecated_arg) |
834 | 1 | << "-fsanitize-coverage=trace-bb" |
835 | 1 | << "-fsanitize-coverage=trace-pc-guard"; |
836 | 50.1k | if (CoverageFeatures & Coverage8bitCounters) |
837 | 1 | D.Diag(clang::diag::warn_drv_deprecated_arg) |
838 | 1 | << "-fsanitize-coverage=8bit-counters" |
839 | 1 | << "-fsanitize-coverage=trace-pc-guard"; |
840 | 50.1k | } |
841 | | |
842 | 58.1k | int InsertionPointTypes = CoverageFunc | CoverageBB | CoverageEdge; |
843 | 58.1k | int InstrumentationTypes = CoverageTracePC | CoverageTracePCGuard | |
844 | 58.1k | CoverageInline8bitCounters | CoverageTraceLoads | |
845 | 58.1k | CoverageTraceStores | CoverageInlineBoolFlag | |
846 | 58.1k | CoverageControlFlow; |
847 | 58.1k | if ((CoverageFeatures & InsertionPointTypes) && |
848 | 58.1k | !(CoverageFeatures & InstrumentationTypes)122 && DiagnoseErrors53 ) { |
849 | 17 | D.Diag(clang::diag::warn_drv_deprecated_arg) |
850 | 17 | << "-fsanitize-coverage=[func|bb|edge]" |
851 | 17 | << "-fsanitize-coverage=[func|bb|edge],[trace-pc-guard|trace-pc],[" |
852 | 17 | "control-flow]"; |
853 | 17 | } |
854 | | |
855 | | // trace-pc w/o func/bb/edge implies edge. |
856 | 58.1k | if (!(CoverageFeatures & InsertionPointTypes)) { |
857 | 58.0k | if (CoverageFeatures & |
858 | 58.0k | (CoverageTracePC | CoverageTracePCGuard | CoverageInline8bitCounters | |
859 | 58.0k | CoverageInlineBoolFlag | CoverageControlFlow)) |
860 | 103 | CoverageFeatures |= CoverageEdge; |
861 | | |
862 | 58.0k | if (CoverageFeatures & CoverageStackDepth) |
863 | 26 | CoverageFeatures |= CoverageFunc; |
864 | 58.0k | } |
865 | | |
866 | | // Parse -fsanitize-coverage-(allow|ignore)list options if coverage enabled. |
867 | | // This also validates special case lists format. |
868 | | // Here, OptSpecifier() acts as a never-matching command-line argument. |
869 | | // So, there is no way to clear coverage lists but you can append to them. |
870 | 58.1k | if (CoverageFeatures) { |
871 | 239 | parseSpecialCaseListArg( |
872 | 239 | D, Args, CoverageAllowlistFiles, |
873 | 239 | options::OPT_fsanitize_coverage_allowlist, OptSpecifier(), |
874 | 239 | clang::diag::err_drv_malformed_sanitizer_coverage_allowlist, |
875 | 239 | DiagnoseErrors); |
876 | 239 | parseSpecialCaseListArg( |
877 | 239 | D, Args, CoverageIgnorelistFiles, |
878 | 239 | options::OPT_fsanitize_coverage_ignorelist, OptSpecifier(), |
879 | 239 | clang::diag::err_drv_malformed_sanitizer_coverage_ignorelist, |
880 | 239 | DiagnoseErrors); |
881 | 239 | } |
882 | | |
883 | | // Parse -f(no-)?sanitize-metadata. |
884 | 58.1k | for (const auto *Arg : |
885 | 58.1k | Args.filtered(options::OPT_fexperimental_sanitize_metadata_EQ, |
886 | 58.1k | options::OPT_fno_experimental_sanitize_metadata_EQ)) { |
887 | 63 | if (Arg->getOption().matches( |
888 | 63 | options::OPT_fexperimental_sanitize_metadata_EQ)) { |
889 | 48 | Arg->claim(); |
890 | 48 | BinaryMetadataFeatures |= |
891 | 48 | parseBinaryMetadataFeatures(D, Arg, DiagnoseErrors); |
892 | 48 | } else { |
893 | 15 | Arg->claim(); |
894 | 15 | BinaryMetadataFeatures &= |
895 | 15 | ~parseBinaryMetadataFeatures(D, Arg, DiagnoseErrors); |
896 | 15 | } |
897 | 63 | } |
898 | | |
899 | | // Parse -fsanitize-metadata-ignorelist option if enabled. |
900 | 58.1k | if (BinaryMetadataFeatures) { |
901 | 33 | parseSpecialCaseListArg( |
902 | 33 | D, Args, BinaryMetadataIgnorelistFiles, |
903 | 33 | options::OPT_fexperimental_sanitize_metadata_ignorelist_EQ, |
904 | 33 | OptSpecifier(), // Cannot clear ignore list, only append. |
905 | 33 | clang::diag::err_drv_malformed_sanitizer_metadata_ignorelist, |
906 | 33 | DiagnoseErrors); |
907 | 33 | } |
908 | | |
909 | 58.1k | SharedRuntime = |
910 | 58.1k | Args.hasFlag(options::OPT_shared_libsan, options::OPT_static_libsan, |
911 | 58.1k | TC.getTriple().isAndroid() || TC.getTriple().isOSFuchsia()57.5k || |
912 | 58.1k | TC.getTriple().isOSDarwin()57.1k ); |
913 | | |
914 | 58.1k | ImplicitCfiRuntime = TC.getTriple().isAndroid(); |
915 | | |
916 | 58.1k | if (AllAddedKinds & SanitizerKind::Address) { |
917 | 665 | NeedPIE |= TC.getTriple().isOSFuchsia(); |
918 | 665 | if (Arg *A = |
919 | 665 | Args.getLastArg(options::OPT_fsanitize_address_field_padding)) { |
920 | 15 | StringRef S = A->getValue(); |
921 | | // Legal values are 0 and 1, 2, but in future we may add more levels. |
922 | 15 | if ((S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 || |
923 | 15 | AsanFieldPadding > 2) && |
924 | 15 | DiagnoseErrors3 ) { |
925 | 1 | D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S; |
926 | 1 | } |
927 | 15 | } |
928 | | |
929 | 665 | if (Arg *WindowsDebugRTArg = |
930 | 665 | Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT, |
931 | 665 | options::OPT__SLASH_MDd, options::OPT__SLASH_MD, |
932 | 665 | options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) { |
933 | 27 | switch (WindowsDebugRTArg->getOption().getID()) { |
934 | 2 | case options::OPT__SLASH_MTd: |
935 | 4 | case options::OPT__SLASH_MDd: |
936 | 11 | case options::OPT__SLASH_LDd: |
937 | 11 | if (DiagnoseErrors) { |
938 | 7 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
939 | 7 | << WindowsDebugRTArg->getAsString(Args) |
940 | 7 | << lastArgumentForMask(D, Args, SanitizerKind::Address); |
941 | 7 | D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime); |
942 | 7 | } |
943 | 27 | } |
944 | 27 | } |
945 | | |
946 | 665 | StableABI = Args.hasFlag(options::OPT_fsanitize_stable_abi, |
947 | 665 | options::OPT_fno_sanitize_stable_abi, false); |
948 | | |
949 | 665 | AsanUseAfterScope = Args.hasFlag( |
950 | 665 | options::OPT_fsanitize_address_use_after_scope, |
951 | 665 | options::OPT_fno_sanitize_address_use_after_scope, AsanUseAfterScope); |
952 | | |
953 | 665 | AsanPoisonCustomArrayCookie = Args.hasFlag( |
954 | 665 | options::OPT_fsanitize_address_poison_custom_array_cookie, |
955 | 665 | options::OPT_fno_sanitize_address_poison_custom_array_cookie, |
956 | 665 | AsanPoisonCustomArrayCookie); |
957 | | |
958 | 665 | AsanOutlineInstrumentation = |
959 | 665 | Args.hasFlag(options::OPT_fsanitize_address_outline_instrumentation, |
960 | 665 | options::OPT_fno_sanitize_address_outline_instrumentation, |
961 | 665 | AsanOutlineInstrumentation); |
962 | | |
963 | 665 | AsanGlobalsDeadStripping = Args.hasFlag( |
964 | 665 | options::OPT_fsanitize_address_globals_dead_stripping, |
965 | 665 | options::OPT_fno_sanitize_address_globals_dead_stripping, true); |
966 | | |
967 | | // Enable ODR indicators which allow better handling of mixed instrumented |
968 | | // and uninstrumented globals. Disable them for Windows where weak odr |
969 | | // indicators (.weak.__odr_asan_gen*) may cause multiple definition linker |
970 | | // errors in the absence of -lldmingw. |
971 | 665 | AsanUseOdrIndicator = |
972 | 665 | Args.hasFlag(options::OPT_fsanitize_address_use_odr_indicator, |
973 | 665 | options::OPT_fno_sanitize_address_use_odr_indicator, |
974 | 665 | !TC.getTriple().isOSWindows()); |
975 | | |
976 | 665 | if (AllAddedKinds & SanitizerKind::PointerCompare & ~AllRemove) { |
977 | 3 | AsanInvalidPointerCmp = true; |
978 | 3 | } |
979 | | |
980 | 665 | if (AllAddedKinds & SanitizerKind::PointerSubtract & ~AllRemove) { |
981 | 3 | AsanInvalidPointerSub = true; |
982 | 3 | } |
983 | | |
984 | 665 | if (TC.getTriple().isOSDarwin() && |
985 | 665 | (67 Args.hasArg(options::OPT_mkernel)67 || |
986 | 67 | Args.hasArg(options::OPT_fapple_kext)64 )) { |
987 | 4 | AsanDtorKind = llvm::AsanDtorKind::None; |
988 | 4 | } |
989 | | |
990 | 665 | if (const auto *Arg = |
991 | 665 | Args.getLastArg(options::OPT_sanitize_address_destructor_EQ)) { |
992 | 7 | auto parsedAsanDtorKind = AsanDtorKindFromString(Arg->getValue()); |
993 | 7 | if (parsedAsanDtorKind == llvm::AsanDtorKind::Invalid && DiagnoseErrors2 ) { |
994 | 1 | TC.getDriver().Diag(clang::diag::err_drv_unsupported_option_argument) |
995 | 1 | << Arg->getSpelling() << Arg->getValue(); |
996 | 1 | } |
997 | 7 | AsanDtorKind = parsedAsanDtorKind; |
998 | 7 | } |
999 | | |
1000 | 665 | if (const auto *Arg = Args.getLastArg( |
1001 | 665 | options::OPT_sanitize_address_use_after_return_EQ)) { |
1002 | 10 | auto parsedAsanUseAfterReturn = |
1003 | 10 | AsanDetectStackUseAfterReturnModeFromString(Arg->getValue()); |
1004 | 10 | if (parsedAsanUseAfterReturn == |
1005 | 10 | llvm::AsanDetectStackUseAfterReturnMode::Invalid && |
1006 | 10 | DiagnoseErrors2 ) { |
1007 | 1 | TC.getDriver().Diag(clang::diag::err_drv_unsupported_option_argument) |
1008 | 1 | << Arg->getSpelling() << Arg->getValue(); |
1009 | 1 | } |
1010 | 10 | AsanUseAfterReturn = parsedAsanUseAfterReturn; |
1011 | 10 | } |
1012 | | |
1013 | 57.4k | } else { |
1014 | 57.4k | AsanUseAfterScope = false; |
1015 | | // -fsanitize=pointer-compare/pointer-subtract requires -fsanitize=address. |
1016 | 57.4k | SanitizerMask DetectInvalidPointerPairs = |
1017 | 57.4k | SanitizerKind::PointerCompare | SanitizerKind::PointerSubtract; |
1018 | 57.4k | if ((AllAddedKinds & DetectInvalidPointerPairs & ~AllRemove) && |
1019 | 57.4k | DiagnoseErrors6 ) { |
1020 | 2 | TC.getDriver().Diag(clang::diag::err_drv_argument_only_allowed_with) |
1021 | 2 | << lastArgumentForMask(D, Args, |
1022 | 2 | SanitizerKind::PointerCompare | |
1023 | 2 | SanitizerKind::PointerSubtract) |
1024 | 2 | << "-fsanitize=address"; |
1025 | 2 | } |
1026 | 57.4k | } |
1027 | | |
1028 | 58.1k | if (AllAddedKinds & SanitizerKind::HWAddress) { |
1029 | 115 | if (Arg *HwasanAbiArg = |
1030 | 115 | Args.getLastArg(options::OPT_fsanitize_hwaddress_abi_EQ)) { |
1031 | 9 | HwasanAbi = HwasanAbiArg->getValue(); |
1032 | 9 | if (HwasanAbi != "platform" && HwasanAbi != "interceptor"6 && |
1033 | 9 | DiagnoseErrors3 ) |
1034 | 1 | D.Diag(clang::diag::err_drv_invalid_value) |
1035 | 1 | << HwasanAbiArg->getAsString(Args) << HwasanAbi; |
1036 | 106 | } else { |
1037 | 106 | HwasanAbi = "interceptor"; |
1038 | 106 | } |
1039 | 115 | if (TC.getTriple().getArch() == llvm::Triple::x86_64) |
1040 | 76 | HwasanUseAliases = Args.hasFlag( |
1041 | 76 | options::OPT_fsanitize_hwaddress_experimental_aliasing, |
1042 | 76 | options::OPT_fno_sanitize_hwaddress_experimental_aliasing, |
1043 | 76 | HwasanUseAliases); |
1044 | 115 | } |
1045 | | |
1046 | 58.1k | if (AllAddedKinds & SanitizerKind::SafeStack) { |
1047 | | // SafeStack runtime is built into the system on Android and Fuchsia. |
1048 | 65 | SafeStackRuntime = |
1049 | 65 | !TC.getTriple().isAndroid() && !TC.getTriple().isOSFuchsia()46 ; |
1050 | 65 | } |
1051 | | |
1052 | 58.1k | LinkRuntimes = |
1053 | 58.1k | Args.hasFlag(options::OPT_fsanitize_link_runtime, |
1054 | 58.1k | options::OPT_fno_sanitize_link_runtime, LinkRuntimes); |
1055 | | |
1056 | | // Parse -link-cxx-sanitizer flag. |
1057 | 58.1k | LinkCXXRuntimes = Args.hasArg(options::OPT_fsanitize_link_cxx_runtime, |
1058 | 58.1k | options::OPT_fno_sanitize_link_cxx_runtime, |
1059 | 58.1k | LinkCXXRuntimes) || |
1060 | 58.1k | D.CCCIsCXX()58.1k ; |
1061 | | |
1062 | 58.1k | NeedsMemProfRt = Args.hasFlag(options::OPT_fmemory_profile, |
1063 | 58.1k | options::OPT_fmemory_profile_EQ, |
1064 | 58.1k | options::OPT_fno_memory_profile, false); |
1065 | | |
1066 | | // Finally, initialize the set of available and recoverable sanitizers. |
1067 | 58.1k | Sanitizers.Mask |= Kinds; |
1068 | 58.1k | RecoverableSanitizers.Mask |= RecoverableKinds; |
1069 | 58.1k | TrapSanitizers.Mask |= TrappingKinds; |
1070 | 58.1k | assert(!(RecoverableKinds & TrappingKinds) && |
1071 | 58.1k | "Overlap between recoverable and trapping sanitizers"); |
1072 | 58.1k | } |
1073 | | |
1074 | 1.08k | static std::string toString(const clang::SanitizerSet &Sanitizers) { |
1075 | 1.08k | std::string Res; |
1076 | 1.08k | #define SANITIZER(NAME, ID) \ |
1077 | 62.6k | if (Sanitizers.has(SanitizerKind::ID)) { \ |
1078 | 4.74k | if (!Res.empty()) \ |
1079 | 4.74k | Res += ","3.66k ; \ |
1080 | 4.74k | Res += NAME; \ |
1081 | 4.74k | } |
1082 | 1.08k | #include "clang/Basic/Sanitizers.def" |
1083 | 1.08k | return Res; |
1084 | 1.08k | } |
1085 | | |
1086 | | static void addSpecialCaseListOpt(const llvm::opt::ArgList &Args, |
1087 | | llvm::opt::ArgStringList &CmdArgs, |
1088 | | const char *SCLOptFlag, |
1089 | 144k | const std::vector<std::string> &SCLFiles) { |
1090 | 144k | for (const auto &SCLPath : SCLFiles) { |
1091 | 109 | SmallString<64> SCLOpt(SCLOptFlag); |
1092 | 109 | SCLOpt += SCLPath; |
1093 | 109 | CmdArgs.push_back(Args.MakeArgString(SCLOpt)); |
1094 | 109 | } |
1095 | 144k | } |
1096 | | |
1097 | | static void addIncludeLinkerOption(const ToolChain &TC, |
1098 | | const llvm::opt::ArgList &Args, |
1099 | | llvm::opt::ArgStringList &CmdArgs, |
1100 | 2 | StringRef SymbolName) { |
1101 | 2 | SmallString<64> LinkerOptionFlag; |
1102 | 2 | LinkerOptionFlag = "--linker-option=/include:"; |
1103 | 2 | if (TC.getTriple().getArch() == llvm::Triple::x86) { |
1104 | | // Win32 mangles C function names with a '_' prefix. |
1105 | 1 | LinkerOptionFlag += '_'; |
1106 | 1 | } |
1107 | 2 | LinkerOptionFlag += SymbolName; |
1108 | 2 | CmdArgs.push_back(Args.MakeArgString(LinkerOptionFlag)); |
1109 | 2 | } |
1110 | | |
1111 | 22 | static bool hasTargetFeatureMTE(const llvm::opt::ArgStringList &CmdArgs) { |
1112 | 23 | for (auto Start = CmdArgs.begin(), End = CmdArgs.end(); Start != End; |
1113 | 23 | ++Start1 ) { |
1114 | 23 | auto It = std::find(Start, End, StringRef("+mte")); |
1115 | 23 | if (It == End) |
1116 | 4 | break; |
1117 | 19 | if (It > Start && *std::prev(It) == StringRef("-target-feature")) |
1118 | 18 | return true; |
1119 | 1 | Start = It; |
1120 | 1 | } |
1121 | 4 | return false; |
1122 | 22 | } |
1123 | | |
1124 | | void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, |
1125 | | llvm::opt::ArgStringList &CmdArgs, |
1126 | 47.9k | types::ID InputType) const { |
1127 | | // NVPTX doesn't currently support sanitizers. Bailing out here means |
1128 | | // that e.g. -fsanitize=address applies only to host code, which is what we |
1129 | | // want for now. |
1130 | 47.9k | if (TC.getTriple().isNVPTX()) |
1131 | 63 | return; |
1132 | | // AMDGPU sanitizer support is experimental and controlled by -fgpu-sanitize. |
1133 | 47.8k | bool GPUSanitize = false; |
1134 | 47.8k | if (TC.getTriple().isAMDGPU()) { |
1135 | 730 | if (!Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize, |
1136 | 730 | true)) |
1137 | 7 | return; |
1138 | 723 | GPUSanitize = true; |
1139 | 723 | } |
1140 | | |
1141 | | // Translate available CoverageFeatures to corresponding clang-cc1 flags. |
1142 | | // Do it even if Sanitizers.empty() since some forms of coverage don't require |
1143 | | // sanitizers. |
1144 | 47.8k | std::pair<int, const char *> CoverageFlags[] = { |
1145 | 47.8k | std::make_pair(CoverageFunc, "-fsanitize-coverage-type=1"), |
1146 | 47.8k | std::make_pair(CoverageBB, "-fsanitize-coverage-type=2"), |
1147 | 47.8k | std::make_pair(CoverageEdge, "-fsanitize-coverage-type=3"), |
1148 | 47.8k | std::make_pair(CoverageIndirCall, "-fsanitize-coverage-indirect-calls"), |
1149 | 47.8k | std::make_pair(CoverageTraceBB, "-fsanitize-coverage-trace-bb"), |
1150 | 47.8k | std::make_pair(CoverageTraceCmp, "-fsanitize-coverage-trace-cmp"), |
1151 | 47.8k | std::make_pair(CoverageTraceDiv, "-fsanitize-coverage-trace-div"), |
1152 | 47.8k | std::make_pair(CoverageTraceGep, "-fsanitize-coverage-trace-gep"), |
1153 | 47.8k | std::make_pair(Coverage8bitCounters, "-fsanitize-coverage-8bit-counters"), |
1154 | 47.8k | std::make_pair(CoverageTracePC, "-fsanitize-coverage-trace-pc"), |
1155 | 47.8k | std::make_pair(CoverageTracePCGuard, |
1156 | 47.8k | "-fsanitize-coverage-trace-pc-guard"), |
1157 | 47.8k | std::make_pair(CoverageInline8bitCounters, |
1158 | 47.8k | "-fsanitize-coverage-inline-8bit-counters"), |
1159 | 47.8k | std::make_pair(CoverageInlineBoolFlag, |
1160 | 47.8k | "-fsanitize-coverage-inline-bool-flag"), |
1161 | 47.8k | std::make_pair(CoveragePCTable, "-fsanitize-coverage-pc-table"), |
1162 | 47.8k | std::make_pair(CoverageNoPrune, "-fsanitize-coverage-no-prune"), |
1163 | 47.8k | std::make_pair(CoverageStackDepth, "-fsanitize-coverage-stack-depth"), |
1164 | 47.8k | std::make_pair(CoverageTraceLoads, "-fsanitize-coverage-trace-loads"), |
1165 | 47.8k | std::make_pair(CoverageTraceStores, "-fsanitize-coverage-trace-stores"), |
1166 | 47.8k | std::make_pair(CoverageControlFlow, "-fsanitize-coverage-control-flow")}; |
1167 | 908k | for (auto F : CoverageFlags) { |
1168 | 908k | if (CoverageFeatures & F.first) |
1169 | 253 | CmdArgs.push_back(F.second); |
1170 | 908k | } |
1171 | 47.8k | addSpecialCaseListOpt( |
1172 | 47.8k | Args, CmdArgs, "-fsanitize-coverage-allowlist=", CoverageAllowlistFiles); |
1173 | 47.8k | addSpecialCaseListOpt(Args, CmdArgs, "-fsanitize-coverage-ignorelist=", |
1174 | 47.8k | CoverageIgnorelistFiles); |
1175 | | |
1176 | 47.8k | if (!GPUSanitize) { |
1177 | | // Translate available BinaryMetadataFeatures to corresponding clang-cc1 |
1178 | | // flags. Does not depend on any other sanitizers. Unsupported on GPUs. |
1179 | 47.1k | const std::pair<int, std::string> BinaryMetadataFlags[] = { |
1180 | 47.1k | std::make_pair(BinaryMetadataCovered, "covered"), |
1181 | 47.1k | std::make_pair(BinaryMetadataAtomics, "atomics"), |
1182 | 47.1k | std::make_pair(BinaryMetadataUAR, "uar")}; |
1183 | 141k | for (const auto &F : BinaryMetadataFlags) { |
1184 | 141k | if (BinaryMetadataFeatures & F.first) |
1185 | 19 | CmdArgs.push_back( |
1186 | 19 | Args.MakeArgString("-fexperimental-sanitize-metadata=" + F.second)); |
1187 | 141k | } |
1188 | 47.1k | addSpecialCaseListOpt(Args, CmdArgs, |
1189 | 47.1k | "-fexperimental-sanitize-metadata-ignorelist=", |
1190 | 47.1k | BinaryMetadataIgnorelistFiles); |
1191 | 47.1k | } |
1192 | | |
1193 | 47.8k | if (TC.getTriple().isOSWindows() && needsUbsanRt()8.76k ) { |
1194 | | // Instruct the code generator to embed linker directives in the object file |
1195 | | // that cause the required runtime libraries to be linked. |
1196 | 9 | CmdArgs.push_back( |
1197 | 9 | Args.MakeArgString("--dependent-lib=" + |
1198 | 9 | TC.getCompilerRTBasename(Args, "ubsan_standalone"))); |
1199 | 9 | if (types::isCXX(InputType)) |
1200 | 2 | CmdArgs.push_back(Args.MakeArgString( |
1201 | 2 | "--dependent-lib=" + |
1202 | 2 | TC.getCompilerRTBasename(Args, "ubsan_standalone_cxx"))); |
1203 | 9 | } |
1204 | 47.8k | if (TC.getTriple().isOSWindows() && needsStatsRt()8.77k ) { |
1205 | 2 | CmdArgs.push_back(Args.MakeArgString( |
1206 | 2 | "--dependent-lib=" + TC.getCompilerRTBasename(Args, "stats_client"))); |
1207 | | |
1208 | | // The main executable must export the stats runtime. |
1209 | | // FIXME: Only exporting from the main executable (e.g. based on whether the |
1210 | | // translation unit defines main()) would save a little space, but having |
1211 | | // multiple copies of the runtime shouldn't hurt. |
1212 | 2 | CmdArgs.push_back(Args.MakeArgString( |
1213 | 2 | "--dependent-lib=" + TC.getCompilerRTBasename(Args, "stats"))); |
1214 | 2 | addIncludeLinkerOption(TC, Args, CmdArgs, "__sanitizer_stats_register"); |
1215 | 2 | } |
1216 | | |
1217 | 47.8k | if (Sanitizers.empty()) |
1218 | 47.0k | return; |
1219 | 843 | CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers))); |
1220 | | |
1221 | 843 | if (!RecoverableSanitizers.empty()) |
1222 | 175 | CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" + |
1223 | 175 | toString(RecoverableSanitizers))); |
1224 | | |
1225 | 843 | if (!TrapSanitizers.empty()) |
1226 | 61 | CmdArgs.push_back( |
1227 | 61 | Args.MakeArgString("-fsanitize-trap=" + toString(TrapSanitizers))); |
1228 | | |
1229 | 843 | addSpecialCaseListOpt(Args, CmdArgs, |
1230 | 843 | "-fsanitize-ignorelist=", UserIgnorelistFiles); |
1231 | 843 | addSpecialCaseListOpt(Args, CmdArgs, |
1232 | 843 | "-fsanitize-system-ignorelist=", SystemIgnorelistFiles); |
1233 | | |
1234 | 843 | if (MsanTrackOrigins) |
1235 | 9 | CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" + |
1236 | 9 | Twine(MsanTrackOrigins))); |
1237 | | |
1238 | 843 | if (MsanUseAfterDtor) |
1239 | 64 | CmdArgs.push_back("-fsanitize-memory-use-after-dtor"); |
1240 | | |
1241 | 843 | if (!MsanParamRetval) |
1242 | 758 | CmdArgs.push_back("-fno-sanitize-memory-param-retval"); |
1243 | | |
1244 | | // FIXME: Pass these parameters as function attributes, not as -llvm flags. |
1245 | 843 | if (!TsanMemoryAccess) { |
1246 | 2 | CmdArgs.push_back("-mllvm"); |
1247 | 2 | CmdArgs.push_back("-tsan-instrument-memory-accesses=0"); |
1248 | 2 | CmdArgs.push_back("-mllvm"); |
1249 | 2 | CmdArgs.push_back("-tsan-instrument-memintrinsics=0"); |
1250 | 2 | } |
1251 | 843 | if (!TsanFuncEntryExit) { |
1252 | 2 | CmdArgs.push_back("-mllvm"); |
1253 | 2 | CmdArgs.push_back("-tsan-instrument-func-entry-exit=0"); |
1254 | 2 | } |
1255 | 843 | if (!TsanAtomics) { |
1256 | 2 | CmdArgs.push_back("-mllvm"); |
1257 | 2 | CmdArgs.push_back("-tsan-instrument-atomics=0"); |
1258 | 2 | } |
1259 | | |
1260 | 843 | if (HwasanUseAliases) { |
1261 | 1 | CmdArgs.push_back("-mllvm"); |
1262 | 1 | CmdArgs.push_back("-hwasan-experimental-use-page-aliases=1"); |
1263 | 1 | } |
1264 | | |
1265 | 843 | if (CfiCrossDso) |
1266 | 8 | CmdArgs.push_back("-fsanitize-cfi-cross-dso"); |
1267 | | |
1268 | 843 | if (CfiICallGeneralizePointers) |
1269 | 2 | CmdArgs.push_back("-fsanitize-cfi-icall-generalize-pointers"); |
1270 | | |
1271 | 843 | if (CfiICallNormalizeIntegers) |
1272 | 0 | CmdArgs.push_back("-fsanitize-cfi-icall-experimental-normalize-integers"); |
1273 | | |
1274 | 843 | if (CfiCanonicalJumpTables) |
1275 | 48 | CmdArgs.push_back("-fsanitize-cfi-canonical-jump-tables"); |
1276 | | |
1277 | 843 | if (Stats) |
1278 | 5 | CmdArgs.push_back("-fsanitize-stats"); |
1279 | | |
1280 | 843 | if (MinimalRuntime) |
1281 | 18 | CmdArgs.push_back("-fsanitize-minimal-runtime"); |
1282 | | |
1283 | 843 | if (AsanFieldPadding) |
1284 | 3 | CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" + |
1285 | 3 | Twine(AsanFieldPadding))); |
1286 | | |
1287 | 843 | if (AsanUseAfterScope) |
1288 | 236 | CmdArgs.push_back("-fsanitize-address-use-after-scope"); |
1289 | | |
1290 | 843 | if (AsanPoisonCustomArrayCookie) |
1291 | 4 | CmdArgs.push_back("-fsanitize-address-poison-custom-array-cookie"); |
1292 | | |
1293 | 843 | if (AsanGlobalsDeadStripping) |
1294 | 238 | CmdArgs.push_back("-fsanitize-address-globals-dead-stripping"); |
1295 | | |
1296 | 843 | if (!AsanUseOdrIndicator) |
1297 | 635 | CmdArgs.push_back("-fno-sanitize-address-use-odr-indicator"); |
1298 | | |
1299 | 843 | if (AsanInvalidPointerCmp) { |
1300 | 1 | CmdArgs.push_back("-mllvm"); |
1301 | 1 | CmdArgs.push_back("-asan-detect-invalid-pointer-cmp"); |
1302 | 1 | } |
1303 | | |
1304 | 843 | if (AsanInvalidPointerSub) { |
1305 | 1 | CmdArgs.push_back("-mllvm"); |
1306 | 1 | CmdArgs.push_back("-asan-detect-invalid-pointer-sub"); |
1307 | 1 | } |
1308 | | |
1309 | 843 | if (AsanOutlineInstrumentation) { |
1310 | 3 | CmdArgs.push_back("-mllvm"); |
1311 | 3 | CmdArgs.push_back("-asan-instrumentation-with-call-threshold=0"); |
1312 | 3 | } |
1313 | | |
1314 | | // When emitting Stable ABI instrumentation, force outlining calls and avoid |
1315 | | // inlining shadow memory poisoning. While this is a big performance burden |
1316 | | // for now it allows full abstraction from implementation details. |
1317 | 843 | if (StableABI) { |
1318 | 2 | CmdArgs.push_back("-mllvm"); |
1319 | 2 | CmdArgs.push_back("-asan-instrumentation-with-call-threshold=0"); |
1320 | 2 | CmdArgs.push_back("-mllvm"); |
1321 | 2 | CmdArgs.push_back("-asan-max-inline-poisoning-size=0"); |
1322 | 2 | CmdArgs.push_back("-mllvm"); |
1323 | 2 | CmdArgs.push_back("-asan-guard-against-version-mismatch=0"); |
1324 | 2 | } |
1325 | | |
1326 | | // Only pass the option to the frontend if the user requested, |
1327 | | // otherwise the frontend will just use the codegen default. |
1328 | 843 | if (AsanDtorKind != llvm::AsanDtorKind::Invalid) { |
1329 | 6 | CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-destructor=" + |
1330 | 6 | AsanDtorKindToString(AsanDtorKind))); |
1331 | 6 | } |
1332 | | |
1333 | 843 | if (AsanUseAfterReturn != llvm::AsanDetectStackUseAfterReturnMode::Invalid) { |
1334 | 4 | CmdArgs.push_back(Args.MakeArgString( |
1335 | 4 | "-fsanitize-address-use-after-return=" + |
1336 | 4 | AsanDetectStackUseAfterReturnModeToString(AsanUseAfterReturn))); |
1337 | 4 | } |
1338 | | |
1339 | 843 | if (!HwasanAbi.empty()) { |
1340 | 43 | CmdArgs.push_back("-default-function-attr"); |
1341 | 43 | CmdArgs.push_back(Args.MakeArgString("hwasan-abi=" + HwasanAbi)); |
1342 | 43 | } |
1343 | | |
1344 | 843 | if (Sanitizers.has(SanitizerKind::HWAddress) && !HwasanUseAliases39 ) { |
1345 | 38 | CmdArgs.push_back("-target-feature"); |
1346 | 38 | CmdArgs.push_back("+tagged-globals"); |
1347 | 38 | } |
1348 | | |
1349 | | // MSan: Workaround for PR16386. |
1350 | | // ASan: This is mainly to help LSan with cases such as |
1351 | | // https://github.com/google/sanitizers/issues/373 |
1352 | | // We can't make this conditional on -fsanitize=leak, as that flag shouldn't |
1353 | | // affect compilation. |
1354 | 843 | if (Sanitizers.has(SanitizerKind::Memory) || |
1355 | 843 | Sanitizers.has(SanitizerKind::Address)782 ) |
1356 | 278 | CmdArgs.push_back("-fno-assume-sane-operator-new"); |
1357 | | |
1358 | | // libFuzzer wants to intercept calls to certain library functions, so the |
1359 | | // following -fno-builtin-* flags force the compiler to emit interposable |
1360 | | // libcalls to these functions. Other sanitizers effectively do the same thing |
1361 | | // by marking all library call sites with NoBuiltin attribute in their LLVM |
1362 | | // pass. (see llvm::maybeMarkSanitizerLibraryCallNoBuiltin) |
1363 | 843 | if (Sanitizers.has(SanitizerKind::FuzzerNoLink)) { |
1364 | 15 | CmdArgs.push_back("-fno-builtin-bcmp"); |
1365 | 15 | CmdArgs.push_back("-fno-builtin-memcmp"); |
1366 | 15 | CmdArgs.push_back("-fno-builtin-strncmp"); |
1367 | 15 | CmdArgs.push_back("-fno-builtin-strcmp"); |
1368 | 15 | CmdArgs.push_back("-fno-builtin-strncasecmp"); |
1369 | 15 | CmdArgs.push_back("-fno-builtin-strcasecmp"); |
1370 | 15 | CmdArgs.push_back("-fno-builtin-strstr"); |
1371 | 15 | CmdArgs.push_back("-fno-builtin-strcasestr"); |
1372 | 15 | CmdArgs.push_back("-fno-builtin-memmem"); |
1373 | 15 | } |
1374 | | |
1375 | | // Require -fvisibility= flag on non-Windows when compiling if vptr CFI is |
1376 | | // enabled. |
1377 | 843 | if (Sanitizers.hasOneOf(CFIClasses) && !TC.getTriple().isOSWindows()43 && |
1378 | 843 | !Args.hasArg(options::OPT_fvisibility_EQ)38 ) { |
1379 | 10 | TC.getDriver().Diag(clang::diag::err_drv_argument_only_allowed_with) |
1380 | 10 | << lastArgumentForMask(TC.getDriver(), Args, |
1381 | 10 | Sanitizers.Mask & CFIClasses) |
1382 | 10 | << "-fvisibility="; |
1383 | 10 | } |
1384 | | |
1385 | 843 | if (Sanitizers.has(SanitizerKind::MemtagStack) && |
1386 | 843 | !hasTargetFeatureMTE(CmdArgs)22 ) |
1387 | 4 | TC.getDriver().Diag(diag::err_stack_tagging_requires_hardware_feature); |
1388 | 843 | } |
1389 | | |
1390 | | SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A, |
1391 | 2.56k | bool DiagnoseErrors) { |
1392 | 2.56k | assert((A->getOption().matches(options::OPT_fsanitize_EQ) || |
1393 | 2.56k | A->getOption().matches(options::OPT_fno_sanitize_EQ) || |
1394 | 2.56k | A->getOption().matches(options::OPT_fsanitize_recover_EQ) || |
1395 | 2.56k | A->getOption().matches(options::OPT_fno_sanitize_recover_EQ) || |
1396 | 2.56k | A->getOption().matches(options::OPT_fsanitize_trap_EQ) || |
1397 | 2.56k | A->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) && |
1398 | 2.56k | "Invalid argument in parseArgValues!"); |
1399 | 2.56k | SanitizerMask Kinds; |
1400 | 5.42k | for (int i = 0, n = A->getNumValues(); i != n; ++i2.85k ) { |
1401 | 2.85k | const char *Value = A->getValue(i); |
1402 | 2.85k | SanitizerMask Kind; |
1403 | | // Special case: don't accept -fsanitize=all. |
1404 | 2.85k | if (A->getOption().matches(options::OPT_fsanitize_EQ) && |
1405 | 2.85k | 0 == strcmp("all", Value)2.61k ) |
1406 | 3 | Kind = SanitizerMask(); |
1407 | 2.85k | else |
1408 | 2.85k | Kind = parseSanitizerValue(Value, /*AllowGroups=*/true); |
1409 | | |
1410 | 2.85k | if (Kind) |
1411 | 2.84k | Kinds |= Kind; |
1412 | 11 | else if (DiagnoseErrors) |
1413 | 4 | D.Diag(clang::diag::err_drv_unsupported_option_argument) |
1414 | 4 | << A->getSpelling() << Value; |
1415 | 2.85k | } |
1416 | 2.56k | return Kinds; |
1417 | 2.56k | } |
1418 | | |
1419 | | int parseCoverageFeatures(const Driver &D, const llvm::opt::Arg *A, |
1420 | 211 | bool DiagnoseErrors) { |
1421 | 211 | assert(A->getOption().matches(options::OPT_fsanitize_coverage) || |
1422 | 211 | A->getOption().matches(options::OPT_fno_sanitize_coverage)); |
1423 | 211 | int Features = 0; |
1424 | 555 | for (int i = 0, n = A->getNumValues(); i != n; ++i344 ) { |
1425 | 344 | const char *Value = A->getValue(i); |
1426 | 344 | int F = llvm::StringSwitch<int>(Value) |
1427 | 344 | .Case("func", CoverageFunc) |
1428 | 344 | .Case("bb", CoverageBB) |
1429 | 344 | .Case("edge", CoverageEdge) |
1430 | 344 | .Case("indirect-calls", CoverageIndirCall) |
1431 | 344 | .Case("trace-bb", CoverageTraceBB) |
1432 | 344 | .Case("trace-cmp", CoverageTraceCmp) |
1433 | 344 | .Case("trace-div", CoverageTraceDiv) |
1434 | 344 | .Case("trace-gep", CoverageTraceGep) |
1435 | 344 | .Case("8bit-counters", Coverage8bitCounters) |
1436 | 344 | .Case("trace-pc", CoverageTracePC) |
1437 | 344 | .Case("trace-pc-guard", CoverageTracePCGuard) |
1438 | 344 | .Case("no-prune", CoverageNoPrune) |
1439 | 344 | .Case("inline-8bit-counters", CoverageInline8bitCounters) |
1440 | 344 | .Case("inline-bool-flag", CoverageInlineBoolFlag) |
1441 | 344 | .Case("pc-table", CoveragePCTable) |
1442 | 344 | .Case("stack-depth", CoverageStackDepth) |
1443 | 344 | .Case("trace-loads", CoverageTraceLoads) |
1444 | 344 | .Case("trace-stores", CoverageTraceStores) |
1445 | 344 | .Case("control-flow", CoverageControlFlow) |
1446 | 344 | .Default(0); |
1447 | 344 | if (F == 0 && DiagnoseErrors3 ) |
1448 | 1 | D.Diag(clang::diag::err_drv_unsupported_option_argument) |
1449 | 1 | << A->getSpelling() << Value; |
1450 | 344 | Features |= F; |
1451 | 344 | } |
1452 | 211 | return Features; |
1453 | 211 | } |
1454 | | |
1455 | | int parseBinaryMetadataFeatures(const Driver &D, const llvm::opt::Arg *A, |
1456 | 63 | bool DiagnoseErrors) { |
1457 | 63 | assert( |
1458 | 63 | A->getOption().matches(options::OPT_fexperimental_sanitize_metadata_EQ) || |
1459 | 63 | A->getOption().matches( |
1460 | 63 | options::OPT_fno_experimental_sanitize_metadata_EQ)); |
1461 | 63 | int Features = 0; |
1462 | 129 | for (int i = 0, n = A->getNumValues(); i != n; ++i66 ) { |
1463 | 66 | const char *Value = A->getValue(i); |
1464 | 66 | int F = llvm::StringSwitch<int>(Value) |
1465 | 66 | .Case("covered", BinaryMetadataCovered) |
1466 | 66 | .Case("atomics", BinaryMetadataAtomics) |
1467 | 66 | .Case("uar", BinaryMetadataUAR) |
1468 | 66 | .Case("all", ~0) |
1469 | 66 | .Default(0); |
1470 | 66 | if (F == 0 && DiagnoseErrors3 ) |
1471 | 1 | D.Diag(clang::diag::err_drv_unsupported_option_argument) |
1472 | 1 | << A->getSpelling() << Value; |
1473 | 66 | Features |= F; |
1474 | 66 | } |
1475 | 63 | return Features; |
1476 | 63 | } |
1477 | | |
1478 | | std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args, |
1479 | 108 | SanitizerMask Mask) { |
1480 | 108 | for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(), |
1481 | 108 | E = Args.rend(); |
1482 | 745 | I != E; ++I637 ) { |
1483 | 745 | const auto *Arg = *I; |
1484 | 745 | if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) { |
1485 | 109 | SanitizerMask AddKinds = |
1486 | 109 | expandSanitizerGroups(parseArgValues(D, Arg, false)); |
1487 | 109 | if (AddKinds & Mask) |
1488 | 108 | return describeSanitizeArg(Arg, Mask); |
1489 | 636 | } else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) { |
1490 | 0 | SanitizerMask RemoveKinds = |
1491 | 0 | expandSanitizerGroups(parseArgValues(D, Arg, false)); |
1492 | 0 | Mask &= ~RemoveKinds; |
1493 | 0 | } |
1494 | 745 | } |
1495 | 0 | llvm_unreachable("arg list didn't provide expected value"); |
1496 | 0 | } |
1497 | | |
1498 | 145 | std::string describeSanitizeArg(const llvm::opt::Arg *A, SanitizerMask Mask) { |
1499 | 145 | assert(A->getOption().matches(options::OPT_fsanitize_EQ) && |
1500 | 145 | "Invalid argument in describeSanitizerArg!"); |
1501 | | |
1502 | 145 | std::string Sanitizers; |
1503 | 363 | for (int i = 0, n = A->getNumValues(); i != n; ++i218 ) { |
1504 | 218 | if (expandSanitizerGroups( |
1505 | 218 | parseSanitizerValue(A->getValue(i), /*AllowGroups=*/true)) & |
1506 | 218 | Mask) { |
1507 | 145 | if (!Sanitizers.empty()) |
1508 | 0 | Sanitizers += ","; |
1509 | 145 | Sanitizers += A->getValue(i); |
1510 | 145 | } |
1511 | 218 | } |
1512 | | |
1513 | 145 | assert(!Sanitizers.empty() && "arg didn't provide expected value"); |
1514 | 145 | return "-fsanitize=" + Sanitizers; |
1515 | 145 | } |