Created
January 15, 2023 17:19
-
-
Save sykwer/ab5e807e5c880ada98b74cfef20017e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _GNU_SOURCE | |
#define _GNU_SOURCE | |
#endif | |
#include <dlfcn.h> | |
#include <stdio.h> | |
// #include <iostream> | |
using malloc_type = void*(*)(size_t); | |
static __thread bool no_hook = false; | |
extern "C" { | |
void *malloc(size_t size) { | |
static malloc_type original_malloc = reinterpret_cast<malloc_type>(dlsym(RTLD_NEXT, "malloc")); | |
if (no_hook) { | |
return original_malloc(size); | |
} | |
no_hook = true; | |
void *caller = __builtin_return_address(0); | |
void *ret = original_malloc(size); | |
//std::cout << caller << ": malloc(" << size << ") -> " << ret << std::endl; | |
printf("%p: malloc(%lu) -> %p\n", caller, size, ret); | |
no_hook = false; | |
return ret; | |
} | |
} // extern "C" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
g++ -g -shared -fPIC -o preloaded.so malloc_hook.cpp