Add timer delete completion callback - #1469
Open
Zepp-Hanzj wants to merge 1 commit into
Open
Conversation
Signed-off-by: hanzhijian <hanzhijian@zepp.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Description
Add a timer-delete completion callback that the timer daemon invokes while the
timer control block, callback function, and
pvTimerIDare still valid.This supports API wrappers that own resources referenced by a timer and need to
release them only after the asynchronous
tmrCOMMAND_DELETEhas actually beenprocessed.
Problem observed in the field
This fixes a real failure encountered on an embedded product using CMSIS-RTOS2
timers. Intermittent HardFaults/system crashes were most reproducible when:
The failure sequence was:
osTimerDelete()callsxTimerDelete().xTimerDelete()returnspdPASSafter placingtmrCOMMAND_DELETEin thetimer command queue. The timer has not necessarily been deleted yet.
referenced by
pvTimerID.wrapper callback dereferences the freed context.
Depending on whether the freed block had been reused, this caused a
use-after-free, invalid function/argument access, and eventually a HardFault or
system hang.
Root cause
FreeRTOS software timer commands are asynchronous. A successful return from
xTimerDelete()means the delete command was queued; it does not mean the timerdaemon has processed it. Wrapper-owned callback context must therefore remain
alive until
tmrCOMMAND_DELETEis handled.Fix
TimerDeleteCallbackFunction_tandvTimerDeleteCallbackRegister().deleted timer control block is freed.
pvTimerID, allowing a wrapperto identify and release only resources it owns.
This avoids using a second queued cleanup command. Such an approach is not
atomic with
xTimerDelete()and can fail when the timer queue has only one freeslot.
The corresponding CMSIS-FreeRTOS wrapper fix is
ARM-software/CMSIS-FreeRTOS#198.
Test Steps
examples/cmake_examplewith GCC and Ninja.use-after-free in 32/32 iterations.
no new use-after-free, exactly one delete callback per live callback, stable
heap usage, and no HardFault/assert/stack overflow.
callback.
Checklist:
Related Issue
ARM-software/CMSIS-FreeRTOS#198
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.