Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@jefftenney
Copy link
Contributor

Fix stop/delete backlogged auto-reload timer

Description

Prior to this PR, if an application attempts to stop or delete an auto-reload timer while it is backlogged, the timer unexpectedly restarts. A timer is "backlogged" when at the time of the auto-reload, the new timer period has already elapsed. In the case of a deleted timer, the timer continues running using freed memory which may eventually lead to a crash or failure of other unrelated timers when the memory is reallocated.

Test Steps

See FreeRTOS/FreeRTOS#553.

Related Issue

https://forums.freertos.org/t/freertos-time-correction/11856
https://forums.freertos.org/t/timer-start-dont-trace-race-condition/12168

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

When reloading an auto-reload timer, the timer task sends the
tmrCOMMAND_START_DONT_TRACE command if the new timer period has already
elapsed.

If the command queue already contains a stop or delete command for the
timer, then prior to this commit the tmrCOMMAND_START_DONT_TRACE command
is processed *after* the stop or delete command.  Consequently the timer
is restarted unexpectedly (in the stop case) or uses freed memory (in
the delete case).  After this commit, the tmrCOMMAND_START_DONT_TRACE
command is processed before the stop or delete command.
@codecov
Copy link

codecov bot commented Apr 8, 2021

Codecov Report

Merging #303 (f2ed318) into main (a31018d) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #303   +/-   ##
=======================================
  Coverage   90.60%   90.60%           
=======================================
  Files           3        3           
  Lines         532      532           
  Branches      142      142           
=======================================
  Hits          482      482           
  Misses         14       14           
  Partials       36       36           
Flag Coverage Δ
unittests 90.60% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a31018d...f2ed318. Read the comment docs.

@RichardBarry
Copy link
Contributor

Preferred solution would be that timer.c will check that timer was already deleted

This is not an easy thing to do because the memory used by the timer's structures is freed so could contain anything after the timer has been deleted. Even in the case of a timer being allocated statically, the statically allocated memory may be re-used to create another timer.

There is an analogous problem with tasks. Kernel aware debuggers that display tasks had no idea if a task had been deleted and re-created since it last viewed the memory. In that situation we added a unique number to each task structure. If the kernel aware debugger saw a task structure (TCB) occupying the same memory location but with a different unique number it knows its a different task even if the task name and handle are the same (the handle just being a pointer to the TCB). I was wondering if we could do something similar with timers. If a timer is recognised from a combination of its handle and unique number contained within its structure - then a command operating on a timer that had a different unique number to that expected could be discarded.

@jefftenney
Copy link
Contributor Author

We could consider that in those cases the error is on the part of the application writer as they are sending a command to a timer they know they have already deleted, whereas the autoreload event is a special case because it happens automatically. That would seem reasonable.

Agreed.

Can we think of any other cases that need to same treatment (sending to the front)?

No, but let me fix this up a little bit to prompt developers in the future to consider any new command for sending to front. It would be too easy with the code as is for a developer not to consider the question.

My first thought is to set a bit in each command value as a tag, either tmrCMD_GROUP_FROM_ISR, tmrCMD_GROUP_PRIORITY, tmrCMD_GROUP_STANDARD. Then xTimerGenericCommand() would use those bits to determine whether to call xQueueSendToBackFromISR(), xQueueSendToFront(), or xQueueSendToBack(). It currently uses xCommandID < tmrFIRST_FROM_ISR_COMMAND to make the determination.

@jefftenney jefftenney force-pushed the Fix-Stop-Delete-Auto-Reload-Timer branch from 7364a41 to 3da78c6 Compare April 9, 2021 14:51
And send priority commands to front even if the scheduler is disabled.
@jefftenney jefftenney force-pushed the Fix-Stop-Delete-Auto-Reload-Timer branch from 3da78c6 to ce66916 Compare April 9, 2021 14:52
@jefftenney jefftenney marked this pull request as draft April 9, 2021 19:14
@jefftenney
Copy link
Contributor Author

jefftenney commented Apr 9, 2021

Seems a good solution, and simpler that those I was contemplating.

You might have been right contemplating other solutions. Please see #305 for what I think is a better solution. Let me know which way you'd like to go. This solution (this PR, #303) does have the advantage of having fewer changes to the code. But it's awkward in its operation and not as clean in its design as the rest of FreeRTOS code. So sorry if you've wasted time on my waffling.

Note also that in #305 I fixed a couple of other issues. Will gladly fix (or not fix) them here in this PR as you prefer.

@jefftenney
Copy link
Contributor Author

Closing this PR since #305 was better.

@jefftenney jefftenney closed this Apr 13, 2021
@jefftenney jefftenney deleted the Fix-Stop-Delete-Auto-Reload-Timer branch May 28, 2021 23:04
laroche pushed a commit to laroche/FreeRTOS-Kernel that referenced this pull request Apr 18, 2024
* deleted old version wolfSSL before updating

* updated wolfSSL to the latest version(v4.4.0)

* updated wolfSSL to the latest version(v4.4.0)

* added macros for timing resistance

* Add wolfSSL-FIPS-Ready to Demo and Source

* Add wolfSSL-FIPS-Ready to Demo and Source

* Update README_wolfSSL_FIPS_Ready.md

* Remove unused files

* Update to wolfSSL-4.5.0-FIPS-Ready

* Increase FIPS version number for the default

* Update wolfSSL to the latest version(v.4.5.0)

* Fix version number

* Fix comments from github

Co-authored-by: RichardBarry <[email protected]>
Co-authored-by: Ming Yue <[email protected]>
Co-authored-by: Aniruddha Kanhere <[email protected]>
Co-authored-by: Cobus van Eeden <[email protected]>
Co-authored-by: Alfred Gedeon <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants