Skip to content

Instantly share code, notes, and snippets.

@pronvit
Created December 2, 2017 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pronvit/25f5e365655f5e398a6c1df42915196b to your computer and use it in GitHub Desktop.
Save pronvit/25f5e365655f5e398a6c1df42915196b to your computer and use it in GitHub Desktop.
typedef void* (*MALLOC)(size_t);
MALLOC old_malloc;
typedef void (*FREE)(intptr_t);
FREE old_free;
void* new_malloc(size_t s)
{
void *addr = old_malloc(s+32);
*(size_t*)((char*)addr+16) = s;
*(uint32_t*)((char*)addr+16+8) = 0x11223344;
void *stack[1]={0};
CaptureStackBackTrace(2,1,stack,NULL);
*(intptr_t*)((char*)addr+0) = (intptr_t)stack[0] - Core::getInstance().vinfo->getRebaseDelta();
return (char*)addr+32;
}
void new_free(intptr_t p)
{
if (!p)
return;
if (*(uint32_t*)((char*)p-8) == 0x11223344)
p -= 32;
old_free(p);
}
void patch_malloc()
{
// 43.05 64bit
// intptr_t faddr = 0x140c49750 + Core::getInstance().vinfo->getRebaseDelta();
// intptr_t maddr = 0x140c49758 + Core::getInstance().vinfo->getRebaseDelta();
// 44.02 64bit
// intptr_t faddr = 0x140cd9750 + Core::getInstance().vinfo->getRebaseDelta();
// intptr_t maddr = 0x140cd9758 + Core::getInstance().vinfo->getRebaseDelta();
// 44.02 32bit
intptr_t faddr = 0xed738c + Core::getInstance().vinfo->getRebaseDelta();
intptr_t maddr = 0xe85994 + Core::getInstance().vinfo->getRebaseDelta(); //malloc call in operator new
old_malloc = (MALLOC)&malloc;
old_free = *(FREE*)faddr;
MemoryPatcher *mp = new MemoryPatcher(Core::getInstance().p);
mp->makeWritable((void*)maddr, sizeof(intptr_t));
intptr_t nm = (intptr_t)&new_malloc - maddr - 4;
memcpy((void*)maddr, &nm, sizeof(intptr_t));
mp->makeWritable((void*)faddr, sizeof(intptr_t));
intptr_t nf = (intptr_t)&new_free;
memcpy((void*)faddr, &nf, sizeof(intptr_t));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment