From ceef2480d58f9cf6b5eb67ad46c7c572978eb1be Mon Sep 17 00:00:00 2001 From: Artem Chikin Date: Thu, 6 Apr 2023 16:51:03 -0700 Subject: [PATCH] Propagate sanitizer arguments to the clang-linker-driver invocations for dynamic libraries Unless we are building a static library, we need to propagate `-fsanitizer=` flag to the clang linker driver, otherwise sanitizers libraries will not be found at link-time. Resolves --- .../Jobs/DarwinToolchain+LinkerSupport.swift | 2 +- Tests/SwiftDriverTests/SwiftDriverTests.swift | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift b/Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift index e7cbefc97..f260e8ec0 100644 --- a/Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift +++ b/Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift @@ -199,7 +199,7 @@ extension DarwinToolchain { // 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 linkerOutputType == .executable && !sanitizers.isEmpty { + if linkerOutputType != .staticLibrary && !sanitizers.isEmpty { let sanitizerNames = sanitizers .map { $0.rawValue } .sorted() // Sort so we get a stable, testable order diff --git a/Tests/SwiftDriverTests/SwiftDriverTests.swift b/Tests/SwiftDriverTests/SwiftDriverTests.swift index e4a0eb1b6..6e3df061e 100644 --- a/Tests/SwiftDriverTests/SwiftDriverTests.swift +++ b/Tests/SwiftDriverTests/SwiftDriverTests.swift @@ -2450,6 +2450,34 @@ final class SwiftDriverTests: XCTestCase { XCTAssertTrue(linkCmd.contains(.flag("-fsanitize=address"))) } + do { + // address sanitizer on a dylib + var driver = try Driver(args: commonArgs + ["-sanitize=address", "-emit-library"]) + let plannedJobs = try driver.planBuild() + + XCTAssertEqual(plannedJobs.count, 3) + + let compileJob = plannedJobs[0] + let compileCmd = compileJob.commandLine + XCTAssertTrue(compileCmd.contains(.flag("-sanitize=address"))) + + let linkJob = plannedJobs[2] + let linkCmd = linkJob.commandLine + XCTAssertTrue(linkCmd.contains(.flag("-fsanitize=address"))) + } + + do { + // *no* address sanitizer on a static lib + var driver = try Driver(args: commonArgs + ["-sanitize=address", "-emit-library", "-static"]) + let plannedJobs = try driver.planBuild() + + XCTAssertEqual(plannedJobs.count, 3) + + let linkJob = plannedJobs[2] + let linkCmd = linkJob.commandLine + XCTAssertFalse(linkCmd.contains(.flag("-fsanitize=address"))) + } + do { // thread sanitizer var driver = try Driver(args: commonArgs + ["-sanitize=thread"])