From: Bart Van Assche Date: Thu, 28 Feb 2008 08:30:43 +0000 (+0000) Subject: Added support for recognizing invalid mutexes. X-Git-Tag: svn/VALGRIND_3_4_0~1013 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=beb0f80401d6f953dc1db0ba8db050e5362d1b96;p=thirdparty%2Fvalgrind.git Added support for recognizing invalid mutexes. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@7497 --- diff --git a/exp-drd/drd_clientreq.h b/exp-drd/drd_clientreq.h index bb701e2983..b0357130c6 100644 --- a/exp-drd/drd_clientreq.h +++ b/exp-drd/drd_clientreq.h @@ -119,6 +119,7 @@ enum { typedef enum { + mutex_type_invalid_mutex = 0, mutex_type_recursive_mutex = 1, mutex_type_errorcheck_mutex = 2, mutex_type_default_mutex = 3, diff --git a/exp-drd/drd_intercepts.c b/exp-drd/drd_intercepts.c index 5ed7a5bc85..07900de4d5 100644 --- a/exp-drd/drd_intercepts.c +++ b/exp-drd/drd_intercepts.c @@ -100,18 +100,8 @@ static MutexT pthread_to_drd_mutex_type(const int kind) case PTHREAD_MUTEX_DEFAULT: case PTHREAD_MUTEX_ADAPTIVE_NP: return mutex_type_default_mutex; -#if 0 - case -1: - printf("Warning: changed mutex type from -1 into %d\n", - mutex_type_default_mutex); - return mutex_type_default_mutex; -#endif } -#if 0 - printf("mutex->__data.__kind = %d\n", kind); - assert(0); -#endif - return mutex_type_default_mutex; + return mutex_type_invalid_mutex; } static MutexT mutex_type(pthread_mutex_t* mutex) diff --git a/exp-drd/drd_mutex.c b/exp-drd/drd_mutex.c index 491d047cc7..e45c6a692a 100644 --- a/exp-drd/drd_mutex.c +++ b/exp-drd/drd_mutex.c @@ -251,12 +251,15 @@ struct mutex_info* mutex_get(const Addr mutex) */ void mutex_pre_lock(const Addr mutex, const SizeT size, MutexT mutex_type) { - struct mutex_info* p = mutex_get(mutex); + struct mutex_info* p; + + p = mutex_get(mutex); if (p == 0) { mutex_init(mutex, size, mutex_type); p = mutex_get(mutex); } + tl_assert(p); if (p->owner == thread_get_running_tid() @@ -295,6 +298,16 @@ int mutex_post_lock(const Addr mutex, const SizeT size, MutexT mutex_type) p ? p->owner : VG_INVALID_THREADID); } + if (mutex_type == mutex_type_invalid_mutex) + { + GenericErrInfo GEI; + VG_(maybe_record_error)(VG_(get_running_tid)(), + GenericErr, + VG_(get_IP)(VG_(get_running_tid)()), + "Invalid mutex", + &GEI); + } + if (p == 0) { GenericErrInfo GEI; @@ -368,6 +381,16 @@ int mutex_unlock(const Addr mutex, const MutexT mutex_type) p->owner); } + if (mutex_type == mutex_type_invalid_mutex) + { + GenericErrInfo GEI; + VG_(maybe_record_error)(VG_(get_running_tid)(), + GenericErr, + VG_(get_IP)(VG_(get_running_tid)()), + "Invalid mutex", + &GEI); + } + if (p == 0) { GenericErrInfo GEI; @@ -448,6 +471,8 @@ const char* mutex_type_name(const MutexT mt) { switch (mt) { + case mutex_type_invalid_mutex: + return "invalid mutex"; case mutex_type_recursive_mutex: return "recursive mutex"; case mutex_type_errorcheck_mutex: diff --git a/exp-drd/tests/tc09_bad_unlock.stderr.exp b/exp-drd/tests/tc09_bad_unlock.stderr.exp index a16e24c7cb..40b301ccc5 100644 --- a/exp-drd/tests/tc09_bad_unlock.stderr.exp +++ b/exp-drd/tests/tc09_bad_unlock.stderr.exp @@ -13,6 +13,11 @@ Mutex not unlocked by owner thread: address 0x........, recursion count 1, owner by 0x........: clone (in /...libc...) Thread 1: +Invalid mutex + at 0x........: pthread_mutex_unlock (drd_intercepts.c:?) + by 0x........: nearly_main (tc09_bad_unlock.c:41) + by 0x........: main (tc09_bad_unlock.c:49) + Not a mutex at 0x........: pthread_mutex_unlock (drd_intercepts.c:?) by 0x........: nearly_main (tc09_bad_unlock.c:41) @@ -32,9 +37,14 @@ Mutex not unlocked by owner thread: address 0x........, recursion count 1, owner by 0x........: clone (in /...libc...) Thread 1: +Invalid mutex + at 0x........: pthread_mutex_unlock (drd_intercepts.c:?) + by 0x........: nearly_main (tc09_bad_unlock.c:41) + by 0x........: main (tc09_bad_unlock.c:50) + Not a mutex at 0x........: pthread_mutex_unlock (drd_intercepts.c:?) by 0x........: nearly_main (tc09_bad_unlock.c:41) by 0x........: main (tc09_bad_unlock.c:50) -ERROR SUMMARY: 6 errors from 6 contexts (suppressed: 0 from 0) +ERROR SUMMARY: 8 errors from 8 contexts (suppressed: 0 from 0) diff --git a/exp-drd/tests/tc20_verifywrap.stderr.exp b/exp-drd/tests/tc20_verifywrap.stderr.exp index a5d0979287..ccc5f40439 100644 --- a/exp-drd/tests/tc20_verifywrap.stderr.exp +++ b/exp-drd/tests/tc20_verifywrap.stderr.exp @@ -24,6 +24,10 @@ Destroying locked mutex: address 0x........, recursion count 1, owner 1. at 0x........: pthread_mutex_destroy (drd_intercepts.c:?) by 0x........: main (tc20_verifywrap.c:102) +Invalid mutex + at 0x........: pthread_mutex_unlock (drd_intercepts.c:?) + by 0x........: main (tc20_verifywrap.c:125) + Mutex not locked: address 0x........, recursion count 0, owner 0. at 0x........: pthread_mutex_unlock (drd_intercepts.c:?) by 0x........: main (tc20_verifywrap.c:125) @@ -67,4 +71,4 @@ FIXME: can't figure out how to verify wrap of sem_post Destroying locked mutex: address 0x........, recursion count 1, owner 1. at 0x........: main (tc20_verifywrap.c:261) -ERROR SUMMARY: 6 errors from 6 contexts (suppressed: 0 from 0) +ERROR SUMMARY: 7 errors from 7 contexts (suppressed: 0 from 0)