diff --git a/.github/workflows/module_qemu.yml b/.github/workflows/module_qemu.yml index dc90b7a06..96f4d0bb3 100644 --- a/.github/workflows/module_qemu.yml +++ b/.github/workflows/module_qemu.yml @@ -127,13 +127,14 @@ jobs: status=0 for port in cortex_m3 cortex_m4 cortex_a7; do for pointer_context in OFF ON; do - config="$port (pointer context $pointer_context)" + config="$port (pointer context and stack checking $pointer_context)" build="build/module_qemu_${port}_$pointer_context" echo "== $config" if ! cmake -S test/tx/cmake/module_qemu -B "$build" -G Ninja \ -DCMAKE_TOOLCHAIN_FILE="$PWD/test/tx/cmake/module_qemu/arm-none-eabi-qemu.cmake" \ - -DMODULE_QEMU_PORT="$port" -DMODULE_QEMU_POINTER_CONTEXT="$pointer_context"; then + -DMODULE_QEMU_PORT="$port" -DMODULE_QEMU_POINTER_CONTEXT="$pointer_context" \ + -DMODULE_QEMU_STACK_CHECKING="$pointer_context"; then echo "::error::$config: CMake configure failed" status=1 continue diff --git a/common/inc/tx_api.h b/common/inc/tx_api.h index af96e0c14..7d35e2f01 100644 --- a/common/inc/tx_api.h +++ b/common/inc/tx_api.h @@ -2032,7 +2032,11 @@ VOID _tx_misra_thread_entry_exit_notify_not_used(VOID (*threa #define TX_TIMER_POINTER_DIF(a,b) _tx_misra_timer_pointer_dif((TX_TIMER_INTERNAL **) (a), (TX_TIMER_INTERNAL **) (b)) #define TX_TIMER_POINTER_ADD(a,b) _tx_misra_timer_pointer_add((TX_TIMER_INTERNAL **) (a), (ULONG) (b)) #define TX_USER_TIMER_POINTER_GET(a,b) _tx_misra_user_timer_pointer_get((TX_TIMER_INTERNAL *) (a), (TX_TIMER **) &(b)); +#if defined(TXM_MODULE_MANAGER) && defined(TX_ENABLE_STACK_CHECKING) +#define TX_THREAD_STACK_CHECK(a) _txm_module_manager_thread_stack_check(a); +#else #define TX_THREAD_STACK_CHECK(a) _tx_misra_thread_stack_check((a), &((a)->tx_thread_stack_highest_ptr)); +#endif #ifdef TX_ENABLE_EVENT_TRACE #define TX_TRACE_IN_LINE_INSERT(i,a,b,c,d,e) _tx_misra_trace_event_insert((ULONG) (i), (VOID *) (a), (ULONG) (b), (ULONG) (c), (ULONG) (d), (ULONG) (e), ((ULONG) TX_TRACE_TIME_SOURCE)); #endif diff --git a/common/inc/tx_thread.h b/common/inc/tx_thread.h index 40e31ddaa..b87151b2a 100644 --- a/common/inc/tx_thread.h +++ b/common/inc/tx_thread.h @@ -8,6 +8,7 @@ * * SPDX-License-Identifier: MIT **************************************************************************/ +// Portions of this file were generated with AI assistance. /**************************************************************************/ @@ -206,7 +207,15 @@ low address to high address (the default logic is for stacks that grow from high address to low address. */ +#if defined(TXM_MODULE_MANAGER) && defined(TX_ENABLE_STACK_CHECKING) +VOID _txm_module_manager_thread_stack_check(TX_THREAD *thread_ptr); +ULONG *_tx_thread_stack_analyze_range(ULONG *stack_lowest, ULONG *stack_limit, ULONG stack_fill); +#endif + #ifndef TX_THREAD_STACK_CHECK +#if defined(TXM_MODULE_MANAGER) && defined(TX_ENABLE_STACK_CHECKING) +#define TX_THREAD_STACK_CHECK(thread_ptr) _txm_module_manager_thread_stack_check(thread_ptr); +#else #define TX_THREAD_STACK_CHECK(thread_ptr) \ { \ TX_INTERRUPT_SAVE_AREA \ @@ -225,7 +234,8 @@ _tx_thread_stack_error_handler((thread_ptr)); \ TX_DISABLE \ } \ - else if (*(((ULONG *) (thread_ptr) -> tx_thread_stack_highest_ptr) - 1) != TX_STACK_FILL) \ + else if ((((ULONG *) (thread_ptr) -> tx_thread_stack_highest_ptr) > ((ULONG *) (thread_ptr) -> tx_thread_stack_start)) && \ + (*(((ULONG *) (thread_ptr) -> tx_thread_stack_highest_ptr) - 1) != TX_STACK_FILL)) \ { \ TX_RESTORE \ _tx_thread_stack_analyze((thread_ptr)); \ @@ -235,6 +245,7 @@ TX_RESTORE \ } #endif +#endif /* Define default post thread delete macro to whitespace, if it hasn't been defined previously (typically in tx_port.h). */ diff --git a/common/inc/tx_user_sample.h b/common/inc/tx_user_sample.h index f06e113d3..73d644449 100644 --- a/common/inc/tx_user_sample.h +++ b/common/inc/tx_user_sample.h @@ -154,7 +154,8 @@ disabled. When the following is defined, ThreadX thread stack checking is enabled. If stack checking is enabled (TX_ENABLE_STACK_CHECKING is defined), the TX_DISABLE_STACK_FILLING define is negated, thereby forcing the stack fill which is necessary for the stack checking - logic. */ + logic. A Module Manager kernel also checks both stacks of a user-mode module thread. + Define TXM_MODULE_MANAGER for such kernels when building outside ThreadX CMake. */ /* #define TX_ENABLE_STACK_CHECKING diff --git a/common/src/tx_misra.c b/common/src/tx_misra.c index 7ac5dc68c..4c5249d2b 100644 --- a/common/src/tx_misra.c +++ b/common/src/tx_misra.c @@ -8,6 +8,7 @@ * * SPDX-License-Identifier: MIT **************************************************************************/ +// Portions of this file were generated with AI assistance. /**************************************************************************/ @@ -292,7 +293,8 @@ TX_INTERRUPT_SAVE_AREA _tx_thread_stack_error_handler((thread_ptr)); TX_DISABLE } - if (*(((ULONG *) *highest_stack) - 1) != TX_STACK_FILL) + else if ((((ULONG *) *highest_stack) > ((ULONG *) (thread_ptr -> tx_thread_stack_start))) && + (*(((ULONG *) *highest_stack) - 1) != TX_STACK_FILL)) { TX_RESTORE _tx_thread_stack_analyze((thread_ptr)); diff --git a/common/src/tx_thread_stack_analyze.c b/common/src/tx_thread_stack_analyze.c index ecd7ee13b..354fe5bff 100644 --- a/common/src/tx_thread_stack_analyze.c +++ b/common/src/tx_thread_stack_analyze.c @@ -31,6 +31,97 @@ #include "tx_thread.h" +/* Find the first used word between the stack base and the known watermark. */ +#if !defined(TXM_MODULE_MANAGER) || !defined(TX_ENABLE_STACK_CHECKING) +static +#endif +ULONG *_tx_thread_stack_analyze_range(ULONG *stack_lowest, ULONG *stack_limit, ULONG stack_fill) +{ +ULONG *stack_ptr; +ULONG *stack_highest = stack_limit; +ULONG *probe_ptr; +ULONG probe_count; +UINT fill_present; +ULONG size; + + /* We need to binary search the remaining stack for missing 0xEFEFEFEF 32-bit data pattern. + This is a best effort algorithm to find the highest stack usage. */ + do + { + + /* Calculate the size again. */ + size = (ULONG) (TX_ULONG_POINTER_DIF(stack_highest, stack_lowest))/((ULONG) 2); + stack_ptr = TX_ULONG_POINTER_ADD(stack_lowest, size); + + /* Determine if the pattern is still there. To avoid stopping on an + unwritten hole inside an otherwise used region, require several + consecutive fill words, working towards the lowest address. The scan + stops at the lowest known fill location, since everything at or below + that point is already known to hold the fill pattern. */ + fill_present = TX_TRUE; + probe_ptr = stack_ptr; + probe_count = TX_THREAD_STACK_ANALYZE_FILL_WORDS; + while (probe_count != ((ULONG) 0)) + { + + /* Determine if this word still holds the fill pattern. */ + if (*probe_ptr != stack_fill) + { + + /* No, the probe location is in use. */ + fill_present = TX_FALSE; + probe_count = ((ULONG) 0); + } + else + { + + /* Yes, account for this word. */ + probe_count--; + + /* Determine if the lowest known fill location has been reached. */ + if (probe_ptr <= stack_lowest) + { + + /* Yes, nothing further to check. */ + probe_count = ((ULONG) 0); + } + else + { + + /* Position to the previous word in the stack. */ + probe_ptr = TX_ULONG_POINTER_SUB(probe_ptr, 1); + } + } + } + + /* Determine if the probe location is in use. */ + if (fill_present == TX_FALSE) + { + + /* Update the stack highest, since we need to look in the upper half now. */ + stack_highest = stack_ptr; + } + else + { + + /* Update the stack lowest, since we need to look in the lower half now. */ + stack_lowest = stack_ptr; + } + + } while(size > ((ULONG) 1)); + + /* Position to first used word - at this point we are within a few words. */ + while ((stack_ptr < stack_limit) && (*stack_ptr == stack_fill)) + { + + /* Position to next word in stack. */ + stack_ptr = TX_ULONG_POINTER_ADD(stack_ptr, 1); + } + + return(stack_ptr); +} + + /**************************************************************************/ /* */ /* FUNCTION RELEASE */ @@ -72,11 +163,6 @@ TX_INTERRUPT_SAVE_AREA ULONG *stack_ptr; ULONG *stack_lowest; ULONG *stack_highest; -ULONG *probe_ptr; -ULONG *stack_limit; -ULONG probe_count; -UINT fill_present; -ULONG size; /* Disable interrupts. */ @@ -107,85 +193,10 @@ ULONG size; if ((stack_highest != TX_NULL) && (stack_highest > stack_lowest)) { - /* Remember the upper bound of the search so the scan below cannot run past it. */ - stack_limit = stack_highest; - /* Restore interrupts. */ TX_RESTORE - /* We need to binary search the remaining stack for missing 0xEFEFEFEF 32-bit data pattern. - This is a best effort algorithm to find the highest stack usage. */ - do - { - - /* Calculate the size again. */ - size = (ULONG) (TX_ULONG_POINTER_DIF(stack_highest, stack_lowest))/((ULONG) 2); - stack_ptr = TX_ULONG_POINTER_ADD(stack_lowest, size); - - /* Determine if the pattern is still there. To avoid stopping on an - unwritten hole inside an otherwise used region, require several - consecutive fill words, working towards the lowest address. The scan - stops at the lowest known fill location, since everything at or below - that point is already known to hold the fill pattern. */ - fill_present = TX_TRUE; - probe_ptr = stack_ptr; - probe_count = TX_THREAD_STACK_ANALYZE_FILL_WORDS; - while (probe_count != ((ULONG) 0)) - { - - /* Determine if this word still holds the fill pattern. */ - if (*probe_ptr != TX_STACK_FILL) - { - - /* No, the probe location is in use. */ - fill_present = TX_FALSE; - probe_count = ((ULONG) 0); - } - else - { - - /* Yes, account for this word. */ - probe_count--; - - /* Determine if the lowest known fill location has been reached. */ - if (probe_ptr <= stack_lowest) - { - - /* Yes, nothing further to check. */ - probe_count = ((ULONG) 0); - } - else - { - - /* Position to the previous word in the stack. */ - probe_ptr = TX_ULONG_POINTER_SUB(probe_ptr, 1); - } - } - } - - /* Determine if the probe location is in use. */ - if (fill_present == TX_FALSE) - { - - /* Update the stack highest, since we need to look in the upper half now. */ - stack_highest = stack_ptr; - } - else - { - - /* Update the stack lowest, since we need to look in the lower half now. */ - stack_lowest = stack_ptr; - } - - } while(size > ((ULONG) 1)); - - /* Position to first used word - at this point we are within a few words. */ - while ((stack_ptr < stack_limit) && (*stack_ptr == TX_STACK_FILL)) - { - - /* Position to next word in stack. */ - stack_ptr = TX_ULONG_POINTER_ADD(stack_ptr, 1); - } + stack_ptr = _tx_thread_stack_analyze_range(stack_lowest, stack_highest, TX_STACK_FILL); /* Optional processing extension. */ TX_THREAD_STACK_ANALYZE_EXTENSION diff --git a/common_modules/inc/txm_module.h b/common_modules/inc/txm_module.h index 0b35844b2..210f2f2d2 100644 --- a/common_modules/inc/txm_module.h +++ b/common_modules/inc/txm_module.h @@ -182,6 +182,11 @@ extern "C" { #endif +/* Define alias for kernel stack highest pointer used when stack checking is enabled. */ + +#define tx_thread_module_kernel_stack_highest_ptr tx_thread_module_reserved + + /* Define object types for object search requests. */ #define TXM_BLOCK_POOL_OBJECT 1 diff --git a/common_modules/module_manager/src/txm_module_manager_thread_create.c b/common_modules/module_manager/src/txm_module_manager_thread_create.c index 180441a40..58831bc34 100644 --- a/common_modules/module_manager/src/txm_module_manager_thread_create.c +++ b/common_modules/module_manager/src/txm_module_manager_thread_create.c @@ -341,17 +341,11 @@ ULONG i; thread_ptr -> tx_thread_stack_fill_value = stack_fill_value; #endif - /* Note that TX_ENABLE_STACK_CHECKING is not supported for module threads. A user mode - module thread owns two stacks and the scheduler swaps tx_thread_stack_start, - tx_thread_stack_end and tx_thread_stack_size over to the kernel stack whenever the - thread enters the module manager. Neither tx_thread_stack_highest_ptr nor the guard - pattern beyond the end of the stack is maintained across that switch, so - TX_THREAD_STACK_CHECK may report spurious stack errors for module threads. */ - /* If the thread runs on user mode, allocate the kernel stack for syscall. */ if((module_instance -> txm_module_instance_property_flags) & TXM_MODULE_USER_MODE) { ULONG status; + ALIGN_TYPE kernel_stack_top; /* Allocate kernel stack space. */ status = _txm_module_manager_object_allocate((VOID **) &(thread_ptr -> tx_thread_module_kernel_stack_start), TXM_MODULE_KERNEL_STACK_SIZE, module_instance); @@ -367,13 +361,22 @@ ULONG i; TX_MEMSET(thread_ptr -> tx_thread_module_kernel_stack_start, ((UCHAR) TX_STACK_FILL), TXM_MODULE_KERNEL_STACK_SIZE); #endif - /* Align kernel stack pointer. */ - thread_ptr -> tx_thread_module_kernel_stack_end = (VOID *) (((ALIGN_TYPE)(thread_ptr -> tx_thread_module_kernel_stack_start) + TXM_MODULE_KERNEL_STACK_SIZE) & ~0x07); + kernel_stack_top = (ALIGN_TYPE) thread_ptr -> tx_thread_module_kernel_stack_start + TXM_MODULE_KERNEL_STACK_SIZE; +#ifdef TX_ENABLE_STACK_CHECKING + /* Leave room for the guard at the aligned, exclusive stack end. */ + kernel_stack_top -= sizeof(ULONG); +#endif + thread_ptr -> tx_thread_module_kernel_stack_end = (VOID *) (kernel_stack_top & ~((ALIGN_TYPE) 0x07)); /* Set kernel stack size. Aligning the end of the kernel stack downwards can consume up to seven bytes of the block returned by the allocator, so the usable size must be derived from the aligned range instead of from TXM_MODULE_KERNEL_STACK_SIZE. */ thread_ptr -> tx_thread_module_kernel_stack_size = (ULONG) ((ALIGN_TYPE) (thread_ptr -> tx_thread_module_kernel_stack_end) - (ALIGN_TYPE) (thread_ptr -> tx_thread_module_kernel_stack_start)); + +#ifdef TX_ENABLE_STACK_CHECKING + /* Setup the highest usage pointer for the kernel stack. */ + thread_ptr -> tx_thread_module_kernel_stack_highest_ptr = thread_ptr -> tx_thread_module_kernel_stack_end; +#endif } /* Place the stack parameters into the thread's control block. */ @@ -816,3 +819,114 @@ UINT _txm_module_manager_thread_create_ptr(TX_THREAD *thread_ptr, TX_NAME_CONST time_slice, auto_start, thread_control_block_size, module_instance)); } #endif + +#if defined(TXM_MODULE_MANAGER) && defined(TX_ENABLE_STACK_CHECKING) +/* Compare addresses without ordering pointers from different stack allocations. + MISRA C:2023 Rules 11.4 and 11.6 apply to the pointer-width address conversions. */ +static UINT _txm_module_manager_stack_contains(const ULONG *start, const ULONG *end, const VOID *pointer) +{ +ALIGN_TYPE address = (ALIGN_TYPE) pointer; + + return((UINT) ((address >= (ALIGN_TYPE) start) && (address <= (ALIGN_TYPE) end))); +} + +/* Check a stack's guards and update its watermark while its allocation is protected. */ +static UINT _txm_module_manager_stack_check_range(ULONG *start, ULONG *guard, VOID *saved_pointer, + VOID **watermark, ULONG fill) +{ +ULONG *highest = TX_VOID_TO_ULONG_POINTER_CONVERT(*watermark); + + if (_txm_module_manager_stack_contains(start, guard, highest) == TX_FALSE) + { + return(TX_TRUE); + } + + if ((*start != fill) || (*guard != fill)) + { + return(TX_TRUE); + } + + if ((saved_pointer != TX_NULL) && (TX_VOID_TO_ULONG_POINTER_CONVERT(saved_pointer) < highest)) + { + highest = TX_VOID_TO_ULONG_POINTER_CONVERT(saved_pointer); + } + + if ((highest > start) && (*TX_ULONG_POINTER_SUB(highest, 1) != fill)) + { + highest = _tx_thread_stack_analyze_range(start, highest, fill); + } + *watermark = highest; + + return(TX_FALSE); +} + +/* Check both module stacks and associate the saved context with its allocation. */ +VOID _txm_module_manager_thread_stack_check(TX_THREAD *thread_ptr) +{ +TX_INTERRUPT_SAVE_AREA + +UINT stack_error = TX_FALSE; +VOID *saved_pointer; +ULONG *stack_start; +ULONG *stack_guard; +ULONG *kernel_start; +ULONG *kernel_guard; +UINT saved_on_stack; +UINT saved_on_kernel; + + TX_DISABLE + if ((thread_ptr != TX_NULL) && (thread_ptr -> tx_thread_id == TX_THREAD_ID)) + { + saved_pointer = thread_ptr -> tx_thread_stack_ptr; + kernel_start = TX_VOID_TO_ULONG_POINTER_CONVERT(thread_ptr -> tx_thread_module_kernel_stack_start); + if (kernel_start == TX_NULL) + { + stack_start = TX_VOID_TO_ULONG_POINTER_CONVERT(thread_ptr -> tx_thread_stack_start); + stack_guard = TX_VOID_TO_ULONG_POINTER_CONVERT(TX_UCHAR_POINTER_ADD( + TX_VOID_TO_UCHAR_POINTER_CONVERT(thread_ptr -> tx_thread_stack_end), 1)); + if (_txm_module_manager_stack_contains(stack_start, stack_guard, saved_pointer) == TX_TRUE) + { + stack_error = _txm_module_manager_stack_check_range(stack_start, stack_guard, saved_pointer, + &thread_ptr -> tx_thread_stack_highest_ptr, TX_STACK_FILL); + } + else + { + stack_error = TX_TRUE; + } + } + else + { + stack_start = TX_VOID_TO_ULONG_POINTER_CONVERT(thread_ptr -> tx_thread_module_stack_start); + stack_guard = TX_VOID_TO_ULONG_POINTER_CONVERT(TX_UCHAR_POINTER_ADD( + TX_VOID_TO_UCHAR_POINTER_CONVERT(thread_ptr -> tx_thread_module_stack_end), 1)); + kernel_guard = TX_VOID_TO_ULONG_POINTER_CONVERT(thread_ptr -> tx_thread_module_kernel_stack_end); + + /* Until the scheduler saves a new context, its saved pointer can name either stack. */ + saved_on_stack = _txm_module_manager_stack_contains(stack_start, stack_guard, saved_pointer); + saved_on_kernel = _txm_module_manager_stack_contains(kernel_start, kernel_guard, saved_pointer); + if ((saved_on_stack == TX_FALSE) && (saved_on_kernel == TX_FALSE)) + { + stack_error = TX_TRUE; + } + else + { + stack_error = _txm_module_manager_stack_check_range(stack_start, stack_guard, + (saved_on_stack == TX_TRUE) ? saved_pointer : TX_NULL, + &thread_ptr -> tx_thread_stack_highest_ptr, TX_STACK_FILL); + if (stack_error == TX_FALSE) + { + stack_error = _txm_module_manager_stack_check_range(kernel_start, kernel_guard, + (saved_on_kernel == TX_TRUE) ? saved_pointer : TX_NULL, + &thread_ptr -> tx_thread_module_kernel_stack_highest_ptr, TX_STACK_FILL); + } + } + } + } + TX_RESTORE + + if (stack_error == TX_TRUE) + { + _tx_thread_stack_error_handler(thread_ptr); + } +} +#endif diff --git a/common_smp/inc/tx_api.h b/common_smp/inc/tx_api.h index 9b90873dd..c3cc03a89 100644 --- a/common_smp/inc/tx_api.h +++ b/common_smp/inc/tx_api.h @@ -2041,7 +2041,11 @@ VOID _tx_misra_thread_entry_exit_notify_not_used(VOID (*threa #define TX_TIMER_POINTER_DIF(a,b) _tx_misra_timer_pointer_dif((TX_TIMER_INTERNAL **) (a), (TX_TIMER_INTERNAL **) (b)) #define TX_TIMER_POINTER_ADD(a,b) _tx_misra_timer_pointer_add((TX_TIMER_INTERNAL **) (a), (ULONG) (b)) #define TX_USER_TIMER_POINTER_GET(a,b) _tx_misra_user_timer_pointer_get((TX_TIMER_INTERNAL *) (a), (TX_TIMER **) &(b)); +#if defined(TXM_MODULE_MANAGER) && defined(TX_ENABLE_STACK_CHECKING) +#define TX_THREAD_STACK_CHECK(a) _txm_module_manager_thread_stack_check(a); +#else #define TX_THREAD_STACK_CHECK(a) _tx_misra_thread_stack_check((a), &((a)->tx_thread_stack_highest_ptr)); +#endif #ifdef TX_ENABLE_EVENT_TRACE #define TX_TRACE_IN_LINE_INSERT(i,a,b,c,d,e) _tx_misra_trace_event_insert((ULONG) (i), (VOID *) (a), (ULONG) (b), (ULONG) (c), (ULONG) (d), (ULONG) (e), ((ULONG) TX_TRACE_TIME_SOURCE)); #endif diff --git a/common_smp/inc/tx_thread.h b/common_smp/inc/tx_thread.h index 01c660a32..04921f329 100644 --- a/common_smp/inc/tx_thread.h +++ b/common_smp/inc/tx_thread.h @@ -63,7 +63,15 @@ low address to high address (the default logic is for stacks that grow from high address to low address. */ +#if defined(TXM_MODULE_MANAGER) && defined(TX_ENABLE_STACK_CHECKING) +VOID _txm_module_manager_thread_stack_check(TX_THREAD *thread_ptr); +ULONG *_tx_thread_stack_analyze_range(ULONG *stack_lowest, ULONG *stack_limit, ULONG stack_fill); +#endif + #ifndef TX_THREAD_STACK_CHECK +#if defined(TXM_MODULE_MANAGER) && defined(TX_ENABLE_STACK_CHECKING) +#define TX_THREAD_STACK_CHECK(thread_ptr) _txm_module_manager_thread_stack_check(thread_ptr); +#else #define TX_THREAD_STACK_CHECK(thread_ptr) \ { \ TX_INTERRUPT_SAVE_AREA \ @@ -82,7 +90,8 @@ _tx_thread_stack_error_handler((thread_ptr)); \ TX_DISABLE \ } \ - else if (*(((ULONG *) (thread_ptr) -> tx_thread_stack_highest_ptr) - 1) != TX_STACK_FILL) \ + else if ((((ULONG *) (thread_ptr) -> tx_thread_stack_highest_ptr) > ((ULONG *) (thread_ptr) -> tx_thread_stack_start)) && \ + (*(((ULONG *) (thread_ptr) -> tx_thread_stack_highest_ptr) - 1) != TX_STACK_FILL)) \ { \ TX_RESTORE \ _tx_thread_stack_analyze((thread_ptr)); \ @@ -92,6 +101,7 @@ TX_RESTORE \ } #endif +#endif /* Define default post thread delete macro to whitespace, if it hasn't been defined previously (typically in tx_port.h). */ diff --git a/common_smp/src/tx_misra.c b/common_smp/src/tx_misra.c index b9144a70c..a4c50aaf4 100644 --- a/common_smp/src/tx_misra.c +++ b/common_smp/src/tx_misra.c @@ -8,6 +8,7 @@ * * SPDX-License-Identifier: MIT **************************************************************************/ +// Portions of this file were generated with AI assistance. /**************************************************************************/ @@ -290,7 +291,8 @@ TX_INTERRUPT_SAVE_AREA _tx_thread_stack_error_handler((thread_ptr)); TX_DISABLE } - if (*(((ULONG *) *highest_stack) - 1) != TX_STACK_FILL) + else if ((((ULONG *) *highest_stack) > ((ULONG *) (thread_ptr -> tx_thread_stack_start))) && + (*(((ULONG *) *highest_stack) - 1) != TX_STACK_FILL)) { TX_RESTORE _tx_thread_stack_analyze((thread_ptr)); diff --git a/common_smp/src/tx_thread_stack_analyze.c b/common_smp/src/tx_thread_stack_analyze.c index ecd7ee13b..354fe5bff 100644 --- a/common_smp/src/tx_thread_stack_analyze.c +++ b/common_smp/src/tx_thread_stack_analyze.c @@ -31,6 +31,97 @@ #include "tx_thread.h" +/* Find the first used word between the stack base and the known watermark. */ +#if !defined(TXM_MODULE_MANAGER) || !defined(TX_ENABLE_STACK_CHECKING) +static +#endif +ULONG *_tx_thread_stack_analyze_range(ULONG *stack_lowest, ULONG *stack_limit, ULONG stack_fill) +{ +ULONG *stack_ptr; +ULONG *stack_highest = stack_limit; +ULONG *probe_ptr; +ULONG probe_count; +UINT fill_present; +ULONG size; + + /* We need to binary search the remaining stack for missing 0xEFEFEFEF 32-bit data pattern. + This is a best effort algorithm to find the highest stack usage. */ + do + { + + /* Calculate the size again. */ + size = (ULONG) (TX_ULONG_POINTER_DIF(stack_highest, stack_lowest))/((ULONG) 2); + stack_ptr = TX_ULONG_POINTER_ADD(stack_lowest, size); + + /* Determine if the pattern is still there. To avoid stopping on an + unwritten hole inside an otherwise used region, require several + consecutive fill words, working towards the lowest address. The scan + stops at the lowest known fill location, since everything at or below + that point is already known to hold the fill pattern. */ + fill_present = TX_TRUE; + probe_ptr = stack_ptr; + probe_count = TX_THREAD_STACK_ANALYZE_FILL_WORDS; + while (probe_count != ((ULONG) 0)) + { + + /* Determine if this word still holds the fill pattern. */ + if (*probe_ptr != stack_fill) + { + + /* No, the probe location is in use. */ + fill_present = TX_FALSE; + probe_count = ((ULONG) 0); + } + else + { + + /* Yes, account for this word. */ + probe_count--; + + /* Determine if the lowest known fill location has been reached. */ + if (probe_ptr <= stack_lowest) + { + + /* Yes, nothing further to check. */ + probe_count = ((ULONG) 0); + } + else + { + + /* Position to the previous word in the stack. */ + probe_ptr = TX_ULONG_POINTER_SUB(probe_ptr, 1); + } + } + } + + /* Determine if the probe location is in use. */ + if (fill_present == TX_FALSE) + { + + /* Update the stack highest, since we need to look in the upper half now. */ + stack_highest = stack_ptr; + } + else + { + + /* Update the stack lowest, since we need to look in the lower half now. */ + stack_lowest = stack_ptr; + } + + } while(size > ((ULONG) 1)); + + /* Position to first used word - at this point we are within a few words. */ + while ((stack_ptr < stack_limit) && (*stack_ptr == stack_fill)) + { + + /* Position to next word in stack. */ + stack_ptr = TX_ULONG_POINTER_ADD(stack_ptr, 1); + } + + return(stack_ptr); +} + + /**************************************************************************/ /* */ /* FUNCTION RELEASE */ @@ -72,11 +163,6 @@ TX_INTERRUPT_SAVE_AREA ULONG *stack_ptr; ULONG *stack_lowest; ULONG *stack_highest; -ULONG *probe_ptr; -ULONG *stack_limit; -ULONG probe_count; -UINT fill_present; -ULONG size; /* Disable interrupts. */ @@ -107,85 +193,10 @@ ULONG size; if ((stack_highest != TX_NULL) && (stack_highest > stack_lowest)) { - /* Remember the upper bound of the search so the scan below cannot run past it. */ - stack_limit = stack_highest; - /* Restore interrupts. */ TX_RESTORE - /* We need to binary search the remaining stack for missing 0xEFEFEFEF 32-bit data pattern. - This is a best effort algorithm to find the highest stack usage. */ - do - { - - /* Calculate the size again. */ - size = (ULONG) (TX_ULONG_POINTER_DIF(stack_highest, stack_lowest))/((ULONG) 2); - stack_ptr = TX_ULONG_POINTER_ADD(stack_lowest, size); - - /* Determine if the pattern is still there. To avoid stopping on an - unwritten hole inside an otherwise used region, require several - consecutive fill words, working towards the lowest address. The scan - stops at the lowest known fill location, since everything at or below - that point is already known to hold the fill pattern. */ - fill_present = TX_TRUE; - probe_ptr = stack_ptr; - probe_count = TX_THREAD_STACK_ANALYZE_FILL_WORDS; - while (probe_count != ((ULONG) 0)) - { - - /* Determine if this word still holds the fill pattern. */ - if (*probe_ptr != TX_STACK_FILL) - { - - /* No, the probe location is in use. */ - fill_present = TX_FALSE; - probe_count = ((ULONG) 0); - } - else - { - - /* Yes, account for this word. */ - probe_count--; - - /* Determine if the lowest known fill location has been reached. */ - if (probe_ptr <= stack_lowest) - { - - /* Yes, nothing further to check. */ - probe_count = ((ULONG) 0); - } - else - { - - /* Position to the previous word in the stack. */ - probe_ptr = TX_ULONG_POINTER_SUB(probe_ptr, 1); - } - } - } - - /* Determine if the probe location is in use. */ - if (fill_present == TX_FALSE) - { - - /* Update the stack highest, since we need to look in the upper half now. */ - stack_highest = stack_ptr; - } - else - { - - /* Update the stack lowest, since we need to look in the lower half now. */ - stack_lowest = stack_ptr; - } - - } while(size > ((ULONG) 1)); - - /* Position to first used word - at this point we are within a few words. */ - while ((stack_ptr < stack_limit) && (*stack_ptr == TX_STACK_FILL)) - { - - /* Position to next word in stack. */ - stack_ptr = TX_ULONG_POINTER_ADD(stack_ptr, 1); - } + stack_ptr = _tx_thread_stack_analyze_range(stack_lowest, stack_highest, TX_STACK_FILL); /* Optional processing extension. */ TX_THREAD_STACK_ANALYZE_EXTENSION diff --git a/test/smp/regression/threadx_thread_stack_checking_test.c b/test/smp/regression/threadx_thread_stack_checking_test.c index 2ef9a2df2..bc9e6a454 100644 --- a/test/smp/regression/threadx_thread_stack_checking_test.c +++ b/test/smp/regression/threadx_thread_stack_checking_test.c @@ -15,12 +15,7 @@ #include #include "tx_api.h" - - -/* Prototype for direct call. */ - -VOID _tx_thread_stack_analyze(TX_THREAD *thread_ptr); - +#include "tx_thread.h" static unsigned long thread_0_counter = 0; @@ -148,6 +143,27 @@ UINT status; /* Inform user of success getting to this test. */ printf("Running Thread Stack Checking Test.................................. "); +#ifdef TX_ENABLE_STACK_CHECKING + { + TX_THREAD *thread_ptr = &thread_2; + VOID *saved_pointer = thread_ptr -> tx_thread_stack_ptr; + VOID *saved_highest = thread_ptr -> tx_thread_stack_highest_ptr; + + /* Check the boundary on a suspended thread with intact guard words. */ + thread_ptr -> tx_thread_stack_ptr = thread_ptr -> tx_thread_stack_start; + thread_ptr -> tx_thread_stack_highest_ptr = thread_ptr -> tx_thread_stack_start; + TX_THREAD_STACK_CHECK(thread_ptr) + thread_ptr -> tx_thread_stack_ptr = saved_pointer; + thread_ptr -> tx_thread_stack_highest_ptr = saved_highest; + + if (stack_error != 0U) + { + printf("ERROR: stack check at the lower guard\n"); + test_control_return(1); + } + } +#endif + /* Resume thread 1 to get the stack checking to take place. */ status = tx_thread_resume(&thread_1); diff --git a/test/tx/cmake/module_manager/CMakeLists.txt b/test/tx/cmake/module_manager/CMakeLists.txt index 4c44597ee..c78ccecc1 100644 --- a/test/tx/cmake/module_manager/CMakeLists.txt +++ b/test/tx/cmake/module_manager/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.13 FATAL_ERROR) cmake_policy(SET CMP0057 NEW) project(module_manager_test LANGUAGES C) +enable_testing() set(REPO_ROOT ${CMAKE_CURRENT_LIST_DIR}/../../../..) set(SOURCE_DIR ${REPO_ROOT}/test/tx/module_manager) @@ -394,6 +395,39 @@ target_compile_options( add_test(${CMAKE_BUILD_TYPE}::threadx_module_manager_load_bounds_test threadx_module_manager_load_bounds_test) +add_executable( + threadx_module_manager_thread_stack_checking_test + ${SOURCE_DIR}/threadx_module_manager_thread_stack_checking_test.c + ${module_manager_dir}/src/txm_module_manager_thread_create.c + ${module_manager_dir}/src/txm_module_manager_object_allocate.c + ${module_manager_dir}/src/txm_module_manager_object_deallocate.c + ${module_manager_dir}/src/txm_module_manager_util.c + ${REPO_ROOT}/common/src/tx_thread_stack_analyze.c + ${REPO_ROOT}/common/src/tx_trace_initialize.c + ${REPO_ROOT}/common/src/tx_trace_object_register.c) + +target_include_directories( + threadx_module_manager_thread_stack_checking_test + PRIVATE ${SOURCE_DIR} + ${REPO_ROOT}/common/inc + ${REPO_ROOT}/common_modules/inc + ${module_manager_dir}/inc + ${cortex_a7_module_dir}/inc) + +target_compile_definitions( + threadx_module_manager_thread_stack_checking_test + PRIVATE TXM_MODULE_MANAGER + TX_ENABLE_STACK_CHECKING) + +target_compile_options( + threadx_module_manager_thread_stack_checking_test + PRIVATE -Wall + -Wextra + -include ${SOURCE_DIR}/threadx_module_manager_host_test_port.h) + +add_test(${CMAKE_BUILD_TYPE}::threadx_module_manager_thread_stack_checking_test + threadx_module_manager_thread_stack_checking_test) + # This test runs thread create and thread reset against the Cortex-A7 module port, # once as shipped and once with the port's MMU support compiled out, which a second # force-included header models. Neither calls a dispatcher. diff --git a/test/tx/cmake/module_qemu/CMakeLists.txt b/test/tx/cmake/module_qemu/CMakeLists.txt index 4f17345cb..5c928dd33 100644 --- a/test/tx/cmake/module_qemu/CMakeLists.txt +++ b/test/tx/cmake/module_qemu/CMakeLists.txt @@ -47,6 +47,7 @@ project(threadx_module_qemu LANGUAGES C ASM) set(MODULE_QEMU_PORT "cortex_m3" CACHE STRING "Arm module port to build and run") set_property(CACHE MODULE_QEMU_PORT PROPERTY STRINGS cortex_m3 cortex_m4 cortex_a7) option(MODULE_QEMU_POINTER_CONTEXT "Exercise the pointer-context module APIs" OFF) +option(MODULE_QEMU_STACK_CHECKING "Check the module and kernel stacks" OFF) if(MODULE_QEMU_PORT STREQUAL "cortex_m3") set(PORT_CPU_FLAGS -mcpu=cortex-m3 -mthumb) @@ -92,6 +93,9 @@ add_link_options(${PORT_CPU_FLAGS}) if(MODULE_QEMU_POINTER_CONTEXT) add_compile_definitions(TX_ENABLE_POINTER_CONTEXT) endif() +if(MODULE_QEMU_STACK_CHECKING) + add_compile_definitions(TX_ENABLE_STACK_CHECKING) +endif() # --------------------------------------------------------------------------- # The kernel and the Module Manager, built with the module port's headers. @@ -112,6 +116,7 @@ file(GLOB TXM_PORT_SRC file(GLOB TXM_MANAGER_SRC ${REPO_ROOT}/common_modules/module_manager/src/*.c) add_library(threadx_module STATIC ${TX_COMMON_SRC} ${TXM_PORT_SRC} ${TXM_MANAGER_SRC}) +target_compile_definitions(threadx_module PUBLIC TXM_MODULE_MANAGER) target_include_directories(threadx_module PUBLIC ${PORT_DIR}/inc ${REPO_ROOT}/common/inc diff --git a/test/tx/cmake/module_qemu/tests/test_module_lifecycle.c b/test/tx/cmake/module_qemu/tests/test_module_lifecycle.c index ed419762c..1ad8bb698 100644 --- a/test/tx/cmake/module_qemu/tests/test_module_lifecycle.c +++ b/test/tx/cmake/module_qemu/tests/test_module_lifecycle.c @@ -38,6 +38,17 @@ extern UCHAR module_services_protected[]; static TXM_MODULE_INSTANCE test_module; +#ifdef TX_ENABLE_STACK_CHECKING +static volatile ULONG stack_error_count; + +/* Count every stack error reported during a normal module lifecycle. */ +static VOID stack_error_notify(TX_THREAD *thread_ptr) +{ + TX_PARAMETER_NOT_USED(thread_ptr); + stack_error_count++; +} +#endif + #ifdef TX_ENABLE_POINTER_CONTEXT /* Timer messages with different callback types must remain separate. */ static VOID check_timer_message_types(VOID) @@ -92,6 +103,10 @@ ULONG available_before; harness_print("Module lifecycle: protected module, load/start/stop/unload twice\n"); +#ifdef TX_ENABLE_STACK_CHECKING + harness_expect("stack error notify registered", tx_thread_stack_error_notify(stack_error_notify), TX_SUCCESS); +#endif + #ifdef TX_ENABLE_POINTER_CONTEXT check_timer_message_types(); #endif @@ -129,4 +144,8 @@ ULONG available_before; #endif harness_expect("module memory returned", harness_module_pool_available(), available_before); } + +#ifdef TX_ENABLE_STACK_CHECKING + harness_expect("no stack errors during normal stack switches", stack_error_count, 0UL); +#endif } diff --git a/test/tx/module_manager/threadx_module_manager_thread_stack_checking_test.c b/test/tx/module_manager/threadx_module_manager_thread_stack_checking_test.c new file mode 100644 index 000000000..267137e23 --- /dev/null +++ b/test/tx/module_manager/threadx_module_manager_thread_stack_checking_test.c @@ -0,0 +1,486 @@ +/***************************************************************************/ +/* 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. */ +/* */ +/* SPDX-License-Identifier: MIT */ +/***************************************************************************/ +// Portions of this file were generated with AI assistance. + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Test */ +/** */ +/** Module Manager thread stack checking test */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#include + +#include "threadx_module_manager_host_test_port.h" + +#include "tx_thread.h" +#include "tx_timer.h" +#include "tx_queue.h" +#include "tx_semaphore.h" +#include "tx_mutex.h" +#include "tx_event_flags.h" +#include "tx_block_pool.h" +#include "tx_byte_pool.h" +#include "txm_module.h" +#include "txm_module_manager_util.h" + +/* Stand-in interrupt locks for the test port. */ +unsigned int test_interrupt_disable_depth; +unsigned int test_interrupt_disable_max_depth; +unsigned int test_interrupt_restore_underflows; + +/* Manager globals. */ +TX_BYTE_POOL _txm_module_manager_object_pool; +UINT _txm_module_manager_object_pool_created; +TX_MUTEX _txm_module_manager_mutex; +TX_THREAD *_tx_thread_current_ptr; + +/* Kernel created object lists. */ +TX_BLOCK_POOL *_tx_block_pool_created_ptr; +ULONG _tx_block_pool_created_count; +TX_BYTE_POOL *_tx_byte_pool_created_ptr; +ULONG _tx_byte_pool_created_count; +TX_EVENT_FLAGS_GROUP *_tx_event_flags_created_ptr; +ULONG _tx_event_flags_created_count; +TX_MUTEX *_tx_mutex_created_ptr; +ULONG _tx_mutex_created_count; +TX_QUEUE *_tx_queue_created_ptr; +ULONG _tx_queue_created_count; +TX_SEMAPHORE *_tx_semaphore_created_ptr; +ULONG _tx_semaphore_created_count; +TX_THREAD *_tx_thread_created_ptr; +ULONG _tx_thread_created_count; +TX_TIMER *_tx_timer_created_ptr; +ULONG _tx_timer_created_count; + +TX_THREAD *_tx_thread_execute_ptr; +volatile UINT _tx_thread_preempt_disable; +volatile ULONG _tx_thread_system_state; +TX_THREAD _tx_timer_thread; + +VOID _tx_thread_system_preempt_check(VOID) +{ +} + +VOID _tx_thread_system_resume(TX_THREAD *thread_ptr) +{ + (VOID) thread_ptr; +} + +VOID _tx_thread_timeout(ULONG timeout_input) +{ + (VOID) timeout_input; +} + +ALIGN_TYPE _txm_module_manager_kernel_dispatch(ULONG kernel_request, ALIGN_TYPE param_1, ALIGN_TYPE param_2, ALIGN_TYPE param_3) +{ + (VOID) kernel_request; + (VOID) param_1; + (VOID) param_2; + (VOID) param_3; + return(0); +} + +ALIGN_TYPE _txm_module_manager_user_mode_entry(ULONG kernel_request, ALIGN_TYPE param_1, ALIGN_TYPE param_2, ALIGN_TYPE param_3) +{ + (VOID) kernel_request; + (VOID) param_1; + (VOID) param_2; + (VOID) param_3; + return(0); +} + +#define TEST_POOL_BYTES 16384 + +static union +{ + ALIGN_TYPE test_pool_alignment; + UCHAR test_pool_bytes[TEST_POOL_BYTES]; +} test_pool_arena; + +static ULONG test_pool_offset; + +static ULONG test_checks; +static UINT test_failures; +static UINT test_stack_error_detected; + +VOID _tx_thread_stack_error_handler(TX_THREAD *thread_ptr) +{ + (VOID) thread_ptr; + test_stack_error_detected++; +} + +static VOID test_expect(const char *description, ULONG actual, ULONG expected) +{ + test_checks++; + if (actual != expected) + { + printf("FAIL: %s (expected %lu, got %lu)\n", description, + (unsigned long) expected, (unsigned long) actual); + test_failures++; + } +} + +/* Check that application threads in the same kernel still use their one stack. */ +static VOID check_single_stack(VOID) +{ + TX_THREAD thread; + TX_THREAD *thread_ptr = &thread; + ULONG stack[32]; + UINT index; + + TX_MEMSET(thread_ptr, 0, sizeof(TX_THREAD)); + for (index = 0U; index < 32U; index++) + { + stack[index] = TX_STACK_FILL; + } + thread_ptr -> tx_thread_stack_start = stack; + thread_ptr -> tx_thread_stack_end = ((UCHAR *) &stack[31]) - 1; + thread_ptr -> tx_thread_stack_highest_ptr = &stack[30]; + thread_ptr -> tx_thread_stack_ptr = &stack[28]; + + test_stack_error_detected = 0U; + TX_THREAD_STACK_CHECK(TX_NULL); + TX_THREAD_STACK_CHECK(thread_ptr); + test_expect("null and uncreated threads ignored", test_stack_error_detected, 0U); + + thread_ptr -> tx_thread_id = TX_THREAD_ID; + TX_THREAD_STACK_CHECK(thread_ptr); + test_expect("application stack checked", test_stack_error_detected, 0U); + test_expect("application watermark updated", + (ULONG) (thread_ptr -> tx_thread_stack_highest_ptr == &stack[28]), TX_TRUE); + + /* Search with a fill-only range, then report invalid saved and highest pointers. */ + _tx_thread_stack_analyze(thread_ptr); + thread_ptr -> tx_thread_stack_ptr = &stack[32]; + TX_THREAD_STACK_CHECK(thread_ptr); + test_expect("application saved pointer outside its stack reported", test_stack_error_detected, 1U); + + thread_ptr -> tx_thread_stack_ptr = &stack[28]; + thread_ptr -> tx_thread_stack_highest_ptr = TX_NULL; + TX_THREAD_STACK_CHECK(thread_ptr); + test_expect("application invalid watermark reported", test_stack_error_detected, 2U); +} + +UINT _txe_byte_allocate(TX_BYTE_POOL *pool_ptr, VOID **memory_ptr, ULONG memory_size, ULONG wait_option) +{ + ULONG rounded_size; + UCHAR *block_start; + + (VOID) pool_ptr; + (VOID) wait_option; + + rounded_size = (memory_size + (sizeof(ALIGN_TYPE) - 1U)) & (~(sizeof(ALIGN_TYPE) - 1U)); + if ((test_pool_offset + rounded_size) > ((ULONG) TEST_POOL_BYTES)) + { + return(TX_NO_MEMORY); + } + + block_start = &test_pool_arena.test_pool_bytes[test_pool_offset]; + test_pool_offset += rounded_size; + *memory_ptr = (VOID *) block_start; + + return(TX_SUCCESS); +} + +UINT _txe_byte_release(VOID *memory_ptr) +{ + (VOID) memory_ptr; + return(TX_SUCCESS); +} + +UINT _txe_mutex_get(TX_MUTEX *mutex_ptr, ULONG wait_option) +{ + (VOID) mutex_ptr; + (VOID) wait_option; + return(TX_SUCCESS); +} + +UINT _txe_mutex_put(TX_MUTEX *mutex_ptr) +{ + (VOID) mutex_ptr; + return(TX_SUCCESS); +} + +UINT _txm_module_manager_inside_data_check(TXM_MODULE_INSTANCE *module_instance, ALIGN_TYPE obj_ptr, + ULONG obj_size, UINT write_request) +{ + (VOID) module_instance; + (VOID) obj_ptr; + (VOID) obj_size; + (VOID) write_request; + return(TX_FALSE); +} + +UINT _txm_module_manager_outside_data_check(TXM_MODULE_INSTANCE *module_instance, ALIGN_TYPE obj_ptr, + ULONG obj_size) +{ + (VOID) module_instance; + (VOID) obj_ptr; + (VOID) obj_size; + return(TX_TRUE); +} + +VOID _txm_module_manager_alignment_adjust(TXM_MODULE_PREAMBLE *module_preamble, ULONG *code_size, + ULONG *code_alignment, ULONG *data_size, ULONG *data_alignment) +{ + (VOID) module_preamble; + (VOID) code_size; + (VOID) code_alignment; + (VOID) data_size; + (VOID) data_alignment; +} + +VOID _txm_module_manager_thread_stack_build(TX_THREAD *thread_ptr, + VOID (*shell_function)(TX_THREAD *, TXM_MODULE_INSTANCE *)) +{ + (VOID) shell_function; + thread_ptr -> tx_thread_stack_ptr = (VOID *) (((ALIGN_TYPE) thread_ptr -> tx_thread_stack_end) + 1U - 64U); +} + +static VOID dummy_shell(TX_THREAD *thread_ptr, TXM_MODULE_INSTANCE *module_instance) +{ + (VOID) thread_ptr; + (VOID) module_instance; +} + +static VOID dummy_entry(ULONG id) +{ + (VOID) id; +} + +UINT _txe_thread_create(TX_THREAD *thread_ptr, TX_NAME_CONST CHAR *name_ptr, + VOID (*entry_function)(ULONG id), ULONG entry_input, + VOID *stack_start, ULONG stack_size, + UINT priority, UINT preempt_threshold, + ULONG time_slice, UINT auto_start, UINT thread_control_block_size) +{ + (VOID) entry_function; + (VOID) entry_input; + (VOID) priority; + (VOID) preempt_threshold; + (VOID) time_slice; + (VOID) auto_start; + (VOID) thread_control_block_size; + + thread_ptr -> tx_thread_id = TX_THREAD_ID; + thread_ptr -> tx_thread_name = (CHAR *) name_ptr; + thread_ptr -> tx_thread_stack_start = stack_start; + thread_ptr -> tx_thread_stack_end = (VOID *) (((ALIGN_TYPE) stack_start) + stack_size - 1); + thread_ptr -> tx_thread_stack_size = stack_size; + thread_ptr -> tx_thread_stack_ptr = (VOID *) (((ALIGN_TYPE) stack_start) + stack_size - 64); + thread_ptr -> tx_thread_stack_highest_ptr = thread_ptr -> tx_thread_stack_ptr; + + return(TX_SUCCESS); +} + +int main(void) +{ + TXM_MODULE_INSTANCE module_instance; + TX_THREAD thread; + TX_THREAD *thread_ptr = &thread; + UINT status; + ULONG user_stack[512]; + ULONG kernel_stack_size; + ULONG *guard_word_ptr; + VOID *saved_user_stack_start; + VOID *saved_user_stack_end; + VOID *saved_user_stack_highest_ptr; + ULONG *user_guard_ptr; + VOID *saved_kernel_pointer; + + setvbuf(stdout, TX_NULL, _IONBF, 0); + + test_failures = 0U; + test_checks = 0U; + test_pool_offset = 0U; + test_stack_error_detected = 0U; + + _txm_module_manager_object_pool.tx_byte_pool_id = TX_BYTE_POOL_ID; + _txm_module_manager_object_pool.tx_byte_pool_start = test_pool_arena.test_pool_bytes; + _txm_module_manager_object_pool.tx_byte_pool_size = TEST_POOL_BYTES; + _txm_module_manager_object_pool.tx_byte_pool_available = TEST_POOL_BYTES; + _txm_module_manager_object_pool_created = TX_TRUE; + + TX_MEMSET(&module_instance, 0, sizeof(TXM_MODULE_INSTANCE)); + module_instance.txm_module_instance_property_flags = TXM_MODULE_USER_MODE | TXM_MODULE_MEMORY_PROTECTION; + + /* Start with a different value so the test observes stack initialization. */ + TX_MEMSET(user_stack, 0, sizeof(user_stack)); + + /* Create module thread with TX_DONT_START. */ + status = _txm_module_manager_thread_create(thread_ptr, "test_thread", + dummy_shell, dummy_entry, 0, + user_stack, sizeof(user_stack), + 1, 1, TX_NO_TIME_SLICE, TX_DONT_START, + sizeof(TX_THREAD), &module_instance); + + test_expect("thread create status success", (ULONG) status, (ULONG) TX_SUCCESS); + test_expect("kernel stack allocated", (ULONG) (thread_ptr -> tx_thread_module_kernel_stack_start != TX_NULL), (ULONG) TX_TRUE); + + if (thread_ptr -> tx_thread_module_kernel_stack_start == TX_NULL) + { + printf("ERROR: Kernel stack allocation failed\n"); + return(1); + } + + saved_user_stack_start = thread_ptr -> tx_thread_stack_start; + saved_user_stack_end = thread_ptr -> tx_thread_stack_end; + saved_user_stack_highest_ptr = thread_ptr -> tx_thread_stack_highest_ptr; + user_guard_ptr = (ULONG *) (((UCHAR *) saved_user_stack_end) + 1); + + /* Inspect the guard words left by thread creation. */ + test_expect("module lower guard initialized", *(ULONG *) saved_user_stack_start, TX_STACK_FILL); + test_expect("module upper guard initialized", *user_guard_ptr, TX_STACK_FILL); + kernel_stack_size = thread_ptr -> tx_thread_module_kernel_stack_size; + test_expect("kernel stack size valid", (ULONG) (kernel_stack_size > 0), (ULONG) TX_TRUE); + guard_word_ptr = (ULONG *) thread_ptr -> tx_thread_module_kernel_stack_end; + test_expect("kernel lower guard initialized", *(ULONG *) thread_ptr -> tx_thread_module_kernel_stack_start, + TX_STACK_FILL); + test_expect("kernel upper guard initialized", *guard_word_ptr, TX_STACK_FILL); + + test_expect("kernel stack highest ptr initialized to end", + (ULONG) (ALIGN_TYPE) thread_ptr -> tx_thread_module_kernel_stack_highest_ptr, + (ULONG) (ALIGN_TYPE) thread_ptr -> tx_thread_module_kernel_stack_end); + + /* 1. Test stack checking in user mode. */ + test_stack_error_detected = 0; + TX_THREAD_STACK_CHECK(thread_ptr); + test_expect("stack check in user mode succeeds without error", (ULONG) test_stack_error_detected, 0); + + /* The kernel is active before the scheduler replaces the saved module pointer. */ + thread_ptr -> tx_thread_module_current_user_mode = 0U; + thread_ptr -> tx_thread_stack_start = thread_ptr -> tx_thread_module_kernel_stack_start; + thread_ptr -> tx_thread_stack_end = thread_ptr -> tx_thread_module_kernel_stack_end; + saved_kernel_pointer = thread_ptr -> tx_thread_module_kernel_stack_highest_ptr; + + TX_THREAD_STACK_CHECK(thread_ptr); + test_expect("saved module pointer while kernel is active", test_stack_error_detected, 0U); + test_expect("saved module pointer left kernel watermark intact", + (ULONG) (thread_ptr -> tx_thread_module_kernel_stack_highest_ptr == saved_kernel_pointer), TX_TRUE); + + /* Save a kernel context and then restore the module's stack metadata. */ + saved_kernel_pointer = guard_word_ptr - 32; + thread_ptr -> tx_thread_stack_ptr = saved_kernel_pointer; + thread_ptr -> tx_thread_module_current_user_mode = thread_ptr -> tx_thread_module_user_mode; + thread_ptr -> tx_thread_stack_start = saved_user_stack_start; + thread_ptr -> tx_thread_stack_end = saved_user_stack_end; + TX_THREAD_STACK_CHECK(thread_ptr); + test_expect("saved kernel pointer while module is active", test_stack_error_detected, 0U); + test_expect("saved kernel pointer updates kernel watermark", + (ULONG) (thread_ptr -> tx_thread_module_kernel_stack_highest_ptr == saved_kernel_pointer), TX_TRUE); + test_expect("saved kernel pointer leaves module watermark intact", + (ULONG) (thread_ptr -> tx_thread_stack_highest_ptr == saved_user_stack_highest_ptr), TX_TRUE); + + /* 3. Test return to user mode. */ + thread_ptr -> tx_thread_module_current_user_mode = thread_ptr -> tx_thread_module_user_mode; + thread_ptr -> tx_thread_stack_start = saved_user_stack_start; + thread_ptr -> tx_thread_stack_end = saved_user_stack_end; + thread_ptr -> tx_thread_stack_ptr = saved_user_stack_highest_ptr; + + TX_THREAD_STACK_CHECK(thread_ptr); + test_expect("stack check after returning to user mode succeeds without spurious error", (ULONG) test_stack_error_detected, 0); + + /* 4. Test stack error detection on corrupted kernel guard pattern. */ + thread_ptr -> tx_thread_module_current_user_mode = 0; + thread_ptr -> tx_thread_stack_start = thread_ptr -> tx_thread_module_kernel_stack_start; + thread_ptr -> tx_thread_stack_end = thread_ptr -> tx_thread_module_kernel_stack_end; + thread_ptr -> tx_thread_stack_ptr = (VOID *) (((ALIGN_TYPE) thread_ptr -> tx_thread_module_kernel_stack_end) - 128); + + test_stack_error_detected = 0; + *guard_word_ptr = 0x12345678; + TX_THREAD_STACK_CHECK(thread_ptr); + test_expect("corrupted kernel guard detected", (ULONG) test_stack_error_detected, 1); + *guard_word_ptr = TX_STACK_FILL; + + /* 5. Test stack error detection on kernel stack overflow (SP below kernel stack start). */ + thread_ptr -> tx_thread_module_current_user_mode = 0; + test_stack_error_detected = 0; + thread_ptr -> tx_thread_stack_ptr = (VOID *) (((ALIGN_TYPE) thread_ptr -> tx_thread_module_kernel_stack_start) - 4); + TX_THREAD_STACK_CHECK(thread_ptr); + test_expect("kernel stack overflow detected", (ULONG) test_stack_error_detected, 1); + + /* 6. Test stack error detection on user stack guard pattern corruption. */ + thread_ptr -> tx_thread_module_current_user_mode = thread_ptr -> tx_thread_module_user_mode; + thread_ptr -> tx_thread_stack_start = saved_user_stack_start; + thread_ptr -> tx_thread_stack_end = saved_user_stack_end; + thread_ptr -> tx_thread_stack_ptr = saved_user_stack_highest_ptr; + + test_stack_error_detected = 0; + *user_guard_ptr = 0xDEADBEEF; + TX_THREAD_STACK_CHECK(thread_ptr); + test_expect("corrupted user guard detected", (ULONG) test_stack_error_detected, 1); + *user_guard_ptr = TX_STACK_FILL; + + /* 7. Test stack error detection on user stack overflow. */ + thread_ptr -> tx_thread_module_current_user_mode = thread_ptr -> tx_thread_module_user_mode; + test_stack_error_detected = 0; + thread_ptr -> tx_thread_stack_ptr = (VOID *) (((ALIGN_TYPE) saved_user_stack_start) - 4); + TX_THREAD_STACK_CHECK(thread_ptr); + test_expect("user stack overflow detected", (ULONG) test_stack_error_detected, 1); + + /* Find used words on both stacks when the saved pointer is still on the module stack. */ + thread_ptr -> tx_thread_stack_ptr = saved_user_stack_highest_ptr; + thread_ptr -> tx_thread_stack_highest_ptr = saved_user_stack_highest_ptr; + thread_ptr -> tx_thread_module_kernel_stack_highest_ptr = saved_kernel_pointer; + ((ULONG *) saved_user_stack_highest_ptr)[-1] = 0UL; + ((ULONG *) saved_kernel_pointer)[-1] = 0UL; + test_stack_error_detected = 0U; + TX_THREAD_STACK_CHECK(thread_ptr); + test_expect("scan checked both stacks without error", test_stack_error_detected, 0U); + test_expect("module scan lowered its watermark", + (ULONG) (thread_ptr -> tx_thread_stack_highest_ptr == ((ULONG *) saved_user_stack_highest_ptr - 1)), + TX_TRUE); + test_expect("kernel scan lowered its watermark", + (ULONG) (thread_ptr -> tx_thread_module_kernel_stack_highest_ptr == ((ULONG *) saved_kernel_pointer - 1)), + TX_TRUE); + + /* 10. Test kernel stack check under TXM_MODULE_KERNEL_STACK_MAINTENANCE_DISABLE scenario. + When stack maintenance is disabled, stack_start is not updated by assembly scheduler, + so stack_start remains pointing to user stack start while current_user_mode is 0. */ + thread_ptr -> tx_thread_module_current_user_mode = 0; + thread_ptr -> tx_thread_stack_start = saved_user_stack_start; + thread_ptr -> tx_thread_stack_end = saved_user_stack_end; + thread_ptr -> tx_thread_stack_ptr = (VOID *) (((ALIGN_TYPE) thread_ptr -> tx_thread_module_kernel_stack_end) - 64); + test_stack_error_detected = 0; + TX_THREAD_STACK_CHECK(thread_ptr); + test_expect("kernel stack check succeeds when stack maintenance is disabled", (ULONG) test_stack_error_detected, 0); + + /* 11. Test boundary condition where highest_ptr equals stack_start (under-read guard). */ + thread_ptr -> tx_thread_module_current_user_mode = 0; + thread_ptr -> tx_thread_stack_start = thread_ptr -> tx_thread_module_kernel_stack_start; + thread_ptr -> tx_thread_stack_end = thread_ptr -> tx_thread_module_kernel_stack_end; + thread_ptr -> tx_thread_stack_ptr = thread_ptr -> tx_thread_module_kernel_stack_start; + thread_ptr -> tx_thread_module_kernel_stack_highest_ptr = thread_ptr -> tx_thread_module_kernel_stack_start; + test_stack_error_detected = 0; + TX_THREAD_STACK_CHECK(thread_ptr); + test_expect("boundary condition highest_ptr == stack_start handled safely", (ULONG) test_stack_error_detected, 0); + + *(ULONG *) thread_ptr -> tx_thread_module_kernel_stack_start = 0UL; + TX_THREAD_STACK_CHECK(thread_ptr); + test_expect("kernel lower guard checked", test_stack_error_detected, 1U); + *(ULONG *) thread_ptr -> tx_thread_module_kernel_stack_start = TX_STACK_FILL; + + check_single_stack(); + test_expect("interrupt restores balanced", test_interrupt_disable_depth, 0U); + test_expect("no interrupt restore underflow", test_interrupt_restore_underflows, 0U); + + if (test_failures == 0U) + { + printf("SUCCESS! %lu expectations\n", (unsigned long) test_checks); + return(0); + } + + printf("ERROR: %u expectation(s) failed of %lu\n", test_failures, (unsigned long) test_checks); + return(1); +} diff --git a/test/tx/regression/threadx_thread_stack_checking_test.c b/test/tx/regression/threadx_thread_stack_checking_test.c index 2ef9a2df2..bc9e6a454 100644 --- a/test/tx/regression/threadx_thread_stack_checking_test.c +++ b/test/tx/regression/threadx_thread_stack_checking_test.c @@ -15,12 +15,7 @@ #include #include "tx_api.h" - - -/* Prototype for direct call. */ - -VOID _tx_thread_stack_analyze(TX_THREAD *thread_ptr); - +#include "tx_thread.h" static unsigned long thread_0_counter = 0; @@ -148,6 +143,27 @@ UINT status; /* Inform user of success getting to this test. */ printf("Running Thread Stack Checking Test.................................. "); +#ifdef TX_ENABLE_STACK_CHECKING + { + TX_THREAD *thread_ptr = &thread_2; + VOID *saved_pointer = thread_ptr -> tx_thread_stack_ptr; + VOID *saved_highest = thread_ptr -> tx_thread_stack_highest_ptr; + + /* Check the boundary on a suspended thread with intact guard words. */ + thread_ptr -> tx_thread_stack_ptr = thread_ptr -> tx_thread_stack_start; + thread_ptr -> tx_thread_stack_highest_ptr = thread_ptr -> tx_thread_stack_start; + TX_THREAD_STACK_CHECK(thread_ptr) + thread_ptr -> tx_thread_stack_ptr = saved_pointer; + thread_ptr -> tx_thread_stack_highest_ptr = saved_highest; + + if (stack_error != 0U) + { + printf("ERROR: stack check at the lower guard\n"); + test_control_return(1); + } + } +#endif + /* Resume thread 1 to get the stack checking to take place. */ status = tx_thread_resume(&thread_1);