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

C function Logos directives #106

Merged
merged 18 commits into from
Feb 7, 2016
Merged

C function Logos directives #106

merged 18 commits into from
Feb 7, 2016

Conversation

uroboro
Copy link
Member

@uroboro uroboro commented Aug 6, 2014

Added %hookf and %origf directives for hooking C functions while supporting groups and using a variant of %orig.
%hookf takes a variadic number of arguments: the function return type, the function name, the function argument list. It generates the original function function pointer from these values and the current group.
%hookf takes a variadic number to pass to the original function, which is calculated from saving the current function name in %hookf and the current group.

Added %hookf and %origf directives for hooking C functions while supporting groups and using a variant of %orig.
%hookf takes a variadic number of arguments: the function return type, the function name, the function argument list. It generates the original function function pointer from these values and the current group.
%hookf takes a variadic number to pass to the original function, which is calculated from saving the current function name in %hookf and the current group.
@uroboro
Copy link
Member Author

uroboro commented Aug 6, 2014

Example code:

#import <notify.h>

%hook NSNotificationCenter
- (void)postNotification:(NSNotification *)notification {
        %orig;
}
%end

// notify_*
%hookf(uint32_t, notify_post, const char *name) {
        uint32_t r = %origf(name);
        return r;
}

%group gNSNotificationCenter

%hook NSNotificationCenter
- (void)postNotification:(NSNotification *)notification {
        %orig;
}
%end

// notify_*
%hookf(uint32_t, notify_post, const char *name) {
        uint32_t r = %origf(name);
        return r;
}

%end /* gNSNotificationCenter */


%ctor {
        if (someCondition) {
                %init(gNSNotificationCenter);
        } else {
                %init();
        }
}

my $self = shift;
my $function = shift;
my $return = "";
$return .= "MSHookFunction(".$function->name.", &_logos_function\$".$function->group->name."\$_".$function->name.", &_logos_orig_function\$".$function->group->name."\$_".$function->name.")); ";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is substrate-only, unfortunately. It should be in Logos::Generator::MobileSubstrate. Maybe Logos::Generator::internal::Function should throw an error.

DHowett and others added 2 commits August 13, 2014 22:26
From: Dustin L. Howett <dustin@howett.net>

git-svn-id: http://svn.howett.net/svn/theos/trunk@643 4410221e-0ddf-4ce3-99c0-2db6c0dbc727
(cherry picked from commit 330c790)
bin/lib/Logos/Function.pm
* added addArg function to modify the function arguments array.

bin/lib/Logos/Generator/Base/Function.pm
* added dummy functions from which to override in internal/MobileSubstrate Generators.

bin/lib/Logos/Generator/MobileSubstrate/Function.pm
* added functions from Base::Function: originalFunctionName, newFunctionName, declaration, initializers.
* added originalFunctionCall for %orig directive. Uses new function getArgumentName.
* added helper functions getArgumentName, escapeFunctionPointer and notSoSmartSplit.
* escapeFunctionPointer uses Logos::Method::declarationForTypeWithName as code base to get the function pointer name starting offset to extract the argument name for %orig directive with original arguments.* notSoSmartSplit splits a string given a delimiter string.

bin/lib/Logos/Group.pm
* understood how arguments are received by perl functions and now addFunction parses them correctly, adding the function arguments using the new Function::addArg function.

bin/logos.pl
* added %orig overload allowing function hooks to call the original function using the same directive that methods use.
* removed unnceessary %origf directive now obsolete due to %orig overload.
* added &%orig overload allowing function hooks to get the function pointer to the original function using the same directive that methods use.
* updated %hookf to use Logos::Patch logic so it supports grouping.
* removed unnecessary helper functions mshookParse, mshookGetFunctionRetval, mshookGetFunctionName, mshookGetFunctionArgs.
@uroboro
Copy link
Member Author

uroboro commented Aug 14, 2014

New example code to test newer additions:

%config(generator=MobileSubstrate);
//%config(generator=internal); //uncomment to break everything

%group _regrouped
%hookf(int, cool, int n, int(*(*fp)(int x))(int (*y)(int m)), const char *name, int sizes[10], struct {int x;} (*fp1)(struct{int y;} w), int(*fp2[10])(void)) {
        otherfp = &%orig;
        return %orig(0, fp, "int(*(*fp)(int x))(int (*y)(int m))", NULL, NULL);
}
%end
%hookf(int, cool, int n, int(*(*fp)(int x))(int (*y)(int m)), const char *name, int sizes[10], struct {int x;} (*fp1)(struct{int y;} w), int(*fp2[10])(void)) {
        return %orig();
}

%ctor {
        if (someCondition) {
                %init(_regrouped);
        } else {
                %init();
        }
}

@tyilo
Copy link

tyilo commented Jan 2, 2015

Maybe add support for other functions than MSHookFunction? (eg. rd_route, mach_override)

Both of the functions they provide takes the same arguments in the same order as MSHookFunction, so you could just use #define MSHookFunction rd_route.
Maybe add a config option instead: %config(hookf=rd_route)?

@ryanb93
Copy link

ryanb93 commented May 12, 2015

Would be great to see this merged soon.

Also changed spaces to tabs.
@ahyattdev
Copy link

Why has this not been merged? Is there another way to hook C functions or something?

@youmingtaiziback
Copy link

Cool feature

It's `_theos_TRUE` but doesn't support "true"? ლ(ಠ益ಠლ)
While Generator::internal still does not support %hookf, it will not break logos anymore; instead it will print a C comment in the initializer. This change will allow using other function hooking mechanisms other than CydiaSubstrate's MSHookFunction.
@kirb kirb merged commit 5d2f3c7 into theos:master Feb 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants