facebook / fishhook

A library that enables dynamically rebinding symbols in Mach-O binaries running on iOS.
BSD 3-Clause "New" or "Revised" License
5.17k stars 965 forks source link

hook malloc_zone_malloc EXC_BAD_ACCESS (code=2, address=0x10ecb50cd) #63

Open wMellon opened 5 years ago

wMellon commented 5 years ago

I try to hook malloc_zone_malloc, but I get crash.Why?

void my_malloc_zone_malloc(malloc_zone_t zone, size_t size){ printf("Calling real malloc( %zu)\n", size); return malloc_zone_malloc(zone, size); }

void my_malloc_zone_free(malloc_zone_t zone, void ptr){ printf("Calling real free( %zu)\n",malloc_size(ptr));

return malloc_zone_free(zone, ptr);

}

rebind_symbols((struct rebinding[2]){{"malloc_zone_malloc", my_malloc_zone_malloc,(void)&malloc_zone_malloc}, {"malloc_zone_free", my_malloc_zone_free,(void)&malloc_zone_free}}, 2);

PotatoMapper commented 5 years ago

I try to avoid contributing responses to poorly defined or not an issue with the project itself.

But it does not appear you ever declared a "Holder" pointer function where the original implementation of _malloc_zonemalloc OR _malloc_zonefree could be invoked from.

If you reference the homepage of the repo it clearly outlines a very simple and clear example of the proper way to swap the implementations.

I very quickly typed up what a single hook for malloc_zone_malloc should look like. Beware copy and pasting this as I typed it up on the fly and am EXTREMELY prone to overlooking typos.

#import "fishhook.h"
#import <Wherever_Malloc_structs_are_defined>

static void * (*orig_malloc_zone_malloc)(malloc_zone_t, size_t);

void * my_malloc_zone_malloc(malloc_zone_t *zone, size_t size) {
    // Do Stuff here
    // Log whatever you wanna play with or what have you
    orig_malloc_zone_malloc(zone,size);
}

rebind_symbols((struct rebinding[1])
    {{"malloc_zone_malloc", my_malloc_zone_malloc, (void *)&orig_malloc_zone_malloc}},1);

Note the static definition of the ptr func for the original implementation you do not have.

Try this and see if you still run into issues. Either way this is not an issue with fishhook itself at first glance, and should really be closed until you get to an error that is indicative of a failure on the project itself.