POSIX port: the tick thread reads pxCurrentTCB while vTaskSwitchContext() writes it, and can signal a thread that is no longer running
I maintainer di solito rispondono entro 3 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 22/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- c, linux
- Ambito
- operating-systems
Direzione di ricerca
Start in portable/ThirdParty/GCC/Posix/port.c at prvTimerTickHandler() (around line 466), where the tick thread reads the current task through xTaskGetCurrentTaskHandle() and calls pthread_kill(); then check prvResumeThread() and prvPortYieldFromISR(). Reproduce with the attached ThreadSanitizer program, which should report a race on pxCurrentTCB between tasks.c:5273 and tasks.c:6654. Done means the race report disappears while the scheduler still ends cleanly; the pending-tick limitation is out of scope.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Describe the bug
The tick thread of the POSIX port chooses which thread to interrupt by reading the current task (portable/ThirdParty/GCC/Posix/port.c:466-467):
Thread_t * thread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
pthread_kill( thread->pthread, SIGALRM );
xTaskGetCurrentTaskHandle() reads pxCurrentTCB without a critical section. The comment in that function (tasks.c:6651-6653) says this is fine because it is "not called from an interrupt and the current TCB will always be the same for any individual execution thread". The tick thread is not the thread of the current task, so that does not hold here. The read races with the write in vTaskSwitchContext() (tasks.c:5273), which runs in the task that yields (prvPortYieldFromISR(), port.c:384). This is a data race, so undefined behaviour under C11.
It also has a visible effect. If the tick thread reads pxCurrentTCB just before a switch and calls pthread_kill() just after, SIGALRM goes to the thread that is switching out. That thread is about to suspend in prvSuspendSelf() with all signals blocked, so the tick waits on it until that task runs again, and the task that is actually running misses it.
#1543 mentions this race as out of scope. It is not the one from #1539, and #1543 doesn't change it.
Target
- Development board: none (POSIX simulator port)
- Instruction Set Architecture: x86_64
- IDE and version: none (command line)
- Toolchain and version: gcc 13.3.0,
-fsanitize=thread - Kernel: main at 83a45bf, heap_3
Host
- Host OS: Ubuntu Linux
- Version: 24.04
To Reproduce
Two tasks with the same priority keep calling taskYIELD() while the tick thread runs, and a third one ends the scheduler after 20 ticks. Files attached: main.c, FreeRTOSConfig.h, build_and_run.sh.
static void vWorker( void * pv ) { ( void ) pv; for( ;; ) { taskYIELD(); } }
static void vEnd( void * pv ) { ( void ) pv; vTaskDelay( 20 ); vTaskEndScheduler(); }
int main( void ) {
xTaskCreate( vWorker, "w1", configMINIMAL_STACK_SIZE, NULL, 1, NULL );
xTaskCreate( vWorker, "w2", configMINIMAL_STACK_SIZE, NULL, 1, NULL );
xTaskCreate( vEnd, "end", configMINIMAL_STACK_SIZE, NULL, 2, NULL );
vTaskStartScheduler();
return 0;
}
./build_and_run.sh <path-to-FreeRTOS-Kernel> builds with -fsanitize=thread and runs it 5 times. Each run takes about 1 s and exits normally. ThreadSanitizer reports the race in every run (5 of 5 here, 20 of 20 in a longer series):
WARNING: ThreadSanitizer: data race
Write of size 8 at 0x555555560040 by thread T3:
#0 vTaskSwitchContext tasks.c:5273
#1 prvPortYieldFromISR portable/ThirdParty/GCC/Posix/port.c:384
#2 vPortYield portable/ThirdParty/GCC/Posix/port.c:400
Previous read of size 8 at 0x555555560040 by thread T5:
#0 xTaskGetCurrentTaskHandle tasks.c:6654
#1 prvTimerTickHandler portable/ThirdParty/GCC/Posix/port.c:466
Location is global 'pxCurrentTCB' of size 8
Thread T5 'Scheduler timer' created by main thread at:
#1 prvSetupTimerInterrupt portable/ThirdParty/GCC/Posix/port.c:482
Full output in tsan_output.txt. The same run also shows the #1539 races and the locked mutex destroy reported separately.
Expected behavior
The tick thread should not read scheduler state that belongs to the running task. The port already knows which thread it resumes, so it can store it in prvResumeThread() and let the tick thread read that instead of pxCurrentTCB (proposed_fix.diff):
static Thread_t * pxRunningThread = NULL;
/* prvTimerTickHandler() */
Thread_t * thread = __atomic_load_n( &pxRunningThread, __ATOMIC_ACQUIRE );
if( thread != NULL )
{
pthread_kill( thread->pthread, SIGALRM );
}
/* prvResumeThread() */
__atomic_store_n( &pxRunningThread, xThreadId, __ATOMIC_RELEASE );
With this change the race is gone in 20 of 20 runs, the program still exits normally (with ThreadSanitizer and with -O2 without it), and no new reports show up. To be clear about the limits: this removes the data race, but a tick sent right before a thread switches out can still stay pending on that thread, since a pending signal belongs to one thread. Fixing that would need a different way of handling pending ticks, which I'm not proposing here. The diff is against main and sits next to the lines #1543 changes, so one of them may need a small rebase.
Additional context
We are master's students at the Federal University of Amazonas (UFAM), Brazil, working on verification of embedded software. We found this with SMART CPS, a tool we are developing on top of the ESBMC model checker. ESBMC checked the unmodified prvTimerTickHandler(), prvPortYieldFromISR(), prvSwitchThread(), prvResumeThread() and xTaskGetCurrentTaskHandle() with two threads (the tick thread and a task that yields), against the property "SIGALRM goes to the thread of the task in pxCurrentTCB, and that thread is not suspended". It found the interleaving described above, and its data race check also flags pxCurrentTCB. The tool then rebuilt the kernel with ThreadSanitizer and matched the report to the same variable. Happy to test other approaches if you prefer a different fix.
- Lingua principale
- C
- Stelle
- 4.6k
- Fork
- 1.6k
- Merge medio
- 2g 22h
- PR unite (30g)
- 4
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di FreeRTOS/FreeRTOS-Kernel
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
FreeRTOS/FreeRTOS-Kernel#1542 ·
I maintainer di solito rispondono entro 3 giorni
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
FreeRTOS/FreeRTOS-Kernel#1520 ·
I maintainer di solito rispondono entro 3 giorni
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
FreeRTOS/FreeRTOS-Kernel#1494 · 3 commenti ·
I maintainer di solito rispondono entro 3 giorni
-
enhancement
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
FreeRTOS/FreeRTOS-Kernel#1554 ·
I maintainer di solito rispondono entro 3 giorni
-
[BUG] event_wait_timed can't return falseForse già presa @TedLyngmo l’ha presa 39 giorni fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 38/100
FreeRTOS/FreeRTOS-Kernel#1546 ·
I maintainer di solito rispondono entro 3 giorni
Tutte le issue di FreeRTOS/FreeRTOS-Kernel
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 63/100
FreeRTOS/FreeRTOS-Plus-TCP#1395 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
qoretechnologies/qore#8222 ·
I maintainer di solito rispondono entro 1 giorno
-
c easy first-contributor first-contributors good first issue help wanted up-for-grabs
Difficoltà 1/5 1-3 ore Idoneità per principianti 96/100
fork-commit-merge/fork-commit-merge#8696 ·
I maintainer di solito rispondono entro 1 giorno