Skip to content

Commit a1db446

Browse files
authored
Merge pull request #498 from swiftlang/susmonteiro/enable-hardening-swift
[Swift C++ Interop] Propagate hardening build setting to Swift
2 parents be93573 + 5ce5042 commit a1db446

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

Sources/SWBUniversalPlatform/Specs/Swift.xcspec

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,21 @@
13531353
};
13541354
},
13551355

1356+
// Hidden clang importer options to control C++ behavior
1357+
// in the clang importer, not visible in build settings.
1358+
{
1359+
Name = "SWIFT_CLANG_CXX_STANDARD_LIBRARY_HARDENING";
1360+
Type = String;
1361+
DefaultValue = "$(CLANG_CXX_STANDARD_LIBRARY_HARDENING)";
1362+
CommandLineArgs = {
1363+
"none" = ("-Xcc", "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_NONE");
1364+
"fast" = ("-Xcc", "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST");
1365+
"extensive" = ("-Xcc", "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE");
1366+
"debug" = ("-Xcc", "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG");
1367+
"<<otherwise>>" = ();
1368+
};
1369+
},
1370+
13561371
{
13571372
Name = "SWIFT_OVERLOAD_PREBUILT_MODULE_CACHE_PATH";
13581373
Type = Path;

Tests/SWBTaskConstructionTests/SwiftTaskConstructionTests.swift

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4042,6 +4042,102 @@ fileprivate struct SwiftTaskConstructionTests: CoreBasedTests {
40424042
}
40434043
}
40444044

4045+
@Test(.requireSDKs(.macOS))
4046+
func enableHardeningInSwift() async throws {
4047+
4048+
func setupHardeningTest(_ tmpDir: Path,
4049+
hardeningMode: String) async throws -> TaskConstructionTester {
4050+
let testProject = try await TestProject(
4051+
"TestProject",
4052+
sourceRoot: tmpDir,
4053+
groupTree: TestGroup(
4054+
"SomeFiles",
4055+
children: [
4056+
TestFile("source.swift"),
4057+
TestFile("source.cpp")
4058+
]),
4059+
targets: [
4060+
TestStandardTarget(
4061+
"testFramework", type: .framework,
4062+
buildConfigurations: [
4063+
TestBuildConfiguration("Debug", buildSettings: [
4064+
"GENERATE_INFOPLIST_FILE": "YES",
4065+
"PRODUCT_NAME": "$(TARGET_NAME)",
4066+
"SWIFT_EXEC": swiftCompilerPath.str,
4067+
"SWIFT_VERSION": swiftVersion,
4068+
"CLANG_CXX_STANDARD_LIBRARY_HARDENING": hardeningMode
4069+
]),
4070+
],
4071+
buildPhases: [
4072+
TestSourcesBuildPhase(["source.swift", "source.cpp"])
4073+
]
4074+
)
4075+
])
4076+
let tester = try await TaskConstructionTester(getCore(), testProject)
4077+
return tester
4078+
}
4079+
4080+
// Verify that we don't enable hardening in Swift compilations when C++
4081+
// hardening is none.
4082+
try await withTemporaryDirectory { tmpDir in
4083+
let tester = try await setupHardeningTest(tmpDir, hardeningMode: "none")
4084+
await tester.checkBuild(runDestination: .macOS) { results in
4085+
results.checkTask(.matchRuleType("SwiftDriver Compilation")) { task in
4086+
task.checkCommandLineContainsUninterrupted(["-Xcc", "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_NONE"])
4087+
}
4088+
}
4089+
}
4090+
4091+
try await withTemporaryDirectory { tmpDir in
4092+
let tester = try await setupHardeningTest(tmpDir, hardeningMode: "fast")
4093+
await tester.checkBuild(runDestination: .macOS) { results in
4094+
results.checkTask(.matchRuleType("SwiftDriver Compilation")) { task in
4095+
task.checkCommandLineContainsUninterrupted(["-Xcc", "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST"])
4096+
}
4097+
}
4098+
}
4099+
4100+
try await withTemporaryDirectory { tmpDir in
4101+
let tester = try await setupHardeningTest(tmpDir, hardeningMode: "extensive")
4102+
await tester.checkBuild(runDestination: .macOS) { results in
4103+
results.checkTask(.matchRuleType("SwiftDriver Compilation")) { task in
4104+
task.checkCommandLineContainsUninterrupted(["-Xcc", "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE"])
4105+
}
4106+
}
4107+
}
4108+
4109+
try await withTemporaryDirectory { tmpDir in
4110+
let tester = try await setupHardeningTest(tmpDir, hardeningMode: "debug")
4111+
await tester.checkBuild(runDestination: .macOS) { results in
4112+
results.checkTask(.matchRuleType("SwiftDriver Compilation")) { task in
4113+
task.checkCommandLineContainsUninterrupted(["-Xcc", "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG"])
4114+
}
4115+
}
4116+
}
4117+
4118+
// Verify that we don't enable hardening in Swift compilations when C++
4119+
// hardening mode is garbage.
4120+
try await withTemporaryDirectory { tmpDir in
4121+
let tester = try await setupHardeningTest(tmpDir, hardeningMode: "unexpected")
4122+
await tester.checkBuild(runDestination: .macOS) { results in
4123+
results.checkTask(.matchRuleType("SwiftDriver Compilation")) { task in
4124+
task.checkCommandLineNoMatch([.prefix("-D_LIBCPP_HARDENING_MODE=")])
4125+
}
4126+
}
4127+
}
4128+
4129+
// Verify that we don't enable hardening in Swift compilations when C++
4130+
// hardening mode is empty.
4131+
try await withTemporaryDirectory { tmpDir in
4132+
let tester = try await setupHardeningTest(tmpDir, hardeningMode: "")
4133+
await tester.checkBuild(runDestination: .macOS) { results in
4134+
results.checkTask(.matchRuleType("SwiftDriver Compilation")) { task in
4135+
task.checkCommandLineNoMatch([.prefix("-D_LIBCPP_HARDENING_MODE=")])
4136+
}
4137+
}
4138+
}
4139+
}
4140+
40454141
@Test(.requireSDKs(.macOS))
40464142
func cxxInteropLinkerArgGeneration() async throws {
40474143
// When Swift is generating additional linker args, we should not try to inject the response file when a target is a dependent of a cxx-interop target but has no Swift source of its own.

0 commit comments

Comments
 (0)