Subject: pkg/34964: dmalloc mutex handling error
To: None <pkg-manager@netbsd.org, gnats-admin@netbsd.org,>
From: None <heas@shrubbery.net>
List: pkgsrc-bugs
Date: 11/01/2006 23:10:00
>Number:         34964
>Category:       pkg
>Synopsis:       dmalloc mutex handling error
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    pkg-manager
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Nov 01 23:10:00 +0000 2006
>Originator:     john heasley
>Release:        NetBSD 3.99.17
>Organization:
	
>Environment:
	
	
System: NetBSD guelah 3.99.17 NetBSD 3.99.17 (guelah) #1: Tue Apr 18 01:51:21 UTC 2006 root@oak:/sys/arch/sparc64/compile/guelah sparc64
Architecture: sparc64
Machine: sparc64
>Description:
dmalloc 5.4.2 tries to unlock an uninitialized and unlocked mutex.
>How-To-Repeat:
Run a program linked with libdmallocth.  It breaks at start-up.
>Fix:
I'm not sure if this works for every pkgsrc platform, but it does work for
NetBSD.

$NetBSD$

--- malloc.c.orig	2004-10-19 14:51:21.000000000 +0000
+++ malloc.c
@@ -118,6 +118,7 @@ static	int		in_alloc_b = 0;		/* can't be
 static	int		do_shutdown_b = 0;	/* execute shutdown soon */
 static	int		memalign_warn_b = 0;	/* memalign warning printed?*/
 static	dmalloc_track_t	tracking_func = NULL;	/* memory trxn tracking func */
+static	int		thread_lock_init = 0;	/* mutex init toggle */
 
 /* debug variables */
 static	char		*start_file = NULL;	/* file to start at */
@@ -188,22 +189,9 @@ static THREAD_MUTEX_T dmalloc_mutex;
  */
 static	void	lock_thread(void)
 {
-  /* we only lock if the lock-on counter has reached 0 */
-  if (thread_lock_c == 0) {
-#if HAVE_PTHREAD_MUTEX_LOCK
-    pthread_mutex_lock(&dmalloc_mutex);
-#endif
-  }
-}
-
-/*
- * mutex unlock the malloc library
- */
-static	void	unlock_thread(void)
-{
   /* if the lock-on counter has not reached 0 then count it down */
-  if (thread_lock_c > 0) {
-    thread_lock_c--;
+  if (thread_lock_init == 0) {
+    thread_lock_init++;
     /*
      * As we approach the time when we start mutex locking the
      * library, we need to init the mutex variable.  This sets how
@@ -211,7 +199,6 @@ static	void	unlock_thread(void)
      * taking in account that the init itself might generate a call
      * into the library.  Ugh.
      */
-    if (thread_lock_c == THREAD_INIT_LOCK) {
 #if HAVE_PTHREAD_MUTEX_INIT
       /*
        * NOTE: we do not use the PTHREAD_MUTEX_INITIALIZER since this
@@ -224,7 +211,22 @@ static	void	unlock_thread(void)
        */
       pthread_mutex_init(&dmalloc_mutex, THREAD_LOCK_INIT_VAL);
 #endif
-    }
+  }
+  /* we only lock if the lock-on counter has reached 0 */
+  if (thread_lock_c == 0) {
+#if HAVE_PTHREAD_MUTEX_LOCK
+    pthread_mutex_lock(&dmalloc_mutex);
+#endif
+  }
+}
+
+/*
+ * mutex unlock the malloc library
+ */
+static	void	unlock_thread(void)
+{
+  if (thread_lock_c > 0) {
+    thread_lock_c--;
   }
   else if (thread_lock_c == 0) {
 #if HAVE_PTHREAD_MUTEX_UNLOCK

>Unformatted: