Skip to content

Commit cc4e7f1

Browse files
committed
Sanitizers: Address and thread sanitizers config changes
Update the Address and Thread sanitizers XCspect properties to support using clang and swiftc as a linker.
1 parent 3c541cc commit cc4e7f1

File tree

5 files changed

+151
-7
lines changed

5 files changed

+151
-7
lines changed

Sources/SWBUniversalPlatform/Specs/Clang.xcspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2896,7 +2896,7 @@
28962896
};
28972897
AdditionalLinkerArgs = {
28982898
YES = (
2899-
"-fsanitize=address",
2899+
"$(LD_ADDRESS_SANITIZER)",
29002900
);
29012901
NO = ();
29022902
};

Sources/SWBUniversalPlatform/Specs/Ld.xcspec

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,17 +582,37 @@
582582
DefaultValue = YES;
583583
},
584584

585+
// Address Sanitizer options
586+
{
587+
Name = LD_ADDRESS_SANITIZER;
588+
DefaultValue = "$(LD_ADDRESS_SANITIZER_$(LINKER_DRIVER))";
589+
},
590+
{
591+
Name = LD_ADDRESS_SANITIZER_clang;
592+
DefaultValue = "-fsanitize=address";
593+
},
594+
{
595+
Name = LD_ADDRESS_SANITIZER_swiftc;
596+
DefaultValue = "-sanitize=address";
597+
},
598+
585599
// Thread Sanitizer options
586600
{
587601
Name = "LD_THREAD_SANITIZER";
588602
Type = Boolean;
589603
DefaultValue = "$(ENABLE_THREAD_SANITIZER)";
604+
},
605+
{
606+
Name = "CLANG_LD_THREAD_SANITIZER";
607+
Type = Boolean;
608+
DefaultValue = "$(LD_THREAD_SANITIZER)";
590609
Architectures = (
591610
x86_64,
592611
x86_64h,
593612
arm64,
594613
arm64e,
595614
);
615+
Condition = "$(LINKER_DRIVER) == clang";
596616
CommandLineArgs = {
597617
YES = (
598618
"-fsanitize=thread",
@@ -601,6 +621,25 @@
601621
};
602622
// Not visible in the build settings editor
603623
},
624+
{
625+
Name = "SWIFTC_LD_THREAD_SANITIZER";
626+
Type = Boolean;
627+
DefaultValue = "$(LD_THREAD_SANITIZER)";
628+
Architectures = (
629+
x86_64,
630+
x86_64h,
631+
arm64,
632+
arm64e,
633+
);
634+
Condition = "$(LINKER_DRIVER) == swiftc";
635+
CommandLineArgs = {
636+
YES = (
637+
"-sanitize=thread",
638+
);
639+
NO = ();
640+
};
641+
// Not visible in the build settings editor
642+
},
604643

605644
{
606645
Name = "LD_DEBUG_VARIANT";

Sources/SWBUniversalPlatform/Specs/Swift.xcspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@
12251225
};
12261226
AdditionalLinkerArgs = {
12271227
YES = (
1228-
"-fsanitize=address",
1228+
"$(LD_ADDRESS_SANITIZER)",
12291229
);
12301230
NO = ();
12311231
};

Tests/SWBTaskConstructionTests/LinkerTaskConstructionTests.swift

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,85 @@ fileprivate struct LinkerTaskConstructionTests: CoreBasedTests {
141141
}
142142
}
143143
}
144+
145+
@Test(
146+
.requireSDKs(.host),
147+
arguments: [
148+
(
149+
buildSettingNameUT: "ENABLE_ADDRESS_SANITIZER",
150+
linkerDriverUT: "clang",
151+
expectedArgument: "-fsanitize=address",
152+
),
153+
(
154+
buildSettingNameUT: "ENABLE_ADDRESS_SANITIZER",
155+
linkerDriverUT: "swiftc",
156+
expectedArgument: "-sanitize=address",
157+
),
158+
(
159+
buildSettingNameUT: "ENABLE_THREAD_SANITIZER",
160+
linkerDriverUT: "clang",
161+
expectedArgument: "-fsanitize=thread",
162+
),
163+
(
164+
buildSettingNameUT: "ENABLE_THREAD_SANITIZER",
165+
linkerDriverUT: "swiftc",
166+
expectedArgument: "-sanitize=thread",
167+
),
168+
],
169+
)
170+
func ldSanitizerArgumentsAppearsOnCommandLine(
171+
buildSettingNameUT: String,
172+
linkerDriverUT: String,
173+
expectedArgument: String,
174+
) async throws {
175+
let testProject = TestProject(
176+
"aProject",
177+
groupTree: TestGroup(
178+
"SomeFiles",
179+
children: [
180+
TestFile("c.c"),
181+
TestFile("cxx.cpp"),
182+
TestFile("s.swift"),
183+
]),
184+
buildConfigurations: [
185+
TestBuildConfiguration("Debug", buildSettings: [
186+
"PRODUCT_NAME": "$(TARGET_NAME)",
187+
"SWIFT_EXEC": try await swiftCompilerPath.str,
188+
"SWIFT_VERSION": try await swiftVersion
189+
]),
190+
],
191+
targets: [
192+
TestStandardTarget(
193+
"Library",
194+
type: .dynamicLibrary,
195+
buildConfigurations: [
196+
TestBuildConfiguration("Debug", buildSettings: [:]),
197+
],
198+
buildPhases: [
199+
TestSourcesBuildPhase(["c.c", "cxx.cpp", "s.swift"]),
200+
],
201+
),
202+
],
203+
)
204+
let core = try await getCore()
205+
let tester = try TaskConstructionTester(core, testProject)
206+
207+
await tester.checkBuild(
208+
BuildParameters(
209+
configuration: "Debug",
210+
overrides: [
211+
"LINKER_DRIVER": linkerDriverUT,
212+
buildSettingNameUT: "YES",
213+
],
214+
),
215+
runDestination: .host,
216+
) { results in
217+
results.checkNoDiagnostics()
218+
results.checkTask(.matchRuleType("Ld")) { task in
219+
task.checkCommandLineContains([expectedArgument])
220+
}
221+
}
222+
223+
}
224+
144225
}

Tests/SWBTaskConstructionTests/TaskConstructionTests.swift

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4061,8 +4061,17 @@ fileprivate struct TaskConstructionTests: CoreBasedTests {
40614061
}
40624062

40634063
/// Test that we properly generate commands for the compiler sanitizer features.
4064-
@Test(.requireSDKs(.macOS))
4065-
func sanitizers() async throws {
4064+
@Test(
4065+
.requireSDKs(.macOS),
4066+
arguments:[
4067+
(linkerDriver: "clang", expectedArgument: "-fsanitize=address"),
4068+
(linkerDriver: "swiftc", expectedArgument: "-sanitize=address"),
4069+
],
4070+
)
4071+
func sanitizers(
4072+
linkerDriver: String,
4073+
expectedArgument: String,
4074+
) async throws {
40664075
try await withTemporaryDirectory { tmpDir in
40674076
let targetName = "AppTarget"
40684077
let testProject = try await TestProject(
@@ -4115,7 +4124,18 @@ fileprivate struct TaskConstructionTests: CoreBasedTests {
41154124
}
41164125

41174126
// Check the LibSystem address sanitizer.
4118-
await tester.checkBuild(BuildParameters(configuration: "Debug", overrides: ["ENABLE_ADDRESS_SANITIZER": "YES","ENABLE_SYSTEM_SANITIZERS": "YES" ]), runDestination: .macOS, fs: fs) { results in
4127+
await tester.checkBuild(
4128+
BuildParameters(
4129+
configuration: "Debug",
4130+
overrides: [
4131+
"LINKER_DRIVER": linkerDriver,
4132+
"ENABLE_ADDRESS_SANITIZER": "YES",
4133+
"ENABLE_SYSTEM_SANITIZERS": "YES",
4134+
],
4135+
),
4136+
runDestination: .macOS,
4137+
fs: fs,
4138+
) { results in
41194139
results.checkTarget(targetName) { target in
41204140
// There should be one CompileC task, which includes the ASan option, and which puts its output in a -asan directory.
41214141
results.checkTask(.matchTarget(target), .matchRuleType("CompileC")) { task in
@@ -4129,8 +4149,12 @@ fileprivate struct TaskConstructionTests: CoreBasedTests {
41294149
}
41304150
results.checkTask(.matchTarget(target), .matchRuleType("Ld")) { task in
41314151
task.checkCommandLineContains(
4132-
["-fsanitize=address", "-fsanitize-stable-abi", "\(SRCROOT)/build/Debug/\(targetName).app/Contents/MacOS/\(targetName)"
4133-
])
4152+
[
4153+
expectedArgument,
4154+
"-fsanitize-stable-abi",
4155+
"\(SRCROOT)/build/Debug/\(targetName).app/Contents/MacOS/\(targetName)",
4156+
]
4157+
)
41344158
}
41354159
}
41364160
}

0 commit comments

Comments
 (0)