How to create stack overflow/underflow exception ?

naseeam
naseeam used Ask the Experts™
on
I'm programming Infineon TC29x Aurix Tricore Microcontroller based embedded system in 'C' language.  

Consider stack starting address 0x5000 0000.  The stack size is 0x2000.  

If I take current stack pointer minus 0x2000, will that create stack exception?  If yes, what might be 'C' or assembly language code that will achieve this?

My thought is if I subtract negative 0x2000 from current stack pointer or add 0x2000 to current stack pointer, I'll be outside stack and stack exception will occur ?

 I need to set program counter to illegal address, i.e. 0x00FF FFFC.  Can this be done with assembly language instructions?
 If yes, please instrument assembly language instruction that would set program counter to 0x00FF FFFC while code is running.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Please disregard following text from my original post:

"I need to set program counter to illegal address, i.e. 0x00FF FFFC.  Can this be done with assembly language instructions?
  If yes, please instrument assembly language instruction that would set program counter to 0x00FF FFFC while code is running."
Top Expert 2016
Commented:
a stack overflow is when you have used up more memory for the stack than the maximum stack size that was defined. for your embedded system you only have 8192 (0x2000) bytes for the stack, and if each function takes up 1024 bytes then you can only have function calls 8 deep - function 1 calls function 2 which calls function 3 ... which calls function 8 which calls function 9, but function 9 overwrites memory beyond the stack size and overwrites other memory, code, etc. it depends on your compiler whether it throws an exception in this case or produces corrupted memory.

probably the easiest way to produce a stack  overflow ist to call a function recursively, say 100 times when you only have a maximum of 8 calls.

consider the following function

int f(int n)
{
    char szBuf[1024] = { '\0' };
    int x = (n*(n+77)*(n+23)*(n+17)+n)%1024;
    szBuf[x] = n;
    return (n > 0)? f(n-1) : x;
}

Open in new window


if you call that function with f(100); it should have a stack overflow.

Sara
evilrixSenior Software Engineer (Avast)
Commented:
A stack overflow is where the call stack pointer exceeds the current stack-frame. As Sara has alluded, the easiest way to create a stack overflow is to have a function just call itself recursively. Note; however, that you have to ensure that the function does something like allocate memory or modify memory after the function calls itself, otherwise the compiler optimiser might implement tail end recursion optimisation.

A stack underflow would be where you somehow manage to manipulate the call stack pointer to point to before the current stack frame. There isn't really any easy way to do this (in C) that doesn't involve some buffer overflow magic where you somehow corrupt the call stack pointer. This isn't something that is easy to do!

In general, a stack underflow is where you try to access more items in a stack than actually exist (ie. trying to pop a stack when its empty). A stack overflow is where you try to put more items in the stack than it can handle due to size limitations (ie trying to push to a stack when it's full).

Author

Commented:
Very nice solution.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial