[BUG] POSIX port: data races on xSchedulerEnd and xTimerTickThreadShouldRun reported by ThreadSanitizer

Đang mở
#1,539 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 3 ngày

@K-S-Manikandan đang làm issue này rồi.

Từ ngày 21/11/2026.

  • #1543 của @K-S-Manikandan — đang mở

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/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, linux
Lĩnh vực
operating-systems

Hướng nghiên cứu

Start in portable/ThirdParty/GCC/Posix/port.c: the racing accesses are xSchedulerEnd (read in xPortStartScheduler near line 295, written in vPortEndScheduler near line 340) and xTimerTickThreadShouldRun (written near line 331, read in prvTimerTickHandler near line 460). Reproduce with the attached build_and_run.sh, which builds the kernel with -fsanitize=thread. Done means the ThreadSanitizer data-race warnings no longer appear across repeated runs, and the program still exits normally.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug

Describe the bug
In portable/ThirdParty/GCC/Posix/port.c, xSchedulerEnd and xTimerTickThreadShouldRun are plain
(non-atomic, non-volatile) globals that are written by one thread and read by another without
synchronization:

  • xSchedulerEnd is read by the main thread in xPortStartScheduler() (while( xSchedulerEnd != pdTRUE ),
    line 295) and written by a task thread in vPortEndScheduler() (line 340).
  • xTimerTickThreadShouldRun is read by the tick thread in prvTimerTickHandler() (line 460) and
    written in vPortEndScheduler() (line 331).

Under the C11 memory model these are data races (undefined behaviour). ThreadSanitizer reports both.

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 ca75944 (also reproduced on V11.3.2, 640d0c4), heap_3

Host

  • Host OS: Ubuntu Linux
  • Version: 24.04

To Reproduce
A single task that ends the scheduler is enough (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 with -fsanitize=thread -pthread together with tasks.c list.c queue.c timers.c port.c wait_for_event.c heap_3.c and run with setarch $(uname -m) -R ./repro_tsan
(./build_and_run.sh <path-to-FreeRTOS-Kernel> does both and runs 5 times).
Reproduced in 5 of 5 runs:

WARNING: ThreadSanitizer: data race
  Write of size 8 by thread T1:
    #0 vPortEndScheduler portable/ThirdParty/GCC/Posix/port.c:340
    #1 vTaskEndScheduler tasks.c:3884
  Previous read of size 8 by main thread:
    #0 xPortStartScheduler portable/ThirdParty/GCC/Posix/port.c:295
    #1 vTaskStartScheduler tasks.c:3818
  Location is global 'xSchedulerEnd'

The full output is in the attached tsan_output.txt (it also contains the report for
xTimerTickThreadShouldRun, port.c:331 x port.c:460).

Expected behavior
No data race. Possible fix: make both variables atomic (e.g. __atomic_load_n/__atomic_store_n,
or volatile sig_atomic_t for the flag polled around sigwait).

Additional context
We are aware that ThreadSanitizer does not model pthread_kill/sigwait as synchronization; however,
the first read of the loop happens before the signal is sent, so the accesses are concurrent. We did
not observe a functional failure without the sanitizer (the program ends normally).
Found with SMART CPS, a verification tool based on the ESBMC bounded model checker
(--data-races-check on the port), which also generated the reproducer: it rebuilds the unmodified
kernel sources with ThreadSanitizer, runs them 5 times and matches the reported race against the
variable in the counterexample.

freertos_posix_xschedulerend_race_repro.zip

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

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của FreeRTOS/FreeRTOS-Kernel

Tất cả issue của FreeRTOS/FreeRTOS-Kernel

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.