diff options
| author | kamil <kamil@NetBSD.org> | 2020-01-29 21:11:24 +0000 |
|---|---|---|
| committer | kamil <kamil@NetBSD.org> | 2020-01-29 21:11:24 +0000 |
| commit | 62659797ca12d8658e16e4449f40d03bf81d4b1e (patch) | |
| tree | 1be5aae899c712c93950349a3210b7e61f39e370 /lib/libpthread | |
| parent | bcd0d17a88cb02c6ce8de6c8d1463b19136459af (diff) | |
Use pthread_mutexattr_t and pthread_mutex_t magic fields
Validate _PT_MUTEX_MAGIC in pthread_mutex_t and _PT_MUTEXATTR_MAGIC
in pthread_mutexattr_t accordingly.
Diffstat (limited to 'lib/libpthread')
| -rw-r--r-- | lib/libpthread/pthread_mutex.c | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/lib/libpthread/pthread_mutex.c b/lib/libpthread/pthread_mutex.c index 7ba6639d815..634c161ddad 100644 --- a/lib/libpthread/pthread_mutex.c +++ b/lib/libpthread/pthread_mutex.c @@ -1,4 +1,4 @@ -/* $NetBSD: pthread_mutex.c,v 1.69 2020/01/29 10:55:23 kamil Exp $ */ +/* $NetBSD: pthread_mutex.c,v 1.70 2020/01/29 21:11:24 kamil Exp $ */ /*- * Copyright (c) 2001, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc. @@ -47,7 +47,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: pthread_mutex.c,v 1.69 2020/01/29 10:55:23 kamil Exp $"); +__RCSID("$NetBSD: pthread_mutex.c,v 1.70 2020/01/29 21:11:24 kamil Exp $"); #include <sys/types.h> #include <sys/lwpctl.h> @@ -197,6 +197,9 @@ pthread_mutex_lock(pthread_mutex_t *ptm) if (__predict_false(__uselibcstub)) return __libc_mutex_lock_stub(ptm); + pthread__error(EINVAL, "Invalid mutex", + ptm->ptm_magic == _PT_MUTEX_MAGIC); + self = pthread__self(); val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self); if (__predict_true(val == NULL)) { @@ -214,6 +217,9 @@ pthread_mutex_timedlock(pthread_mutex_t* ptm, const struct timespec *ts) pthread_t self; void *val; + pthread__error(EINVAL, "Invalid mutex", + ptm->ptm_magic == _PT_MUTEX_MAGIC); + self = pthread__self(); val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self); if (__predict_true(val == NULL)) { @@ -298,9 +304,6 @@ pthread__mutex_lock_slow(pthread_mutex_t *ptm, const struct timespec *ts) int serrno; int error; - pthread__error(EINVAL, "Invalid mutex", - ptm->ptm_magic == _PT_MUTEX_MAGIC); - owner = ptm->ptm_owner; self = pthread__self(); @@ -410,6 +413,9 @@ pthread_mutex_trylock(pthread_mutex_t *ptm) if (__predict_false(__uselibcstub)) return __libc_mutex_trylock_stub(ptm); + pthread__error(EINVAL, "Invalid mutex", + ptm->ptm_magic == _PT_MUTEX_MAGIC); + self = pthread__self(); val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self); if (__predict_true(val == NULL)) { @@ -450,6 +456,9 @@ pthread_mutex_unlock(pthread_mutex_t *ptm) if (__predict_false(__uselibcstub)) return __libc_mutex_unlock_stub(ptm); + pthread__error(EINVAL, "Invalid mutex", + ptm->ptm_magic == _PT_MUTEX_MAGIC); + #ifndef PTHREAD__ATOMIC_IS_MEMBAR membar_exit(); #endif @@ -468,9 +477,6 @@ pthread__mutex_unlock_slow(pthread_mutex_t *ptm) pthread_t self, owner, new; int weown, error; - pthread__error(EINVAL, "Invalid mutex", - ptm->ptm_magic == _PT_MUTEX_MAGIC); - self = pthread__self(); owner = ptm->ptm_owner; weown = (MUTEX_OWNER(owner) == (uintptr_t)self); @@ -725,6 +731,9 @@ pthread_mutexattr_getpshared(const pthread_mutexattr_t * __restrict attr, int * __restrict pshared) { + pthread__error(EINVAL, "Invalid mutex attribute", + attr->ptma_magic == _PT_MUTEXATTR_MAGIC); + *pshared = PTHREAD_PROCESS_PRIVATE; return 0; } @@ -733,6 +742,9 @@ int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared) { + pthread__error(EINVAL, "Invalid mutex attribute", + attr->ptma_magic == _PT_MUTEXATTR_MAGIC); + switch(pshared) { case PTHREAD_PROCESS_PRIVATE: return 0; @@ -772,6 +784,10 @@ pthread__mutex_deferwake(pthread_t self, pthread_mutex_t *ptm) int pthread_mutex_getprioceiling(const pthread_mutex_t *ptm, int *ceil) { + + pthread__error(EINVAL, "Invalid mutex", + ptm->ptm_magic == _PT_MUTEX_MAGIC); + *ceil = ptm->ptm_ceiling; return 0; } @@ -781,6 +797,9 @@ pthread_mutex_setprioceiling(pthread_mutex_t *ptm, int ceil, int *old_ceil) { int error; + pthread__error(EINVAL, "Invalid mutex", + ptm->ptm_magic == _PT_MUTEX_MAGIC); + error = pthread_mutex_lock(ptm); if (error == 0) { *old_ceil = ptm->ptm_ceiling; |
