How to create stack overflow/underflow exception ?

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.
LVL 1
naseeamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

naseeamAuthor 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."
sarabandeCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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).
naseeamAuthor Commented:
Very nice solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.