[PATCH 4/4] score: Ensure stack alignment requirement

Sebastian Huber sebastian.huber at embedded-brains.de
Thu Mar 4 08:01:22 UTC 2021

On 03/03/2021 23:08, Gedare Bloom wrote:

> I think it is just invalid to shrink the stack size smaller than it is
> requested.
> If I call pthread_attr_setstacksize(), then "The stacksize attribute
> shall define the minimum stack size (in bytes) allocated for the
> created threads stack." I should not get back a stack size smaller
> than I requested when I create the pthread. It's just wrong.
> https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_getstacksize.html

Allocated is one thing, usable is another thing. Stacks are 
architecture-dependent. For example on powerpc, you have to set up an 
initial stack frame at the top of the stack:

void _CPU_Context_Initialize(
   Context_Control  *the_context,
   void             *stack_base,
   size_t            size,
   uint32_t          new_level,
   void             *entry_point,
   bool              is_fp,
   void             *tls_area
   ppc_context *the_ppc_context;
   uint32_t   msr_value = 0;
   uintptr_t  sp;
   uintptr_t  stack_alignment;

   sp = (uintptr_t) stack_base + size - PPC_MINIMUM_STACK_FRAME_SIZE;

   stack_alignment = CPU_STACK_ALIGNMENT;
   sp &= ~(stack_alignment - 1);

   sp = (uintptr_t) memset((void *) sp, 0, PPC_MINIMUM_STACK_FRAME_SIZE);

Here we align the top of stack and set the initial frame.

I am not sure which actual problem we have to solve currently. Is this 
related to your CANARY example? In order to support things like this, we 
probably need an API to get the usable/actual/whatever stack area for a 

If you want to enforce that a user-provided size is the minimum 
allocated size, then I think we have to align it up to the next stack 
boundary. This makes sense from my point of view, however, this is not 
the purpose of the current patch, it addresses issues related to the 
heap allocator vs. the required stack alignment.

embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
email: sebastian.huber at embedded-brains.de
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:

More information about the devel mailing list