]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
DRD: avoid unaligned reads.
authorBart Van Assche <bvanassche@acm.org>
Sun, 19 Sep 2010 11:14:31 +0000 (11:14 +0000)
committerBart Van Assche <bvanassche@acm.org>
Sun, 19 Sep 2010 11:14:31 +0000 (11:14 +0000)
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@11365

drd/drd_pthread_intercepts.c

index f3026edd3dfc2aa6f220888a856d9ff8ec611b68..57e509534aee1f2b1d71c4fbd538b0c5bc0c616c 100644 (file)
@@ -51,6 +51,7 @@
 #include <assert.h>         /* assert() */
 #include <pthread.h>        /* pthread_mutex_t */
 #include <semaphore.h>      /* sem_t */
+#include <stdint.h>         /* uintptr_t */
 #include <stdio.h>          /* fprintf() */
 #include <stdlib.h>         /* malloc(), free() */
 #include <unistd.h>         /* confstr() */
@@ -181,6 +182,8 @@ static MutexT DRD_(pthread_to_drd_mutex_type)(const int kind)
    }
 }
 
+#define IS_ALIGNED(p) (((uintptr_t)(p) & (sizeof(*(p)) - 1)) == 0)
+
 /**
  * Read the mutex type stored in the client memory used for the mutex
  * implementation.
@@ -198,19 +201,25 @@ static __always_inline MutexT DRD_(mutex_type)(pthread_mutex_t* mutex)
 {
 #if defined(HAVE_PTHREAD_MUTEX_T__M_KIND)
    /* glibc + LinuxThreads. */
-   const int kind = mutex->__m_kind & 3;
-   return DRD_(pthread_to_drd_mutex_type)(kind);
+   if (IS_ALIGNED(&mutex->__m_kind))
+   {
+      const int kind = mutex->__m_kind & 3;
+      return DRD_(pthread_to_drd_mutex_type)(kind);
+   }
 #elif defined(HAVE_PTHREAD_MUTEX_T__DATA__KIND)
    /* glibc + NPTL. */
-   const int kind = mutex->__data.__kind & 3;
-   return DRD_(pthread_to_drd_mutex_type)(kind);
+   if (IS_ALIGNED(&mutex->__data.__kind))
+   {
+      const int kind = mutex->__data.__kind & 3;
+      return DRD_(pthread_to_drd_mutex_type)(kind);
+   }
 #else
    /*
     * Another POSIX threads implementation. The mutex type won't be printed
     * when enabling --trace-mutex=yes.
     */
-   return mutex_type_unknown;
 #endif
+   return mutex_type_unknown;
 }
 
 /**