c - LD_PRELOAD problems with pthread_create -
i playing around concept of ld_preload. seems works fine until start use pthread library functions in code. when that, segmentation fault. ld_preload has kind of aversion pthread library or what?
shown here both program , ld_preloaded .so code.
program code
// main.c #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/mman.h> #include <sys/types.h> #define use_pthreads #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!\n", getpid(), tid, total, 0 ); pthread_mutex_unlock( &m ); printf("hello world! it's me, thread #%ld!\n", 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!\n", p, p[0], getpid() ); m = (char*)malloc( 80 ); printf( "m = %p!\n", m ); #ifdef use_pthreads // if disable part of code, ld_preload works fine for(t=0; t<num_threads; t++) { printf("in main: creating thread %ld\n", t); rc = pthread_create(&threads[t], null, printhello, (void *)t); if (rc){ printf("error; return code pthread_create() %d\n", rc); exit(-1); } } for(t=0; t<num_threads; t++) pthread_join(threads[t], null); printf( "\n\ntotal = %g\n\n", total ); /* last thing main() should */ pthread_exit(null); #endif printf( "\n\n%d: done without major problems\n\n", getpid() ); return 0; } ld_preloaded .so code
// prelib.c #define _gnu_source //#include <sys/syscall.h> //#include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <dlfcn.h> typedef pid_t (*getpidtype)(void); typedef void* (*mmaptype)(void *addr, size_t len, int prot, int flags, int fildes, off_t off); pid_t getpid(void) { static int first_time = 1; static getpidtype f; printf("hello, getpid!\n"); if ( first_time ) { f = (getpidtype)dlsym(rtld_next, "getpid"); first_time = 0; } return f(); } void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off) { static int first_time = 1; static mmaptype f; printf( "my mmap!\n" ); if ( first_time ) { f = (mmaptype)dlsym(rtld_next, "mmap" ); first_time = 0; } return f( addr, len, prot, flags, fildes, off ); } i error when use_pthreads defined.
my mmap! << mutex_trylock >> << mutex_unlock >> hello, getpid! p = 0x2ab9bc969000, p[0] = 78, pid = 9763! m = 0x179d4010! in main: creating thread 0 mmap! << mutex_trylock >> mmap! << mutex_trylock >> << mutex_unlock >> hello, getpid! p = 0x2ab9bc969000, p[0] = 78, pid = 9762! m = 0x179d4010! in main: creating thread 0 mmap! << mutex_trylock >> segmentation fault
i doubt problem, "first_time" hack not synchronized , not thread-safe. should using pthread_once function purpose, or roll own version mutex or semaphore.
Comments
Post a Comment