when a pthread_rwlock is used in an invalid way.
Recent glibcs use transactional memory instructions to do lock ellision
but will sometimes, when locks are used in an invalid way, may calls to
xend on systems which don't support it, on the grounds that the program
is invalid anyway.
So we try and catch and ignore the resulting SIGILL in our tests that
deliberately work with invalid locks.
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@15565
Reader-writer lock not locked by calling thread: rwlock 0x.........
at 0x........: pthread_rwlock_unlock (drd_pthread_intercepts.c:?)
+ by 0x........: safe_pthread_rwlock_unlock (safe-pthread.h:43)
by 0x........: main (tc12_rwl_trivial.c:29)
rwlock 0x........ was first observed at:
at 0x........: pthread_rwlock_init (drd_pthread_intercepts.c:?)
prereq: ./supported_libpthread
+vgopts: --sigill-diagnostics=no
prog: ../../helgrind/tests/tc12_rwl_trivial
--- /dev/null
+#include <pthread.h>
+#include <signal.h>
+#include <setjmp.h>
+#include <errno.h>
+#include <assert.h>
+
+static jmp_buf env;
+
+/*
+ * Starting with glibc 2.20 some pthread calls may execute
+ * an xend instruction unconditionally when a lock is used in
+ * a way that is invalid so defined a sigill handler that can
+ * convert these invalid instructions to a normal error.
+ */
+static void sigill_handler( int signum, siginfo_t *siginfo, void *sigcontext ) {
+ unsigned char *pc = siginfo->si_addr;
+ assert( pc[0] == 0x0f && pc[1] == 0x01 && pc[2] == 0xd5 );
+ longjmp( env, EPERM );
+}
+
+/*
+ * Wrapper for pthread_rwlock_unlock which may execute xend
+ * unconditionally when used on a lock that is not locked.
+ *
+ * Note that we return 0 instead of EPERM because that is what
+ * glibc normally does - error reporting is optional.
+ */
+static int safe_pthread_rwlock_unlock( pthread_rwlock_t *rwlock ) {
+#if __GLIBC_PREREQ(2,20) && ( defined(__i386__) || defined(__x86_64__) )
+ struct sigaction sa;
+ struct sigaction oldsa;
+ int r;
+
+ sa.sa_handler = NULL;
+ sa.sa_sigaction = sigill_handler;
+ sigemptyset( &sa.sa_mask );
+ sa.sa_flags = SA_SIGINFO;
+ sa.sa_restorer = NULL;
+
+ sigaction( SIGILL, &sa, &oldsa );
+
+ if ( ( r = setjmp( env ) ) == 0 ) {
+ r = pthread_rwlock_unlock( rwlock );
+ } else {
+ r = 0;
+ }
+
+ sigaction( SIGILL, &oldsa, NULL );
+
+ return r;
+#else
+ return pthread_rwlock_unlock( rwlock );
+#endif
+}
+
+#define pthread_rwlock_unlock safe_pthread_rwlock_unlock
#define _GNU_SOURCE 1
#include <stdio.h>
-#include <pthread.h>
+#include "safe-pthread.h"
#include <assert.h>
/* Do trivial stuff with a reader-writer lock. */
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc12_rwl_trivial.c:29)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init_WRK (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc12_rwl_trivial.c:29)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init (hg_intercepts.c:...)
+vgopts: --sigill-diagnostics=no
prog: tc12_rwl_trivial
#include <string.h>
#include <assert.h>
#include <unistd.h>
-#include <pthread.h>
+#include "safe-pthread.h"
#include <semaphore.h>
#if !defined(__APPLE__)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:189)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init_WRK (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:206)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init_WRK (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:227)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init_WRK (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:189)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init_WRK (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:206)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init_WRK (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:189)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init_WRK (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:206)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init_WRK (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:227)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init_WRK (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:189)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init_WRK (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:206)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init_WRK (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:227)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init_WRK (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:189)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init_WRK (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:206)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init_WRK (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:227)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init_WRK (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:189)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:206)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init (hg_intercepts.c:...)
Thread #x unlocked a not-locked lock at 0x........
at 0x........: pthread_rwlock_unlock_WRK (hg_intercepts.c:...)
by 0x........: pthread_rwlock_unlock (hg_intercepts.c:...)
+ ...
by 0x........: main (tc20_verifywrap.c:227)
Lock at 0x........ was first observed
at 0x........: pthread_rwlock_init (hg_intercepts.c:...)
prereq: test -e tc20_verifywrap
prog: tc20_verifywrap
-vgopts: --read-var-info=yes
+vgopts: --read-var-info=yes --sigill-diagnostics=no