Skip to content

Commit

Permalink
Propagate sanitizer arguments to the clang-linker-driver invocations …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
artemcm committed Apr 6, 2023
1 parent 7a96ffd commit ceef248
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Expand Up @@ -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"])
Expand Down

0 comments on commit ceef248

Please sign in to comment.