Skip to content

[Swift C++ Interop] Propagate hardening build setting to Swift #494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Sources/SWBUniversalPlatform/Specs/Swift.xcspec
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,21 @@
};
},

// Hidden clang importer options to control C++ behavior
// in the clang importer, not visible in build settings.
{
Name = "SWIFT_CLANG_CXX_STANDARD_LIBRARY_HARDENING";
Type = String;
DefaultValue = "$(CLANG_CXX_STANDARD_LIBRARY_HARDENING)";
CommandLineArgs = {
"none" = ("-Xcc", "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_NONE");
"fast" = ("-Xcc", "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST");
"extensive" = ("-Xcc", "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE");
"debug" = ("-Xcc", "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG");
"<<otherwise>>" = ();
};
},

{
Name = "SWIFT_OVERLOAD_PREBUILT_MODULE_CACHE_PATH";
Type = Path;
Expand Down
96 changes: 96 additions & 0 deletions Tests/SWBTaskConstructionTests/SwiftTaskConstructionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3984,6 +3984,102 @@ fileprivate struct SwiftTaskConstructionTests: CoreBasedTests {
}
}

@Test(.requireSDKs(.macOS))
func enableHardeningInSwift() async throws {

func setupHardeningTest(_ tmpDir: Path,
hardeningMode: String) async throws -> TaskConstructionTester {
let testProject = try await TestProject(
"TestProject",
sourceRoot: tmpDir,
groupTree: TestGroup(
"SomeFiles",
children: [
TestFile("source.swift"),
TestFile("source.cpp")
]),
targets: [
TestStandardTarget(
"testFramework", type: .framework,
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"GENERATE_INFOPLIST_FILE": "YES",
"PRODUCT_NAME": "$(TARGET_NAME)",
"SWIFT_EXEC": swiftCompilerPath.str,
"SWIFT_VERSION": swiftVersion,
"CLANG_CXX_STANDARD_LIBRARY_HARDENING": hardeningMode
]),
],
buildPhases: [
TestSourcesBuildPhase(["source.swift", "source.cpp"])
]
)
])
let tester = try await TaskConstructionTester(getCore(), testProject)
return tester
}

// Verify that we don't enable hardening in Swift compilations when C++
// hardening is none.
try await withTemporaryDirectory { tmpDir in
let tester = try await setupHardeningTest(tmpDir, hardeningMode: "none")
await tester.checkBuild(runDestination: .macOS) { results in
results.checkTask(.matchRuleType("SwiftDriver Compilation")) { task in
task.checkCommandLineContainsUninterrupted(["-Xcc", "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_NONE"])
}
}
}

try await withTemporaryDirectory { tmpDir in
let tester = try await setupHardeningTest(tmpDir, hardeningMode: "fast")
await tester.checkBuild(runDestination: .macOS) { results in
results.checkTask(.matchRuleType("SwiftDriver Compilation")) { task in
task.checkCommandLineContainsUninterrupted(["-Xcc", "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST"])
}
}
}

try await withTemporaryDirectory { tmpDir in
let tester = try await setupHardeningTest(tmpDir, hardeningMode: "extensive")
await tester.checkBuild(runDestination: .macOS) { results in
results.checkTask(.matchRuleType("SwiftDriver Compilation")) { task in
task.checkCommandLineContainsUninterrupted(["-Xcc", "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE"])
}
}
}

try await withTemporaryDirectory { tmpDir in
let tester = try await setupHardeningTest(tmpDir, hardeningMode: "debug")
await tester.checkBuild(runDestination: .macOS) { results in
results.checkTask(.matchRuleType("SwiftDriver Compilation")) { task in
task.checkCommandLineContainsUninterrupted(["-Xcc", "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG"])
}
}
}

// Verify that we don't enable hardening in Swift compilations when C++
// hardening mode is garbage.
try await withTemporaryDirectory { tmpDir in
let tester = try await setupHardeningTest(tmpDir, hardeningMode: "unexpected")
await tester.checkBuild(runDestination: .macOS) { results in
results.checkTask(.matchRuleType("SwiftDriver Compilation")) { task in
task.checkCommandLineNoMatch([.prefix("-D_LIBCPP_HARDENING_MODE=")])
}
}
}

// Verify that we don't enable hardening in Swift compilations when C++
// hardening mode is empty.
try await withTemporaryDirectory { tmpDir in
let tester = try await setupHardeningTest(tmpDir, hardeningMode: "")
await tester.checkBuild(runDestination: .macOS) { results in
results.checkTask(.matchRuleType("SwiftDriver Compilation")) { task in
task.checkCommandLineNoMatch([.prefix("-D_LIBCPP_HARDENING_MODE=")])
}
}
}
}

@Test(.requireSDKs(.macOS))
func cxxInteropLinkerArgGeneration() async throws {
// 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.
Expand Down
Loading