@@ -4042,6 +4042,102 @@ fileprivate struct SwiftTaskConstructionTests: CoreBasedTests {
4042
4042
}
4043
4043
}
4044
4044
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
+
4045
4141
@Test( . requireSDKs( . macOS) )
4046
4142
func cxxInteropLinkerArgGeneration( ) async throws {
4047
4143
// 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