Skip to content

[Sanitizer] Add new flag -sanitize-stable-abi flag to link against the #1495

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
18 changes: 16 additions & 2 deletions Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,8 @@ public struct Driver {

Self.validateSanitizerAddressUseOdrIndicatorFlag(&parsedOptions, diagnosticEngine: diagnosticsEngine, addressSanitizerEnabled: enabledSanitizers.contains(.address))

Self.validateSanitizeStableABI(&parsedOptions, diagnosticEngine: diagnosticsEngine, addressSanitizerEnabled: enabledSanitizers.contains(.address))

Self.validateSanitizerRecoverArgValues(&parsedOptions, diagnosticEngine: diagnosticsEngine, enabledSanitizers: enabledSanitizers)

Self.validateSanitizerCoverageArgs(&parsedOptions,
Expand Down Expand Up @@ -2434,14 +2436,15 @@ extension Driver {
continue
}

let stableAbi = sanitizer == .address && parsedOptions.contains(.sanitizeStableAbiEQ)
// Support is determined by existence of the sanitizer library.
// FIXME: Should we do this? This prevents cross-compiling with sanitizers
// enabled.
var sanitizerSupported = try toolchain.runtimeLibraryExists(
for: sanitizer,
for: stableAbi ? .address_stable_abi : sanitizer,
targetInfo: targetInfo,
parsedOptions: &parsedOptions,
isShared: sanitizer != .fuzzer
isShared: sanitizer != .fuzzer && !stableAbi
)

if sanitizer == .thread {
Expand Down Expand Up @@ -3057,6 +3060,17 @@ extension Driver {
}
}

private static func validateSanitizeStableABI(
_ parsedOptions: inout ParsedOptions,
diagnosticEngine: DiagnosticsEngine,
addressSanitizerEnabled: Bool
) {
if (parsedOptions.hasArgument(.sanitizeStableAbiEQ) && !addressSanitizerEnabled) {
diagnosticEngine.emit(
.warning_option_requires_sanitizer(currentOption: .sanitizeStableAbiEQ, currentOptionValue: "", sanitizerRequired: .address))
}
}

/// Validates the set of `-sanitize-recover={sanitizer}` arguments
private static func validateSanitizerRecoverArgValues(
_ parsedOptions: inout ParsedOptions,
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ extension DarwinToolchain {
.sorted() // Sort so we get a stable, testable order
.joined(separator: ",")
commandLine.appendFlag("-fsanitize=\(sanitizerNames)")

if parsedOptions.contains(.sanitizeStableAbiEQ) {
commandLine.appendFlag("-fsanitize-stable-abi")
}
}

if parsedOptions.contains(.embedBitcode) {
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ extension Driver {
try commandLine.appendLast(.sanitizeEQ, from: &parsedOptions)
try commandLine.appendLast(.sanitizeRecoverEQ, from: &parsedOptions)
try commandLine.appendLast(.sanitizeAddressUseOdrIndicator, from: &parsedOptions)
try commandLine.appendLast(.sanitizeStableAbiEQ, from: &parsedOptions)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we please guard this with isFrontendArgSupported(.sanitizeStableAbiEQ)?

try commandLine.appendLast(.sanitizeCoverageEQ, from: &parsedOptions)
try commandLine.appendLast(.static, from: &parsedOptions)
try commandLine.appendLast(.swiftVersion, from: &parsedOptions)
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftDriver/Utilities/Sanitizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public enum Sanitizer: String, Hashable {
/// Address sanitizer (ASan)
case address

// Address sanitizer Stable ABI (ASan)
case address_stable_abi

/// Thread sanitizer (TSan)
case thread

Expand All @@ -34,6 +37,7 @@ public enum Sanitizer: String, Hashable {
var libraryName: String {
switch self {
case .address: return "asan"
case .address_stable_abi: return "asan_abi"
case .thread: return "tsan"
case .undefinedBehavior: return "ubsan"
case .fuzzer: return "fuzzer"
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftOptions/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ extension Option {
public static let sanitizeAddressUseOdrIndicator: Option = Option("-sanitize-address-use-odr-indicator", .flag, attributes: [.helpHidden, .frontend, .noInteractive], 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")
public static let sanitizeCoverageEQ: Option = Option("-sanitize-coverage=", .commaJoined, attributes: [.frontend, .noInteractive], metaVar: "<type>", helpText: "Specify the type of coverage instrumentation for Sanitizers and additional options separated by commas")
public static let sanitizeRecoverEQ: Option = Option("-sanitize-recover=", .commaJoined, attributes: [.frontend, .noInteractive], metaVar: "<check>", helpText: "Specify which sanitizer runtime checks (see -sanitize=) will generate instrumentation that allows error recovery. Listed checks should be comma separated. Default behavior is to not allow error recovery.")
public static let sanitizeStableAbiEQ: Option = Option("-sanitize-stable-abi", .flag, attributes: [.frontend, .noInteractive], helpText: "ABI instrumentation for sanitizer runtime.")
public static let sanitizeEQ: Option = Option("-sanitize=", .commaJoined, attributes: [.frontend, .noInteractive], metaVar: "<check>", helpText: "Turn on runtime checks for erroneous behavior.")
public static let saveOptimizationRecordPasses: Option = Option("-save-optimization-record-passes", .separate, attributes: [.frontend], metaVar: "<regex>", helpText: "Only include passes which match a specified regular expression in the generated optimization record (by default, include all passes)")
public static let saveOptimizationRecordPath: Option = Option("-save-optimization-record-path", .separate, attributes: [.frontend, .argumentIsPath], helpText: "Specify the file name of any generated optimization record")
Expand Down Expand Up @@ -1523,6 +1524,7 @@ extension Option {
Option.sanitizeAddressUseOdrIndicator,
Option.sanitizeCoverageEQ,
Option.sanitizeRecoverEQ,
Option.sanitizeStableAbiEQ,
Option.sanitizeEQ,
Option.saveOptimizationRecordPasses,
Option.saveOptimizationRecordPath,
Expand Down
21 changes: 21 additions & 0 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2731,6 +2731,27 @@ final class SwiftDriverTests: XCTestCase {
}
}

func testSanitizeStableAbi() throws {
do {
var driver = try Driver(args: ["swiftc", "-sanitize=address", "-sanitize-stable-abi", "Test.swift"])

let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
XCTAssertEqual(plannedJobs.count, 2)
XCTAssertEqual(plannedJobs[0].kind, .compile)
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-sanitize=address")))
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-sanitize-stable-abi")))

XCTAssert(plannedJobs[1].commandLine.contains(.flag("-fsanitize=address")))
XCTAssert(plannedJobs[1].commandLine.contains(.flag("-fsanitize-stable-abi")))
}

do {
try assertDriverDiagnostics(args: ["swiftc","-sanitize-stable-abi", "Test.swift"]) {
$1.expect(.warning("option '-sanitize-stable-abi' has no effect when 'address' sanitizer is disabled. Use -sanitize=address to enable the sanitizer"))
}
}
}

func testADDITIONAL_SWIFT_DRIVER_FLAGS() throws {
var env = ProcessEnv.vars
env["ADDITIONAL_SWIFT_DRIVER_FLAGS"] = "-Xfrontend -unknown1 -Xfrontend -unknown2"
Expand Down