[BUG] Renesas SH2A_FPU port: FPU context buffer leaked on task deletion (no `portCLEAN_UP_TCB`)
メンテナーはふだん 3 日以内に返信
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 25/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- c
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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()callsportCLEAN_UP_TCB( pxTCB )precisely so a port can free
task-specific memory. -
portmacro.hfor SH2A_FPU does not defineportCLEAN_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_STOREis currently defined inport.c; if the macro
lives inportmacro.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 ifconfigUSE_APPLICATION_TASK_TAG
lets users set the tag. A cleaner design stores the allocation base in a
port-specific TCB extension field instead of overloadingpxTaskTag.
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;
}
- 主要言語
- 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
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
shinyquagsire23/OpenJKDF2#540 ·
メンテナーはふだん 12 日以内に返信
-
Prison work hall: laundry and kitchen stations stand at bare walls, the hatch has no counter or signオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
Life-Framework/Life-Framework#5939 ·
メンテナーはふだん 1 日以内に返信
-
autoconf failes to find libsodium even when present due to implicit function declaration対応中かも @coderjoe が今日担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 13 日以内に返信
-
Bug Status: Needs Triage
難易度 1/5 1時間未満 初心者へのやさしさ 74/100
メンテナーはふだん 1 日以内に返信
-
bug python
難易度 2/5 1〜3時間 初心者へのやさしさ 87/100
Jstar269/nakagawa-recomp#3521 ·
メンテナーはふだん 1 日以内に返信