Detect stack overflow

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
YoIs_1298666
Level 5
Level 5
250 sign-ins 100 replies posted 100 sign-ins

Hello,

We would like to prepare the detection of stack overflow.

We will clear the last 4bytes of stack pointer address to 0x00000000 . And  we will detect the stack overlow by reading these 4bytes whether stack pointer has been written.

How to read/write the last 4bytes

    *(reg32 *)(CYDEV_SRAM_BASE + CYDEV_SRAM_SIZE - CYDEV_STACK_SIZE)

Is this correct?

for example

[In case of clearing the last 4bytes of stack pointer to 0x00000000]

        uint32 check_sp_ov;

                 ......

        check_sp_ov = 0x00000000;

        *(reg32 *)(CYDEV_SRAM_BASE + CYDEV_SRAM_SIZE - CYDEV_STACK_SIZE) = check_sp_ov;

[In case of checking the last 4bytes of stack pointer]

        if( *(reg32 *)(CYDEV_SRAM_BASE + CYDEV_SRAM_SIZE - CYDEV_STACK_SIZE) != 0x0)

        {

               error processing code

        }

Best regards,

Yocchi

0 Likes
1 Solution

Yes, you can.

AN89056 - PSoCR 4 - IEC 60730 Class B and IEC 61508 SIL Safety Software Library, especially for RAM test, could be one of reference.

Please double check the SelfTest_Stack.h file.

View solution in original post

0 Likes
4 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

When you want to check for stack overflow while developing and debugging easiest solution would be to set an "Address breakpoint" near top of stack. Configure break on write access and your program gets halted at a stack violation.

Bob

0 Likes
BragadeeshV
Moderator
Moderator
Moderator
First question asked 1000 replies posted 750 replies posted

Dear user_483038750​-san,

This thread might be useful for you.

How to trace stack overflow in running mode?

Regards,

Bragadeesh

Regards,
Bragadeesh

Hello,

Thank you for your reply.

Can I read and write below setting in last 4bytes of stack?

*(reg32 *)(CYDEV_SRAM_BASE + CYDEV_SRAM_SIZE - CYDEV_STACK_SIZE)

Best regards,

Yocchi

0 Likes

Yes, you can.

AN89056 - PSoCR 4 - IEC 60730 Class B and IEC 61508 SIL Safety Software Library, especially for RAM test, could be one of reference.

Please double check the SelfTest_Stack.h file.

0 Likes