drd/tests/Makefile.am: Add dlopen.stdout.exp to EXTRA_DIST
[valgrind.git] / helgrind / tests / tc09_bad_unlock.c
blob238ce9bf6518fbb968c850baa4fdc4cac664b568
2 /* Check that an error is reported for various kinds of bogus
3 pthread_mutex_unlock calls. */
5 #include <pthread.h>
6 #include <stdio.h>
7 #include <stdlib.h>
9 void* child_fn ( void* arg )
11 pthread_mutex_unlock( (pthread_mutex_t*)arg ); /* ERROR */
12 return NULL;
15 void nearly_main ( void )
17 pthread_t child;
18 pthread_mutex_t mx1, mx2;
19 int bogus[100], i;
20 /* fill bogus with values which will cause glibc's pth_mx_unlock to fail */
21 for (i = 0; i < 100; i++) bogus[i] = 0xFFFFFFFF;
22 /* Unlocking a lock that is already unlocked */
23 pthread_mutex_init( &mx1, NULL );
24 pthread_mutex_lock( &mx1 );
25 pthread_mutex_unlock( &mx1 );
27 pthread_mutex_unlock( &mx1 ); /* ERROR */
29 /* Unlocking a lock that is held by a different thread */
31 pthread_mutex_init( &mx2, NULL );
32 pthread_mutex_lock( &mx2 );
33 // start child and get it to unlock this lock
35 pthread_create( &child, NULL, child_fn, (void*)&mx2 );
36 /* child runs and attempts to unlock our lock. Error
37 is reported in child_fn. */
38 pthread_join(child, NULL );
40 /* Unlocking a totally bogus lock. */
41 pthread_mutex_unlock( (pthread_mutex_t*) &bogus[50] ); /* ERROR */
43 /* Now we get a freeing-locked-lock error, since the stack
44 frame is removed whilst mx2 is still locked. */
47 int main ( void )
49 nearly_main(); fprintf(stderr, "---------------------\n" );
50 nearly_main();
51 return 0;