multicore-locks / litl

LiTL: Library for Transparent Lock Interposition
MIT License
75 stars 21 forks source link

conflict with google flags library #1

Closed woonhak closed 7 years ago

woonhak commented 7 years ago

If an application uses google flags, the call pthread_mutex_init invokes segment fault.

Following is a simple example:

example.cc

#include <iostream>                                                                      
#include <gflags/gflags.h>                                                               
#include <pthread.h>                                                                     

DEFINE_bool(verbose, false, "Display program name before message");                      
DEFINE_string(message, "Hello world!", "Message to print");                              

int main(int argc, char **argv)                                                          
{                                                                                        
        static pthread_mutex_t mutex;                                                    
        google::ParseCommandLineFlags(&argc, &argv, true);                               
        pthread_mutex_init(&mutex, NULL);                                                

        if (FLAGS_verbose) std::cout << google::ProgramInvocationShortName() << ": ";    
        std::cout << FLAGS_message << std::endl;                                         
        google::ShutDownCommandLineFlags();                                              

        pthread_mutex_destroy(&mutex);                                                   

        return 0;                                                                        
}                                                                                        

woonhak commented 7 years ago

I found what is the problem.

if rwlock_init called before previous lock variable not unlocked, then clht can't find previous lock object.

Following example1.cc always dead because of this problem.

#include <iostream>
#include <pthread.h>

int main(int argc, char **argv)
{
        static pthread_rwlock_t rwlock, rwlock1;
        pthread_rwlock_init(&rwlock, NULL);
        pthread_rwlock_wrlock(&rwlock);

        pthread_rwlock_init(&rwlock1, NULL);
        pthread_rwlock_wrlock(&rwlock1);

       //segment fault at following line
        pthread_rwlock_unlock(&rwlock);
        pthread_rwlock_unlock(&rwlock1);

        pthread_rwlock_destroy(&rwlock);
        pthread_rwlock_destroy(&rwlock1);

        std::cout<<"done"<<std::endl;
        return 0;
}
HugoGuiroux commented 7 years ago

Hi @woonhak,

Thanks for discovering this bug. I pushed a fix that should avoid the crashes (at least on your two examples). Can you please test with the latest master version and tell me if it's OK?

woonhak commented 7 years ago

You fixed this issue exactly what I thought It works fine now. Thanks for the fix.