Hard fault: NULL-pointer dereference in `teseo_rx_callback_error()` (UART mode)
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 76/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 静か
- 技術スタック
- c
- 領域
- embedded-iot
調査の方向性
Drivers/BSP/Components/teseo_liv3f/teseo_liv3f_uart.c から始め、teseo_rx_callback_error()、teseo_uart_rx_onoff()、teseo_rx_callback_ok() を読んで、wr_msg が NULL になる FSM 状態を確認します。ハードウェアが利用可能なら、破棄モード中に UART エラーパスを実行します。完了条件は、オーバーランで fault が発生しなくなり、FSM が次の '$' で再同期することです。
索引モデルが issue の本文から書いたものです。
説明
Summary
teseo_rx_callback_error() in Drivers/BSP/Components/teseo_liv3f/teseo_liv3f_uart.c
unconditionally dereferences Teseo_UART_Data.wr_msg:
static void teseo_rx_callback_error(void)
{
const TESEO_LIV3F_ctx_t *pCtx = Teseo_UART_Data.pCtx;
PRINT_DBG("E");
Teseo_UART_Data.fsm_next_state = fsm_discard;
Teseo_UART_Data.wr_msg->len = 0; /* <-- wr_msg may be NULL */
pCtx->ClearOREF(pCtx->Handle);
//__HAL_UART_CLEAR_FLAG(teseo_io_data->huart, UART_FLAG_ORE);
pCtx->Receive(pCtx->Handle, &Teseo_UART_Data.dummy_char, 1);
}
However, the driver's own FSM sets wr_msg to NULL in two places in the same file:
teseo_uart_rx_onoff()— before reception is (re)started:Teseo_UART_Data.wr_msg = NULL; Teseo_UART_Data.fsm_state = fsm_discard; Teseo_UART_Data.fsm_next_state = fsm_discard;teseo_rx_callback_ok()— after every completed NMEA sentence
(fsm_synchcase, on receiving the terminating'$'):teseo_queue_release_wr_buffer(pCtx->pQueue, Teseo_UART_Data.wr_msg); Teseo_UART_Data.wr_msg = NULL; Teseo_UART_Data.fsm_next_state = fsm_discard;
wr_msg is only assigned again once the FSM, in discard mode, receives the next
'$' and claims a buffer (teseo_queue_claim_wr_buffer).
Consequently, any UART error (e.g. overrun) that occurs while the FSM is in
discard mode — i.e. before the first sentence sync, and in the window between
every two sentences — executes Teseo_UART_Data.wr_msg->len = 0; with
wr_msg == NULL. Because the callback runs in UART interrupt context, this is a
write to address 0x00000004 from an ISR, which raises a BusFault that
escalates to a HardFault.
Environment
- Package: X-CUBE-GNSS1,
Drivers/BSP/Components/teseo_liv3f/teseo_liv3f_uart.c
(verified identical on currentmain:
https://raw.githubusercontent.com/STMicroelectronics/x-cube-gnss1/main/Drivers/BSP/Components/teseo_liv3f/teseo_liv3f_uart.c) - Interface: UART mode (Teseo-LIV3F on USART, 1-byte interrupt-driven reception)
- Host MCU: STM32H562VGT6, Azure RTOS ThreadX application (issue is
RTOS-independent — any latency source suffices)
Reproduction / trigger conditions
The error callback fires on UART overrun. With the driver's 1-byte-at-a-time
HAL_UART_Receive_IT scheme, an overrun happens whenever interrupt latency
exceeds ~2 byte times. In a minimal example this is rare, which is presumably
why the bug goes unnoticed; in a realistic application (SDMMC writes, other
ISRs, RTOS critical sections) it is easy to hit.
Deterministic scenario observed on our hardware:
- Cold power-on with the Teseo-LIV3F powered from the same rail as the MCU.
- The Teseo boots in parallel and immediately transmits its boot output while
the host is still initializing peripherals (elevated interrupt latency). - An overrun occurs while the FSM is still in discard mode (
wr_msg == NULL,
as set byteseo_uart_rx_onoff). - Hard fault.
A warm MCU reset (Teseo already running and streaming steadily) rarely
triggers it, because the FSM syncs within a few bytes and errors tend to land
while a buffer is claimed — which makes the failure look intermittent and
power-cycle-related.
Evidence
Disassembly of the faulting build (arm-none-eabi-gcc, -O0):
156 Teseo_UART_Data.wr_msg->len = 0;
0800da7e: ldr r3, [pc, #40] ; r3 = &Teseo_UART_Data
0800da80: ldr r3, [r3, #4] ; r3 = Teseo_UART_Data.wr_msg (== NULL)
0800da82: movs r2, #0
0800da84: strh r2, [r3, #4] ; write to 0x00000004 -> BusFault -> HardFault
The stacked PC in the hard fault handler points at the strh (0x0800DA84).
Suggested fix
Guard the dereference (and, defensively, pCtx). Recovery behavior is
unchanged: the FSM is reset to discard mode either way, and
teseo_queue_claim_wr_buffer() resets len = 0 when the next buffer is
claimed, so skipping the reset for a NULL wr_msg loses nothing.
static void teseo_rx_callback_error(void)
{
const TESEO_LIV3F_ctx_t *pCtx = Teseo_UART_Data.pCtx;
PRINT_DBG("E");
Teseo_UART_Data.fsm_next_state = fsm_discard;
/* wr_msg is NULL while the FSM is in discard mode (set NULL in
* teseo_uart_rx_onoff() and after every completed sentence in
* teseo_rx_callback_ok()) */
if (Teseo_UART_Data.wr_msg != NULL)
{
Teseo_UART_Data.wr_msg->len = 0;
}
if (pCtx != NULL)
{
pCtx->ClearOREF(pCtx->Handle);
pCtx->Receive(pCtx->Handle, &Teseo_UART_Data.dummy_char, 1);
}
}
We have been running with this patch; the cold power-on scenario now recovers
cleanly (the FSM resynchronizes on the next '$').
Related observation (minor)
PRINT_DBG is invoked from this interrupt context. If an application maps
PRINT_OUT/PRINT_DBG (via teseo_liv3f_conf.h) to printf-based logging,
newlib's first printf call allocates its stream buffer with malloc, which
is not safe in an ISR. It may be worth documenting that the PRINT_* hooks can
be called from interrupt context and must be ISR-safe.
- 主要言語
- C
- スター
- 11
- フォーク
- 5
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
STMicroelectronics/x-cube-gnss1 のほかの issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
STMicroelectronics/x-cube-gnss1#3 · コメント 1 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 20/100
STMicroelectronics/x-cube-gnss1#1 · コメント 1 件 ·
STMicroelectronics/x-cube-gnss1 の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
Polprzewodnikowy/N64FlashcartMenu#486 · コメント 3 件 ·
-
crash
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
`core.output` prints string properties without escaping their quotes対応中かも @JensOgorek が今日担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
zauberzeug/lizard#399 ·
メンテナーはふだん 2 日以内に返信