Skip to content

Instantly share code, notes, and snippets.

@xixitalk
Last active July 10, 2016 08:49
Show Gist options
  • Save xixitalk/633303a13cd3711b4efc94881e1cc0da to your computer and use it in GitHub Desktop.
Save xixitalk/633303a13cd3711b4efc94881e1cc0da to your computer and use it in GitHub Desktop.
函数栈破坏检查
#define STACK_FIND_SIZE 2048 //find from SP to SP+STACK_FIND_SIZE
void funcStackCheck(unsigned long *psp, unsigned long lr)
{
if(psp && (*psp != lr))
{
assert(0);
}
}
/*find lr in stack mem address*/
unsigned long * funcLrFindPoint(unsigned long sp, unsigned long lr)
{
unsigned long * psp = NULL;
int n = 0;
for(n = 0; n < STACK_FIND_SIZE; n++)
{
psp = (unsigned long *)(sp + n);
if(*psp == lr)
{
break;
}
}
return psp;
}
void testFunc(void)
{
unsigned long *psp = NULL;
unsigned long v_lr = 0, v_sp = 0;
v_lr = (unsigned long)__builtin_return_address(0); //R14 arch ARM
v_sp = (unsigned long)__builtin_frame_address(0); //R13 arch ARM
psp = funcLrFindPoint(v_sp, v_lr);
if(NULL == psp)
{
/*Not found lr in stack*/
assert(0);
}
/*do something*/
funcStackCheck(psp, v_lr);
/*do something more*/
funcStackCheck(psp, v_lr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment