Skip to content

Commit

Permalink
Merge pull request Tencent#17 from biosli/master
Browse files Browse the repository at this point in the history
Add more memory func for QQleakCheck.
  • Loading branch information
rosen0510 authored Feb 8, 2018
2 parents ef85c45 + 42a3476 commit 205cc58
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
28 changes: 21 additions & 7 deletions Demo/OOMDetectorDemo/Demos/DemoViewController0.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,29 @@ @interface DemoViewController0 ()

@end

//realloc need a larger size set to different memory zone.
#define Demo0Code \
dispatch_async(dispatch_get_global_queue(0, 0), ^{\
int i = 0;\
while (i < 50) {\
int size = 100 * 1024;\
char *info = malloc(size);\
memset(info, 1, size);\
++i;\
}\
int i = 0; \
while (i < 50) { \
int size = 1 * 1024; \
char *info = calloc(100, size); \
memset(info, 1, size); \
\
info = valloc(size); \
memset(info, 1, size); \
\
info = malloc(size); \
memset(info, 1, size); \
\
size = 100 * 1024; \
char *info2 = realloc(info, size); \
memset(info2, 1, size); \
\
info = malloc(size); \
memset(info, 1, size); \
++i; \
} \
});

@implementation DemoViewController0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
static bool isPaused;
static void* (*orig_malloc)(size_t);
static void* (*orig_calloc)(size_t, size_t);
static void* (*orig_realloc)(void *, size_t);
static void* (*orig_valloc)(size_t);
static void* (*orig_block_copy)(const void *aBlock);

Expand Down Expand Up @@ -91,19 +92,37 @@ void rebind_symbols_for_imagename(struct rebinding rebindings[],
{
void *ptr = orig_calloc(n,size);
if(!isPaused){
global_leakChecker->recordMallocStack((vm_address_t)ptr, (uint32_t)(n*size),"malloc",2);
global_leakChecker->recordMallocStack((vm_address_t)ptr, (uint32_t)(n*size),"calloc",2);
}
#ifdef __enable_malloc_logger__
malloc_printf("calloc ptr:%p size:%lu thread:%lu\n",ptr, size*n,mach_thread_self());
#endif
return ptr;
}

void *new_realloc(void *old_ptr, size_t size)
{
void *ptr = orig_realloc(old_ptr, size);
if (ptr != old_ptr) {
if(!isPaused){
if (old_ptr) {
global_leakChecker->removeMallocStack((vm_address_t)old_ptr);
}
global_leakChecker->recordMallocStack((vm_address_t)ptr, (uint32_t)(size),"realloc",2);
}
}
#ifdef __enable_malloc_logger__
malloc_printf("realloc newptr: %p ptr:%p size:%lu thread:%lu\n", ptr, old_ptr, size, mach_thread_self());
#endif

return ptr;
}

void *new_valloc(size_t size)
{
void *ptr = orig_valloc(size);
if(!isPaused){
global_leakChecker->recordMallocStack((vm_address_t)ptr, (uint32_t)size,"malloc",2);
global_leakChecker->recordMallocStack((vm_address_t)ptr, (uint32_t)size,"valloc",2);
}
#ifdef __enable_malloc_logger__
malloc_printf("valloc ptr:%p size:%lu thread:%lu\n",ptr, size,mach_thread_self());
Expand All @@ -122,14 +141,43 @@ void rebind_symbols_for_imagename(struct rebinding rebindings[],
return block;
}

void beSureAllRebindingFuncBeenCalled()
{
//Note: https://github.com/facebook/fishhook/issues/43
// The issue still open. Keep watching.
void *info = malloc(1024);
info = realloc(info, 100 * 1024);
free(info);

info = calloc(10, 1024);
free(info);

info = valloc(1024);
free(info);

dispatch_block_t temp = Block_copy(^{});
Block_release(temp);
}

void hookMalloc()
{
if(!isPaused){
beSureAllRebindingFuncBeenCalled();

orig_malloc = malloc;
orig_calloc = calloc;
orig_valloc = valloc;
orig_realloc = realloc;
orig_block_copy = _Block_copy;
rebind_symbols_for_imagename((struct rebinding[4]){{"malloc", (void*)new_malloc, (void **)&orig_malloc},{"valloc",(void*)valloc,(void**)&orig_valloc},{"calloc",(void*)new_calloc,(void**)&orig_calloc},{"_Block_copy",(void*)new_block_copy,(void**)&orig_block_copy}},4,getImagename());
rebind_symbols_for_imagename(
(struct rebinding[5]){
{"realloc",(void*)new_realloc,(void**)&orig_realloc},
{"malloc", (void*)new_malloc, (void **)&orig_malloc},
{"valloc",(void*)new_valloc,(void**)&orig_valloc},
{"calloc",(void*)new_calloc,(void**)&orig_calloc},
{"_Block_copy",(void*)new_block_copy,(void**)&orig_block_copy}},
5,
getImagename());
}
else{
isPaused = false;
Expand Down

0 comments on commit 205cc58

Please sign in to comment.