sys/threads: assert on uninitialized mutex/cond handles - #489
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a static inline helper function handleIsValid to validate resource handles in sys/threads.c. It also adds assertions using this helper to verify the validity of handles in mutexLock, condWait, and mutexLock2. There are no review comments, and I have no additional feedback to provide.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Unit Test Results12 336 tests 11 557 ✅ 57m 46s ⏱️ For more details on these failures, see this check. Results for commit 7fe9cc4. ♻️ This comment has been updated with latest results. |
41a8cb7 to
460afc1
Compare
|
IIRC imxrt uses uninitialized mutex in |
oI0ck
left a comment
There was a problem hiding this comment.
Unsure about this one, maybe asserting things like these should be behind a different macro definition in addition to NDEBUG. I can imagine that this might break in unexpected places (which is a good thing in the end, since we shouldn't have uninitialized mutexes, and we often don't check the return value).
Making this opt-in for now would be good for preserving code containing improper uses, as long as it is not fixed. We can then roll this out to be enabled by default and opt-out.
|
|
||
|
|
||
| #ifndef NDEBUG | ||
| static inline bool handleIsValid(handle_t h) |
There was a problem hiding this comment.
Wouldn't a macro be more fitting here? Also, provide an alternative definition in #elsif
There was a problem hiding this comment.
#ifndef NDEBUG is needed here only to avoid unused function warnings. Note that at this point only llvm shows warning here as GCC disables unused check for all static inline functions regardless and not only those included from headers.
I prefer to avoid macros when possible. Here function provides better type safety without any drawbacks.
There was a problem hiding this comment.
I'd still provide an alternate definition or state here that this has to be used only inside the assert macro
There are some further checks that would be nice to have ie. After this change is used for some time we could consider removing return value from default mutex API calls and raising trap from kernel. For now NDEBUG is a good way to ensure checks can be disabled on production, but are not missed in CI |
460afc1 to
70e5a44
Compare
|
|
||
|
|
||
| #ifndef NDEBUG | ||
| static inline bool handleIsValid(handle_t h) |
There was a problem hiding this comment.
this name could be somewhat misleading as we don't really check if it is valid for sure, only if it could be valid
There was a problem hiding this comment.
Maybe reverse the logic (handleIsInvalid()) and add comment that currently false negative is possible.
There was a problem hiding this comment.
Changed to:
/* TODO: expose RESOURCE_ID_MIN from kernel headers */
#define HANDLE_MIN 1
...
assert(m >= HANDLE_MIN);
70e5a44 to
7fe9cc4
Compare
|
|
||
|
|
||
| #ifndef NDEBUG | ||
| static inline bool handleIsValid(handle_t h) |
There was a problem hiding this comment.
Maybe reverse the logic (handleIsInvalid()) and add comment that currently false negative is possible.
This is first step in improving detection of uninitialized handles. Adding checks to non DEBUG builds needs to be considered, but requires more care before it can be safely adopted.
7fe9cc4 to
602f7b1
Compare
This is first step in improving detection of uninitialized handles. Adding checks to non DEBUG builds needs to be considered, but requires more care before it can be safely adopted.
Motivation and Context
mutexLockis frequently used without checking return value. If mutex create is not invoked then allmutexLockinvocations are silently ignored without any sign that locks are never actually held.Types of changes
How Has This Been Tested?
Checklist:
Special treatment