Semaphore error on Helgrind and Drd?

Paulo Ferreira <[email protected]> Sat, 25 May 2024 11:29:56 +0100
Newsgroups gmane.comp.debugging.valgrind
Message-ID <[email protected]>
Error message from Helgrind: 

$ valgrind --tool=helgrind  ./prog 
...
==6587== Thread #1: Bug in libpthread: sem_wait succeeded on semaphore without prior sem_post
==6587==    at 0x4850069: sem_wait_WRK (hg_intercepts.c:3155)
==6587==    by 0x4851180: sem_wait@* (hg_intercepts.c:3174)
==6587==    by 0x109271: main (in /media/sf_antiX/erros/duvida/prog)
…

Error message from Drd: 

$ valgrind --tool=drd --trace-semaphore=yes  ./prog
...
==6828== [1] sem_wait      0x487f000 value 0 -> 4294967295
==6828== Invalid semaphore: semaphore 0x487f000
==6828==    at 0x4866C1A: sem_wait_intercept (drd_pthread_intercepts.c:1570)
==6828==    by 0x4866C1A: sem_wait@* (drd_pthread_intercepts.c:1578)
==6828==    by 0x109271: main (in /media/sf_antiX/erros/duvida/prog)
==6828== semaphore 0x487f000 was first observed at:
==6828==    at 0x4865A16: sem_open_intercept (drd_pthread_intercepts.c:1527)
==6828==    by 0x4865A16: sem_open@* (drd_pthread_intercepts.c:1538)
==6828==    by 0x1091D3: main (in /media/sf_antiX/erros/duvida/prog)
…  

So the issue seems to be that according to Drd a semaphore that has the value of zero, with a sem_wait() gets decremented to 4294967295. (????????) 
I have the sample code on the end of this message, and I have tried to run the code on several recent versions of Valgrind (32.3 and others), and on x86-64 using antiX Linux, MX Linux, Ubuntu Linux and FreeBSD. I have also tried ARM 64 bits under Ubuntu, and the behaviour is the same. The sample program seems to work ok. 

My thanks to all the Valgrind developers for the wonderful software they created, and I sincerely hope the issue is on my side (between chair and keyboard).

Paulo Ferreira 
======================
#include <stdio.h>
#include <fcntl.h>
#include <semaphore.h>
#include <unistd.h>
#include <sys/wait.h>

#define NUM 10 

int main(void)
{
    int i;
    sem_t *sem1; // First semaphore
    sem_t *sem2; // Second semaphore

    // create, initialize semaphores
    sem1 = sem_open("/semaphore1", O_CREAT,  0600, 0);
    sem2 = sem_open("/semaphore2", O_CREAT,  0600, 1);
	pid_t pid = fork(); 
    if (pid>0) // Parent process
    {
        for (i = 0; i < NUM;  i++)
        {
            sem_wait(sem2); // Lock the semaphore
            write(1, "a\n", 2);
            sem_post(sem1); // Release the semaphore lock
        }
        wait(NULL);
    }
    else // Child process
    {
        for (i = 0; i < NUM; i++)
        {   
		    sem_wait(sem1); // Lock the semaphore
            write(1, "b\n", 2);
            sem_post(sem2); // Release the semaphore lock
        }
    }


	// Close the Semaphores
    sem_close(sem1);
    sem_unlink("/semaphore1");
    sem_close(sem2);
    sem_unlink("/semaphore2");
    return 0;
}
==========================

 

    



_______________________________________________
Valgrind-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/valgrind-users