Skip to content
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

Find a workaround for protecting selectors from being called from sources other than specific instances #39

Open
vittoriom opened this issue Jan 7, 2014 · 5 comments
Assignees
Labels
Milestone

Comments

@vittoriom
Copy link
Owner

Check if there is any way to get the address of the caller of a method.
The debugger already does that, jumping on the previous frame of the stacktrace.

@ghost ghost assigned vittoriom Jan 7, 2014
@vittoriom
Copy link
Owner Author

@vittoriom
Copy link
Owner Author

Code snippet is:

Caller Inspection
The compiler builtin __builtin_return_address will give you the address of the code that called you:

    void *addr = __builtin_return_address(0);

From that, we can get information about the caller, including its name:

    Dl_info info;
    dladdr(addr, &info);
    NSString *callerName = [NSString stringWithUTF8String: info.dli_sname];

With this, we can do some seriously nefarious stuff, like behaving completely differently depending on what called a certain method:

    @interface CallerInspection : NSObject @end
    @implementation CallerInspection

    - (void)method
    {
        void *addr = __builtin_return_address(0);
        Dl_info info;
        dladdr(addr, &info);
        NSString *callerName = [NSString stringWithUTF8String: info.dli_sname];
        if([callerName isEqualToString: @"__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__"])
            NSLog(@"Do some notification stuff");
        else
            NSLog(@"Do some regular stuff");
    }

    @end

@vittoriom
Copy link
Owner Author

Useful information at http://linux.die.net/man/3/dlopen

Public Attributes of the struct:

const char *    dli_fname
void *  dli_fbase
const char *    dli_sname
void *  dli_saddr
int     dli_version 
int     dli_reserved1
long    dli_reserved [4]

@stanislaw
Copy link

@vittoriom, it is nice to follow these your explorations! 👍

@vittoriom
Copy link
Owner Author

@stanislaw I'm glad you're interested in this kind of things! If you know something more about inspecting the caller, help is welcome! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants