destroy() can run concurrently with unlock() as soon as unlock()
sets the mutex state to UNLOCKED. In particular, destroy() doesn't
have to wait for unlock() to return.
unlock()
{
PART A
SET STATE TO UNLOCKED
PART B <----------- this can run in parallel with destroy()
}
In the current implementation unlock() does two things:
1. Exchanges the mutex state with UNLOCKED.
2. If previous state was CONTENDED, signals the internal event.
2) can run concurrently with destroy(), so we have a potential
use-after-free. To overcome that, make destroy() wait on the event.
References:
1. https://lkml.org/lkml/2014/11/27/472
2. https://lwn.net/Articles/575460/
3. https://austingroupbugs.net/view.php?id=811
---
mingw-w64-libraries/winpthreads/src/mutex.c | 54 +++++++++++++++++++--
1 file changed, 51 insertions(+), 3 deletions(-)
diff --git a/mingw-w64-libraries/winpthreads/src/mutex.c
b/mingw-w64-libraries/winpthreads/src/mutex.c
index 29cda2f5..ef9d36c7 100644
--- a/mingw-w64-libraries/winpthreads/src/mutex.c
+++ b/mingw-w64-libraries/winpthreads/src/mutex.c
@@ -80,6 +80,12 @@
* - pthread_mutex_clocklock()
* - pthread_mutex_getprioceiling()
* - pthread_mutex_setprioceiling()
+ *
+ * Additional reads:
+ *
+ * [1] LKML - "POSIX mutex destruction requirements vs. futexes"
+ * -> https://lkml.org/lkml/2014/11/27/472
+ *
*/
/**
@@ -708,7 +714,11 @@ static int WinpthreadsNormalMutexInit (WinpthreadsMutex
**wMutex, const Winpthre
return ENOMEM;
}
- mutex->Event = CreateEventW (NULL, FALSE, FALSE, NULL);
+ /**
+ * Start in the signaled state to support destruction of mutexes that
+ * have never been contended. See `WinpthreadsNormalMutexDestroy`.
+ */
+ mutex->Event = CreateEventW (NULL, FALSE, TRUE, NULL);
/**
* The pthread_mutex_init() function shall fail if:
@@ -734,6 +744,11 @@ static void WinpthreadsNormalMutexDestroy
(WinpthreadsMutex *wMutex) {
HANDLE event = InterlockedExchangePointer ((void **) &mutex->Event, NULL);
if (event != NULL) {
+ /**
+ * `Destroy` can run concurrently with `Unlock`. Wait on the event
+ * to ensure that `Unlock` has finished working [1].
+ */
+ _pthread_wait_for_single_object (event, INFINITE);
CloseHandle (event);
}
@@ -772,6 +787,11 @@ static int WinpthreadsNormalMutexUnlock (WinpthreadsMutex
*wMutex) {
* waiting for `mutex->Event` to become signaled.
*/
if (InterlockedExchange (&mutex->LockState, Unlocked) == LockedWithBlocking)
{
+ /**
+ * Beware: anything that goes after the exchange to `Unlocked`
+ * can run concurrently with `Destroy` [1]. This is safe because
+ * `Destroy` waits on `mutex->Event`.
+ */
if (!SetEvent (mutex->Event)) {
return EINVAL;
}
@@ -815,7 +835,11 @@ static int WinpthreadsErrorCheckMutexInit
(WinpthreadsMutex **wMutex, const Winp
return ENOMEM;
}
- mutex->Event = CreateEventW (NULL, FALSE, FALSE, NULL);
+ /**
+ * Start in the signaled state to support destruction of mutexes that
+ * have never been contended. See `WinpthreadsErrorCheckMutexDestroy`.
+ */
+ mutex->Event = CreateEventW (NULL, FALSE, TRUE, NULL);
/**
* The pthread_mutex_init() function shall fail if:
@@ -843,6 +867,11 @@ static void WinpthreadsErrorCheckMutexDestroy
(WinpthreadsMutex *wMutex) {
HANDLE event = InterlockedExchangePointer ((void **) &mutex->Event, NULL);
if (event != NULL) {
+ /**
+ * `Destroy` can run concurrently with `Unlock`. Wait on the event
+ * to ensure that `Unlock` has finished working [1].
+ */
+ _pthread_wait_for_single_object (event, INFINITE);
CloseHandle (event);
}
@@ -904,6 +933,11 @@ static int WinpthreadsErrorCheckMutexUnlock
(WinpthreadsMutex *wMutex) {
* waiting for `mutex->Event` to become signaled.
*/
if (InterlockedExchange (&mutex->LockState, Unlocked) == LockedWithBlocking)
{
+ /**
+ * Beware: anything that goes after the exchange to `Unlocked`
+ * can run concurrently with `Destroy` [1]. This is safe because
+ * `Destroy` waits on `mutex->Event`.
+ */
if (!SetEvent (mutex->Event)) {
return EINVAL;
}
@@ -947,7 +981,11 @@ static int WinpthreadsRecursiveMutexInit (WinpthreadsMutex
**wMutex, const Winpt
return ENOMEM;
}
- mutex->Event = CreateEventW (NULL, FALSE, FALSE, NULL);
+ /**
+ * Start in the signaled state to support destruction of mutexes that
+ * have never been contended. See `WinpthreadsRecursiveMutexDestroy`.
+ */
+ mutex->Event = CreateEventW (NULL, FALSE, TRUE, NULL);
/**
* The pthread_mutex_init() function shall fail if:
@@ -976,6 +1014,11 @@ static void WinpthreadsRecursiveMutexDestroy
(WinpthreadsMutex *wMutex) {
HANDLE event = InterlockedExchangePointer ((void **) &mutex->Event, NULL);
if (event != NULL) {
+ /**
+ * `Destroy` can run concurrently with `Unlock`. Wait on the event
+ * to ensure that `Unlock` has finished working [1].
+ */
+ _pthread_wait_for_single_object (event, INFINITE);
CloseHandle (event);
}
@@ -1072,6 +1115,11 @@ static int WinpthreadsRecursiveMutexUnlock
(WinpthreadsMutex *wMutex) {
* waiting for `mutex->Event` to become signaled.
*/
if (InterlockedExchange (&mutex->LockState, Unlocked) == LockedWithBlocking)
{
+ /**
+ * Beware: anything that goes after the exchange to `Unlocked`
+ * can run concurrently with `Destroy` [1]. This is safe because
+ * `Destroy` waits on `mutex->Event`.
+ */
if (!SetEvent (mutex->Event)) {
return EINVAL;
}
--
2.55.0.windows.5
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public