Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Stack overflow

Status
Not open for further replies.

electroRF

Member
Hi guys,

I got a design problem in the code which I need to solve, and thought I could get nice ideas here.

Each task in the OS has its own dedicated stack. (The OS was internally written here).

When a failure occur in any of the tasks, the task calls an Error Handler Function which handles the error.

The problem is that the Error Handler Function uses ~1KB due to local variables, and therefore it causes a stack overflow to the stack of the Task which called the Error Handler function.

Increasing the size of each Task's Stack is not something that can be afforded.

How would you handle this situation?

I thought of dedicating the Error Handler Function its own stack.

But perhaps something in the flow should be changed?

Any advice is appreciated :)

Thank you.
 
This is a huge design flaw.

The routine that handles death situations must understand that the death may have happened because of limited resources, therefore it needs all the memory pre-allocated when OS starts.
 
Hi North Guy and 3V
Thanks!

The thing is that we'd sometime call the Error Handler Function not just because we got limited resources, but for unexpected scenarios.

for example, a function which has a 'switch case' block and received unexpected 'case', would call the Error Handler Function.

The Error Handler Function would use the stack of the task who called it, and will overflow that stack.
 
You need to take few steps back and redesign the software.. at least the error handling. And why are there "unexpected" case values? Sounds like a really poorly designed software.
Maybe you need an error handler for your error handler.. haha. (seriously don't do that)
 
Hi T and Northguy,
Thank you again.

I discussed it with my manager and he currently reconsiders it.

For assembly knowledge,
is the below code correct for stack's context switch?
Code:
_wrapperFunc:
    [--SP] = RETS;
    [--SP] = FP;

    //Context Switch of Stack - before calling _coreFunc
    P1 = 0x04000000; //P1 register points to new buffer
    [P1] = SP;            
    SP = P1;
 
    CALL _coreFunc;
 
   //Context Switch of Stack - after calling _coreFunc
   P1 = 0x04000000; //P1 register points to new buffer 
   SP = [P1];
     
    RETS = [FP+4];
    SP = FP + 8;
    FP = [FP];

    RTS;
_wrapperFunc.end:
 
I cannot comment on the asm because I don't know what the processor is.

If you have an exception handling routine, you do not want to return stack back to where it was. If it happened as a part of structured exception handling, such as try/catch, the handling function should unroll the stack and jump to the "catch" part. If not, it is supposed to kill the process and thereby destroy the stack.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top