Repository navigation
Description
Describe the bug
OS_BSP_Unlock_Impl() can restore the cancel state of a different thread.
OS_BSP_Lock_Impl() disables cancellation while a thread holds the BSP lock. It saves the previous cancel state in one global variable, AccessCancelState. OS_BSP_Unlock_Impl() restores this state. But it reads AccessCancelState after it releases the mutex:
status = pthread_mutex_unlock(&OS_BSP_GenericLinuxGlobal.AccessMutex);
...
pthread_setcancelstate(OS_BSP_GenericLinuxGlobal.AccessCancelState, NULL); /* read after unlock */After the mutex is free, a different thread can call OS_BSP_Lock_Impl() and write its own state to AccessCancelState. The first thread then restores the wrong state.
To Reproduce
sequenceDiagram
participant A as Thread A
participant S as Shared saved state
participant B as Thread B
Note over A: Initially: cancellation ENABLED
Note over B: Initially: cancellation DISABLED
A->>A: Lock mutex
A->>S: Save ENABLED
A->>A: Disable cancellation and do work
A->>A: Unlock mutex
Note over A,B: A possible thread switch happens here
B->>B: Lock mutex
B->>S: Save DISABLED — overwrites A's value
S-->>A: Read DISABLED
Note over A: BUG: restores DISABLED<br/>Expected: ENABLED
- Thread A has cancellation enabled. It calls
OS_BSP_Lock_Impl(). It locks the mutex, savesENABLEDinAccessCancelStateand disables cancellation. - Thread A calls
OS_BSP_Unlock_Impl(). It releases the mutex. - Thread B has cancellation disabled. It calls
OS_BSP_Lock_Impl()before thread A readsAccessCancelState. It savesDISABLEDinAccessCancelState. This replaces the value of thread A. - Thread A reads
AccessCancelStateand restoresDISABLED. The correct state for thread A isENABLED.
The program below shows the problem. It uses a copy of the two functions. Two threads with different cancel states lock and unlock in a loop. Each thread counts the number of times that it gets the wrong state.
Reproducer (build with gcc -O2 repro.c -lpthread)
/* Same logic as OS_BSP_Lock_Impl / OS_BSP_Unlock_Impl in src/bsp/generic-linux/src/bsp_start.c */
#include <pthread.h>
#include <stdio.h>
static pthread_mutex_t AccessMutex = PTHREAD_MUTEX_INITIALIZER;
static int AccessCancelState;
static volatile int done;
static void Lock(void)
{
pthread_mutex_lock(&AccessMutex);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &AccessCancelState);
}
static void Unlock(void)
{
pthread_mutex_unlock(&AccessMutex);
pthread_setcancelstate(AccessCancelState, NULL); /* read after unlock */
}
/* Each thread keeps its own cancel state and checks what Unlock() restored */
static void *worker(void *arg)
{
int mine = *(int *)arg, restored, wrong = 0, n = 0;
pthread_setcancelstate(mine, NULL);
while (!done && n < 1000000)
{
Lock();
Unlock();
pthread_setcancelstate(mine, &restored); /* read back, then reset */
wrong += (restored != mine);
++n;
}
done = 1;
printf("thread with %s: wrong state restored %d / %d times\n",
mine == PTHREAD_CANCEL_ENABLE ? "ENABLE " : "DISABLE", wrong, n);
return NULL;
}
int main(void)
{
int s[2] = {PTHREAD_CANCEL_ENABLE, PTHREAD_CANCEL_DISABLE};
pthread_t t[2];
for (int i = 0; i < 2; ++i) pthread_create(&t[i], NULL, worker, &s[i]);
for (int i = 0; i < 2; ++i) pthread_join(t[i], NULL);
return 0;
}Sample output (Ubuntu 22.04, glibc 2.35):
thread with ENABLE : wrong state restored 5583 / 952696 times
thread with DISABLE: wrong state restored 5038 / 1000000 times
Expected behavior
Each thread restores its own previous cancel state. To do this, copy AccessCancelState to a local variable before the call to pthread_mutex_unlock().
Code snips
osal/src/bsp/generic-linux/src/bsp_start.c
Lines 126 to 140 in 2a96a4f
| void OS_BSP_Unlock_Impl(void) | |
| { | |
| int status; | |
| status = pthread_mutex_unlock(&OS_BSP_GenericLinuxGlobal.AccessMutex); | |
| if (status < 0) | |
| { | |
| BSP_DEBUG("pthread_mutex_unlock: %s\n", strerror(status)); | |
| } | |
| else | |
| { | |
| /* Restore previous cancellability state */ | |
| pthread_setcancelstate(OS_BSP_GenericLinuxGlobal.AccessCancelState, NULL); | |
| } | |
| } |
The generic-qnx and generic-vxworks-rtp BSPs have the same code:
osal/src/bsp/generic-qnx/src/bsp_start.c
Lines 96 to 110 in 2a96a4f
void OS_BSP_Unlock_Impl(void) { int status; status = pthread_mutex_unlock(&OS_BSP_GenericQnxGlobal.AccessMutex); if (status < 0) { BSP_DEBUG("pthread_mutex_unlock: %s\n", strerror(status)); } else { /* Restore previous cancellability state */ pthread_setcancelstate(OS_BSP_GenericQnxGlobal.AccessCancelState, NULL); } } osal/src/bsp/generic-vxworks-rtp/src/bsp_start.c
Lines 97 to 111 in 2a96a4f
void OS_BSP_Unlock_Impl(void) { int status; status = pthread_mutex_unlock(&OS_BSP_GenericVxWorksRtpGlobal.AccessMutex); if (status < 0) { BSP_DEBUG("pthread_mutex_unlock: %s\n", strerror(status)); } else { /* Restore previous cancellability state */ pthread_setcancelstate(OS_BSP_GenericVxWorksRtpGlobal.AccessCancelState, NULL); } }
System observed on:
Ubuntu 22.04, OSAL dev (2a96a4f)
Reporter Info
Jiahao Shi, Purdue University
Activity