cortex-m0, an attempted store to an unaligned address in thread_create


I have observed an attempted store to an unaligned address in thread_create that causes a hard_fault.

In thread_create: tcb_address will be aligned on 32-bit boundary but stackp not. If a threads stack is placed not aligned in memory then   *stackp = (unsignedint)stackp; causes a hard_fault on cortex-m0.

I can bypass the problem with: char stack [STACK_SIZE] __attribute__ ((aligned(32)));

I think gcc can't see 32-bit access in thread_create and places stack unaligned.


Johann Fischer

Hi Johann,

thanks for your report of the bug. I just got my cortex-m0 port up and I stumbled across the exactly same error. I am not quite sure about the best solution for a fix, but the aligned attribute fixes it at least for the short term. I'll let you know about my solution once I find one...

Cheers, Hauke

Hello Hauke,

there is a useful gcc option -Wcast-align: "Warn whenever a pointer is cast such that the required alignment of the target is increased. For example, warn if a char * is cast to an int * on machines where integers can only be accessed at two- or four-byte boundaries."   -- source gcc-docu

I have tested this one currently:

diff --git a/core/include/thread.h b/core/include/thread.h index 065f172..4e0f952 100644 --- a/core/include/thread.h +++ b/core/include/thread.h @@ -34,6 +34,14 @@ #define MINIMUM_STACK_SIZE (sizeof(tcb_t)) #endif

+#if defined(ARMV6M) + typedef uint32_t stack_t; +# define ARCH_STACK_SIZE(s) (s>>2) +#else + typedef uint8_t stack_t; +# define ARCH_STACK_SIZE(s) (s) +#endif