Skip to content

Commit f602d24

Browse files
authored
Merge pull request #1326 from artemcm/59SanitizersForAll_dylibs
[5.9] Propagate sanitizer arguments to the clang-linker-driver invocations for dynamic libraries
2 parents 3e902fc + a6bb02e commit f602d24

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ extension DarwinToolchain {
199199
// Linking sanitizers will add rpaths, which might negatively interact when
200200
// other rpaths are involved, so we should make sure we add the rpaths after
201201
// all user-specified rpaths.
202-
if linkerOutputType == .executable && !sanitizers.isEmpty {
202+
if linkerOutputType != .staticLibrary && !sanitizers.isEmpty {
203203
let sanitizerNames = sanitizers
204204
.map { $0.rawValue }
205205
.sorted() // Sort so we get a stable, testable order

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2450,6 +2450,34 @@ final class SwiftDriverTests: XCTestCase {
24502450
XCTAssertTrue(linkCmd.contains(.flag("-fsanitize=address")))
24512451
}
24522452

2453+
do {
2454+
// address sanitizer on a dylib
2455+
var driver = try Driver(args: commonArgs + ["-sanitize=address", "-emit-library"])
2456+
let plannedJobs = try driver.planBuild()
2457+
2458+
XCTAssertEqual(plannedJobs.count, 3)
2459+
2460+
let compileJob = plannedJobs[0]
2461+
let compileCmd = compileJob.commandLine
2462+
XCTAssertTrue(compileCmd.contains(.flag("-sanitize=address")))
2463+
2464+
let linkJob = plannedJobs[2]
2465+
let linkCmd = linkJob.commandLine
2466+
XCTAssertTrue(linkCmd.contains(.flag("-fsanitize=address")))
2467+
}
2468+
2469+
do {
2470+
// *no* address sanitizer on a static lib
2471+
var driver = try Driver(args: commonArgs + ["-sanitize=address", "-emit-library", "-static"])
2472+
let plannedJobs = try driver.planBuild()
2473+
2474+
XCTAssertEqual(plannedJobs.count, 3)
2475+
2476+
let linkJob = plannedJobs[2]
2477+
let linkCmd = linkJob.commandLine
2478+
XCTAssertFalse(linkCmd.contains(.flag("-fsanitize=address")))
2479+
}
2480+
24532481
do {
24542482
// thread sanitizer
24552483
var driver = try Driver(args: commonArgs + ["-sanitize=thread"])

0 commit comments

Comments
 (0)