Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/module_qemu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,14 @@ jobs:
status=0
for port in cortex_m3 cortex_m4; 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
Expand Down
4 changes: 4 additions & 0 deletions common/inc/tx_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion common/inc/tx_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* SPDX-License-Identifier: MIT
**************************************************************************/
// Portions of this file were generated with AI assistance.


/**************************************************************************/
Expand Down Expand Up @@ -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 \
Expand All @@ -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)); \
Expand All @@ -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). */
Expand Down
3 changes: 2 additions & 1 deletion common/inc/tx_user_sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion common/src/tx_misra.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* SPDX-License-Identifier: MIT
**************************************************************************/
// Portions of this file were generated with AI assistance.


/**************************************************************************/
Expand Down Expand Up @@ -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));
Expand Down
173 changes: 92 additions & 81 deletions common/src/tx_thread_stack_analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions common_modules/inc/txm_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading