Skip to content

Commit

Permalink
Fixed a stupid mistake on callback, fixing a large memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitSeb committed Mar 3, 2019
1 parent 3fca45b commit bfd4c8c
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ x86emu_t* AddCallback(x86emu_t* emu, uintptr_t fnc, int nb_args, void* arg1, voi
callbacklist_t *callbacks = emu->context->callbacks;
int stsize = 2*1024*1024; // 2MB stack (1MB is not enough for Xenonauts)
void* stack = malloc(stsize);
if(!stack) {printf_log(LOG_NONE, "BOX86: Error, cannot allocate 2MB Stack for callback\n");}
x86emu_t * newemu = NewX86Emu(emu->context, fnc, (uintptr_t)stack, stsize);
if(!stack) {
printf_log(LOG_NONE, "BOX86: Error, cannot allocate 2MB Stack for callback\n");
}
x86emu_t * newemu = NewX86Emu(emu->context, fnc, (uintptr_t)stack, stsize, 1);
SetupX86Emu(newemu, emu->shared_global, emu->globals);
newemu->trace_start = emu->trace_start;
newemu->trace_end = emu->trace_end;
Expand Down Expand Up @@ -62,8 +64,10 @@ x86emu_t* AddSmallCallback(x86emu_t* emu, uintptr_t fnc, int nb_args, void* arg1
callbacklist_t *callbacks = emu->context->callbacks;
int stsize = 64*1024; // 64KB stack
void* stack = malloc(stsize);
if(!stack) {printf_log(LOG_NONE, "BOX86: Error, cannot allocate 64KB Stack for small callback\n");}
x86emu_t * newemu = NewX86Emu(emu->context, fnc, (uintptr_t)stack, stsize);
if(!stack) {
printf_log(LOG_NONE, "BOX86: Error, cannot allocate 64KB Stack for small callback\n");
}
x86emu_t * newemu = NewX86Emu(emu->context, fnc, (uintptr_t)stack, stsize, 1);
SetupX86Emu(newemu, emu->shared_global, emu->globals);
newemu->trace_start = emu->trace_start;
newemu->trace_end = emu->trace_end;
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ int main(int argc, const char **argv, const char **env) {
return -1;
}
// init x86 emu
context->emu = NewX86Emu(context, context->ep, (uintptr_t)context->stack, context->stacksz);
context->emu = NewX86Emu(context, context->ep, (uintptr_t)context->stack, context->stacksz, 0);
// stack setup is much more complicated then just that!
SetupInitialStack(context);
// this is probably useless
Expand Down
2 changes: 1 addition & 1 deletion src/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int EXPORT my_pthread_create(x86emu_t *emu, void* t, void* attr, void* start_rou
// TODO: get stack size inside attr
void* stack = calloc(1, stacksize);
x86emu_t *emuthread = NewX86Emu(emu->context, (uintptr_t)start_routine, (uintptr_t)stack,
stacksize);
stacksize, 1);
SetupX86Emu(emuthread, emu->shared_global, emu->globals);
emuthread->trace_start = emu->trace_start;
emuthread->trace_end = emu->trace_end;
Expand Down
Empty file modified src/wrappedlibz.c
100644 → 100755
Empty file.
5 changes: 4 additions & 1 deletion src/x86emu.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int getrand(int maxval)
}
}

x86emu_t *NewX86Emu(box86context_t *context, uintptr_t start, uintptr_t stack, int stacksize)
x86emu_t *NewX86Emu(box86context_t *context, uintptr_t start, uintptr_t stack, int stacksize, int ownstack)
{
printf_log(LOG_DEBUG, "Allocate a new X86 Emu, with EIP=%p and Stack=%p/0x%X\n", (void*)start, (void*)stack, stacksize);

Expand All @@ -48,6 +48,9 @@ x86emu_t *NewX86Emu(box86context_t *context, uintptr_t start, uintptr_t stack, i
emu->sbiidx[4] = &emu->zero;
emu->packed_eflags.x32 = 0x02; // default flags?
UnpackFlags(emu);
// own stack?
if(ownstack)
emu->stack = (void*)stack;
// set default value
R_EIP = start;
R_ESP = stack + stacksize;
Expand Down
2 changes: 1 addition & 1 deletion src/x86emu.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
typedef struct x86emu_s x86emu_t;
typedef struct box86context_s box86context_t;

x86emu_t *NewX86Emu(box86context_t *context, uintptr_t start, uintptr_t stack, int stacksize);
x86emu_t *NewX86Emu(box86context_t *context, uintptr_t start, uintptr_t stack, int stacksize, int ownstack);
void SetupX86Emu(x86emu_t *emu, int* shared_gloabl, void* globals);
void SetTraceEmu(x86emu_t *emu, uintptr_t trace_start, uintptr_t trace_end);
void FreeX86Emu(x86emu_t **x86emu);
Expand Down
3 changes: 1 addition & 2 deletions src/x86int3.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ x86emu_t* x86emu_fork(x86emu_t* e)
newstack = malloc(emu->context->stacksz);
}
memcpy(newstack, emu->context->stack, emu->context->stacksz);
x86emu_t* newemu = NewX86Emu(emu->context, R_EIP, (uintptr_t)newstack, emu->context->stacksz);
x86emu_t* newemu = NewX86Emu(emu->context, R_EIP, (uintptr_t)newstack, emu->context->stacksz, 1);
SetupX86Emu(newemu, emu->shared_global, emu->globals);
CloneEmu(newemu, emu);
emu->stack = newstack;
// ready to fork
++emu->context->forked;
int v = fork();
Expand Down

0 comments on commit bfd4c8c

Please sign in to comment.