From 4d5c6b73ce7c4e50d3c22806b05c20d840127252 Mon Sep 17 00:00:00 2001 From: Dev10-sys Date: Sat, 21 Nov 2026 18:05:14 +0530 Subject: [PATCH 1/2] Validated SMP next-priority range before shifting 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: #680 Signed-off-by: Dev10-sys --- common_smp/inc/tx_thread.h | 18 +++++------------- common_smp/src/tx_thread_smp_utilities.c | 18 +++++------------- .../threadx_thread_priority_change.c | 11 ++++++++++- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/common_smp/inc/tx_thread.h b/common_smp/inc/tx_thread.h index 1ffe88287..928ac6b3c 100644 --- a/common_smp/inc/tx_thread.h +++ b/common_smp/inc/tx_thread.h @@ -643,20 +643,12 @@ UINT first_bit_set; ULONG local_priority_map; UINT next_priority; - local_priority_map = _tx_thread_priority_maps[0]; - local_priority_map = local_priority_map >> priority; - next_priority = priority; - if (local_priority_map == ((ULONG) 0)) - { - next_priority = ((UINT) TX_MAX_PRIORITIES); - } - else + next_priority = ((UINT) TX_MAX_PRIORITIES); + if (priority < ((UINT) TX_MAX_PRIORITIES)) { - if (next_priority >= ((UINT) TX_MAX_PRIORITIES)) - { - next_priority = ((UINT) TX_MAX_PRIORITIES); - } - else + local_priority_map = _tx_thread_priority_maps[0]; + local_priority_map = local_priority_map >> priority; + if (local_priority_map != ((ULONG) 0)) { TX_LOWEST_SET_BIT_CALCULATE(local_priority_map, first_bit_set) next_priority = priority + first_bit_set; diff --git a/common_smp/src/tx_thread_smp_utilities.c b/common_smp/src/tx_thread_smp_utilities.c index 0569a8192..5760bdc2b 100644 --- a/common_smp/src/tx_thread_smp_utilities.c +++ b/common_smp/src/tx_thread_smp_utilities.c @@ -136,20 +136,12 @@ UINT first_bit_set; ULONG local_priority_map; UINT next_priority; - local_priority_map = _tx_thread_priority_maps[0]; - local_priority_map = local_priority_map >> priority; - next_priority = priority; - if (local_priority_map == ((ULONG) 0)) - { - next_priority = ((UINT) TX_MAX_PRIORITIES); - } - else + next_priority = ((UINT) TX_MAX_PRIORITIES); + if (priority < ((UINT) TX_MAX_PRIORITIES)) { - if (next_priority >= ((UINT) TX_MAX_PRIORITIES)) - { - next_priority = ((UINT) TX_MAX_PRIORITIES); - } - else + local_priority_map = _tx_thread_priority_maps[0]; + local_priority_map = local_priority_map >> priority; + if (local_priority_map != ((ULONG) 0)) { TX_LOWEST_SET_BIT_CALCULATE(local_priority_map, first_bit_set) next_priority = priority + first_bit_set; diff --git a/test/smp/regression/threadx_thread_priority_change.c b/test/smp/regression/threadx_thread_priority_change.c index d22f3d721..42c8db235 100644 --- a/test/smp/regression/threadx_thread_priority_change.c +++ b/test/smp/regression/threadx_thread_priority_change.c @@ -56,8 +56,9 @@ extern unsigned long _tx_thread_priority_map; /* Prototype for test control return. */ void test_control_return(UINT status); -/* Prototype for internal function. */ +/* Prototype for internal functions. */ VOID _tx_thread_smp_simple_priority_change(TX_THREAD *thread_ptr,UINT new_priority); +UINT _tx_thread_smp_next_priority_find(UINT priority); /* This test routine is used to get a thread of a non ready state. */ @@ -487,6 +488,14 @@ TX_THREAD *temp_thread; thread_0.tx_thread_inherit_priority = TX_MAX_PRIORITIES; TX_RESTORE + /* Verify that calling _tx_thread_smp_next_priority_find with a priority + at the maximum limit returns TX_MAX_PRIORITIES without an out-of-range shift. */ + if (_tx_thread_smp_next_priority_find(TX_MAX_PRIORITIES) != ((UINT) TX_MAX_PRIORITIES)) + { + printf("ERROR #16\n"); + test_control_return(1); + } + /* Successful test. */ printf("SUCCESS!\n"); test_control_return(0); From 8e0f7ec49cef7f8fd14300f4d44b5aea2b510797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Desbiens?= Date: Tue, 1 Dec 2026 16:49:11 -0500 Subject: [PATCH 2/2] Tested both SMP priority lookups for invalid shifts The boundary test checks the returned sentinel. The lookup can still produce that sentinel after an undefined shift. Run focused CTest cases for the inline and out-of-line helpers with GCC shift traps enabled. Check the highest valid priority, an empty lookup, and the range limit. Both tests trapped with the previous implementation and passed with the fix. GCC 14 passed 123/123 Linux SMP tests on the merge candidate. Assisted-by: Codex (GPT-6.2-Sol) --- test/smp/cmake/CMakeLists.txt | 6 +++ test/smp/cmake/next_priority/CMakeLists.txt | 26 ++++++++++ test/smp/cmake/next_priority/next_priority.c | 52 ++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 test/smp/cmake/next_priority/CMakeLists.txt create mode 100644 test/smp/cmake/next_priority/next_priority.c diff --git a/test/smp/cmake/CMakeLists.txt b/test/smp/cmake/CMakeLists.txt index 28b1a2664..cacbe1192 100644 --- a/test/smp/cmake/CMakeLists.txt +++ b/test/smp/cmake/CMakeLists.txt @@ -112,3 +112,9 @@ endif() if(THREADX_ARCH STREQUAL "win64") add_subdirectory(windows_port) endif() + +# Check both priority lookup implementations with GCC shift instrumentation. +if(THREADX_ARCH STREQUAL "linux" AND CMAKE_C_COMPILER_ID STREQUAL "GNU" + AND CMAKE_BUILD_TYPE STREQUAL "default_build_coverage") + add_subdirectory(next_priority) +endif() diff --git a/test/smp/cmake/next_priority/CMakeLists.txt b/test/smp/cmake/next_priority/CMakeLists.txt new file mode 100644 index 000000000..59b96f98d --- /dev/null +++ b/test/smp/cmake/next_priority/CMakeLists.txt @@ -0,0 +1,26 @@ +# Copyright (c) 2026 Eclipse ThreadX contributors +# SPDX-License-Identifier: MIT + +get_filename_component(THREADX_SOURCE "${CMAKE_CURRENT_LIST_DIR}/../../../.." ABSOLUTE) + +foreach(implementation inline out_of_line) + set(test_target smp_next_priority_${implementation}) + add_executable(${test_target} next_priority.c) + target_include_directories(${test_target} PRIVATE + "${THREADX_SOURCE}/common_smp/inc" + "${THREADX_SOURCE}/ports_smp/linux/gnu/inc") + target_compile_definitions(${test_target} PRIVATE _GNU_SOURCE) + + # Use a 32-bit priority map. Trap mode avoids a multilib libubsan dependency. + target_compile_options(${test_target} PRIVATE -UTX_MAX_PRIORITIES -DTX_MAX_PRIORITIES=32 + -fsanitize=shift -fsanitize-undefined-trap-on-error -ffunction-sections -fdata-sections) + target_link_options(${test_target} PRIVATE -Wl,--gc-sections) + + if(implementation STREQUAL "inline") + target_compile_definitions(${test_target} PRIVATE TX_TEST_INLINE_PRIORITY_FIND) + else() + target_sources(${test_target} PRIVATE + "${THREADX_SOURCE}/common_smp/src/tx_thread_smp_utilities.c") + endif() + add_test(NAME ${test_target} COMMAND ${test_target}) +endforeach() diff --git a/test/smp/cmake/next_priority/next_priority.c b/test/smp/cmake/next_priority/next_priority.c new file mode 100644 index 000000000..a865105d5 --- /dev/null +++ b/test/smp/cmake/next_priority/next_priority.c @@ -0,0 +1,52 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * AI Disclosure: This file was largely AI-generated by Codex (GPT-6.2-Sol). + * The AI-generated portions may be considered public domain (CC0-1.0) + * and not subject to the project's licence. The human contributor has + * reviewed and verified that the code is correct. + * + * SPDX-License-Identifier: MIT and CC0-1.0 + **************************************************************************/ + +#define TX_THREAD_SMP_SOURCE_CODE +#include "tx_api.h" + +#ifdef TX_TEST_INLINE_PRIORITY_FIND +/* MISRA C:2023 Rule 20.5 deviation. Enable the inline helper in this test translation unit. */ +#undef TX_DISABLE_INLINE +#endif + +#include "tx_thread.h" + +ULONG _tx_thread_priority_maps[1]; + +/* Check the highest valid priority, the empty result, and the range limit. */ +int main(void) +{ + const UINT highest_priority = ((UINT) TX_MAX_PRIORITIES) - 1U; + + _tx_thread_priority_maps[0] = 0x80000000UL; + if (_tx_thread_smp_next_priority_find(highest_priority) != highest_priority) + { + return(1); + } + + _tx_thread_priority_maps[0] = 1UL; + if (_tx_thread_smp_next_priority_find(highest_priority) != ((UINT) TX_MAX_PRIORITIES)) + { + return(2); + } + + /* The range limit must return the sentinel. Shift sanitization checks the operation itself. */ + if (_tx_thread_smp_next_priority_find(TX_MAX_PRIORITIES) != ((UINT) TX_MAX_PRIORITIES)) + { + return(3); + } + + return(0); +}