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 | 84 ++++++++++++++++++---
 1 file changed, 75 insertions(+), 9 deletions(-)

diff --git a/mingw-w64-libraries/winpthreads/src/mutex.c 
b/mingw-w64-libraries/winpthreads/src/mutex.c
index 29cda2f5..2f368937 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 with 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].
+     */
+    WaitForSingleObject (event, INFINITE);
     CloseHandle (event);
   }
 
@@ -767,11 +782,22 @@ static int WinpthreadsNormalMutexTryLock 
(WinpthreadsMutex *wMutex, BOOL exclusi
 static int WinpthreadsNormalMutexUnlock (WinpthreadsMutex *wMutex) {
   WinpthreadsNormalMutex *mutex = &wMutex->NormalMutex;
 
+  LONG previousState;
+
+  previousState = InterlockedExchange (&mutex->LockState, Unlocked);
+
   /**
-   * If `mutex->LockState` is `LockedWithBlocking`, then some other thread is
+   * Please, note that code below can run concurrently with `Destroy` [1].
+   */
+
+  /**
+   * If `mutex->LockState` was `LockedWithBlocking`, then some other thread is
    * waiting for `mutex->Event` to become signaled.
    */
-  if (InterlockedExchange (&mutex->LockState, Unlocked) == LockedWithBlocking) 
{
+  if (previousState == LockedWithBlocking) {
+    /**
+     * This is safe because `Destroy` first waits on `mutex->Event`.
+     */
     if (!SetEvent (mutex->Event)) {
       return EINVAL;
     }
@@ -815,7 +841,11 @@ static int WinpthreadsErrorCheckMutexInit 
(WinpthreadsMutex **wMutex, const Winp
     return ENOMEM;
   }
 
-  mutex->Event = CreateEventW (NULL, FALSE, FALSE, NULL);
+  /**
+   * Start with 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 +873,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].
+     */
+    WaitForSingleObject (event, INFINITE);
     CloseHandle (event);
   }
 
@@ -895,15 +930,26 @@ static int WinpthreadsErrorCheckMutexUnlock 
(WinpthreadsMutex *wMutex) {
 
   LONG threadId = (LONG) GetCurrentThreadId ();
 
+  LONG previousState;
+
   if (InterlockedCompareExchange ((LONG *) &mutex->Owner, (LONG) 
THREAD_ID_NO_OWNER, threadId) != threadId) {
     return EPERM;
   }
 
+  previousState = InterlockedExchange (&mutex->LockState, Unlocked);
+
+  /**
+   * Please, note that code below can run concurrently with `Destroy` [1].
+   */
+
   /**
-   * If `mutex->LockState` is `LockedWithBlocking`, then some other thread is
+   * If `mutex->LockState` was `LockedWithBlocking`, then some other thread is
    * waiting for `mutex->Event` to become signaled.
    */
-  if (InterlockedExchange (&mutex->LockState, Unlocked) == LockedWithBlocking) 
{
+  if (previousState == LockedWithBlocking) {
+    /**
+     * This is safe because `Destroy` first waits on `mutex->Event`.
+     */
     if (!SetEvent (mutex->Event)) {
       return EINVAL;
     }
@@ -947,7 +993,11 @@ static int WinpthreadsRecursiveMutexInit (WinpthreadsMutex 
**wMutex, const Winpt
     return ENOMEM;
   }
 
-  mutex->Event = CreateEventW (NULL, FALSE, FALSE, NULL);
+  /**
+   * Start with 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 +1026,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].
+     */
+    WaitForSingleObject (event, INFINITE);
     CloseHandle (event);
   }
 
@@ -1055,6 +1110,8 @@ static int WinpthreadsRecursiveMutexUnlock 
(WinpthreadsMutex *wMutex) {
 
   DWORD threadId = GetCurrentThreadId ();
 
+  LONG previousState;
+
   if (mutex->Owner != threadId) {
     return EPERM;
   }
@@ -1067,11 +1124,20 @@ static int WinpthreadsRecursiveMutexUnlock 
(WinpthreadsMutex *wMutex) {
 
   mutex->Owner = THREAD_ID_NO_OWNER;
 
+  previousState = InterlockedExchange (&mutex->LockState, Unlocked);
+
   /**
-   * If `mutex->LockState` is `LockedWithBlocking`, then some other thread is
+   * Please, note that code below can run concurrently with `Destroy` [1].
+   */
+
+  /**
+   * If `mutex->LockState` was `LockedWithBlocking`, then some other thread is
    * waiting for `mutex->Event` to become signaled.
    */
-  if (InterlockedExchange (&mutex->LockState, Unlocked) == LockedWithBlocking) 
{
+  if (previousState == LockedWithBlocking) {
+    /**
+     * This is safe because `Destroy` first 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

Reply via email to