rweichler / shitstrate

demo of why asm is necessary in ObjC hooking

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Shitstrate

This is mostly for my reference, but this demonstrates why assembly is necessary in MobileSubstrate, even with hooking Objective C.

<rpetrich> rweichler: the problems are around calling through to the "original" method implementation

<rpetrich> rweichler: imagine you have classes Foo and Bar, where Bar is a subclass of Foo

<rpetrich> rweichler: Foo has a -baz method, which isn't overridden on Bar

<rpetrich> rweichler: your tweak hooks the -baz method on Bar to add its behaviours, and like a good citizen calls through to the original implementation

<rpetrich> rweichler: later on, my tweak decides it also wants to hook -baz, this time on Foo

<rpetrich> rweichler: will your call to the original method implementation call my -baz hook?

<rpetrich> rweichler: if you aren't careful about how you apply the hook on your end, the answer might be no!

The first thing I thought of was, if you don't have an original method, then you just have a dummy original method that just calls the superclass's.

NSString *generic_original(id self, SEL _cmd)
{
    Class super = class_getSuperclass(self);
    Method m;
    find_method(super, _cmd, &m);
    IMP i = method_getImplementation(m);

    return i(self, _cmd);
}

But the problem with that is the arguments can be anything! What do you do if it's something like this?

-(NSString *)takesArguments(int arg1)
{
    //...
}

Then you'd need to make something like:

NSString *generic_original2(id self, SEL _cmd, int arg1)
{
    ///...
}

And that's obviously not generic at all. You'd somehow need to figure out the # of arguments on the fly. Hence the assembly.


A followup is I wonder if you could use method_getTypeEncoding to somehow create this generic function.

About

demo of why asm is necessary in ObjC hooking


Languages

Language:Objective-C 97.9%Language:Makefile 2.1%