POSIX port: the tick thread reads pxCurrentTCB while vTaskSwitchContext() writes it, and can signal a thread that is no longer running
メンテナーはふだん 3 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 22/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- c, linux
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- C
- スター
- 4.6k
- フォーク
- 1.6k
- 平均マージ
- 2日 22時間
- マージ済み PR(30日)
- 4
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
FreeRTOS/FreeRTOS-Kernel のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
FreeRTOS/FreeRTOS-Kernel#1542 ·
メンテナーはふだん 3 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
FreeRTOS/FreeRTOS-Kernel#1520 ·
メンテナーはふだん 3 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
FreeRTOS/FreeRTOS-Kernel#1494 · コメント 3 件 ·
メンテナーはふだん 3 日以内に返信
-
enhancement
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
FreeRTOS/FreeRTOS-Kernel#1554 ·
メンテナーはふだん 3 日以内に返信
-
[BUG] event_wait_timed can't return false対応中かも @TedLyngmo が 39 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 38/100
FreeRTOS/FreeRTOS-Kernel#1546 ·
メンテナーはふだん 3 日以内に返信
FreeRTOS/FreeRTOS-Kernel の issue をすべて見る
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 63/100
FreeRTOS/FreeRTOS-Plus-TCP#1395 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
qoretechnologies/qore#8222 ·
メンテナーはふだん 1 日以内に返信
-
c easy first-contributor first-contributors good first issue help wanted up-for-grabs
難易度 1/5 1〜3時間 初心者へのやさしさ 96/100
fork-commit-merge/fork-commit-merge#8696 ·
メンテナーはふだん 1 日以内に返信