SHARE
TWEET

Untitled

a guest Apr 19th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define __USE_GNU
  4. #include <dlfcn.h>
  5.  
  6. extern void *__libc_free(void *ptr);
  7.  
  8. static void *(*real_malloc) (size_t);
  9. int cnt_blocks = 0;
  10.  
  11. void *malloc (size_t size)
  12. {
  13.     if (cnt_blocks >= 77) {
  14.         fprintf (stderr, "Number of allocated blocks is more then 77\n");        
  15.         return (NULL);
  16.     }
  17.     cnt_blocks++;
  18.     return (*real_malloc) (size);
  19. }
  20.  
  21. void free(void *ptr)
  22. {
  23.     if (cnt_blocks > 0) {
  24.         cnt_blocks--;
  25.         __libc_free(ptr);
  26.         printf("Nubmer of allocated blocks: %d\n", cnt_blocks);
  27.     } else printf("Cant free unallocated block\n");
  28. }
  29.  
  30. static void __malloc_init (void) __attribute__((constructor));
  31. static void __malloc_init (void)
  32. {
  33.     void *ptr;
  34.     ptr = dlopen (NULL, RTLD_LAZY);
  35.     if (ptr == NULL)
  36.         abort ();
  37.     real_malloc = dlsym (RTLD_NEXT, "malloc");
  38.     if (NULL == real_malloc) {
  39.         fprintf (stderr, "Error in `dlsym`: %s\n", dlerror ());
  40.         return;
  41.     }
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top