Skip to content

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

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
18 changes: 16 additions & 2 deletions Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,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 @@ -2439,14 +2441,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 @@ -3062,6 +3065,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
3 changes: 3 additions & 0 deletions Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ extension Driver {
try commandLine.appendLast(.sanitizeEQ, from: &parsedOptions)
try commandLine.appendLast(.sanitizeRecoverEQ, from: &parsedOptions)
try commandLine.appendLast(.sanitizeAddressUseOdrIndicator, from: &parsedOptions)
if isFrontendArgSupported(.sanitizeStableAbiEQ) {
try commandLine.appendLast(.sanitizeStableAbiEQ, from: &parsedOptions)
}
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
24 changes: 24 additions & 0 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2781,6 +2781,30 @@ final class SwiftDriverTests: XCTestCase {
}
}

func testSanitizeStableAbi() throws {
var driver = try Driver(args: ["swiftc", "-sanitize=address", "-sanitize-stable-abi", "Test.swift"])
guard driver.isFrontendArgSupported(.sanitizeStableAbiEQ) else {
return
}

do {
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