From 90f330732f23835283ea533ceee5b3fd49132c02 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Fri, 16 Feb 2024 06:23:19 +0900 Subject: [PATCH 1/3] fix to avoid `EXC_BAD_ACCESS` when exchanging func implementation --- Sources/SwiftHook/SwiftHook+function.swift | 28 +++++++++++++--------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/Sources/SwiftHook/SwiftHook+function.swift b/Sources/SwiftHook/SwiftHook+function.swift index 98ffe2e..d725f5d 100644 --- a/Sources/SwiftHook/SwiftHook+function.swift +++ b/Sources/SwiftHook/SwiftHook+function.swift @@ -41,7 +41,6 @@ extension SwiftHook { throw SwiftHookError.failedToExchangeFuncImplementation } - public static func hookFunction( _ target: String, _ replacement: String, @@ -130,6 +129,12 @@ extension SwiftHook { return (firstSymbol, secondSymbol) } +} + +extension SwiftHook { + private static var replaced1: UnsafeMutableRawPointer? + private static var replaced2: UnsafeMutableRawPointer? + @discardableResult private static func _exchangeFuncImplementation( _ first: String, @@ -144,14 +149,16 @@ extension SwiftHook { print(firstSymbol, secondSymbol) #endif - var replaced1 = UnsafeMutableRawPointer(bitPattern: -1) - var replaced2 = UnsafeMutableRawPointer(bitPattern: -1) - + // hook first function + replaced1 = nil let f2s: Bool = rebindSymbol( name: first, replacement: secondSymbol, replaced: &replaced1 ) + + // hook second function + replaced2 = nil let s2f: Bool = rebindSymbol( name: second, replacement: firstSymbol, @@ -162,19 +169,18 @@ extension SwiftHook { return false } - guard let replaced1, - Int(bitPattern: replaced1) != -1 else { + guard replaced1 != nil else { throw SwiftHookError.failedToHookFirstFunction } - - guard let replaced2, - Int(bitPattern: replaced2) != -1 else { + guard replaced2 != nil else { throw SwiftHookError.failedToHookSecondFunction } return true } +} +extension SwiftHook { @discardableResult private static func _hookFuncImplementation( _ target: String, @@ -193,7 +199,7 @@ extension SwiftHook { print(stdlib_demangleName(replacement)) #endif - var replaced = UnsafeMutableRawPointer(bitPattern: -1) + var replaced: UnsafeMutableRawPointer? let result: Bool = rebindSymbol( name: target, @@ -209,7 +215,7 @@ extension SwiftHook { } if let original { - var originalReplaced = UnsafeMutableRawPointer(bitPattern: -1) + var originalReplaced: UnsafeMutableRawPointer? let result: Bool = rebindSymbol( name: original, replacement: replaced, From aa1c7a235e0899578da4bdcc8c817fa4886a07a7 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Fri, 16 Feb 2024 06:38:31 +0900 Subject: [PATCH 2/3] fix to avoid EXC_BAD_ACCESS when hooking functions --- Sources/SwiftHook/SwiftHook+function.swift | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Sources/SwiftHook/SwiftHook+function.swift b/Sources/SwiftHook/SwiftHook+function.swift index d725f5d..5eb4142 100644 --- a/Sources/SwiftHook/SwiftHook+function.swift +++ b/Sources/SwiftHook/SwiftHook+function.swift @@ -199,18 +199,17 @@ extension SwiftHook { print(stdlib_demangleName(replacement)) #endif - var replaced: UnsafeMutableRawPointer? + Self.replaced1 = nil let result: Bool = rebindSymbol( name: target, replacement: replacementSymbol, - replaced: &replaced + replaced: &Self.replaced1 ) guard result else { return false } - guard let replaced, - Int(bitPattern: replaced) != -1 else { + guard let replaced = Self.replaced1 else { return false } From 6eaa38390471e49597cb361680262976dbeaa3e9 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Fri, 16 Feb 2024 06:39:16 +0900 Subject: [PATCH 3/3] refactor --- Sources/SwiftHook/SwiftHook+function.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Sources/SwiftHook/SwiftHook+function.swift b/Sources/SwiftHook/SwiftHook+function.swift index 5eb4142..41ddeb4 100644 --- a/Sources/SwiftHook/SwiftHook+function.swift +++ b/Sources/SwiftHook/SwiftHook+function.swift @@ -150,29 +150,29 @@ extension SwiftHook { #endif // hook first function - replaced1 = nil + Self.replaced1 = nil let f2s: Bool = rebindSymbol( name: first, replacement: secondSymbol, - replaced: &replaced1 + replaced: &Self.replaced1 ) // hook second function - replaced2 = nil + Self.replaced2 = nil let s2f: Bool = rebindSymbol( name: second, replacement: firstSymbol, - replaced: &replaced2 + replaced: &Self.replaced2 ) guard f2s && s2f else { return false } - guard replaced1 != nil else { + guard Self.replaced1 != nil else { throw SwiftHookError.failedToHookFirstFunction } - guard replaced2 != nil else { + guard Self.replaced2 != nil else { throw SwiftHookError.failedToHookSecondFunction }