I am trying to move the stack pointer to the mmap-ed area to simulate a context switch, but somehow the code below always gives a segmentation error:
C:
struct savectx {
void *regs[JB_SIZE];
};
struct savectx* initctx=(struct savectx*)malloc(sizeof(savectx));
void *newsp;
if ((newsp=mmap(0,STACK_SIZE,PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS,0,0))==MAP_FAILED){
perror("mmap failed");
}
initctx->regs[4]=newsp;
restorectx(initctx,0);
x86:
restorectx:
movl 4(%esp),%ecx /*Move jump buffer addr to ecx */
movl 8(%esp),%eax /*Longjmp return value */
movl (JB_SP*4)(%ecx),%esp /*JB_SP is defined to be 4,*/
The program does not work in the last line of the assembly.
For malloc, I know that I may have to add 0x000f0000 to the pointer, but what about mmap? Or how do we create a stack adjusted to the location of mmapp-ed. (manap page for mmap: http://linux.die.net/man/3/mmap compiled using GCC on ubuntu)
source
share