Skip to content

Simplify cross-platform hashing implementations #388

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

Merged
merged 1 commit into from
Apr 9, 2025
Merged
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
3 changes: 0 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ let package = Package(
"SWBCSupport",
"SWBLibc",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Crypto", package: "swift-crypto", condition: .when(platforms: [.linux, .openbsd, .android])),
.product(name: "SystemPackage", package: "swift-system", condition: .when(platforms: [.linux, .android, .windows])),
],
exclude: ["CMakeLists.txt"],
Expand Down Expand Up @@ -427,7 +426,6 @@ for target in package.targets {
// `SWIFTCI_USE_LOCAL_DEPS` configures if dependencies are locally available to build
if useLocalDependencies {
package.dependencies += [
.package(path: "../swift-crypto"),
.package(path: "../swift-driver"),
.package(path: "../swift-system"),
.package(path: "../swift-argument-parser"),
Expand All @@ -437,7 +435,6 @@ if useLocalDependencies {
}
} else {
package.dependencies += [
.package(url: "https://github.com/apple/swift-crypto.git", "2.0.0"..<"4.0.0"),
.package(url: "https://github.com/swiftlang/swift-driver.git", branch: "main"),
.package(url: "https://github.com/apple/swift-system.git", .upToNextMajor(from: "1.4.1")),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.0.3"),
Expand Down
22 changes: 1 addition & 21 deletions Plugins/cmake-smoke-test/cmake-smoke-test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ struct CMakeSmokeTest: CommandPlugin {
let swiftSystemURL = try findSiblingRepository("swift-system", swiftBuildURL: swiftBuildURL)
let swiftSystemBuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-system")

let swiftAsn1URL = try findSiblingRepository("swift-asn1", swiftBuildURL: swiftBuildURL)
let swiftAsn1BuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-asn1")

let swiftCryptoURL = try findSiblingRepository("swift-crypto", swiftBuildURL: swiftBuildURL)
let swiftCryptoBuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-crypto")

let llbuildURL = try findSiblingRepository("llbuild", swiftBuildURL: swiftBuildURL)
let llbuildBuildURL = context.pluginWorkDirectoryURL.appending(component: "llbuild")

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

for url in [swiftToolsSupportCoreBuildURL, swiftAsn1BuildURL, swiftCryptoBuildURL, swiftSystemBuildURL, llbuildBuildURL, swiftArgumentParserBuildURL, swiftDriverBuildURL, swiftBuildBuildURL] {
for url in [swiftToolsSupportCoreBuildURL, swiftSystemBuildURL, llbuildBuildURL, swiftArgumentParserBuildURL, swiftDriverBuildURL, swiftBuildBuildURL] {
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
}

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

if hostOS != .macOS && hostOS != .windows {
print("Building swift-asn1")
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftAsn1URL.path()], workingDirectory: swiftAsn1BuildURL)
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftAsn1BuildURL)
print("Built swift-asn1")

print("Building swift-crypto")
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftCryptoURL.path()], workingDirectory: swiftCryptoBuildURL)
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftCryptoBuildURL)
print("Built swift-crypto")
}

if hostOS != .macOS {
print("Building swift-system")
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftSystemURL.path()], workingDirectory: swiftSystemBuildURL)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SWBBuildSystem/BuildOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,7 @@ internal final class OperationSystemAdaptor: SWBLLBuild.BuildSystemDelegate, Act
return
}

let signatureCtx = MD5Context()
let signatureCtx = InsecureHashContext()
signatureCtx.add(string: "CleanupCompileCache")
signatureCtx.add(string: cachePath.str)
let signature = signatureCtx.signature
Expand Down
2 changes: 1 addition & 1 deletion Sources/SWBCore/EnvironmentBindings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public struct EnvironmentBindings: Sendable {
}

/// Add a signature of the bindings into the given context.
public func computeSignature(into ctx: MD5Context) {
public func computeSignature(into ctx: InsecureHashContext) {
for (variable, val) in bindings {
// The signature computation should record the variable name and value data, and the positions which divide them.
ctx.add(string: variable)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SWBCore/LibSwiftDriver/PlannedBuild.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public struct SwiftDriverJob: Serializable, CustomDebugStringConvertible {
self.cacheKeys = job.outputCacheKeys.reduce(into: [String]()) { result, key in
result.append(key.value)
}.sorted()
let md5 = MD5Context()
let md5 = InsecureHashContext()
for arg in commandLine {
md5.add(bytes: arg)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/SWBCore/PlannedTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public struct TaskIdentifier: Comparable, Hashable, Serializable, CustomStringCo
}

public var sandboxProfileSentinel: String {
let taskIdentifierChecksumContext = MD5Context()
let taskIdentifierChecksumContext = InsecureHashContext()
taskIdentifierChecksumContext.add(string: self.rawValue)
return taskIdentifierChecksumContext.signature.asString
}
Expand All @@ -45,7 +45,7 @@ extension TaskIdentifier {
}

public init(forTarget: ConfiguredTarget?, dynamicTaskPayload: ByteString, priority: TaskPriority) {
let ctx = MD5Context()
let ctx = InsecureHashContext()
ctx.add(bytes: dynamicTaskPayload)
self.rawValue = "P\(priority.rawValue):\(forTarget?.guid.stringValue ?? ""):\(forTarget?.parameters.configuration ?? ""):\(ctx.signature.asString)"
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SWBCore/ProjectModel/BuildPhase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public class BuildPhaseWithBuildFiles: BuildPhase, @unchecked Sendable

/// 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.
public static func filenameUniquefierSuffixFor(path: Path) -> String {
let digester = MD5Context.init()
let digester = InsecureHashContext.init()
digester.add(string: path.normalize().str)
return digester.signature.asString
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/SWBCore/SpecImplementations/Tools/CCompiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ public class ClangCompilerSpec : CompilerSpec, SpecIdentifierType, GCCCompatible
previousArg = argAsByteString
}

let ctx = MD5Context()
let ctx = InsecureHashContext()
ctx.add(string: inputFileType.identifier)
ctx.add(string: self.identifier)

Expand Down Expand Up @@ -1412,7 +1412,7 @@ public class ClangCompilerSpec : CompilerSpec, SpecIdentifierType, GCCCompatible
return nil
}

let md5 = MD5Context()
let md5 = InsecureHashContext()
md5.add(string: prefixHeader.str)
let sharingIdentHashValue = md5.signature
let baseCachePath = scope.evaluate(BuiltinMacros.SHARED_PRECOMPS_DIR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ public final class SwiftCommandOutputParser: TaskOutputParser {
ruleInfo = "\(ruleInfo) \(path.str.quotedDescription)"
}
let signature: ByteString = {
let md5 = MD5Context()
let md5 = InsecureHashContext()
md5.add(string: ruleInfo)
return md5.signature
}()
Expand Down Expand Up @@ -3584,7 +3584,7 @@ extension SwiftCompilerSpec {
static public func computeRuleInfoAndSignatureForPerFileVirtualBatchSubtask(variant: String, arch: String, path: Path) -> ([String], ByteString) {
let ruleInfo = ["SwiftCompile", variant, arch, path.str.quotedDescription]
let signature: ByteString = {
let md5 = MD5Context()
let md5 = InsecureHashContext()
md5.add(string: ruleInfo.joined(separator: " "))
return md5.signature
}()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class CustomTaskProducer: PhasedTaskProducer, TaskProducer {

if outputs.isEmpty {
// If there are no outputs, create a virtual output that can be wired up to gates
let md5Context = MD5Context()
let md5Context = InsecureHashContext()
for arg in commandLine {
md5Context.add(string: arg)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SWBTaskExecution/BuildDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ package final class BuildDescriptionBuilder {
}

package static func computeShellToolSignature(args: [ByteString], environment: EnvironmentBindings?, dependencyData: DependencyDataStyle?, isUnsafeToInterrupt: Bool, additionalSignatureData: String) -> ByteString {
let ctx = MD5Context()
let ctx = InsecureHashContext()
for arg in args {
ctx.add(bytes: arg)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SWBTaskExecution/BuildDescriptionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ private final class BuildSystemTaskPlanningDelegate: TaskPlanningDelegate {
}

package func recordAttachment(contents: SWBUtil.ByteString) -> SWBUtil.Path {
let digester = MD5Context()
let digester = InsecureHashContext()
digester.add(bytes: contents)
let path = descriptionPath.join("attachments").join(digester.signature.asString)
do {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SWBTaskExecution/BuildDescriptionSignature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ extension BuildDescriptionSignatureComponents {
}

func signatureStringValue(humanReadableString: ByteString) -> BuildDescriptionSignature {
let hashContext = MD5Context()
let hashContext = InsecureHashContext()
hashContext.add(bytes: humanReadableString)
return hashContext.signature
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ package final class CompilationCachingDataPruner: Sendable {
startedAction()
let serializer = MsgPackSerializer()
key.serialize(to: serializer)
let signatureCtx = MD5Context()
let signatureCtx = InsecureHashContext()
signatureCtx.add(string: "ClangCachingPruneData")
signatureCtx.add(bytes: serializer.byteString)
let signature = signatureCtx.signature
Expand Down Expand Up @@ -159,7 +159,7 @@ package final class CompilationCachingDataPruner: Sendable {
startedAction()
let serializer = MsgPackSerializer()
key.serialize(to: serializer)
let signatureCtx = MD5Context()
let signatureCtx = InsecureHashContext()
signatureCtx.add(string: "ClangCachingPruneData")
signatureCtx.add(bytes: serializer.byteString)
let signature = signatureCtx.signature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ package final class CompilationCachingUploader {
}

startedUpload()
let signatureCtx = MD5Context()
let signatureCtx = InsecureHashContext()
signatureCtx.add(string: "ClangCachingUpload")
signatureCtx.add(string: cacheKey)
let signature = signatureCtx.signature
Expand Down Expand Up @@ -122,7 +122,7 @@ package final class CompilationCachingUploader {
}

startedUpload()
let signatureCtx = MD5Context()
let signatureCtx = InsecureHashContext()
signatureCtx.add(string: "SwiftCachingUpload")
signatureCtx.add(string: cacheKey)
let signature = signatureCtx.signature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public final class AuxiliaryFileTaskAction: TaskAction {
serializer.serialize(context.logContents)
super.serialize(to: serializer)
}
let md5 = MD5Context()
let md5 = InsecureHashContext()
md5.add(bytes: serializer.byteString)
return md5.signature
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ open class SwiftDriverJobSchedulingTaskAction: TaskAction {
return
}

let signatureCtx = MD5Context()
let signatureCtx = InsecureHashContext()
signatureCtx.add(string: task.identifier.rawValue)
signatureCtx.add(string: "swiftdriverjobdiscoveryactivity")
signatureCtx.add(number: dependencyID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public final class SwiftDriverJobTaskAction: TaskAction, BuildValueValidatingTas
private var state = State()

public override func getSignature(_ task: any ExecutableTask, executionDelegate: any TaskExecutionDelegate) -> ByteString {
let md5 = MD5Context()
let md5 = InsecureHashContext()
// We intentionally do not integrate the superclass signature here, because the driver job's signature captures the same information without requiring expensive serialization.
md5.add(bytes: driverJob.driverJob.signature)
task.environment.computeSignature(into: md5)
Expand Down
4 changes: 2 additions & 2 deletions Sources/SWBTaskExecution/TaskActions/TaskAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ open class TaskAction: PlannedTaskAction, PolymorphicSerializable
// 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.
let sz = MsgPackSerializer()
serialize(to: sz)
let md5 = MD5Context()
let md5 = InsecureHashContext()
md5.add(bytes: sz.byteString)
return md5.signature
}
Expand All @@ -67,7 +67,7 @@ open class TaskAction: PlannedTaskAction, PolymorphicSerializable
/// This is checked to determine if the command needs to rebuild versus the last time it was run.
open func getSignature(_ task: any ExecutableTask, executionDelegate: any TaskExecutionDelegate) -> ByteString
{
let md5 = MD5Context()
let md5 = InsecureHashContext()
md5.add(bytes: serializedRepresentationSignature!)
for arg in task.commandLine {
md5.add(bytes: arg.asByteString)
Expand Down
4 changes: 2 additions & 2 deletions Sources/SWBTestSupport/SkippedTestSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ extension Trait where Self == Testing.ConditionTrait {

/// Constructs a condition trait that causes a test to be disabled if not running on the specified host OS.
/// - 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.
package static func requireHostOS(_ os: OperatingSystem, when condition: Bool = true) -> Self {
enabled("This test requires a \(os) host OS.", { try ProcessInfo.processInfo.hostOperatingSystem() == os && condition })
package static func requireHostOS(_ os: OperatingSystem..., when condition: Bool = true) -> Self {
enabled("This test requires a \(os) host OS.", { os.contains(try ProcessInfo.processInfo.hostOperatingSystem()) && condition })
}

/// Constructs a condition trait that causes a test to be disabled if running on the specified host OS.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SWBTestSupport/TaskPlanningTestSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ package class TestTaskPlanningDelegate: TaskPlanningDelegate, @unchecked Sendabl
}

package func recordAttachment(contents: SWBUtil.ByteString) -> SWBUtil.Path {
let digester = MD5Context()
let digester = InsecureHashContext()
digester.add(bytes: contents)
if let path = tmpDir?.path.join(digester.signature.asString) {
do {
Expand Down
1 change: 0 additions & 1 deletion Sources/SWBUtil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,4 @@ target_link_libraries(SWBUtil PUBLIC
SWBCSupport
SWBLibc
ArgumentParser
$<$<AND:$<NOT:$<PLATFORM_ID:Windows>>,$<NOT:$<PLATFORM_ID:Darwin>>>:Crypto::Crypto>
$<$<NOT:$<PLATFORM_ID:Darwin>>:SwiftSystem::SystemPackage>)
2 changes: 1 addition & 1 deletion Sources/SWBUtil/FilesSignature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fileprivate extension FSProxy {

/// Returns the signature of a list of files.
func filesSignature(_ statInfos: [(Path, stat?)]) -> ByteString {
let md5Context = MD5Context()
let md5Context = InsecureHashContext()
for (path, statInfo) in statInfos {
md5Context.add(string: path.str)
if let statInfo {
Expand Down
Loading