Skip to content

Instantly share code, notes, and snippets.

@7shi
Created October 2, 2011 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 7shi/1257249 to your computer and use it in GitHub Desktop.
Save 7shi/1257249 to your computer and use it in GitHub Desktop.
自前setjmp/longjmp
#include <stdio.h>
typedef struct {
unsigned long eip, esp, ebp, ebx, edi, esi;
} myjmp_buf;
_declspec(naked) int mysetjmp(myjmp_buf *jbuf) {
_asm {
mov edx, [esp+4]
mov eax, [esp]
mov [edx ], eax
mov [edx+ 4], esp
mov [edx+ 8], ebp
mov [edx+12], ebx
mov [edx+16], edi
mov [edx+20], esi
xor eax, eax
ret
}
}
_declspec(naked) void mylongjmp(myjmp_buf *jbuf, int value) {
_asm {
mov eax, [esp+ 8]
mov edx, [esp+ 4]
mov ecx, [edx ]
mov esp, [edx+ 4]
mov ebp, [edx+ 8]
mov ebx, [edx+12]
mov edi, [edx+16]
mov esi, [edx+20]
mov [esp], ecx
ret
}
}
myjmp_buf jb;
int main() {
if (mysetjmp(&jb) == 0) {
printf("setjmp\n");
printf("test\n");
mylongjmp(&jb, 1);
printf("never reach here\n");
}
printf("longjmp done.\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment