Last active
March 27, 2025 12:06
-
-
Save xorz57/c5e04751cafacf2338e4d34040161072 to your computer and use it in GitHub Desktop.
This file contains 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
#include <cstdio> | |
#include <cstdlib> | |
#include <cstring> | |
#include <dlfcn.h> | |
#include <unistd.h> | |
extern "C" { | |
void *malloc(std::size_t size) { | |
using malloc_t = decltype(&std::malloc); | |
static malloc_t native_malloc = reinterpret_cast<malloc_t>(dlsym(RTLD_NEXT, "malloc")); | |
char buf[80]; | |
std::snprintf(buf, sizeof(buf), "malloc(%zu)\n", size); | |
write(2, buf, strlen(buf)); | |
return native_malloc(size); | |
} | |
void free(void *ptr) { | |
using free_t = decltype(&std::free); | |
static free_t native_free = reinterpret_cast<free_t>(dlsym(RTLD_NEXT, "free")); | |
char buf[80]; | |
std::snprintf(buf, sizeof(buf), "free(%p)\n", ptr); | |
write(2, buf, strlen(buf)); | |
return native_free(ptr); | |
} | |
void *calloc(std::size_t num, std::size_t size) { | |
using calloc_t = decltype(&std::calloc); | |
static calloc_t native_calloc = reinterpret_cast<calloc_t>(dlsym(RTLD_NEXT, "calloc")); | |
char buf[80]; | |
std::snprintf(buf, sizeof(buf), "calloc(%zu, %zu)\n", num, size); | |
write(2, buf, strlen(buf)); | |
return native_calloc(num, size); | |
} | |
void *realloc(void *ptr, std::size_t size) { | |
using realloc_t = decltype(&std::realloc); | |
static realloc_t native_realloc = reinterpret_cast<realloc_t>(dlsym(RTLD_NEXT, "realloc")); | |
char buf[80]; | |
std::snprintf(buf, sizeof(buf), "realloc(%p, %zu)\n", ptr, size); | |
write(2, buf, strlen(buf)); | |
return native_realloc(ptr, size); | |
} | |
} |
Author
xorz57
commented
Mar 24, 2025
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment