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/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); +} 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);