Write magic bytes to the stack to monitor its usage

Posted by tkarls on Stack Overflow See other posts from Stack Overflow or by tkarls
Published on 2012-11-01T16:57:45Z Indexed on 2012/11/01 17:00 UTC
Read the original article Hit count: 183

Filed under:

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.

© Stack Overflow or respective owner

Related posts about c++

Related posts about c