[BUG] POSIX port: event_delete() destroys a mutex left locked by a thread cancelled inside event_wait()
Maintainer thường phản hồi trong vòng 3 ngày
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- Nửa ngày
- Mức phù hợp với người mới
- 66/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- c
- Lĩnh vực
- operating-systems
Hướng nghiên cứu
Start in portable/ThirdParty/GCC/Posix/utils/wait_for_event.c, where event_wait() blocks in pthread_cond_wait() and event_delete() destroys the mutex, then read vPortCancelThread() in portable/ThirdParty/GCC/Posix/port.c around lines 537-541. Run build_and_run.sh from the attached reproducer with -fsanitize=thread to see the 'destroy of a locked mutex' report. Done when the mutex is released on cancellation (for example with a cleanup handler), the ThreadSanitizer report no longer appears across repeated runs, and the program exits normally.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Describe the bug
When a task is deleted, vPortCancelThread() (portable/ThirdParty/GCC/Posix/port.c:537-541) does:
pthread_cancel( pxThreadToCancel->pthread );
event_signal( pxThreadToCancel->ev );
pthread_join( pxThreadToCancel->pthread, NULL );
event_delete( pxThreadToCancel->ev );
At that point the thread being cancelled is suspended in prvSuspendSelf() -> event_wait(), blocked in pthread_cond_wait() (utils/wait_for_event.c:84). pthread_cond_wait() is a cancellation point, and POSIX says the mutex is re-acquired before the thread acts on the cancellation. event_wait() has no cleanup handler, so the thread exits with ev->mutex still locked. Then event_delete() calls pthread_mutex_destroy() on that locked mutex (utils/wait_for_event.c:65). That is undefined behaviour in POSIX. glibc returns EBUSY, the return value is ignored, and the memory is freed right after.
I think this is what is left of #1217. The robust mutex fixed the deadlock when another thread locks the mutex later, but the cancelled thread still leaves it locked and it is still destroyed in that state.
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
One task that ends the scheduler. The scheduler then deletes the remaining tasks (including idle) through vPortCancelThread(). Files attached: main.c, FreeRTOSConfig.h, build_and_run.sh.
static void vTask( void * pv ) { ( void ) pv; vTaskEndScheduler(); }
int main( void ) {
xTaskCreate( vTask, "end", configMINIMAL_STACK_SIZE, NULL, 1, NULL );
vTaskStartScheduler();
return 0;
}
./build_and_run.sh <path-to-FreeRTOS-Kernel> builds with -fsanitize=thread and runs it 5 times. ThreadSanitizer reports this in 5 of 5 runs:
WARNING: ThreadSanitizer: destroy of a locked mutex
#1 event_delete portable/ThirdParty/GCC/Posix/utils/wait_for_event.c:65
#2 vPortCancelThread portable/ThirdParty/GCC/Posix/port.c:541
#3 prvDeleteTCB tasks.c:6573
#4 vTaskDelete tasks.c:2335
#5 vTaskEndScheduler tasks.c:3865
and:
#1 event_wait portable/ThirdParty/GCC/Posix/utils/wait_for_event.c:84
#2 prvSuspendSelf portable/ThirdParty/GCC/Posix/port.c:619
Mutex M0 created at:
#1 event_create portable/ThirdParty/GCC/Posix/utils/wait_for_event.c:55
...
#6 prvCreateIdleTasks tasks.c:3702
Full output in tsan_output.txt. The same run also shows the races from #1539.
Expected behavior
The mutex should be unlocked before it is destroyed. One way is a cleanup handler in event_wait(), so the mutex is released if the thread is cancelled while waiting:
static void prvUnlockMutex( void * pvMutex )
{
pthread_mutex_unlock( ( pthread_mutex_t * ) pvMutex );
}
bool event_wait( struct event * ev )
{
/* ... lock as today ... */
pthread_cleanup_push( prvUnlockMutex, &ev->mutex );
while( ev->event_triggered == false )
{
pthread_cond_wait( &ev->cond, &ev->mutex );
}
ev->event_triggered = false;
pthread_cleanup_pop( 1 ); /* unlocks, also on cancellation */
return true;
}
With this change (proposed_fix.diff) the same program runs 5 of 5 times without the report and still exits normally.
Additional context
This is not related to #1525, which changes event_wait_timed(). The path here is event_wait(), called from prvSuspendSelf().
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 wait_for_event.c in the vPortCancelThread() sequence against the POSIX precondition of pthread_mutex_destroy(), and the tool then rebuilt the kernel with ThreadSanitizer to confirm it on the real code. Happy to adjust the reproducer or test a different fix if that helps.
- Ngôn ngữ chính
- C
- Star
- 4.6k
- Fork
- 1.6k
- Merge trung bình
- 2 ngày 22 giờ
- Pull request đã merge (30 ngày)
- 4
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của FreeRTOS/FreeRTOS-Kernel
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
FreeRTOS/FreeRTOS-Kernel#1542 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
[BUG] Release asset FreeRTOS-KernelV11.3.2.zip has the files of main, not of the tag V11.3.2Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
FreeRTOS/FreeRTOS-Kernel#1520 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
FreeRTOS/FreeRTOS-Kernel#1494 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 3 ngày
-
enhancement
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
FreeRTOS/FreeRTOS-Kernel#1554 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
[BUG] event_wait_timed can't return falseCó thể đã có người làm @TedLyngmo đã nhận 39 ngày trước. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 38/100
FreeRTOS/FreeRTOS-Kernel#1546 ·
Maintainer thường phản hồi trong vòng 3 ngày
Tất cả issue của FreeRTOS/FreeRTOS-Kernel
Issue tương tự
-
RecompLauncher supportĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
shinyquagsire23/OpenJKDF2#540 ·
Maintainer thường phản hồi trong vòng 12 ngày
-
Prison work hall: laundry and kitchen stations stand at bare walls, the hatch has no counter or signĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Life-Framework/Life-Framework#5939 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
autoconf failes to find libsodium even when present due to implicit function declarationCó thể đã có người làm @coderjoe đã nhận hôm nay. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 13 ngày
-
SessionInterfaceHandler is throwing a deprecation notice as a warning instead of a deprecationĐang mởBug Status: Needs Triage
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement_package: a site operand of another JSON type raises TypeError, not a named refusalĐang mởbug python
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 87/100
Jstar269/nakagawa-recomp#3521 ·
Maintainer thường phản hồi trong vòng 1 ngày