about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2023-04-22 10:12:09 +0200
committerptitSeb <sebastien.chev@gmail.com>2023-04-22 10:12:09 +0200
commitc74d8bb6028ec1d16aef04f7ae4cbc6bca34a044 (patch)
treee232c29f770a0506251360d671f387b37f40abba /src
parentec7844072f5ad5a74de9f4a42c91aefc2f174691 (diff)
downloadbox64-c74d8bb6028ec1d16aef04f7ae4cbc6bca34a044.tar.gz
box64-c74d8bb6028ec1d16aef04f7ae4cbc6bca34a044.zip
Fixed and improved __cxa_atexit handling
Diffstat (limited to 'src')
-rwxr-xr-xsrc/elfs/elfloader.c1
-rwxr-xr-xsrc/emu/x64emu.c9
-rwxr-xr-xsrc/include/x64emu.h4
-rwxr-xr-xsrc/librarian/librarian.c4
-rwxr-xr-xsrc/wrapped/wrappedlibc.c10
5 files changed, 17 insertions, 11 deletions
diff --git a/src/elfs/elfloader.c b/src/elfs/elfloader.c
index 8a4078b6..37de9dc2 100755
--- a/src/elfs/elfloader.c
+++ b/src/elfs/elfloader.c
@@ -311,6 +311,7 @@ void FreeElfMemory(elfheader_t* head)
 #ifdef DYNAREC
             dynarec_log(LOG_INFO, "Free DynaBlocks for %s\n", head->path);
             cleanDBFromAddressRange((uintptr_t)head->multiblock[i], head->multiblock_size[i], 1);
+            freeProtection((uintptr_t)head->multiblock[i], head->multiblock_size[i]);
 #endif
             munmap(head->multiblock[i], head->multiblock_size[i]);
         }
diff --git a/src/emu/x64emu.c b/src/emu/x64emu.c
index 2e869225..de4dd50e 100755
--- a/src/emu/x64emu.c
+++ b/src/emu/x64emu.c
@@ -38,6 +38,7 @@ typedef struct cleanup_s {
     void*       f;
     int         arg;
     void*       a;
+    void*       dso;
 } cleanup_t;
 
 static uint32_t x86emu_parity_tab[8] =
@@ -139,7 +140,7 @@ void SetTraceEmu(uintptr_t start, uintptr_t end)
 }
 #endif
 
-void AddCleanup(x64emu_t *emu, void *p)
+void AddCleanup(x64emu_t *emu, void *p, void* dso_handle)
 {
     (void)emu;
     
@@ -149,10 +150,11 @@ void AddCleanup(x64emu_t *emu, void *p)
     }
     my_context->cleanups[my_context->clean_sz].arg = 0;
     my_context->cleanups[my_context->clean_sz].a = NULL;
+    my_context->cleanups[my_context->clean_sz].dso = dso_handle;
     my_context->cleanups[my_context->clean_sz++].f = p;
 }
 
-void AddCleanup1Arg(x64emu_t *emu, void *p, void* a)
+void AddCleanup1Arg(x64emu_t *emu, void *p, void* a, void* dso_handle)
 {
     (void)emu;
     
@@ -162,6 +164,7 @@ void AddCleanup1Arg(x64emu_t *emu, void *p, void* a)
     }
     my_context->cleanups[my_context->clean_sz].arg = 1;
     my_context->cleanups[my_context->clean_sz].a = a;
+    my_context->cleanups[my_context->clean_sz].dso = dso_handle;
     my_context->cleanups[my_context->clean_sz++].f = p;
 }
 
@@ -169,7 +172,7 @@ void CallCleanup(x64emu_t *emu, void* p)
 {
     printf_log(LOG_DEBUG, "Calling atexit registered functions for %p mask\n", p);
     for(int i=my_context->clean_sz-1; i>=0; --i) {
-        if(p==my_context->cleanups[i].f) {
+        if(p==my_context->cleanups[i].dso) {
             printf_log(LOG_DEBUG, "Call cleanup #%d\n", i);
             RunFunctionWithEmu(emu, 0, (uintptr_t)(my_context->cleanups[i].f), my_context->cleanups[i].arg, my_context->cleanups[i].a );
             // now remove the cleanup
diff --git a/src/include/x64emu.h b/src/include/x64emu.h
index 17252b68..c7f2e20e 100755
--- a/src/include/x64emu.h
+++ b/src/include/x64emu.h
@@ -47,8 +47,8 @@ void StopEmu(x64emu_t* emu, const char* reason);
 void PushExit(x64emu_t* emu);
 void* GetExit();
 void EmuCall(x64emu_t* emu, uintptr_t addr);
-void AddCleanup(x64emu_t *emu, void *p);
-void AddCleanup1Arg(x64emu_t *emu, void *p, void* a);
+void AddCleanup(x64emu_t *emu, void *p, void* dso_handle);
+void AddCleanup1Arg(x64emu_t *emu, void *p, void* a, void* dso_handle);
 void CallCleanup(x64emu_t *emu, void* p);
 void CallAllCleanup(x64emu_t *emu);
 void UnimpOpcode(x64emu_t* emu);
diff --git a/src/librarian/librarian.c b/src/librarian/librarian.c
index f704239d..6733c3af 100755
--- a/src/librarian/librarian.c
+++ b/src/librarian/librarian.c
@@ -39,12 +39,12 @@ void FreeLibrarian(lib_t **maplib, x64emu_t *emu)
     library_t* owner = (*maplib)->owner;
     (*maplib)->owner = NULL;    // to avoid recursive free...
 
-    if((*maplib)->ownlibs && (*maplib)->libsz) {
+    /*if((*maplib)->ownlibs && (*maplib)->libsz) {
         for(int i=0; i<(*maplib)->libsz; ++i) {
             printf_log(LOG_DEBUG, "Unloading %s\n", (*maplib)->libraries[i]->name);
             DecRefCount(&(*maplib)->libraries[i], emu);
         }
-    }
+    }*/
     box_free((*maplib)->libraries);
     (*maplib)->libraries = NULL;
 
diff --git a/src/wrapped/wrappedlibc.c b/src/wrapped/wrappedlibc.c
index 6f617fca..0e87ca92 100755
--- a/src/wrapped/wrappedlibc.c
+++ b/src/wrapped/wrappedlibc.c
@@ -447,8 +447,7 @@ void EXPORT my___gmon_start__(x64emu_t *emu)
 
 int EXPORT my___cxa_atexit(x64emu_t* emu, void* p, void* a, void* dso_handle)
 {
-    (void)dso_handle;
-    AddCleanup1Arg(emu, p, a);
+    AddCleanup1Arg(emu, p, a, dso_handle);
     return 0;
 }
 void EXPORT my___cxa_finalize(x64emu_t* emu, void* p)
@@ -462,7 +461,7 @@ void EXPORT my___cxa_finalize(x64emu_t* emu, void* p)
 }
 int EXPORT my_atexit(x64emu_t* emu, void *p)
 {
-    AddCleanup(emu, p);
+    AddCleanup(emu, p, NULL);   // should grab current dso_handle?
     return 0;
 }
 
@@ -2197,7 +2196,10 @@ EXPORT void my__Jv_RegisterClasses() {}
 EXPORT int32_t my___cxa_thread_atexit_impl(x64emu_t* emu, void* dtor, void* obj, void* dso)
 {
     (void)emu;
-    printf_log(LOG_INFO, "Warning, call to __cxa_thread_atexit_impl(%p, %p, %p) ignored\n", dtor, obj, dso);
+    //printf_log(LOG_INFO, "Warning, call to __cxa_thread_atexit_impl(%p, %p, %p) ignored\n", dtor, obj, dso);
+    AddCleanup1Arg(emu, dtor, obj, dso);
+    return 0;
+
     return 0;
 }