arch: Add stack alignment and stack size checking when CONFIG_TLS_ALIGNED=y

Add validation to ensure allocated stack size does not exceed TLS_MAXSTACK when
CONFIG_TLS_ALIGNED is enabled, and verify proper stack alignment using STACK_ALIGN_MASK
across all architectures. This improves stack safety and prevents potential TLS overflow conditions.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
This commit is contained in:
hujun5 2025-06-23 17:06:05 +08:00 committed by Xiang Xiao
parent 3522ee372d
commit a3273e6a96
20 changed files with 245 additions and 41 deletions

View file

@ -77,10 +77,20 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -88,10 +88,20 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -74,10 +74,20 @@
int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
{
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -76,10 +76,20 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -71,13 +71,21 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
uintptr_t top_of_stack;
size_t size_of_stack;
#ifdef CONFIG_TLS
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT((uintptr_t)stack & (TLS_STACK_ALIGN - 1) == 0);
#else
DEBUGASSERT((uintptr_t)stack & STACK_ALIGN_MASK == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -75,10 +75,20 @@ int up_use_stack(FAR struct tcb_s *tcb, FAR void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -76,10 +76,20 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -76,10 +76,20 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -76,10 +76,20 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -76,10 +76,20 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -76,10 +76,20 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -76,10 +76,20 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -75,10 +75,20 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -73,6 +73,22 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t top_of_stack;
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */
if (tcb->stack_alloc_ptr)

View file

@ -76,10 +76,20 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -76,10 +76,20 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -76,10 +76,20 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -76,10 +76,20 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -76,10 +76,20 @@ int up_use_stack(FAR struct tcb_s *tcb, FAR void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */

View file

@ -75,10 +75,20 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Make certain that the user provided stack is properly aligned */
DEBUGASSERT(((uintptr_t)stack & TLS_STACK_MASK) == 0);
#endif
DEBUGASSERT(((uintptr_t)stack & STACK_ALIGN_MASK) == 0);
/* Is there already a stack allocated? */