Skip to content

Validated SMP next-priority range before shifting - #845

Open
Dev10-sys wants to merge 1 commit into
eclipse-threadx:devfrom
Dev10-sys:fix/smp-next-priority-range-check
Open

Dev10-sys wants to merge 1 commit into
eclipse-threadx:devfrom
Dev10-sys:fix/smp-next-priority-range-check

Conversation

@Dev10-sys

Copy link
Copy Markdown

Fixes #680

In _tx_thread_smp_next_priority_find (when TX_MAX_PRIORITIES <= 32), local_priority_map was shifted right by priority before checking if priority was within range. If priority is 32 or higher, shifting a 32-bit value by 32 or more bits is undefined behavior in C. Because of this, the range check inside the else block was placed after the undefined shift had already happened, making it practically unreachable and leaving it uncovered.

The TX_MAX_PRIORITIES > 32 version already handles this properly by checking priority < TX_MAX_PRIORITIES upfront before doing any shift. I updated the <= 32 version in both common_smp/src/tx_thread_smp_utilities.c and common_smp/inc/tx_thread.h to match that same logic. Now next_priority defaults to TX_MAX_PRIORITIES, and the shift and bit lookup only run when priority is strictly less than TX_MAX_PRIORITIES.

I also added a test in test/smp/regression/threadx_thread_priority_change.c that calls _tx_thread_smp_next_priority_find(TX_MAX_PRIORITIES) directly to make sure it returns TX_MAX_PRIORITIES safely without an out-of-range shift. Built and ran the full SMP test suite locally on Linux: all 118 tests pass, and gcov confirms _tx_thread_smp_next_priority_find now has 100% line coverage.

In the TX_MAX_PRIORITIES <= 32 variant of _tx_thread_smp_next_priority_find, the priority map was shifted right by priority before checking if priority exceeded TX_MAX_PRIORITIES. When priority is equal to or greater than 32, shifting a 32-bit unsigned long by 32 or more bits produces undefined behavior in C. The boundary check placed inside the else block was therefore evaluated only after the undefined shift had already taken place.

The TX_MAX_PRIORITIES > 32 variant of the same function validates the range upfront before indexing and shifting. The <= 32 variant now mirrors this structure by initializing next_priority to TX_MAX_PRIORITIES and performing the shift and lowest-bit lookup only when priority is within valid bounds.

A regression check calling _tx_thread_smp_next_priority_find(TX_MAX_PRIORITIES) has been added to threadx_thread_priority_change.c to exercise the boundary guard and ensure the boundary path is covered.

Fixes: eclipse-threadx#680

Signed-off-by: Dev10-sys <kalpanagola9897@gmail.com>

This branch has not been deployed

No deployments
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.

1 participant