Skip to content

Commit

Permalink
Save and restore the first stack frame of the alternate stack
Browse files Browse the repository at this point in the history
  • Loading branch information
clementgallet committed Oct 16, 2018
1 parent 7d0d478 commit bc62551
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -22,6 +22,7 @@
* Fix SDL1 keyboard layout
* Wait for threads to register
* Detect auto-repeat cases when detectable auto-repeat is not supported by the server
* Fix crash when loading a savestate on a non-draw frame

## [1.3.1] - 2018-08-30
### Added
Expand Down
17 changes: 17 additions & 0 deletions src/libTAS/checkpoint/AltStack.cpp
Expand Up @@ -34,6 +34,13 @@ namespace libtas {
*/
static stack_t oss;

/* We must save the last stack frame of the alternate stack in the savestate, so
* that we return at the correct location from the alternate stack.
*/
#define STACKFRAME_OFFSET 0x700
#define STACKFRAME_SIZE 0x700
static uint8_t stack_frame[STACKFRAME_SIZE];

void AltStack::saveStack()
{
int ret;
Expand Down Expand Up @@ -62,4 +69,14 @@ void AltStack::restoreStack()
MYASSERT(ret == 0)
}

void AltStack::saveStackFrame()
{
memcpy(stack_frame, ReservedMemory::getAddr(ReservedMemory::STACK_ADDR + ReservedMemory::STACK_SIZE - STACKFRAME_OFFSET), STACKFRAME_SIZE);
}

void AltStack::restoreStackFrame()
{
memcpy(ReservedMemory::getAddr(ReservedMemory::STACK_ADDR + ReservedMemory::STACK_SIZE - STACKFRAME_OFFSET), stack_frame, STACKFRAME_SIZE);
}

}
3 changes: 3 additions & 0 deletions src/libTAS/checkpoint/AltStack.h
Expand Up @@ -28,6 +28,9 @@ namespace AltStack
void saveStack();
void prepareStack();
void restoreStack();

void saveStackFrame();
void restoreStackFrame();
};
}

Expand Down
7 changes: 7 additions & 0 deletions src/libTAS/checkpoint/Checkpoint.cpp
Expand Up @@ -21,6 +21,7 @@

#include "Checkpoint.h"
#include "ThreadManager.h"
#include "AltStack.h"
#include "../logging.h"
#include "ProcMapsArea.h"
#include "ProcSelfMaps.h"
Expand Down Expand Up @@ -308,6 +309,9 @@ void Checkpoint::handler(int signum)

/* Restore the entire xcb connection struct */
memcpy(cur_xcb_conn, &xcb_conn, sizeof(xcb_connection_t));

/* We must restore the current stack frame from the savestate */
AltStack::restoreStackFrame();
}
else {
/* Check that base savestate exists, otherwise save it */
Expand All @@ -326,6 +330,9 @@ void Checkpoint::handler(int signum)
}
}

/* We must store the current stack frame in the savestate */
AltStack::saveStackFrame();

writeAllAreas(false);
}
debuglogstdio(LCF_CHECKPOINT, "End restore.");
Expand Down

0 comments on commit bc62551

Please sign in to comment.