KKR
KKR

Reputation: 11

How to restore stack frame in gcc?

I want to build my own checkpoint library. I'm able to save the stack frame to a file calling checkpoint_here(stack pointer) and that can be restored later via calling recover(stack pointer) function.

Here is my problem: I'm able to jump from function recover(sp) to main() but the stack frame gets changed(stack pointer,frame pointer). So I want to jump to main from recover(sp) just after is checkpoint_here(sp) called retaining the stack frame of main(). I've tried setjmp/longjmp but can't make them working.Thanks in anticipation.

//jmp_buf env;

void *get_pc () { return __builtin_return_address(1); }



void checkpoint_here(register int *sp){


//printf("%p\n",get_pc());
void *pc;

pc=get_pc();//getting the program counter of caller

//printf("pc inside chk:%p\n",pc);
size_t i;
long size;

//if(!setjmp(env)){

void *l=__builtin_frame_address(1);//frame pointer of caller



int fd=open("ckpt1.bin", O_WRONLY|O_CREAT,S_IWUSR|S_IRUSR|S_IRGRP);
int mfd=open("map.bin", O_WRONLY|O_CREAT,S_IWUSR|S_IRUSR|S_IRGRP);

size=(long)l-(long)sp;
//printf("s->%ld\n",size);
write(mfd,&size,sizeof(long));    //writing the size of the data to be written to file. 
write(mfd,&pc,sizeof(long));         //writing program counter of the caller.
write(fd,(char *)sp,(long)l-(long)sp);   //writing local variables on the stack frame of caller.
close(fd);
close(mfd);
//}

}


void recover(register int *sp){

//int dummy;
long size;
void *pc;
//printf("old %p\n",sp);

/*void *newsp=(void *)&dummy;

printf("new %p old %p\n",newsp,sp);
if(newsp>=(void *)sp)
recover(sp);*/

int fd=open("ckpt1.bin", O_RDONLY,0644);
int mfd=open("map.bin", O_RDONLY,0644);
read(mfd,&size,sizeof(long));       //reading size of data written
read(mfd,&pc,sizeof(long));     //reading program counter
read(fd,(char *)sp,size);       //reading local variables
close(mfd);
close(fd);

//printf("got->%ld\n",size);
//longjmp(env,1);


void (*foo)(void) =pc;      
foo();          //trying to jump to main just after checkpoint_here() is called.
//asm volatile("jmp %0" : : "r" (pc));
}







int main(int argc,char **argv)
{
register int *sp asm ("rsp");

 if(argc==2){
   if(strcmp(argv[1],"recover")==0){
     recover(sp);    //restoring local variables
     exit(0);
   }
  }

   int a, b, c;
   float s, area;
   char x='a';

   printf("Enter the sides of triangle\n");
   //printf("\na->%p b->%p c->%p s->%p area->%p\n",&a,&b,&c,&s,&area);
   scanf("%d %d %d",&a,&b,&c);

   s = (a+b+c)/2.0;

  //printf("%p\n",get_pc());

   checkpoint_here(sp);      //saving stack

   //printf("here\n");
   //printf("nsp->%p\n",sp);
   area = (s*(s-a)*(s-b)*(s-c));

   printf("%d %d %d %f %f %d\n",a,b,c,s,area,x);
   printf("Area of triangle = %f\n", area);
   printf("%f\n",s);
   return 0;
}

Upvotes: 1

Views: 858

Answers (1)

You cannot do that in general.

You might try non-portable extended asm instructions (to restore %rsp and %rbp on x86-64). You could use longjmp (see setjmp(3) and longjmp(3)) -since longjmp is restoring the stack pointer- assuming you understand the implementation details.

The stack has, thanks to ASLR, a "random", non reproducible, location. In other words, if you start twice the same program, the stack pointer of main would be different. And in C some stack frames contain a pointer into other stack frames. See also this answer.

Read more about application checkpointing (see this) and study the source code (or use) BLCR.

You could perhaps restrict the C code to be used (e.g. if you generate the C code) and you might perhaps extend GCC using MELT for your needs. This is a significant amount of work.

BTW, MELT is (internally also) generating C++ code, with restricted stack frames which could be easily checkpointable. You could take that as an inspiration source.

Read also about x86 calling conventions and garbage collection (since a precise GC has to scan local pointers, which is similar to your needs).

Upvotes: 1

Related Questions