Relocate a process' Stack correctly

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
saurabh.bharambe
Posts: 1
Joined: Sun Nov 04, 2007 9:10 pm

Relocate a process' Stack correctly

Post by saurabh.bharambe »

I am trying to relocate the stack of a process.
I have kept the process very simple.
In a way that it has many restrictions like,
1. It does not use pointers
2. Its single threaded
3. Does not use any file I/O
4. No global & dynamic data.

I extract the stack info from /proc/[pid]/maps & simply dump all the binary data from the stack.
When I want to restore the process to its earlier state, I copy this data in the new stack's address.

Problem is that when the restored process makes a function call, or returns from any call, the frame pointer/base pointer (BP) stored in stack in the earlier run is not valid in the current run of the process.
Is there a way by which I can find the location of the base pointer in the call stack & update it before restoring it?

Can anyone please help me with this?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

I do the very same thing in my kernel. You can look at the current base pointer (you'd have to extract this from the program somehow), then follow the trail of base pointers up the stack.

1. Look at the current base pointer.
2. The next base pointer is at the address of the value of the current base pointer.

Code: Select all

myNextEbp = *(unsigned int *)ebp;
3. relocate the base pointer, and overwrite the stack's version.
4. goto 1, with the 'current base pointer' as the next one. (myNextEbp).

Cheers,

JamesM
Post Reply