Skip to content

Fix crash due to IKTAddSuperImplementationToClass being stripped #30

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 6 additions & 10 deletions Sources/InterposeKit/InterposeSubclass.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import SuperBuilder

class InterposeSubclass {

Expand Down Expand Up @@ -90,19 +91,14 @@ class InterposeSubclass {
NSClassFromString("SuperBuilder")?.value(forKey: "isSupportedArchitecure") as? Bool ?? false
}

private lazy var addSuperImpl: @convention(c) (AnyClass, Selector, NSErrorPointer) -> Bool = {
let handle = dlopen(nil, RTLD_LAZY)
let imp = dlsym(handle, "IKTAddSuperImplementationToClass")
return unsafeBitCast(imp, to: (@convention(c) (AnyClass, Selector, NSErrorPointer) -> Bool).self)
}()

func addSuperTrampoline(selector: Selector) {
var error: NSError?
if addSuperImpl(dynamicClass, selector, &error) == false {
Interpose.log("Failed to add super implementation to -[\(dynamicClass).\(selector)]: \(error!)")
} else {
do {
try SuperBuilder.addSuperInstanceMethod(to: dynamicClass, selector: selector)

let imp = class_getMethodImplementation(dynamicClass, selector)!
Interpose.log("Added super for -[\(dynamicClass).\(selector)]: \(imp)")
} catch {
Interpose.log("Failed to add super implementation to -[\(dynamicClass).\(selector)]: \(error)")
}
}
#else
Expand Down
6 changes: 0 additions & 6 deletions Sources/SuperBuilder/src/ITKSuperBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ static IMP ITKGetTrampolineForTypeEncoding(__unused const char *typeEncoding) {
return requiresStructDispatch ? (IMP)msgSendSuperStretTrampoline : (IMP)msgSendSuperTrampoline;
}

// Helper for binding with Swift
BOOL IKTAddSuperImplementationToClass(Class originalClass, SEL selector, NSError **error);
BOOL IKTAddSuperImplementationToClass(Class originalClass, SEL selector, NSError **error) {
return [SuperBuilder addSuperInstanceMethodToClass:originalClass selector:selector error:error];
}

#define ERROR_AND_RETURN(CODE, STRING)\
if (error) { *error = [NSError errorWithDomain:SuperBuilderErrorDomain code:CODE userInfo:@{NSLocalizedDescriptionKey: STRING}];} return NO;

Expand Down