Skip to content

Disable "UseOdrIndicator" ASan instrumentation mode by default. #35074

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 17, 2020
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: 4 additions & 0 deletions include/swift/AST/IRGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ class IRGenOptions {
/// Which sanitizer(s) have recovery instrumentation enabled.
OptionSet<SanitizerKind> SanitizersWithRecoveryInstrumentation;

/// Whether to enable ODR indicators when building with ASan.
unsigned SanitizeAddressUseODRIndicator : 1;

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

Expand Down Expand Up @@ -345,6 +348,7 @@ class IRGenOptions {
Verify(true), OptMode(OptimizationMode::NotSet),
Sanitizers(OptionSet<SanitizerKind>()),
SanitizersWithRecoveryInstrumentation(OptionSet<SanitizerKind>()),
SanitizeAddressUseODRIndicator(false),
DebugInfoLevel(IRGenDebugInfoLevel::None),
DebugInfoFormat(IRGenDebugInfoFormat::None),
DisableClangModuleSkeletonCUs(false), UseJIT(false),
Expand Down
7 changes: 7 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,13 @@ def sanitize_recover_EQ
"checks should be comma separated. Default behavior is to not "
"allow error recovery.">;

def sanitize_address_use_odr_indicator
: Flag<["-"], "sanitize-address-use-odr-indicator">,
Flags<[FrontendOption, NoInteractiveOption, HelpHidden]>,
HelpText<"When using AddressSanitizer enable ODR indicator globals to "
"avoid false ODR violation reports in partially sanitized "
"programs at the cost of an increase in binary size">;

def sanitize_coverage_EQ : CommaJoined<["-"], "sanitize-coverage=">,
Flags<[FrontendOption, NoInteractiveOption]>,
MetaVarName<"<type>">,
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 @@ -47,6 +47,11 @@ llvm::SanitizerCoverageOptions parseSanitizerCoverageArgValue(
DiagnosticEngine &Diag,
OptionSet<SanitizerKind> sanitizers);

/// Parse -sanitize-address-use-odr-indicator's value.
bool parseSanitizerAddressUseODRIndicator(
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
2 changes: 2 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
options::OPT_no_warnings_as_errors);
inputArgs.AddLastArg(arguments, options::OPT_sanitize_EQ);
inputArgs.AddLastArg(arguments, options::OPT_sanitize_recover_EQ);
inputArgs.AddLastArg(arguments,
options::OPT_sanitize_address_use_odr_indicator);
inputArgs.AddLastArg(arguments, options::OPT_sanitize_coverage_EQ);
inputArgs.AddLastArg(arguments, options::OPT_static);
inputArgs.AddLastArg(arguments, options::OPT_swift_version);
Expand Down
6 changes: 6 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,12 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
/*emitWarnings=*/true);
}

if (const Arg *A =
Args.getLastArg(options::OPT_sanitize_address_use_odr_indicator)) {
IRGenOpts.SanitizeAddressUseODRIndicator =
parseSanitizerAddressUseODRIndicator(A, Opts.Sanitizers, Diags);
}

if (auto A = Args.getLastArg(OPT_enable_verify_exclusivity,
OPT_disable_verify_exclusivity)) {
Opts.VerifyExclusivity
Expand Down
6 changes: 4 additions & 2 deletions lib/IRGen/IRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
auto recover =
bool(BuilderWrapper.IRGOpts.SanitizersWithRecoveryInstrumentation &
SanitizerKind::Address);
auto useODRIndicator = BuilderWrapper.IRGOpts.SanitizeAddressUseODRIndicator;
PM.add(createAddressSanitizerFunctionPass(/*CompileKernel=*/false, recover));
PM.add(createModuleAddressSanitizerLegacyPassPass(/*CompileKernel=*/false,
recover));
PM.add(createModuleAddressSanitizerLegacyPassPass(
/*CompileKernel=*/false, recover, /*UseGlobalsGC=*/true,
useODRIndicator));
Copy link
Contributor

Choose a reason for hiding this comment

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

Just note to myself: here is the place we are switching the default. We are now using an explicit value determined by the driver instead of the default specified in the header (which is true).

}

static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
Expand Down
16 changes: 16 additions & 0 deletions lib/Option/SanitizerOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,22 @@ OptionSet<SanitizerKind> swift::parseSanitizerRecoverArgValues(
return sanitizerRecoverSet;
}

// Note this implementation cannot be inlined at its use site because it calls
// `toStringRef(const SanitizerKind).`
bool swift::parseSanitizerAddressUseODRIndicator(
const llvm::opt::Arg *A, const OptionSet<SanitizerKind> &enabledSanitizers,
Copy link
Member

Choose a reason for hiding this comment

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

Given that we don't expect this to be NULL (see below), I think that it would be better to change the signature to take the Arg by reference (const llvm::opt::Arg &A).

Copy link
Contributor Author

@danliew-apple danliew-apple Dec 15, 2020

Choose a reason for hiding this comment

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

This is a valid criticism but the use of llvm::opt::Arg* here is deliberate. It's being used so that this new function follows a similar design as the other functions in include/swift/Option/SanitizerOptions.h.

I'd prefer to keep the APIs as uniform as possible. Note I'm aware there are some oddities already in this file (inconsistent argument order) but we shouldn't add to them if we don't have to.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think its adding any inconsistency. The LLVM style actually encourages this - pass by reference unless you need to be able to pass in nullptr as a reference to nullptr is not well-formed.

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 wasn't talking about inconsistency with respect to LLVM's recommended style. I'm talking about inconsistency with respect to the APIs that already exist in SanitizerOptions.h. They all take a llvm::opt::Arg*. All of these could actually be Arg& because they all assume that Arg* A is not null. Deliberately adding an API that doesn't match the existing APIs is an unnecessary inconsistency. We could change all of the functions in the header but that's really out-of-scope for this PR. If you really want this that let's do that in a separate PR.

I suspect the reason these APIs take a llvm::opt::Arg* right now is that is the type available at the use sites and so passing it directly was the most convenient thing to do.

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
11 changes: 11 additions & 0 deletions test/Driver/sanitize_address_use_odr_indicator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// REQUIRES: asan_runtime
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -sanitize-address-use-odr-indicator %s 2>&1 | %FileCheck %s

// CHECK: swift
// CHECK-DAG: -sanitize=address
// CHECK-DAG: -sanitize-address-use-odr-indicator

// Missing `-sanitize=address` causes warning to be emitted
// RUN: %swiftc_driver -sanitize-address-use-odr-indicator \
// RUN: %s -o %t 2>&1 | %FileCheck -check-prefix=CHECK-MISSING-ARG %s
// CHECK-MISSING-ARG: warning: option '-sanitize-address-use-odr-indicator' has no effect when 'address' sanitizer is disabled. Use -sanitize=address to enable the sanitizer
18 changes: 18 additions & 0 deletions test/IRGen/address_sanitizer_use_odr_indicator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// REQUIRES: asan_runtime

// Default instrumentation that does not use ODR indicators
// and private aliases.
// RUN: %target-swift-frontend -emit-ir -sanitize=address %s | %FileCheck %s
// CHECK: @"[[MANGLED_GLOBAL:.+aGlobal.+]]" =
// CHECK-NOT: __odr_asan_gen_[[MANGLED_GLOBAL]]
// CHECK-NOT: @{{.+}} = private alias {{.*}}@"[[MANGLED_GLOBAL]]"

// Instrumentation with ODR indicators (implies private aliases)
// RUN: %target-swift-frontend -emit-ir -sanitize=address \
// RUN: -sanitize-address-use-odr-indicator %s | \
// RUN: %FileCheck -check-prefix=CHECK-ODR %s
// CHECK-ODR: @"[[MANGLED_GLOBAL:.+aGlobal.+]]" =
// CHECK-ODR: __odr_asan_gen_[[MANGLED_GLOBAL]]
// CHECK-ODR: @{{.+}} = private alias {{.*}}@"[[MANGLED_GLOBAL]]"

let aGlobal:Int = 128;
8 changes: 8 additions & 0 deletions test/Sanitizers/asan.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
// RUN: %target-swiftc_driver %s -target %sanitizers-target-triple -g -sanitize=address -o %t_asan-binary
// RUN: %target-codesign %t_asan-binary
// RUN: env %env-ASAN_OPTIONS=abort_on_error=0 not %target-run %t_asan-binary 2>&1 | %FileCheck %s

// ODR Indicator variant
// RUN: %target-swiftc_driver %s -target %sanitizers-target-triple -g -sanitize=address \
// RUN: -sanitize-address-use-odr-indicator -o %t_asan-binary-odr-indicator
// RUN: %target-codesign %t_asan-binary-odr-indicator
// RUN: env %env-ASAN_OPTIONS=abort_on_error=0 not %target-run \
// RUN: %t_asan-binary-odr-indicator 2>&1 | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: asan_runtime

Expand Down