| 1 | #if 0
 | 
|---|
| 2 | #include <dlfcn.h>
 | 
|---|
| 3 | #include <unistd.h>
 | 
|---|
| 4 | #include <stdlib.h>
 | 
|---|
| 5 | 
 | 
|---|
| 6 | bool recursion = false;
 | 
|---|
| 7 | 
 | 
|---|
| 8 | static char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
 | 
|---|
| 9 | 
 | 
|---|
| 10 | static union {
 | 
|---|
| 11 |     void *addr;
 | 
|---|
| 12 |     unsigned char bytes[sizeof(void *)];
 | 
|---|
| 13 | };
 | 
|---|
| 14 | 
 | 
|---|
| 15 | struct Mallocmsg {
 | 
|---|
| 16 |     const char start[9];
 | 
|---|
| 17 |     char addr[16];
 | 
|---|
| 18 |     const char sep[3];
 | 
|---|
| 19 |     char size[16];
 | 
|---|
| 20 |     const char end[1];
 | 
|---|
| 21 | } mallocmsg = {
 | 
|---|
| 22 |     'm', 'a', 'l', 'l', 'o', 'c', ' ', '0', 'x',
 | 
|---|
| 23 |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 | 
|---|
| 24 |     ' ', '0', 'x',
 | 
|---|
| 25 |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 | 
|---|
| 26 |     '\n'
 | 
|---|
| 27 | };
 | 
|---|
| 28 | 
 | 
|---|
| 29 | void * malloc( size_t size ) {
 | 
|---|
| 30 |     if ( recursion ) { write( STDERR_FILENO, "recursion\n", 10 ); abort(); }
 | 
|---|
| 31 |     recursion = true;
 | 
|---|
| 32 |     __typeof__( ::malloc ) *libc_malloc = (__typeof__( ::malloc ) *)dlsym(RTLD_NEXT, "malloc");
 | 
|---|
| 33 |     addr = (void *)size;
 | 
|---|
| 34 |     for ( int i = 0, j = 7; i < 16; i += 2, j -= 1 ) {
 | 
|---|
| 35 |         mallocmsg.size[i] = hex[bytes[j] >> 4];
 | 
|---|
| 36 |         mallocmsg.size[i + 1] = hex[bytes[j] & 0x0f];
 | 
|---|
| 37 |     } // for
 | 
|---|
| 38 |     addr = libc_malloc( size );
 | 
|---|
| 39 |     for ( int i = 0, j = 7; i < 16; i += 2, j -= 1 ) {
 | 
|---|
| 40 |         mallocmsg.addr[i] = hex[bytes[j] >> 4];
 | 
|---|
| 41 |         mallocmsg.addr[i + 1] = hex[bytes[j] & 0x0f];
 | 
|---|
| 42 |     } // for
 | 
|---|
| 43 |     write( STDERR_FILENO, &mallocmsg, sizeof(mallocmsg) );
 | 
|---|
| 44 |     recursion = false;
 | 
|---|
| 45 |     return addr;
 | 
|---|
| 46 | }
 | 
|---|
| 47 | 
 | 
|---|
| 48 | struct Freemsg {
 | 
|---|
| 49 |     const char start[7];
 | 
|---|
| 50 |     char addr[16];
 | 
|---|
| 51 |     const char end[1];
 | 
|---|
| 52 | } freemsg = {
 | 
|---|
| 53 |     'f', 'r', 'e', 'e', ' ', '0', 'x',
 | 
|---|
| 54 |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 | 
|---|
| 55 |     '\n'
 | 
|---|
| 56 | };
 | 
|---|
| 57 | 
 | 
|---|
| 58 | void free( void * x ) {
 | 
|---|
| 59 |     if ( recursion ) { write( STDERR_FILENO, "recursion\n", 10 ); abort(); }
 | 
|---|
| 60 |     recursion = true;
 | 
|---|
| 61 |     __typeof__( ::free ) *libc_free = (__typeof__( ::free ) *)dlsym(RTLD_NEXT, "free");
 | 
|---|
| 62 |     addr = x;
 | 
|---|
| 63 |     for ( int i = 0, j = 7; i < 16; i += 2, j -= 1 ) {
 | 
|---|
| 64 |         freemsg.addr[i] = hex[bytes[j] >> 4];
 | 
|---|
| 65 |         freemsg.addr[i + 1] = hex[bytes[j] & 0x0f];
 | 
|---|
| 66 |     } // for
 | 
|---|
| 67 |     write( STDERR_FILENO, &freemsg, sizeof(freemsg) );
 | 
|---|
| 68 |     recursion = false;
 | 
|---|
| 69 |     libc_free( addr );
 | 
|---|
| 70 | }
 | 
|---|
| 71 | #endif // 0
 | 
|---|