[BUG] Renesas SH2A_FPU port: FPU context buffer leaked on task deletion (no `portCLEAN_UP_TCB`)

Đang mở
#1,443 1 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

@Old-Ding đang làm issue này rồi.

Từ ngày 4/7/2026.

  • #1444 của @Old-Ding — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
c
Lĩnh vực
operating-systems

Hướng nghiên cứu

Start in portable/Renesas/SH2A_FPU/port.c at xPortUsesFloatingPoint() and in portmacro.h where traceTASK_SWITCHED_OUT and traceTASK_SWITCHED_IN use pxTaskTag. Decide whether the buffer should be freed through a portCLEAN_UP_TCB definition or a port-specific TCB field, and check the open pull request #1444 before starting. Done means deleting a task that called the function frees its buffer, and a second call does not leak.

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

Mô tả

bug

In portable/Renesas/SH2A_FPU/port.c, xPortUsesFloatingPoint() dynamically
allocates a 72-byte FPU register save buffer and hands ownership to the task by
storing a pointer to it in the task's application tag (pxTaskTag). The port
never defines portCLEAN_UP_TCB, so when a task that uses the FPU is deleted the
buffer is never freed and becomes unreachable. Calling the function twice on the
same task also leaks the first buffer immediately.

Affected code

portable/Renesas/SH2A_FPU/port.c, xPortUsesFloatingPoint():

#define portFLOP_REGISTERS_TO_STORE    ( 18 )
#define portFLOP_STORAGE_SIZE          ( portFLOP_REGISTERS_TO_STORE * 4 )   /* 72 bytes */

    /* Allocate a buffer large enough to hold all the flop registers. */
    pulFlopBuffer = ( uint32_t * ) pvPortMalloc( portFLOP_STORAGE_SIZE );

    if( pulFlopBuffer != NULL )
    {
        memset( ( void * ) pulFlopBuffer, 0x00, portFLOP_STORAGE_SIZE );
        *pulFlopBuffer = get_fpscr();

        /* Use the task tag to point to the flop buffer.  Pass pointer to just
         * above the buffer because the flop save routine uses a pre-decrement. */
        vTaskSetApplicationTaskTag( xTask,
            ( void * ) ( pulFlopBuffer + portFLOP_REGISTERS_TO_STORE ) );
        xReturn = pdPASS;
    }
    else
    {
        xReturn = pdFAIL;
    }

    return xReturn;

The tag stores a pointer to just past the end of the buffer (the assembly save
routine pre-decrements), so the only reference to the original allocation base is
pxTaskTag - portFLOP_REGISTERS_TO_STORE.

portable/Renesas/SH2A_FPU/portmacro.h uses pxTaskTag exclusively for the FPU
context:

#define traceTASK_SWITCHED_OUT()  do { if( pxCurrentTCB->pxTaskTag != NULL ) vPortSaveFlopRegisters( pxCurrentTCB->pxTaskTag ); } while( 0 )
#define traceTASK_SWITCHED_IN()   do { if( pxCurrentTCB->pxTaskTag != NULL ) vPortRestoreFlopRegisters( pxCurrentTCB->pxTaskTag ); } while( 0 )

Problem

The buffer is not a leak on successful return — ownership legitimately escapes to
the task. But that transfer requires a matching destructor when the task is
deleted, and none exists:

  • prvDeleteTCB() calls portCLEAN_UP_TCB( pxTCB ) precisely so a port can free
    task-specific memory.

  • portmacro.h for SH2A_FPU does not define portCLEAN_UP_TCB, so the
    kernel default is used (include/FreeRTOS.h):

    #ifndef portCLEAN_UP_TCB
        #define portCLEAN_UP_TCB( pxTCB )    ( void ) ( pxTCB )
    #endif
    
  • The kernel does not free the application task tag as generic memory (in other
    ports it may hold a user callback).

Result: deleting any task that called xPortUsesFloatingPoint() leaks 72 bytes.

Secondary: repeated calls leak immediately

xPortUsesFloatingPoint() does not check whether the task already has an FPU
buffer. A second call allocates a new buffer, overwrites pxTaskTag, and drops
the only pointer to the previous allocation — no task deletion required.

Trigger condition

  • Target is portable/Renesas/SH2A_FPU.
  • Dynamic allocation is enabled.
  • A task calls xPortUsesFloatingPoint().
  • The task is deleted (or the function is called twice).

Each occurrence leaks 72 bytes. In a system that repeatedly creates, FPU-enables,
runs and deletes worker tasks, the leak accumulates steadily and can eventually
cause pvPortMalloc() to fail. Embedded systems have no process-exit reclamation,
so the loss persists until reboot.

Suggested fix

Define portCLEAN_UP_TCB for this port, recovering the base pointer from the
stored end pointer:

#define portCLEAN_UP_TCB( pxTCB )                                    \
    do                                                               \
    {                                                                \
        if( ( pxTCB )->pxTaskTag != NULL )                           \
        {                                                            \
            uint32_t * pulBufferEnd = ( uint32_t * ) ( pxTCB )->pxTaskTag; \
            vPortFree( pulBufferEnd - portFLOP_REGISTERS_TO_STORE );  \
            ( pxTCB )->pxTaskTag = NULL;                             \
        }                                                            \
    } while( 0 )

Implementation notes:

  • portFLOP_REGISTERS_TO_STORE is currently defined in port.c; if the macro
    lives in portmacro.h, the constant must be shared/moved.
  • The cleanup must only free tags created by this port's FPU init. Reusing the
    application task tag makes this ambiguous if configUSE_APPLICATION_TASK_TAG
    lets users set the tag. A cleaner design stores the allocation base in a
    port-specific TCB extension field instead of overloading pxTaskTag.

For the repeated-call issue, guard the allocation (only safe if the tag is known
to be port-owned):

    if( xTaskGetApplicationTaskTag( xTask ) != NULL )
    {
        return pdPASS;
    }
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.