Abhishek Sagar
Abhishek Sagar

Reputation: 1326

segfault at pthread_mutex_lock()

I have a code which prints some data structure.

void
print_wheel_timer(wheel_timer_t *wt){

 <code to print data structure>
}

Then i sandwiched the code in-between lock and unlock mutex calls. 

void
print_wheel_timer(wheel_timer_t *wt){
pthread_mutex_lock(&wt->global_lock);
<code to print data structure>
pthread_mutex_unlock(&wt->global_lock);
}

And now, it segfault. I have disabled all threads in the code, and now program is single threaded, still a segfault. I am using gcc, ubuntu 19.04.

gdb shows pthread_mutex_lock() call is screwing up data structures !! This is weird ! I put the watch point on address which is being written by arbitrary value, and it shows a call to pthread_mutex_lock() is modifying it. When i remove these lock/unlock calls, no segfault seen.

(gdb) watch *(glthread_t *)&(wt->slotlist[1].slots.right)
Hardware watchpoint 2: *(glthread_t *)&(wt->slotlist[1].slots.right)
(gdb) c
Continuing.
Printing Wheel Timer DS

Thread 1 "test.exe" hit Hardware watchpoint 2: *(glthread_t *)&(wt->slotlist[1].slots.right)

Old value = {left = 0x0, right = 0x0}
New value = {left = 0x1, right = 0x0}     <<< corruption !!
0x00007ffff7fa3934 in __GI___pthread_mutex_lock (mutex=0x5555555771d8)
at ../nptl/pthread_mutex_lock.c:80
80      ../nptl/pthread_mutex_lock.c: No such file or directory.
(gdb) bt
#0  0x00007ffff7fa3934 in __GI___pthread_mutex_lock (mutex=0x5555555771d8)
at ../nptl/pthread_mutex_lock.c:80
#1  0x000055555555f3a2 in print_wheel_timer (wt=0x555555577180)
at WheelTimer/WheelTimer.c:276

Code :
========
void
print_wheel_timer(wheel_timer_t *wt){

int i = 0, j = 0;
glthread_t *curr;
glthread_t *slot_list_head = NULL;
wheel_timer_elem_t *wt_elem = NULL;

printf("Printing Wheel Timer DS\n");
pthread_mutex_lock(&wt->global_lock);
printf("wt->current_clock_tic  = %d\n", wt->current_clock_tic);
printf("wt->clock_tic_interval = %d\n", wt->clock_tic_interval);
printf("wt->wheel_size         = %d\n", wt->wheel_size);
printf("wt->current_cycle_no   = %d\n", wt->current_cycle_no);
printf("wt->wheel_thread       = %p\n", &wt->wheel_thread);
printf("WT uptime              = %s\n", hrs_min_sec_format(WT_UPTIME(wt)));
printf("wt->no_of_wt_elem      = %u\n", wt->no_of_wt_elem);
printf("printing slots : \n");

for(; i < wt->wheel_size; i++){
    slot_list_head = WT_SLOTLIST_HEAD(wt, i);
    ITERATE_GLTHREAD_BEGIN(slot_list_head, curr){           << segfaulting here for i = 1
        wt_elem = glthread_to_wt_elem(curr);

If some holy soul wants to try it out on his machine. pls download the code here : https://github.com/sachinites/tcpip_stack

After downloading :
switch to branch "Hellos"
git checkout Hellos
Compile :
make all
run
./test.exe
to reproduce , run the cmd : 
debug show node H1 timer 

Now it will core in function : print_wheel_timer() implemented in WheelTimer/WheelTimer.c

Upvotes: 0

Views: 6921

Answers (3)

Chuck
Chuck

Reputation: 2102

This was the top question when I was googling around for ../nptl/pthread_mutex_lock.c: No such file or directory., so I figured I'd post the fix for me.

In my case, I was trying to make an interactive marker server with ROS, but I kept getting this segfault. When I got into it with gdb I saw the error I mentioned above, and could NOT figure out what was going on with a mutex because there simply weren't any in my code.

It turns out that my interactive_markers::InteractiveMarkerServer server is not actually an InteractiveMarkerSever, but instead it's a smart pointer that was not initialized!

I have no idea why I didn't just get a null reference exception or something else. No idea why it went to a mutex file missing, but when I initialized the pointer the error went away. My class definition has:

std::shared_ptr<interactive_markers::InteractiveMarkerServer> server;

And when I added:

server = std::make_shared<interactive_markers::InteractiveMarkerServer>("ns", "id", false);

to the class constructor I stopped getting the error.

Upvotes: 1

Abhishek Sagar
Abhishek Sagar

Reputation: 1326

The problem was with my Data structure, in which zero length array was the non-last member of the C structure which corrupt memory when read/written into such arrays. Stretchable arrays should be last member of the structures.

typedef struct _wheel_timer_t {
    int current_clock_tic;
    int clock_tic_interval;
    int wheel_size;
    int current_cycle_no;
    pthread_t wheel_thread;
    slotlist_t slotlist[0];            << problem, make it last member
    slotlist_t reschd_list;
    unsigned int no_of_wt_elem;
    pthread_mutex_t global_lock;
} wheel_timer_t;

Upvotes: 3

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215193

Almost surely your wheel_timer_t structure has a global_lock member of type pthread_mutex_t * rather than pthread_mutex_t, meaning that you're passing a pointer of the wrong type, that points to only 8 bytes of storage (assuming a 64-bit implementation), to pthread_mutex_lock. This would be detected if you used compiler warnings; pthread_mutex_t ** cannot convert implicitly to pthread_mutex_t * and the compiler should issue a diagnostic for this.

Note that this probably means you have significant other errors and that your mutex is not properly initialized.

Upvotes: 0

Related Questions