Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
989 views
in Technique[技术] by (71.8m points)

c - LD_PRELOADing malloc and free

I wrote my own malloc and free and compiled them in a shared library. I LD_PRELOAD that library with my program. In this way would my program always use my custom malloc and free or are there cases where it is not so. I've heard that gcc has built in malloc and free too. Is it possible that the glibc that came with my gcc is using the builtin malloc and free.

Secondly, I notice that when I run my program, I'm seeing the free function call more often than the malloc/calloc calls (98 to 16). I don't do any memory allocation myself (except in one place), so all allocation is being done by standard library functions that I use. And also note I'm using pthread in my program. If you want to know, my program looks like this.

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>

#define NUM_THREADS     8

pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;

int sum;
float total = 1;
extern int __did_libc_start_main;

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   pthread_mutex_lock( &m );
   sum++;
   total *= total + tid * 0.097891313423578;
   printf( "p%d, tid%d, total = %g, start = %d!
", getpid(), tid, total, 0 );
   pthread_mutex_unlock( &m );
   printf("Hello World! It's me, thread #%ld!
", tid);
   pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   char * p;
   char * m;

   fork();

   p = (char*)mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
   p[0] = 78;
   printf( "p = %p, p[0] = %d, pid = %d!
", p, p[0], getpid() );
   m = (char*)malloc( 80 );
   printf( "m = %p!
", m );
#if 1  
   for(t=0; t<NUM_THREADS; t++)
   {
      printf("In main: creating thread %ld
", t);
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
      if (rc){
         printf("ERROR; return code from pthread_create() is %d
", rc);
         exit(-1);
      }
   }
   for(t=0; t<NUM_THREADS; t++)
    pthread_join(threads[t], NULL);

   printf( "

Total = %g

", total );

   /* Last thing that main() should do */
   pthread_exit(NULL);
#endif
   printf( "

%d: Done without major problems

", getpid() );
   return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using LD_PRELOAD to override malloc etc. is expected to work; this is how e.g. DUMA works.

In addition to malloc, calloc and free, make sure you override realloc, memalign and valloc. In addition you might need to override C++ new, new[], delete and delete[].

See Overriding 'malloc' using the LD_PRELOAD mechanism for an example of how to do this right.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...