Module Name:    src
Committed By:   uwe
Date:           Sun Dec 15 23:13:33 UTC 2019

Modified Files:
        src/lib/libpthread: pthread_rwlock.c

Log Message:
_DIAGASSERT that RW_FLAGMASK bits are not set in a thread pointer.

rwlock uses lower bits of a thread pointer for flags in the lock owner
field.  Assert that the pointer is properly aligned and those bits are
actually free to use.  This may not be the case when a program uses
its own allocator that can return less aligned pointers.


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/lib/libpthread/pthread_rwlock.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libpthread/pthread_rwlock.c
diff -u src/lib/libpthread/pthread_rwlock.c:1.34 src/lib/libpthread/pthread_rwlock.c:1.35
--- src/lib/libpthread/pthread_rwlock.c:1.34	Sun Jul  3 14:24:58 2016
+++ src/lib/libpthread/pthread_rwlock.c	Sun Dec 15 23:13:33 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_rwlock.c,v 1.34 2016/07/03 14:24:58 christos Exp $ */
+/*	$NetBSD: pthread_rwlock.c,v 1.35 2019/12/15 23:13:33 uwe Exp $ */
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,11 +30,12 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_rwlock.c,v 1.34 2016/07/03 14:24:58 christos Exp $");
+__RCSID("$NetBSD: pthread_rwlock.c,v 1.35 2019/12/15 23:13:33 uwe Exp $");
 
 #include <sys/types.h>
 #include <sys/lwpctl.h>
 
+#include <assert.h>
 #include <time.h>
 #include <errno.h>
 #include <stddef.h>
@@ -275,6 +276,7 @@ pthread__rwlock_wrlock(pthread_rwlock_t 
 	int error;
 
 	self = pthread__self();
+	_DIAGASSERT(((uintptr_t)self & RW_FLAGMASK) == 0);
 
 #ifdef ERRORCHECK
 	if (ptr->ptr_magic != _PT_RWLOCK_MAGIC)
@@ -373,6 +375,7 @@ pthread_rwlock_trywrlock(pthread_rwlock_
 #endif
 
 	self = pthread__self();
+	_DIAGASSERT(((uintptr_t)self & RW_FLAGMASK) == 0);
 
 	for (owner = (uintptr_t)ptr->ptr_owner;; owner = next) {
 		if (owner != 0)
@@ -509,6 +512,7 @@ pthread_rwlock_unlock(pthread_rwlock_t *
 		 */
 		self = pthread__self();
 		if ((thread = PTQ_FIRST(&ptr->ptr_wblocked)) != NULL) {
+			_DIAGASSERT(((uintptr_t)thread & RW_FLAGMASK) == 0);
 			new = (uintptr_t)thread | RW_WRITE_LOCKED;
 
 			if (PTQ_NEXT(thread, pt_sleep) != NULL)

Reply via email to