about summary refs log tree commit diff stats
path: root/src/box64context.c
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2021-03-03 16:43:42 +0100
committerptitSeb <sebastien.chev@gmail.com>2021-03-03 16:43:42 +0100
commitf4829a8ce42b1abbcc8621802d6c6fad3a56cd5d (patch)
tree211276c3587721126ded00be46487bf23b605591 /src/box64context.c
parentf73fbd3cee38b4c0086c934d73972375c9c8c7d6 (diff)
downloadbox64-f4829a8ce42b1abbcc8621802d6c6fad3a56cd5d.tar.gz
box64-f4829a8ce42b1abbcc8621802d6c6fad3a56cd5d.zip
More infrastructure added to elf and x64 emu
Diffstat (limited to 'src/box64context.c')
-rwxr-xr-xsrc/box64context.c68
1 files changed, 61 insertions, 7 deletions
diff --git a/src/box64context.c b/src/box64context.c
index fa4b74b7..4c75e98e 100755
--- a/src/box64context.c
+++ b/src/box64context.c
@@ -13,7 +13,10 @@
 #include "threads.h"
 #include "x64trace.h"
 #include "bridge.h"
-
+#include "librarian.h"
+#include "library.h"
+#include "wrapper.h"
+#include "x64emu.h"
 
 EXPORTDYN
 void initAllHelpers(box64context_t* context)
@@ -35,7 +38,7 @@ void finiAllHelpers(box64context_t* context)
         return;
     fini_pthread_helper(context);
     //fini_signal_helper();
-    //cleanAlternate();
+    cleanAlternate();
     fini_custommem_helper(context);
     finied = 1;
 }
@@ -80,16 +83,28 @@ box64context_t *NewBox64Context(int argc)
 
     init_custommem_helper(context);
 
+    context->maplib = NewLibrarian(context, 1);
+    context->local_maplib = NewLibrarian(context, 1);
     context->system = NewBridge();
     // create vsyscall
-//    context->vsyscall = AddBridge(context->system, vFv, x64Syscall, 0);
+    context->vsyscall = AddBridge(context->system, vFv, x64Syscall, 0);
     context->box64lib = dlopen(NULL, RTLD_NOW|RTLD_GLOBAL);
-    //context->dlprivate = NewDLPrivate();
+    context->dlprivate = NewDLPrivate();
 
     context->argc = argc;
     context->argv = (char**)calloc(context->argc+1, sizeof(char*));
 
+    pthread_mutex_init(&context->mutex_once, NULL);
+    pthread_mutex_init(&context->mutex_once2, NULL);
+    pthread_mutex_init(&context->mutex_trace, NULL);
+#ifndef DYNAREC
+    pthread_mutex_init(&context->mutex_lock, NULL);
+#else
+    pthread_mutex_init(&context->mutex_dyndump, NULL);
+#endif
+    pthread_mutex_init(&context->mutex_tls, NULL);
     pthread_mutex_init(&context->mutex_thread, NULL);
+    pthread_key_create(&context->tlskey, free_tlsdatasize);
 
 
     for (int i=0; i<4; ++i) context->canary[i] = 1 +  getrand(255);
@@ -107,8 +122,21 @@ void FreeBox64Context(box64context_t** context)
     if(!context)
         return;
     
+    if(--(*context)->forked >= 0)
+        return;
+
     box64context_t* ctx = *context;   // local copy to do the cleanning
 
+    for(int i=0; i<ctx->elfsize; ++i) {
+        FreeElfHeader(&ctx->elfs[i]);
+    }
+    free(ctx->elfs);
+
+    if(ctx->maplib)
+        FreeLibrarian(&ctx->maplib);
+    if(ctx->local_maplib)
+        FreeLibrarian(&ctx->local_maplib);
+
     FreeCollection(&ctx->box64_path);
     FreeCollection(&ctx->box64_ld_lib);
     FreeCollection(&ctx->box64_emulated_libs);
@@ -118,12 +146,21 @@ void FreeBox64Context(box64context_t** context)
     if(ctx->zydis)
         DeleteX64Trace(ctx);
 
+    if(ctx->deferedInitList)
+        free(ctx->deferedInitList);
+
     free(ctx->argv);
     
     for (int i=0; i<ctx->envc; ++i)
         free(ctx->envv[i]);
     free(ctx->envv);
 
+    if(ctx->atfork_sz) {
+        free(ctx->atforks);
+        ctx->atforks = NULL;
+        ctx->atfork_sz = ctx->atfork_cap = 0;
+    }
+
     for(int i=0; i<MAX_SIGNAL; ++i)
         if(ctx->signals[i]!=0 && ctx->signals[i]!=1) {
             signal(i, SIG_DFL);
@@ -133,7 +170,7 @@ void FreeBox64Context(box64context_t** context)
 
     CleanStackSize(ctx);
 
-    //FreeDLPrivate(&ctx->dlprivate);
+    FreeDLPrivate(&ctx->dlprivate);
 
     free(ctx->stack);
 
@@ -142,6 +179,10 @@ void FreeBox64Context(box64context_t** context)
 
     FreeBridge(&ctx->system);
 
+//    freeGLProcWrapper(ctx);
+//    freeALProcWrapper(ctx);
+
+
     void* ptr;
     if ((ptr = pthread_getspecific(ctx->tlskey)) != NULL) {
         free_tlsdatasize(ptr);
@@ -152,8 +193,22 @@ void FreeBox64Context(box64context_t** context)
     if(ctx->tlsdata)
         free(ctx->tlsdata);
 
+    pthread_mutex_destroy(&ctx->mutex_once);
+    pthread_mutex_destroy(&ctx->mutex_once2);
+    pthread_mutex_destroy(&ctx->mutex_trace);
+#ifndef DYNAREC
+    pthread_mutex_destroy(&ctx->mutex_lock);
+#else
+    pthread_mutex_destroy(&ctx->mutex_dyndump);
+#endif
+    pthread_mutex_destroy(&ctx->mutex_tls);
     pthread_mutex_destroy(&ctx->mutex_thread);
 
+    free_neededlib(&ctx->neededlibs);
+
+    if(ctx->emu_sig)
+        FreeX64Emu(&ctx->emu_sig);
+
     finiAllHelpers(ctx);
 
     free(ctx);
@@ -187,7 +242,7 @@ int AddTLSPartition(box64context_t* context, int tlssize) {
 
     return -context->tlssize;   // negative offset
 }
-/*
+
 void add_neededlib(needed_libs_t* needed, library_t* lib)
 {
     if(!needed)
@@ -209,4 +264,3 @@ void free_neededlib(needed_libs_t* needed)
         free(needed->libs);
     needed->libs = NULL;
 }
-*/
\ No newline at end of file