Skip to content

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

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -1394,6 +1394,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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the rpath still be added? If it's not necessary is it just a NOP or could it cause a problem?

if (context.OI.SanitizerUseStableABI)
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "asan_abi",
*this, false);
else
addLinkSanitizerLibArgsForDarwin(context.Args, Arguments, "asan", *this);
Comment on lines +321 to +325
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use something like this to reduce diff/duplication.
auto good_name = context.OI.SanitizerUseStableABI ? "asan_abi" : "asan";

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an extra boolean argument in one of the cases, so this would require two ternary operators, so using an if feels cleaner here.

}

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 @@ -2380,6 +2380,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
5 changes: 5 additions & 0 deletions test/Driver/sanitize_stable_abi.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// REQUIRES: asan_runtime
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -sanitize-stable-abi %s 2>&1 | %FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I read "-sanitize-stable-abi` as "sanitize stable ABI" it's not the right meaning. Compare "Sanitize address" which describes pretty well what it does.

The variable names for this are SanitizerUseStableABI, so we just forgot the r here?

I think the "sanitizer" prefix here is a noun vs. "sanitize" (verb). If we want to include a verb, we could spell the option with "use".

I would pick one of these:

-sanitizer-stable-abi
-sanitizer-use-stable-abi

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I matched the convention used in clang where we use -fsanitize-stable-abi flag. In the last team meeting, @devincoughlin suggested that the naming should be consistent in clang and swift. We currently are not planning to change this in clang


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