From 01234a6f63b1126a8f40f0bf9eded228cf35b113 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Tue, 1 Oct 2024 20:57:33 +0900 Subject: [PATCH 1/4] Attach `(at)_implementationOnly` to `Crypto` import --- .../Model/Codesign/CodeDirectory/CodeSignCodeDirectory.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/MachOKit/Model/Codesign/CodeDirectory/CodeSignCodeDirectory.swift b/Sources/MachOKit/Model/Codesign/CodeDirectory/CodeSignCodeDirectory.swift index fd6f7eb..0db512b 100644 --- a/Sources/MachOKit/Model/Codesign/CodeDirectory/CodeSignCodeDirectory.swift +++ b/Sources/MachOKit/Model/Codesign/CodeDirectory/CodeSignCodeDirectory.swift @@ -8,7 +8,7 @@ import Foundation import MachOKitC -import Crypto +@_implementationOnly import Crypto public struct CodeSignCodeDirectory: LayoutWrapper { public typealias Layout = CS_CodeDirectory From f7d116654d40dd79f673664f86522f39ecebef86 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Tue, 1 Oct 2024 21:05:20 +0900 Subject: [PATCH 2/4] Add `MachOKitError` --- Sources/MachOKit/DyldCache.swift | 11 ++++++++--- Sources/MachOKit/MachOKit.swift | 12 +++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Sources/MachOKit/DyldCache.swift b/Sources/MachOKit/DyldCache.swift index 24437c2..c0ccb8c 100644 --- a/Sources/MachOKit/DyldCache.swift +++ b/Sources/MachOKit/DyldCache.swift @@ -50,9 +50,14 @@ public class DyldCache { ) // check magic of header - // FIXME: error instead of unwrap - let cpuType = header._cpuType! - let cpuSubType = header._cpuSubType! + guard header.magic.starts(with: "dyld_") else { + throw MachOKitError.invalidMagic + } + + guard let cpuType = header._cpuType, + let cpuSubType = header._cpuSubType else { + throw MachOKitError.invalidCpuType + } self.cpu = .init( typeRawValue: cpuType.rawValue, subtypeRawValue: cpuSubType.rawValue diff --git a/Sources/MachOKit/MachOKit.swift b/Sources/MachOKit/MachOKit.swift index 8eecf43..2865aee 100644 --- a/Sources/MachOKit/MachOKit.swift +++ b/Sources/MachOKit/MachOKit.swift @@ -5,12 +5,18 @@ public enum File { case fat(FatFile) } +public enum MachOKitError: LocalizedError { + case invalidMagic + case invalidCpuType +} + public func loadFromFile(url: URL) throws -> File { let fileHandle = try FileHandle(forReadingFrom: url) let magicRaw: UInt32 = fileHandle.read(offset: 0) - - // FIXME: error instead of unwrap - let magic = Magic(rawValue: magicRaw)! + + guard let magic = Magic(rawValue: magicRaw) else { + throw MachOKitError.invalidMagic + } if magic.isFat { return .fat(try FatFile(url: url)) From c07943751068f23a1918de775781376bd18f5046 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Tue, 1 Oct 2024 21:11:06 +0900 Subject: [PATCH 3/4] Fixe for APIs only available in the Darwin env --- Sources/MachOKit/Header/CPU.swift | 2 + Sources/MachOKit/Header/CPUSubType.swift | 6 +-- Sources/MachOKit/Header/CPUType.swift | 66 +++++++++++------------ Sources/MachOKit/MachOImage+static.swift | 7 +-- Tests/MachOKitTests/MachOPrintTests.swift | 4 -- 5 files changed, 38 insertions(+), 47 deletions(-) diff --git a/Sources/MachOKit/Header/CPU.swift b/Sources/MachOKit/Header/CPU.swift index 6d735fe..e4629ce 100644 --- a/Sources/MachOKit/Header/CPU.swift +++ b/Sources/MachOKit/Header/CPU.swift @@ -44,6 +44,7 @@ extension CPU { } } +#if canImport(Darwin) extension CPU { /// CPU type and subtype of host pc static var current: CPU? { @@ -57,3 +58,4 @@ extension CPU { ) } } +#endif diff --git a/Sources/MachOKit/Header/CPUSubType.swift b/Sources/MachOKit/Header/CPUSubType.swift index cda272e..4f8c48b 100644 --- a/Sources/MachOKit/Header/CPUSubType.swift +++ b/Sources/MachOKit/Header/CPUSubType.swift @@ -1051,10 +1051,10 @@ extension CPUARM64_32SubType: CustomStringConvertible { } } +#if canImport(Darwin) extension CPUSubType { /// CPU subtype of host pc static var current: CPUSubType? { -#if canImport(Darwin) guard let cpuType: CPUType = .current else { return nil } @@ -1063,11 +1063,9 @@ extension CPUSubType { let ret = sysctlbyname("hw.cpusubtype", &subtype, &size, nil, 0) guard ret != -1 else { return nil } return .init(rawValue: subtype, of: cpuType) -#else - return nil -#endif } } +#endif /* I386 series declarations cannot be used from swift diff --git a/Sources/MachOKit/Header/CPUType.swift b/Sources/MachOKit/Header/CPUType.swift index dd5046c..bbef775 100644 --- a/Sources/MachOKit/Header/CPUType.swift +++ b/Sources/MachOKit/Header/CPUType.swift @@ -49,21 +49,21 @@ extension CPUType: RawRepresentable { public init?(rawValue: cpu_type_t) { switch rawValue { case RawValue(CPU_TYPE_ANY): self = .any - case CPU_TYPE_VAX: self = .vax - case CPU_TYPE_MC680x0: self = .mc680x0 - case CPU_TYPE_X86: self = .x86 - case CPU_TYPE_I386: self = .i386 - case CPU_TYPE_X86_64: self = .x86_64 - case CPU_TYPE_MC98000: self = .mc98000 - case CPU_TYPE_HPPA: self = .hppa - case CPU_TYPE_ARM: self = .arm - case CPU_TYPE_ARM64: self = .arm64 - case CPU_TYPE_ARM64_32: self = .arm64_32 - case CPU_TYPE_MC88000: self = .mc88000 - case CPU_TYPE_SPARC: self = .sparc - case CPU_TYPE_I860: self = .i860 - case CPU_TYPE_POWERPC: self = .powerpc - case CPU_TYPE_POWERPC64: self = .powerpc64 + case RawValue(CPU_TYPE_VAX): self = .vax + case RawValue(CPU_TYPE_MC680x0): self = .mc680x0 + case RawValue(CPU_TYPE_X86): self = .x86 + case RawValue(CPU_TYPE_I386): self = .i386 + case RawValue(CPU_TYPE_X86_64): self = .x86_64 + case RawValue(CPU_TYPE_MC98000): self = .mc98000 + case RawValue(CPU_TYPE_HPPA): self = .hppa + case RawValue(CPU_TYPE_ARM): self = .arm + case RawValue(CPU_TYPE_ARM64): self = .arm64 + case RawValue(CPU_TYPE_ARM64_32): self = .arm64_32 + case RawValue(CPU_TYPE_MC88000): self = .mc88000 + case RawValue(CPU_TYPE_SPARC): self = .sparc + case RawValue(CPU_TYPE_I860): self = .i860 + case RawValue(CPU_TYPE_POWERPC): self = .powerpc + case RawValue(CPU_TYPE_POWERPC64): self = .powerpc64 default: return nil } @@ -72,21 +72,21 @@ extension CPUType: RawRepresentable { public var rawValue: cpu_type_t { switch self { case .any: RawValue(CPU_TYPE_ANY) - case .vax: CPU_TYPE_VAX - case .mc680x0: CPU_TYPE_MC680x0 - case .x86: CPU_TYPE_X86 - case .i386: CPU_TYPE_I386 - case .x86_64: CPU_TYPE_X86_64 - case .mc98000: CPU_TYPE_MC98000 - case .hppa: CPU_TYPE_HPPA - case .arm: CPU_TYPE_ARM - case .arm64: CPU_TYPE_ARM64 - case .arm64_32: CPU_TYPE_ARM64_32 - case .mc88000: CPU_TYPE_MC88000 - case .sparc: CPU_TYPE_SPARC - case .i860: CPU_TYPE_I860 - case .powerpc: CPU_TYPE_POWERPC - case .powerpc64: CPU_TYPE_POWERPC64 + case .vax: RawValue(CPU_TYPE_VAX) + case .mc680x0: RawValue(CPU_TYPE_MC680x0) + case .x86: RawValue(CPU_TYPE_X86) + case .i386: RawValue(CPU_TYPE_I386) + case .x86_64: RawValue(CPU_TYPE_X86_64) + case .mc98000: RawValue(CPU_TYPE_MC98000) + case .hppa: RawValue(CPU_TYPE_HPPA) + case .arm: RawValue(CPU_TYPE_ARM) + case .arm64: RawValue(CPU_TYPE_ARM64) + case .arm64_32: RawValue(CPU_TYPE_ARM64_32) + case .mc88000: RawValue(CPU_TYPE_MC88000) + case .sparc: RawValue(CPU_TYPE_SPARC) + case .i860: RawValue(CPU_TYPE_I860) + case .powerpc: RawValue(CPU_TYPE_POWERPC) + case .powerpc64: RawValue(CPU_TYPE_POWERPC64) } } } @@ -124,10 +124,10 @@ extension CPUType { } } +#if canImport(Darwin) extension CPUType { /// CPU type of host pc static var current: CPUType? { -#if canImport(Darwin) var type: cpu_type_t = 0 var size = MemoryLayout.size let ret = sysctlbyname("hw.cputype", &type, &size, nil, 0) @@ -141,8 +141,6 @@ extension CPUType { } return .init(rawValue: type) -#else - return nil -#endif } } +#endif diff --git a/Sources/MachOKit/MachOImage+static.swift b/Sources/MachOKit/MachOImage+static.swift index 53a16df..714a7d5 100644 --- a/Sources/MachOKit/MachOImage+static.swift +++ b/Sources/MachOKit/MachOImage+static.swift @@ -8,11 +8,10 @@ import Foundation - +#if canImport(Darwin) extension MachOImage { /// Sequence of loaded machO images. public static var images: AnySequence { - #if canImport(Darwin) AnySequence( (0..<_dyld_image_count()) .lazy @@ -21,9 +20,6 @@ extension MachOImage { MachOImage(ptr: $0) } ) - #else - AnySequence([]) - #endif } } @@ -148,3 +144,4 @@ fileprivate extension MachOImage { return addressRange.contains(address) } } +#endif diff --git a/Tests/MachOKitTests/MachOPrintTests.swift b/Tests/MachOKitTests/MachOPrintTests.swift index 9fe4787..057908c 100644 --- a/Tests/MachOKitTests/MachOPrintTests.swift +++ b/Tests/MachOKitTests/MachOPrintTests.swift @@ -406,7 +406,6 @@ extension MachOPrintTests { } } -#if canImport(Darwin) extension MachOPrintTests { func testClosestSymbol() { for symbol in machO.symbols.shuffled().prefix(100) { @@ -445,7 +444,6 @@ extension MachOPrintTests { } } } -#endif extension MachOPrintTests { func testFindSymbolByName() { @@ -473,7 +471,6 @@ extension MachOPrintTests { } } -#if canImport(Darwin) extension MachOPrintTests { func testFunctionStarts() { guard let functionStarts = machO.functionStarts else { return } @@ -500,7 +497,6 @@ extension MachOPrintTests { } } } -#endif extension MachOPrintTests { func testDataInCode() { From ec0ed0d36ff9b218d2d02c7cf3980b237e5aff2f Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Tue, 1 Oct 2024 21:30:15 +0900 Subject: [PATCH 4/4] Use access level import in the future --- .../Model/Codesign/CodeDirectory/CodeSignCodeDirectory.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/MachOKit/Model/Codesign/CodeDirectory/CodeSignCodeDirectory.swift b/Sources/MachOKit/Model/Codesign/CodeDirectory/CodeSignCodeDirectory.swift index 0db512b..7f2e7fa 100644 --- a/Sources/MachOKit/Model/Codesign/CodeDirectory/CodeSignCodeDirectory.swift +++ b/Sources/MachOKit/Model/Codesign/CodeDirectory/CodeSignCodeDirectory.swift @@ -8,7 +8,11 @@ import Foundation import MachOKitC +//#if compiler(>=5.10) +//private import Crypto +//#else @_implementationOnly import Crypto +//#endif public struct CodeSignCodeDirectory: LayoutWrapper { public typealias Layout = CS_CodeDirectory