Last active
July 10, 2016 08:49
-
-
Save xixitalk/633303a13cd3711b4efc94881e1cc0da to your computer and use it in GitHub Desktop.
函数栈破坏检查
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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