Masked the core maps passed to the SMP remap solution search against TX_THREAD_SMP_CORE_MASK, so the computed core index is provably within the schedule list and GCC no longer reports a false array bounds error at -O2 - #728
Open
fdesbiens wants to merge 1 commit into
Conversation
…TX_THREAD_SMP_CORE_MASK, so the computed core index is provably within the schedule list and GCC no longer reports a false array bounds error at -O2 The remap solution search derives a core number from the lowest set bit of the supplied core maps and uses it to index _tx_thread_smp_schedule_list and the local remap list, both of which have TX_THREAD_SMP_MAX_CORES entries. Every map reaching the function is already restricted to the cores that are present, because tx_thread_smp_cores_allowed is masked with TX_THREAD_SMP_CORE_MASK in tx_thread_create and tx_thread_smp_core_exclude. The compiler cannot see that invariant, however, so when the function is inlined into _tx_thread_system_suspend at -O2 GCC assumes the bit number can be as high as 31 and reports an out of bounds array subscript, which fails the build under -Werror. Masking the three incoming maps with TX_THREAD_SMP_CORE_MASK makes the invariant explicit. The masks are semantic no-ops, so scheduling behaviour is unchanged. The first core queue entry is now initialized as well, since the narrowed value range lets the compiler consider an empty queue on entry. Fixes eclipse-threadx#469 Assisted-by: Copilot (Opus 5) <noreply@github.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
_tx_thread_smp_remap_solution_findderives a core number from the lowest set bit of the core maps it is given, then uses that number to index_tx_thread_smp_schedule_listand the localthread_remap_list, both of which haveTX_THREAD_SMP_MAX_CORESentries.Every map that reaches the function is already restricted to the cores that are present:
tx_thread_smp_cores_allowedis masked withTX_THREAD_SMP_CORE_MASKin_tx_thread_createand in_tx_thread_smp_core_exclude, and the available cores map is built from the schedule list. The compiler cannot see that invariant, so when the function is inlined into_tx_thread_system_suspendat-O2, GCC assumes the returned bit number can be as high as 31 and reports an out of bounds array subscript. Under-Werrorthat fails the build.This change masks the three incoming maps with
TX_THREAD_SMP_CORE_MASKat the top of the function, in both the inline version incommon_smp/inc/tx_thread.hand the out of line twin incommon_smp/src/tx_thread_smp_utilities.c. The masks are semantic no-ops, so scheduling behaviour is unchanged, but the value range is now visible to the optimizer. The first core queue entry is also initialized, because the narrowed range lets GCC consider the case of an empty queue on entry and it would otherwise emit a-Wmaybe-uninitializedwarning in its place.Fixes #469
Testing
Reproduced the original error with
arm-none-eabi-gcc13.2.1 against the Cortex-A9 SMP port at-O2with the reporter's warning set, and confirmed it is gone after the change. Swept-O2,-O3and-Osagainst 2, 4 and 8 core configurations, with and withoutTX_THREAD_SMP_DYNAMIC_CORE_MAX,TX_NOT_INTERRUPTABLEandTX_THREAD_SMP_EQUAL_PRIORITY, all clean under-Wall -Wextra -Werror. The SMP regression suite passes 116 of 116.