I would like to make a checksum of the current stack in the main, in order to check if it has been altered between two points.
For example:
int main(void) {
...
stack_checksum();
... process ...
if(stack_checksum() != ...)
altered.
}
How can I grab the base stack address and the current address of the stack’s top ?
EDIT:
With @Miroslav Bajtoš help, step for approach:
- Put local variable in a structure
- Check backtrace return array
It depends on which compiler/implementation of standard library you are using.
For gcc (or any other compiler that uses glibc), you can use
backtrace()functions inexecinfo.h– see these answers for more details: How to generate a stacktrace when my gcc C++ app crashes and How to get more detailed backtraceFor Microsoft compiler, you can use
StackWalk64()function, see this article for more details: Walking-the-callstack. There was also a similar quesion asked here on StackOverflow: StackWalk64 on Windows – Get symbol nameComputing checksum should be easy once you can walk the stack.