Skip to content

Commit 7319148

Browse files
authored
Simplify cross-platform hashing implementations (#388)
Eliminate the package dependency on swift-crypto to simplify the bootstrapped build. Instead, vendor the SHA256 implementation from swift-tools-support-core and use it as a fallback if neither WinSDK nor CryptoKit is available.
1 parent 253fbdf commit 7319148

29 files changed

+248
-83
lines changed

Package.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ let package = Package(
195195
"SWBCSupport",
196196
"SWBLibc",
197197
.product(name: "ArgumentParser", package: "swift-argument-parser"),
198-
.product(name: "Crypto", package: "swift-crypto", condition: .when(platforms: [.linux, .openbsd, .android])),
199198
.product(name: "SystemPackage", package: "swift-system", condition: .when(platforms: [.linux, .android, .windows])),
200199
],
201200
exclude: ["CMakeLists.txt"],
@@ -427,7 +426,6 @@ for target in package.targets {
427426
// `SWIFTCI_USE_LOCAL_DEPS` configures if dependencies are locally available to build
428427
if useLocalDependencies {
429428
package.dependencies += [
430-
.package(path: "../swift-crypto"),
431429
.package(path: "../swift-driver"),
432430
.package(path: "../swift-system"),
433431
.package(path: "../swift-argument-parser"),
@@ -437,7 +435,6 @@ if useLocalDependencies {
437435
}
438436
} else {
439437
package.dependencies += [
440-
.package(url: "https://github.com/apple/swift-crypto.git", "2.0.0"..<"4.0.0"),
441438
.package(url: "https://github.com/swiftlang/swift-driver.git", branch: "main"),
442439
.package(url: "https://github.com/apple/swift-system.git", .upToNextMajor(from: "1.4.1")),
443440
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.0.3"),

Plugins/cmake-smoke-test/cmake-smoke-test.swift

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ struct CMakeSmokeTest: CommandPlugin {
4141
let swiftSystemURL = try findSiblingRepository("swift-system", swiftBuildURL: swiftBuildURL)
4242
let swiftSystemBuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-system")
4343

44-
let swiftAsn1URL = try findSiblingRepository("swift-asn1", swiftBuildURL: swiftBuildURL)
45-
let swiftAsn1BuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-asn1")
46-
47-
let swiftCryptoURL = try findSiblingRepository("swift-crypto", swiftBuildURL: swiftBuildURL)
48-
let swiftCryptoBuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-crypto")
49-
5044
let llbuildURL = try findSiblingRepository("llbuild", swiftBuildURL: swiftBuildURL)
5145
let llbuildBuildURL = context.pluginWorkDirectoryURL.appending(component: "llbuild")
5246

@@ -56,7 +50,7 @@ struct CMakeSmokeTest: CommandPlugin {
5650
let swiftDriverURL = try findSiblingRepository("swift-driver", swiftBuildURL: swiftBuildURL)
5751
let swiftDriverBuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-driver")
5852

59-
for url in [swiftToolsSupportCoreBuildURL, swiftAsn1BuildURL, swiftCryptoBuildURL, swiftSystemBuildURL, llbuildBuildURL, swiftArgumentParserBuildURL, swiftDriverBuildURL, swiftBuildBuildURL] {
53+
for url in [swiftToolsSupportCoreBuildURL, swiftSystemBuildURL, llbuildBuildURL, swiftArgumentParserBuildURL, swiftDriverBuildURL, swiftBuildBuildURL] {
6054
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
6155
}
6256

@@ -69,8 +63,6 @@ struct CMakeSmokeTest: CommandPlugin {
6963
"-DArgumentParser_DIR=\(swiftArgumentParserBuildURL.appending(components: "cmake", "modules").path())",
7064
"-DLLBuild_DIR=\(llbuildBuildURL.appending(components: "cmake", "modules").path())",
7165
"-DTSC_DIR=\(swiftToolsSupportCoreBuildURL.appending(components: "cmake", "modules").path())",
72-
"-DSwiftASN1_DIR=\(swiftAsn1BuildURL.appending(components: "cmake", "modules").path())",
73-
"-DSwiftCrypto_DIR=\(swiftCryptoBuildURL.appending(components: "cmake", "modules").path())",
7466
"-DSwiftDriver_DIR=\(swiftDriverBuildURL.appending(components: "cmake", "modules").path())",
7567
"-DSwiftSystem_DIR=\(swiftSystemBuildURL.appending(components: "cmake", "modules").path())"
7668
]
@@ -87,18 +79,6 @@ struct CMakeSmokeTest: CommandPlugin {
8779
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftToolsSupportCoreBuildURL)
8880
print("Built swift-tools-support-core")
8981

90-
if hostOS != .macOS && hostOS != .windows {
91-
print("Building swift-asn1")
92-
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftAsn1URL.path()], workingDirectory: swiftAsn1BuildURL)
93-
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftAsn1BuildURL)
94-
print("Built swift-asn1")
95-
96-
print("Building swift-crypto")
97-
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftCryptoURL.path()], workingDirectory: swiftCryptoBuildURL)
98-
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftCryptoBuildURL)
99-
print("Built swift-crypto")
100-
}
101-
10282
if hostOS != .macOS {
10383
print("Building swift-system")
10484
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftSystemURL.path()], workingDirectory: swiftSystemBuildURL)

Sources/SWBBuildSystem/BuildOperation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,7 @@ internal final class OperationSystemAdaptor: SWBLLBuild.BuildSystemDelegate, Act
15301530
return
15311531
}
15321532

1533-
let signatureCtx = MD5Context()
1533+
let signatureCtx = InsecureHashContext()
15341534
signatureCtx.add(string: "CleanupCompileCache")
15351535
signatureCtx.add(string: cachePath.str)
15361536
let signature = signatureCtx.signature

Sources/SWBCore/EnvironmentBindings.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public struct EnvironmentBindings: Sendable {
4141
}
4242

4343
/// Add a signature of the bindings into the given context.
44-
public func computeSignature(into ctx: MD5Context) {
44+
public func computeSignature(into ctx: InsecureHashContext) {
4545
for (variable, val) in bindings {
4646
// The signature computation should record the variable name and value data, and the positions which divide them.
4747
ctx.add(string: variable)

Sources/SWBCore/LibSwiftDriver/PlannedBuild.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public struct SwiftDriverJob: Serializable, CustomDebugStringConvertible {
103103
self.cacheKeys = job.outputCacheKeys.reduce(into: [String]()) { result, key in
104104
result.append(key.value)
105105
}.sorted()
106-
let md5 = MD5Context()
106+
let md5 = InsecureHashContext()
107107
for arg in commandLine {
108108
md5.add(bytes: arg)
109109
}

Sources/SWBCore/PlannedTask.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public struct TaskIdentifier: Comparable, Hashable, Serializable, CustomStringCo
2828
}
2929

3030
public var sandboxProfileSentinel: String {
31-
let taskIdentifierChecksumContext = MD5Context()
31+
let taskIdentifierChecksumContext = InsecureHashContext()
3232
taskIdentifierChecksumContext.add(string: self.rawValue)
3333
return taskIdentifierChecksumContext.signature.asString
3434
}
@@ -45,7 +45,7 @@ extension TaskIdentifier {
4545
}
4646

4747
public init(forTarget: ConfiguredTarget?, dynamicTaskPayload: ByteString, priority: TaskPriority) {
48-
let ctx = MD5Context()
48+
let ctx = InsecureHashContext()
4949
ctx.add(bytes: dynamicTaskPayload)
5050
self.rawValue = "P\(priority.rawValue):\(forTarget?.guid.stringValue ?? ""):\(forTarget?.parameters.configuration ?? ""):\(ctx.signature.asString)"
5151
}

Sources/SWBCore/ProjectModel/BuildPhase.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public class BuildPhaseWithBuildFiles: BuildPhase, @unchecked Sendable
124124

125125
/// Returns a string that can be used as a suffix for the base name of a derived file in order to make its filename unique. This is based on the file's path but contains only characters that have no special meaning in filenames. For example, the string will never contain a path separator character.
126126
public static func filenameUniquefierSuffixFor(path: Path) -> String {
127-
let digester = MD5Context.init()
127+
let digester = InsecureHashContext.init()
128128
digester.add(string: path.normalize().str)
129129
return digester.signature.asString
130130
}

Sources/SWBCore/SpecImplementations/Tools/CCompiler.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ public class ClangCompilerSpec : CompilerSpec, SpecIdentifierType, GCCCompatible
690690
previousArg = argAsByteString
691691
}
692692

693-
let ctx = MD5Context()
693+
let ctx = InsecureHashContext()
694694
ctx.add(string: inputFileType.identifier)
695695
ctx.add(string: self.identifier)
696696

@@ -1412,7 +1412,7 @@ public class ClangCompilerSpec : CompilerSpec, SpecIdentifierType, GCCCompatible
14121412
return nil
14131413
}
14141414

1415-
let md5 = MD5Context()
1415+
let md5 = InsecureHashContext()
14161416
md5.add(string: prefixHeader.str)
14171417
let sharingIdentHashValue = md5.signature
14181418
let baseCachePath = scope.evaluate(BuiltinMacros.SHARED_PRECOMPS_DIR)

Sources/SWBCore/SpecImplementations/Tools/SwiftCompiler.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ public final class SwiftCommandOutputParser: TaskOutputParser {
951951
ruleInfo = "\(ruleInfo) \(path.str.quotedDescription)"
952952
}
953953
let signature: ByteString = {
954-
let md5 = MD5Context()
954+
let md5 = InsecureHashContext()
955955
md5.add(string: ruleInfo)
956956
return md5.signature
957957
}()
@@ -3584,7 +3584,7 @@ extension SwiftCompilerSpec {
35843584
static public func computeRuleInfoAndSignatureForPerFileVirtualBatchSubtask(variant: String, arch: String, path: Path) -> ([String], ByteString) {
35853585
let ruleInfo = ["SwiftCompile", variant, arch, path.str.quotedDescription]
35863586
let signature: ByteString = {
3587-
let md5 = MD5Context()
3587+
let md5 = InsecureHashContext()
35883588
md5.add(string: ruleInfo.joined(separator: " "))
35893589
return md5.signature
35903590
}()

Sources/SWBTaskConstruction/TaskProducers/OtherTaskProducers/CustomTaskProducer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final class CustomTaskProducer: PhasedTaskProducer, TaskProducer {
3737

3838
if outputs.isEmpty {
3939
// If there are no outputs, create a virtual output that can be wired up to gates
40-
let md5Context = MD5Context()
40+
let md5Context = InsecureHashContext()
4141
for arg in commandLine {
4242
md5Context.add(string: arg)
4343
}

Sources/SWBTaskExecution/BuildDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ package final class BuildDescriptionBuilder {
928928
}
929929

930930
package static func computeShellToolSignature(args: [ByteString], environment: EnvironmentBindings?, dependencyData: DependencyDataStyle?, isUnsafeToInterrupt: Bool, additionalSignatureData: String) -> ByteString {
931-
let ctx = MD5Context()
931+
let ctx = InsecureHashContext()
932932
for arg in args {
933933
ctx.add(bytes: arg)
934934
}

Sources/SWBTaskExecution/BuildDescriptionManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ private final class BuildSystemTaskPlanningDelegate: TaskPlanningDelegate {
728728
}
729729

730730
package func recordAttachment(contents: SWBUtil.ByteString) -> SWBUtil.Path {
731-
let digester = MD5Context()
731+
let digester = InsecureHashContext()
732732
digester.add(bytes: contents)
733733
let path = descriptionPath.join("attachments").join(digester.signature.asString)
734734
do {

Sources/SWBTaskExecution/BuildDescriptionSignature.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ extension BuildDescriptionSignatureComponents {
114114
}
115115

116116
func signatureStringValue(humanReadableString: ByteString) -> BuildDescriptionSignature {
117-
let hashContext = MD5Context()
117+
let hashContext = InsecureHashContext()
118118
hashContext.add(bytes: humanReadableString)
119119
return hashContext.signature
120120
}

Sources/SWBTaskExecution/DynamicTaskSpecs/CompilationCachingDataPruner.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ package final class CompilationCachingDataPruner: Sendable {
9090
startedAction()
9191
let serializer = MsgPackSerializer()
9292
key.serialize(to: serializer)
93-
let signatureCtx = MD5Context()
93+
let signatureCtx = InsecureHashContext()
9494
signatureCtx.add(string: "ClangCachingPruneData")
9595
signatureCtx.add(bytes: serializer.byteString)
9696
let signature = signatureCtx.signature
@@ -159,7 +159,7 @@ package final class CompilationCachingDataPruner: Sendable {
159159
startedAction()
160160
let serializer = MsgPackSerializer()
161161
key.serialize(to: serializer)
162-
let signatureCtx = MD5Context()
162+
let signatureCtx = InsecureHashContext()
163163
signatureCtx.add(string: "ClangCachingPruneData")
164164
signatureCtx.add(bytes: serializer.byteString)
165165
let signature = signatureCtx.signature

Sources/SWBTaskExecution/DynamicTaskSpecs/CompilationCachingUploader.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ package final class CompilationCachingUploader {
6262
}
6363

6464
startedUpload()
65-
let signatureCtx = MD5Context()
65+
let signatureCtx = InsecureHashContext()
6666
signatureCtx.add(string: "ClangCachingUpload")
6767
signatureCtx.add(string: cacheKey)
6868
let signature = signatureCtx.signature
@@ -122,7 +122,7 @@ package final class CompilationCachingUploader {
122122
}
123123

124124
startedUpload()
125-
let signatureCtx = MD5Context()
125+
let signatureCtx = InsecureHashContext()
126126
signatureCtx.add(string: "SwiftCachingUpload")
127127
signatureCtx.add(string: cacheKey)
128128
let signature = signatureCtx.signature

Sources/SWBTaskExecution/TaskActions/AuxiliaryFileTaskAction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public final class AuxiliaryFileTaskAction: TaskAction {
117117
serializer.serialize(context.logContents)
118118
super.serialize(to: serializer)
119119
}
120-
let md5 = MD5Context()
120+
let md5 = InsecureHashContext()
121121
md5.add(bytes: serializer.byteString)
122122
return md5.signature
123123
}

Sources/SWBTaskExecution/TaskActions/SwiftDriverJobSchedulingTaskAction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ open class SwiftDriverJobSchedulingTaskAction: TaskAction {
137137
return
138138
}
139139

140-
let signatureCtx = MD5Context()
140+
let signatureCtx = InsecureHashContext()
141141
signatureCtx.add(string: task.identifier.rawValue)
142142
signatureCtx.add(string: "swiftdriverjobdiscoveryactivity")
143143
signatureCtx.add(number: dependencyID)

Sources/SWBTaskExecution/TaskActions/SwiftDriverJobTaskAction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public final class SwiftDriverJobTaskAction: TaskAction, BuildValueValidatingTas
164164
private var state = State()
165165

166166
public override func getSignature(_ task: any ExecutableTask, executionDelegate: any TaskExecutionDelegate) -> ByteString {
167-
let md5 = MD5Context()
167+
let md5 = InsecureHashContext()
168168
// We intentionally do not integrate the superclass signature here, because the driver job's signature captures the same information without requiring expensive serialization.
169169
md5.add(bytes: driverJob.driverJob.signature)
170170
task.environment.computeSignature(into: md5)

Sources/SWBTaskExecution/TaskActions/TaskAction.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ open class TaskAction: PlannedTaskAction, PolymorphicSerializable
5252
// FIXME: This is quite inefficient as practically used by the build system, because we end up serializing every task action twice, effectively. We could do a lot better if we were willing to lift this signature out somewhere else, but this is simply and ensures that by default we tend to capture every interesting piece of information in the signature.
5353
let sz = MsgPackSerializer()
5454
serialize(to: sz)
55-
let md5 = MD5Context()
55+
let md5 = InsecureHashContext()
5656
md5.add(bytes: sz.byteString)
5757
return md5.signature
5858
}
@@ -67,7 +67,7 @@ open class TaskAction: PlannedTaskAction, PolymorphicSerializable
6767
/// This is checked to determine if the command needs to rebuild versus the last time it was run.
6868
open func getSignature(_ task: any ExecutableTask, executionDelegate: any TaskExecutionDelegate) -> ByteString
6969
{
70-
let md5 = MD5Context()
70+
let md5 = InsecureHashContext()
7171
md5.add(bytes: serializedRepresentationSignature!)
7272
for arg in task.commandLine {
7373
md5.add(bytes: arg.asByteString)

Sources/SWBTestSupport/SkippedTestSupport.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ extension Trait where Self == Testing.ConditionTrait {
122122

123123
/// Constructs a condition trait that causes a test to be disabled if not running on the specified host OS.
124124
/// - parameter when: An additional constraint to apply such that the host OS requirement is only applied if this parameter is _also_ true. Defaults to true.
125-
package static func requireHostOS(_ os: OperatingSystem, when condition: Bool = true) -> Self {
126-
enabled("This test requires a \(os) host OS.", { try ProcessInfo.processInfo.hostOperatingSystem() == os && condition })
125+
package static func requireHostOS(_ os: OperatingSystem..., when condition: Bool = true) -> Self {
126+
enabled("This test requires a \(os) host OS.", { os.contains(try ProcessInfo.processInfo.hostOperatingSystem()) && condition })
127127
}
128128

129129
/// Constructs a condition trait that causes a test to be disabled if running on the specified host OS.

Sources/SWBTestSupport/TaskPlanningTestSupport.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ package class TestTaskPlanningDelegate: TaskPlanningDelegate, @unchecked Sendabl
316316
}
317317

318318
package func recordAttachment(contents: SWBUtil.ByteString) -> SWBUtil.Path {
319-
let digester = MD5Context()
319+
let digester = InsecureHashContext()
320320
digester.add(bytes: contents)
321321
if let path = tmpDir?.path.join(digester.signature.asString) {
322322
do {

Sources/SWBUtil/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,4 @@ target_link_libraries(SWBUtil PUBLIC
108108
SWBCSupport
109109
SWBLibc
110110
ArgumentParser
111-
$<$<AND:$<NOT:$<PLATFORM_ID:Windows>>,$<NOT:$<PLATFORM_ID:Darwin>>>:Crypto::Crypto>
112111
$<$<NOT:$<PLATFORM_ID:Darwin>>:SwiftSystem::SystemPackage>)

Sources/SWBUtil/FilesSignature.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fileprivate extension FSProxy {
7171

7272
/// Returns the signature of a list of files.
7373
func filesSignature(_ statInfos: [(Path, stat?)]) -> ByteString {
74-
let md5Context = MD5Context()
74+
let md5Context = InsecureHashContext()
7575
for (path, statInfo) in statInfos {
7676
md5Context.add(string: path.str)
7777
if let statInfo {

0 commit comments

Comments
 (0)