Skip to content

[Sanitizers] Add new sanitize-stable-abi flag for libsanitizers. #70508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/swift/AST/IRGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ class IRGenOptions {
/// Whether to enable ODR indicators when building with ASan.
unsigned SanitizeAddressUseODRIndicator : 1;

unsigned SanitizerUseStableABI : 1;

/// Path prefixes that should be rewritten in debug info.
PathRemapper DebugPrefixMap;

Expand Down Expand Up @@ -516,7 +518,7 @@ class IRGenOptions {
Verify(true), OptMode(OptimizationMode::NotSet),
Sanitizers(OptionSet<SanitizerKind>()),
SanitizersWithRecoveryInstrumentation(OptionSet<SanitizerKind>()),
SanitizeAddressUseODRIndicator(false),
SanitizeAddressUseODRIndicator(false), SanitizerUseStableABI(false),
DebugInfoLevel(IRGenDebugInfoLevel::None),
DebugInfoFormat(IRGenDebugInfoFormat::None),
DisableClangModuleSkeletonCUs(false), UseJIT(false),
Expand Down
2 changes: 2 additions & 0 deletions include/swift/Driver/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ class OutputInfo {

OptionSet<SanitizerKind> SelectedSanitizers;

unsigned SanitizerUseStableABI = 0;

/// Might this sort of compile have explicit primary inputs?
/// When running a single compile for the whole module (in other words
/// "whole-module-optimization" mode) there must be no -primary-input's and
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,10 @@ def sanitize_coverage_EQ : CommaJoined<["-"], "sanitize-coverage=">,
HelpText<"Specify the type of coverage instrumentation for Sanitizers and"
" additional options separated by commas">;

def sanitize_stable_abi_EQ : Flag<["-"], "sanitize-stable-abi">,
Flags<[FrontendOption, NoInteractiveOption]>,
HelpText<"Link against the Sanitizers stable ABI.">;

def scan_dependencies : Flag<["-"], "scan-dependencies">,
HelpText<"Scan dependencies of the given Swift sources">, ModeOpt,
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
Expand Down
5 changes: 5 additions & 0 deletions include/swift/Option/SanitizerOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ bool parseSanitizerAddressUseODRIndicator(
const llvm::opt::Arg *A, const OptionSet<SanitizerKind> &enabledSanitizers,
DiagnosticEngine &Diags);

/// Parse -sanitize-stable-abi's value
bool parseSanitizerUseStableABI(
const llvm::opt::Arg *A, const OptionSet<SanitizerKind> &enabledSanitizers,
DiagnosticEngine &Diags);

/// Returns the active sanitizers as a comma-separated list.
std::string getSanitizerList(const OptionSet<SanitizerKind> &Set);
}
Expand Down
9 changes: 7 additions & 2 deletions lib/Driver/DarwinToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,13 @@ toolchains::Darwin::addSanitizerArgs(ArgStringList &Arguments,
// Linking sanitizers will add rpaths, which might negatively interact when
// other rpaths are involved, so we should make sure we add the rpaths after
// all user-specified rpaths.
if (context.OI.SelectedSanitizers & SanitizerKind::Address)
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "asan", *this);
if (context.OI.SelectedSanitizers & SanitizerKind::Address) {
if (context.OI.SanitizerUseStableABI)
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "asan_abi",
*this, false);
else
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "asan", *this);
}

if (context.OI.SelectedSanitizers & SanitizerKind::Thread)
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "tsan", *this);
Expand Down
5 changes: 5 additions & 0 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,11 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,

}

if (const Arg *A = Args.getLastArg(options::OPT_sanitize_stable_abi_EQ)) {
OI.SanitizerUseStableABI =
parseSanitizerUseStableABI(A, OI.SelectedSanitizers, Diags);
}

if (TC.getTriple().isOSWindows()) {
if (const Arg *A = Args.getLastArg(options::OPT_libc)) {
OI.RuntimeVariant =
Expand Down
1 change: 1 addition & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
inputArgs.AddLastArg(arguments,
options::OPT_sanitize_address_use_odr_indicator);
inputArgs.AddLastArg(arguments, options::OPT_sanitize_coverage_EQ);
inputArgs.AddLastArg(arguments, options::OPT_sanitize_stable_abi_EQ);
inputArgs.AddLastArg(arguments, options::OPT_static);
inputArgs.AddLastArg(arguments, options::OPT_swift_version);
inputArgs.AddLastArg(arguments, options::OPT_enforce_exclusivity_EQ);
Expand Down
5 changes: 5 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2409,6 +2409,11 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
parseSanitizerAddressUseODRIndicator(A, Opts.Sanitizers, Diags);
}

if (const Arg *A = Args.getLastArg(options::OPT_sanitize_stable_abi_EQ)) {
IRGenOpts.SanitizerUseStableABI =
parseSanitizerUseStableABI(A, Opts.Sanitizers, Diags);
}

if (auto A = Args.getLastArg(OPT_enable_verify_exclusivity,
OPT_disable_verify_exclusivity)) {
Opts.VerifyExclusivity
Expand Down
5 changes: 5 additions & 0 deletions lib/IRGen/IRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ void swift::performLLVMOptimizations(const IRGenOptions &Opts,
SanitizerKind::Address);
ASOpts.UseAfterScope = false;
ASOpts.UseAfterReturn = llvm::AsanDetectStackUseAfterReturnMode::Runtime;
if (Opts.SanitizerUseStableABI) {
ASOpts.MaxInlinePoisoningSize = 0;
ASOpts.InstrumentationWithCallsThreshold = 0;
ASOpts.InsertVersionCheck = false;
}
MPM.addPass(AddressSanitizerPass(
ASOpts, /*UseGlobalGC=*/true, Opts.SanitizeAddressUseODRIndicator,
/*DestructorKind=*/llvm::AsanDtorKind::Global));
Expand Down
14 changes: 14 additions & 0 deletions lib/Option/SanitizerOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,20 @@ bool swift::parseSanitizerAddressUseODRIndicator(
return true;
}

bool swift::parseSanitizerUseStableABI(
const llvm::opt::Arg *A, const OptionSet<SanitizerKind> &enabledSanitizers,
DiagnosticEngine &Diags) {
// Warn if ASan isn't enabled.
if (!(enabledSanitizers & SanitizerKind::Address)) {
Diags.diagnose(
SourceLoc(), diag::warning_option_requires_specific_sanitizer,
A->getOption().getPrefixedName(), toStringRef(SanitizerKind::Address));
return false;
}

return true;
}

std::string swift::getSanitizerList(const OptionSet<SanitizerKind> &Set) {
std::string list;
#define SANITIZER(_, kind, name, file) \
Expand Down
6 changes: 6 additions & 0 deletions test/Driver/sanitize_stable_abi.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// REQUIRES: asan_runtime
// REQUIRES: platform=Darwin
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -sanitize-stable-abi %s 2>&1 | %FileCheck %s

// Link against stable ABI
// CHECK: libclang_rt.asan_abi{{.*}}.a