I have a problem on an embedded device that I think might be related to a stack overflow.
In order to test this I was planning to fill the stack with magic bytes and then periodically check if the stack has overflowed by examining how much of my magic bytes that are left intact.
But I can’t get the routine for marking the stack to work. The application keeps crashing instantly. This is what I have done just at the entry point of the program.
//fill most of stack with magic bytes
int stackvar = 0;
int stackAddr = (int)&stackvar;
int stackAddrEnd = stackAddr - 25000;
BYTE* stackEnd = (BYTE*) stackAddrEnd;
for(int i = 0; i < 25000; ++i)
{
*(stackEnd + i) = 0xFA;
}
Please note that the allocated stack is larger than 25k. So I’m counting on some stack space to already be used at this point. Also note that the stack grows from higher to lower addresses that’s why I’m trying to fill from the bottom and up.
But as I said, this will crash. I must be missing something here.
From what I can see, you may be easily overwriting the contents of the
stackEndvariable in the last few iterations of the loop. This is obviously a bad thing, as you’re using it in the very same loop. Does stopping in your loop at, say24900, help?I’d suggest to stop the loop at a well calculated value depending on the size of integer on your platform then.