Original Post
I'm currently using the following code (adapted from a Gamasutra article) to obtain stack frame information on a running EXE: This works great provided that everything has valid stack frames. The problem is, I link to a third party DLL which isn't built with stack frame information embedded. This DLL has callbacks into my code, which in turn call GetCallStackPtr(). The problem is, as the code walks up the list of calls, eventually it hits that DLL and the stack frame becomes gibberish. At this point, GetCallStackPtr() crashes because it starts dereferencing garbage memory addresses. Does anyone know of a way to detect that a stack frame has gone bogus? Visual Studio and WinDbg can both do it, and IIRC OllyDbg can as well, so I know it's at least theoretically possible... I just haven't got the first clue how to go about doing it.
unsigned GetCallStackPtr(unsigned numskips)
{
struct StackFrame
{
StackFrame* Previous;
unsigned ReturnAddress;
};
StackFrame* frame;
__asm mov [frame], ebp;
while (numskips > 0) {
if (frame->Previous) {
frame = frame->Previous;
}
else {
return NULL;
}
--numskips;
}
return frame->ReturnAddress;
}