Useful patch to integrate

Asked by Ahmed Ammar

http://patchwork.ozlabs.org/patch/115832/

Very useful for cortex-m stack checking. I have ported it to gcc-arm-none-eabi-4_9-2015q1-20150306, if you want the patch.

Question information

Language:
English Edit question
Status:
Expired
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Tony Liu (mrtoniliu) said :
#1

Hi,

Would you provide a small demo to demonstrate the usage of this patch? It seems this is a very old patch (originally proposed in 2011) and did not get enough response in gcc patch mailing list.

BR,
Tony

Revision history for this message
Ahmed Ammar (ahmed-a-ammar) said :
#2

Yes it didn't get much attention from anyone maintaining ARM, and requires someone with better gcc knowledge than me to do a complete port to 4.9. I have the direct path using a global register working, but the path using a symbol lookup is failing badly.

Not sure what you're expecting for a 'demo', but we have custom O/S and we don't use the MPU, so checking/detecting if any thread overflows the allocated stack this patch/approach is very useful. Basically I reserver a register for this (but like I mentioned a symbol could be used too) and on every context switch and change the value to be the base of the stack for that thread, gcc then generates a check and jump if the stack is overflowed in every function prologue, e.g.:

ifeq ($(CONFIG_GCC_STACK_CHECK), y)
 EXTRA_CFLAGS += -ffixed-r10
 EXTRA_CFLAGS += -fstack-check=direct -fstack-limit-register=r10
endif

void *ktable_alloc(ktable_t *kt)
{
     3a8: e92d 43f0 stmdb sp!, {r4, r5, r6, r7, r8, r9, lr}
     3ac: 45d5 cmp sp, sl
     3ae: d201 bcs.n 3b4 <ktable_alloc+0xc>
     3b0: f004 ffc6 bl 5340 <__thumb_stack_failure>

So, if the stack pointer has overflowed the call to __thumb_stack_failure is triggered.

Regards,
Ahmed.

Revision history for this message
Tony Liu (mrtoniliu) said :
#3

Hi,

We will study this patch to see whether we can port it or not. Thanks for the information.

BR,
Tony

Revision history for this message
Ahmed Ammar (ahmed-a-ammar) said :
#4

http://pastebin.com/NxhTXDjc

Just wanted to share my version of the patch in-case it's useful, it makes the path I gave an example to above work. Can we create a bug/feature request for this?

Thanks,
Ahmed

Revision history for this message
Ahmed Ammar (ahmed-a-ammar) said :
#5

An updated patch, symbol lookup working too.

ifeq ($(CONFIG_GCC_STACK_CHECK), y)
 EXTRA_CFLAGS += -fstack-check=indirect -fstack-limit-symbol=current_stack
endif

http://pastebin.com/raw.php?i=ujMY6R5F

Revision history for this message
Launchpad Janitor (janitor) said :
#6

This question was expired because it remained in the 'Open' state without activity for the last 15 days.