Skip to content

Commit 8282bed

Browse files
committed
[Sanitizers] Add new sanitize-stable-abi flag for libsanitizers.
This patch adds a new flag sanitize-stable-abi to support linking against the Sanitizers stable ABI added recently in compiler-rt. The patch also passes extra options for the ASan pass when using this flag to outline instrumentation code and remove version check. rdar://112915278
1 parent 49f28c8 commit 8282bed

File tree

11 files changed

+56
-3
lines changed

11 files changed

+56
-3
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ class IRGenOptions {
273273
/// Whether to enable ODR indicators when building with ASan.
274274
unsigned SanitizeAddressUseODRIndicator : 1;
275275

276+
unsigned SanitizerUseStableABI : 1;
277+
276278
/// Path prefixes that should be rewritten in debug info.
277279
PathRemapper DebugPrefixMap;
278280

@@ -516,7 +518,7 @@ class IRGenOptions {
516518
Verify(true), OptMode(OptimizationMode::NotSet),
517519
Sanitizers(OptionSet<SanitizerKind>()),
518520
SanitizersWithRecoveryInstrumentation(OptionSet<SanitizerKind>()),
519-
SanitizeAddressUseODRIndicator(false),
521+
SanitizeAddressUseODRIndicator(false), SanitizerUseStableABI(false),
520522
DebugInfoLevel(IRGenDebugInfoLevel::None),
521523
DebugInfoFormat(IRGenDebugInfoFormat::None),
522524
DisableClangModuleSkeletonCUs(false), UseJIT(false),

include/swift/Driver/Driver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ class OutputInfo {
153153

154154
OptionSet<SanitizerKind> SelectedSanitizers;
155155

156+
unsigned SanitizerUseStableABI = 0;
157+
156158
/// Might this sort of compile have explicit primary inputs?
157159
/// When running a single compile for the whole module (in other words
158160
/// "whole-module-optimization" mode) there must be no -primary-input's and

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,10 @@ def sanitize_coverage_EQ : CommaJoined<["-"], "sanitize-coverage=">,
13941394
HelpText<"Specify the type of coverage instrumentation for Sanitizers and"
13951395
" additional options separated by commas">;
13961396

1397+
def sanitize_stable_abi_EQ : Flag<["-"], "sanitize-stable-abi">,
1398+
Flags<[FrontendOption, NoInteractiveOption]>,
1399+
HelpText<"Link against the Sanitizers stable ABI.">;
1400+
13971401
def scan_dependencies : Flag<["-"], "scan-dependencies">,
13981402
HelpText<"Scan dependencies of the given Swift sources">, ModeOpt,
13991403
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;

include/swift/Option/SanitizerOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ bool parseSanitizerAddressUseODRIndicator(
5252
const llvm::opt::Arg *A, const OptionSet<SanitizerKind> &enabledSanitizers,
5353
DiagnosticEngine &Diags);
5454

55+
/// Parse -sanitize-stable-abi's value
56+
bool parseSanitizerUseStableABI(
57+
const llvm::opt::Arg *A, const OptionSet<SanitizerKind> &enabledSanitizers,
58+
DiagnosticEngine &Diags);
59+
5560
/// Returns the active sanitizers as a comma-separated list.
5661
std::string getSanitizerList(const OptionSet<SanitizerKind> &Set);
5762
}

lib/Driver/DarwinToolChains.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,13 @@ toolchains::Darwin::addSanitizerArgs(ArgStringList &Arguments,
317317
// Linking sanitizers will add rpaths, which might negatively interact when
318318
// other rpaths are involved, so we should make sure we add the rpaths after
319319
// all user-specified rpaths.
320-
if (context.OI.SelectedSanitizers & SanitizerKind::Address)
321-
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "asan", *this);
320+
if (context.OI.SelectedSanitizers & SanitizerKind::Address) {
321+
if (context.OI.SanitizerUseStableABI)
322+
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "asan_abi",
323+
*this, false);
324+
else
325+
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "asan", *this);
326+
}
322327

323328
if (context.OI.SelectedSanitizers & SanitizerKind::Thread)
324329
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "tsan", *this);

lib/Driver/Driver.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,11 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
18491849

18501850
}
18511851

1852+
if (const Arg *A = Args.getLastArg(options::OPT_sanitize_stable_abi_EQ)) {
1853+
OI.SanitizerUseStableABI =
1854+
parseSanitizerUseStableABI(A, OI.SelectedSanitizers, Diags);
1855+
}
1856+
18521857
if (TC.getTriple().isOSWindows()) {
18531858
if (const Arg *A = Args.getLastArg(options::OPT_libc)) {
18541859
OI.RuntimeVariant =

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
289289
inputArgs.AddLastArg(arguments,
290290
options::OPT_sanitize_address_use_odr_indicator);
291291
inputArgs.AddLastArg(arguments, options::OPT_sanitize_coverage_EQ);
292+
inputArgs.AddLastArg(arguments, options::OPT_sanitize_stable_abi_EQ);
292293
inputArgs.AddLastArg(arguments, options::OPT_static);
293294
inputArgs.AddLastArg(arguments, options::OPT_swift_version);
294295
inputArgs.AddLastArg(arguments, options::OPT_enforce_exclusivity_EQ);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,6 +2380,11 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
23802380
parseSanitizerAddressUseODRIndicator(A, Opts.Sanitizers, Diags);
23812381
}
23822382

2383+
if (const Arg *A = Args.getLastArg(options::OPT_sanitize_stable_abi_EQ)) {
2384+
IRGenOpts.SanitizerUseStableABI =
2385+
parseSanitizerUseStableABI(A, Opts.Sanitizers, Diags);
2386+
}
2387+
23832388
if (auto A = Args.getLastArg(OPT_enable_verify_exclusivity,
23842389
OPT_disable_verify_exclusivity)) {
23852390
Opts.VerifyExclusivity

lib/IRGen/IRGen.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ void swift::performLLVMOptimizations(const IRGenOptions &Opts,
278278
SanitizerKind::Address);
279279
ASOpts.UseAfterScope = false;
280280
ASOpts.UseAfterReturn = llvm::AsanDetectStackUseAfterReturnMode::Runtime;
281+
if (Opts.SanitizerUseStableABI) {
282+
ASOpts.MaxInlinePoisoningSize = 0;
283+
ASOpts.InstrumentationWithCallsThreshold = 0;
284+
ASOpts.InsertVersionCheck = false;
285+
}
281286
MPM.addPass(AddressSanitizerPass(
282287
ASOpts, /*UseGlobalGC=*/true, Opts.SanitizeAddressUseODRIndicator,
283288
/*DestructorKind=*/llvm::AsanDtorKind::Global));

lib/Option/SanitizerOptions.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,20 @@ bool swift::parseSanitizerAddressUseODRIndicator(
265265
return true;
266266
}
267267

268+
bool swift::parseSanitizerUseStableABI(
269+
const llvm::opt::Arg *A, const OptionSet<SanitizerKind> &enabledSanitizers,
270+
DiagnosticEngine &Diags) {
271+
// Warn if ASan isn't enabled.
272+
if (!(enabledSanitizers & SanitizerKind::Address)) {
273+
Diags.diagnose(
274+
SourceLoc(), diag::warning_option_requires_specific_sanitizer,
275+
A->getOption().getPrefixedName(), toStringRef(SanitizerKind::Address));
276+
return false;
277+
}
278+
279+
return true;
280+
}
281+
268282
std::string swift::getSanitizerList(const OptionSet<SanitizerKind> &Set) {
269283
std::string list;
270284
#define SANITIZER(_, kind, name, file) \

test/Driver/sanitize_stable_abi.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// REQUIRES: asan_runtime
2+
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -sanitize-stable-abi %s 2>&1 | %FileCheck %s
3+
4+
// Link against stable ABI
5+
// CHECK: libclang_rt.asan_abi{{.*}}.a

0 commit comments

Comments
 (0)