multicore-locks/litl

conflict with google flags library

Closed this issue · 3 comments

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

  • Env
    Ubuntu 14.04
    libgflags-dev 2.0-1.1ubuntu1
    gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3)

  • Topology
    #define NUMA_NODES 4
    #define CPU_NUMBER 64
    #define L_CACHE_LINE_SIZE 128
    #define PAGE_SIZE 4096

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;                                                                        
}                                                                                        

  • build
    g++ -o example example.cc -lgflags -lpthread

  • run
    /path/to/litl/lib{lock}_{waiting_policy} ./example
    -> segment fault

  • reason
    call (REAL)(pthread_mutex_init) failed,
    because real_pthread_mutex_init_{lock} is NULL and not correctly linked.

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;
}

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?

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