From a3273e6a96d92a577e333f12bee3c0a35aef9ca3 Mon Sep 17 00:00:00 2001 From: hujun5 Date: Mon, 23 Jun 2025 17:06:05 +0800 Subject: [PATCH] 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 --- arch/arm/src/common/arm_usestack.c | 14 ++++++++++++-- arch/arm64/src/common/arm64_usestack.c | 14 ++++++++++++-- arch/avr/src/avr/avr_usestack.c | 14 ++++++++++++-- arch/avr/src/avr32/avr_usestack.c | 14 ++++++++++++-- arch/ceva/src/common/ceva_usestack.c | 18 +++++++++++++----- arch/hc/src/common/hc_usestack.c | 14 ++++++++++++-- arch/mips/src/common/mips_usestack.c | 14 ++++++++++++-- arch/misoc/src/lm32/lm32_usestack.c | 14 ++++++++++++-- arch/misoc/src/minerva/minerva_usestack.c | 14 ++++++++++++-- arch/or1k/src/common/or1k_usestack.c | 14 ++++++++++++-- arch/renesas/src/common/renesas_usestack.c | 14 ++++++++++++-- arch/risc-v/src/common/riscv_usestack.c | 14 ++++++++++++-- arch/sim/src/sim/sim_usestack.c | 14 ++++++++++++-- arch/sparc/src/common/sparc_usestack.c | 16 ++++++++++++++++ arch/tricore/src/common/tricore_usestack.c | 14 ++++++++++++-- arch/x86/src/i486/i486_usestack.c | 14 ++++++++++++-- arch/x86_64/src/intel64/intel64_usestack.c | 14 ++++++++++++-- arch/xtensa/src/common/xtensa_usestack.c | 14 ++++++++++++-- arch/z16/src/common/z16_usestack.c | 14 ++++++++++++-- arch/z80/src/common/z80_usestack.c | 14 ++++++++++++-- 20 files changed, 245 insertions(+), 41 deletions(-) diff --git a/arch/arm/src/common/arm_usestack.c b/arch/arm/src/common/arm_usestack.c index 52e6e44d37e..8c2661c4fe8 100644 --- a/arch/arm/src/common/arm_usestack.c +++ b/arch/arm/src/common/arm_usestack.c @@ -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? */ diff --git a/arch/arm64/src/common/arm64_usestack.c b/arch/arm64/src/common/arm64_usestack.c index eaa0a37c9e0..9784515c083 100644 --- a/arch/arm64/src/common/arm64_usestack.c +++ b/arch/arm64/src/common/arm64_usestack.c @@ -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? */ diff --git a/arch/avr/src/avr/avr_usestack.c b/arch/avr/src/avr/avr_usestack.c index 99bca205ae9..6a2c9934bfb 100644 --- a/arch/avr/src/avr/avr_usestack.c +++ b/arch/avr/src/avr/avr_usestack.c @@ -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? */ diff --git a/arch/avr/src/avr32/avr_usestack.c b/arch/avr/src/avr32/avr_usestack.c index efc77f8f1b0..bd811737b88 100644 --- a/arch/avr/src/avr32/avr_usestack.c +++ b/arch/avr/src/avr32/avr_usestack.c @@ -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? */ diff --git a/arch/ceva/src/common/ceva_usestack.c b/arch/ceva/src/common/ceva_usestack.c index cc0690b7919..e9b19a8bc20 100644 --- a/arch/ceva/src/common/ceva_usestack.c +++ b/arch/ceva/src/common/ceva_usestack.c @@ -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? */ diff --git a/arch/hc/src/common/hc_usestack.c b/arch/hc/src/common/hc_usestack.c index e265fa32e7e..495931e43d9 100644 --- a/arch/hc/src/common/hc_usestack.c +++ b/arch/hc/src/common/hc_usestack.c @@ -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? */ diff --git a/arch/mips/src/common/mips_usestack.c b/arch/mips/src/common/mips_usestack.c index e7a55af5142..5768e46cf41 100644 --- a/arch/mips/src/common/mips_usestack.c +++ b/arch/mips/src/common/mips_usestack.c @@ -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? */ diff --git a/arch/misoc/src/lm32/lm32_usestack.c b/arch/misoc/src/lm32/lm32_usestack.c index b4bbceba773..5c77d5a4f53 100644 --- a/arch/misoc/src/lm32/lm32_usestack.c +++ b/arch/misoc/src/lm32/lm32_usestack.c @@ -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? */ diff --git a/arch/misoc/src/minerva/minerva_usestack.c b/arch/misoc/src/minerva/minerva_usestack.c index ef40ffa8abc..3e55fa4449f 100644 --- a/arch/misoc/src/minerva/minerva_usestack.c +++ b/arch/misoc/src/minerva/minerva_usestack.c @@ -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? */ diff --git a/arch/or1k/src/common/or1k_usestack.c b/arch/or1k/src/common/or1k_usestack.c index 93a57e0b2ac..b7f42b83aea 100644 --- a/arch/or1k/src/common/or1k_usestack.c +++ b/arch/or1k/src/common/or1k_usestack.c @@ -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? */ diff --git a/arch/renesas/src/common/renesas_usestack.c b/arch/renesas/src/common/renesas_usestack.c index 10951eb7742..c02d5147d62 100644 --- a/arch/renesas/src/common/renesas_usestack.c +++ b/arch/renesas/src/common/renesas_usestack.c @@ -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? */ diff --git a/arch/risc-v/src/common/riscv_usestack.c b/arch/risc-v/src/common/riscv_usestack.c index 985a89bf599..bd4c8fea4c5 100644 --- a/arch/risc-v/src/common/riscv_usestack.c +++ b/arch/risc-v/src/common/riscv_usestack.c @@ -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? */ diff --git a/arch/sim/src/sim/sim_usestack.c b/arch/sim/src/sim/sim_usestack.c index a47ff3425e0..b0de2897f43 100644 --- a/arch/sim/src/sim/sim_usestack.c +++ b/arch/sim/src/sim/sim_usestack.c @@ -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? */ diff --git a/arch/sparc/src/common/sparc_usestack.c b/arch/sparc/src/common/sparc_usestack.c index 1f13be3518b..93149b11d3b 100644 --- a/arch/sparc/src/common/sparc_usestack.c +++ b/arch/sparc/src/common/sparc_usestack.c @@ -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) diff --git a/arch/tricore/src/common/tricore_usestack.c b/arch/tricore/src/common/tricore_usestack.c index 3c00ca7cdf3..8bdd6a1fc16 100644 --- a/arch/tricore/src/common/tricore_usestack.c +++ b/arch/tricore/src/common/tricore_usestack.c @@ -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? */ diff --git a/arch/x86/src/i486/i486_usestack.c b/arch/x86/src/i486/i486_usestack.c index 4930a4f9b2d..f275169d909 100644 --- a/arch/x86/src/i486/i486_usestack.c +++ b/arch/x86/src/i486/i486_usestack.c @@ -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? */ diff --git a/arch/x86_64/src/intel64/intel64_usestack.c b/arch/x86_64/src/intel64/intel64_usestack.c index d1c4462b217..a5654d1631c 100644 --- a/arch/x86_64/src/intel64/intel64_usestack.c +++ b/arch/x86_64/src/intel64/intel64_usestack.c @@ -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? */ diff --git a/arch/xtensa/src/common/xtensa_usestack.c b/arch/xtensa/src/common/xtensa_usestack.c index 977248cd183..cd1ff14491e 100644 --- a/arch/xtensa/src/common/xtensa_usestack.c +++ b/arch/xtensa/src/common/xtensa_usestack.c @@ -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? */ diff --git a/arch/z16/src/common/z16_usestack.c b/arch/z16/src/common/z16_usestack.c index 357697789d8..aa00ecb5225 100644 --- a/arch/z16/src/common/z16_usestack.c +++ b/arch/z16/src/common/z16_usestack.c @@ -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? */ diff --git a/arch/z80/src/common/z80_usestack.c b/arch/z80/src/common/z80_usestack.c index b0a46aac71b..96c2a717009 100644 --- a/arch/z80/src/common/z80_usestack.c +++ b/arch/z80/src/common/z80_usestack.c @@ -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? */