about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2022-07-31 10:32:12 +0200
committerptitSeb <sebastien.chev@gmail.com>2022-08-06 16:10:17 +0200
commit2ac265d52951c2951281b25aa0aba3ad21b6565f (patch)
tree4c8a08922981a5b279a52c1d8bae1bf7218e4e3d /src
parente261270eb8719a5a2e0c7fd86140390290d49ae9 (diff)
downloadbox64-2ac265d52951c2951281b25aa0aba3ad21b6565f.tar.gz
box64-2ac265d52951c2951281b25aa0aba3ad21b6565f.zip
All box internal memory alloc use __libc_ internal one (except on Android, using libc one gather from dlsym)
Diffstat (limited to 'src')
-rwxr-xr-xsrc/box64context.c50
-rw-r--r--src/custommem.c106
-rwxr-xr-xsrc/dynarec/arm64/dynarec_arm64_pass0.h2
-rwxr-xr-xsrc/dynarec/dynablock.c24
-rwxr-xr-xsrc/dynarec/dynarec_native.c40
-rw-r--r--src/elfs/elfdwarf_private.c68
-rwxr-xr-xsrc/elfs/elfloader.c72
-rwxr-xr-xsrc/elfs/elfparser.c12
-rwxr-xr-xsrc/emu/x64emu.c10
-rwxr-xr-xsrc/emu/x64run_private.c2
-rwxr-xr-xsrc/emu/x64trace.c8
-rwxr-xr-xsrc/include/debug.h18
-rwxr-xr-xsrc/librarian/dictionnary.c4
-rwxr-xr-xsrc/librarian/librarian.c22
-rwxr-xr-xsrc/librarian/library.c68
-rw-r--r--src/librarian/symbols.c6
-rwxr-xr-xsrc/libtools/signals.c6
-rwxr-xr-xsrc/libtools/threads.c34
-rwxr-xr-xsrc/main.c67
-rw-r--r--src/mallochook.c28
-rw-r--r--src/steam.c2
-rwxr-xr-xsrc/tools/box64stack.c6
-rwxr-xr-xsrc/tools/bridge.c8
-rwxr-xr-xsrc/tools/fileutils.c4
-rwxr-xr-xsrc/tools/gtkclass.c10
-rwxr-xr-xsrc/tools/pathcoll.c24
-rwxr-xr-xsrc/wrapped/wrappedgtkx112.c2
-rwxr-xr-xsrc/wrapped/wrappedlibc.c59
-rwxr-xr-xsrc/wrapped/wrappedlibdl.c66
29 files changed, 438 insertions, 390 deletions
diff --git a/src/box64context.c b/src/box64context.c
index 1d940b39..355d4c68 100755
--- a/src/box64context.c
+++ b/src/box64context.c
@@ -63,8 +63,8 @@ void free_tlsdatasize(void* p)
     if(!p)
         return;
     tlsdatasize_t *data = (tlsdatasize_t*)p;
-    free(data->ptr);
-    free(p);
+    box_free(data->ptr);
+    box_free(p);
 }
 
 void x64Syscall(x64emu_t *emu);
@@ -152,12 +152,12 @@ box64context_t *NewBox64Context(int argc)
     }
 #endif
     // init and put default values
-    box64context_t *context = my_context = (box64context_t*)calloc(1, sizeof(box64context_t));
+    box64context_t *context = my_context = (box64context_t*)box_calloc(1, sizeof(box64context_t));
 
     if(cycle_log)
         for(int i=0; i<CYCLE_LOG; ++i) {
-            context->log_call[i] = (char*)calloc(256, 1);
-            context->log_ret[i] = (char*)calloc(128, 1);
+            context->log_call[i] = (char*)box_calloc(256, 1);
+            context->log_ret[i] = (char*)box_calloc(128, 1);
         }
 
     context->deferedInit = 1;
@@ -184,7 +184,7 @@ box64context_t *NewBox64Context(int argc)
     context->dlprivate = NewDLPrivate();
 
     context->argc = argc;
-    context->argv = (char**)calloc(context->argc+1, sizeof(char*));
+    context->argv = (char**)box_calloc(context->argc+1, sizeof(char*));
 
     init_mutexes(context);
     pthread_atfork(NULL, NULL, atfork_child_box64context);
@@ -221,7 +221,7 @@ void FreeBox64Context(box64context_t** context)
     for(int i=0; i<ctx->elfsize; ++i) {
         FreeElfHeader(&ctx->elfs[i]);
     }
-    free(ctx->elfs);
+    box_free(ctx->elfs);
 
     FreeCollection(&ctx->box64_path);
     FreeCollection(&ctx->box64_ld_lib);
@@ -233,16 +233,16 @@ void FreeBox64Context(box64context_t** context)
         DeleteX64Trace(ctx);
 
     if(ctx->deferedInitList)
-        free(ctx->deferedInitList);
+        box_free(ctx->deferedInitList);
 
-    /*free(ctx->argv);*/
+    /*box_free(ctx->argv);*/
     
     /*for (int i=0; i<ctx->envc; ++i)
-        free(ctx->envv[i]);
-    free(ctx->envv);*/
+        box_free(ctx->envv[i]);
+    box_free(ctx->envv);*/
 
     if(ctx->atfork_sz) {
-        free(ctx->atforks);
+        box_free(ctx->atforks);
         ctx->atforks = NULL;
         ctx->atfork_sz = ctx->atfork_cap = 0;
     }
@@ -258,8 +258,8 @@ void FreeBox64Context(box64context_t** context)
 
     FreeDLPrivate(&ctx->dlprivate);
 
-    free(ctx->fullpath);
-    free(ctx->box64path);
+    box_free(ctx->fullpath);
+    box_free(ctx->box64path);
 
     FreeBridge(&ctx->system);
 
@@ -267,7 +267,7 @@ void FreeBox64Context(box64context_t** context)
     freeALProcWrapper(ctx);
 
     if(ctx->stack_clone)
-        free(ctx->stack_clone);
+        box_free(ctx->stack_clone);
 
 
     void* ptr;
@@ -278,7 +278,7 @@ void FreeBox64Context(box64context_t** context)
     pthread_key_delete(ctx->tlskey);
 
     if(ctx->tlsdata)
-        free(ctx->tlsdata);
+        box_free(ctx->tlsdata);
 
     free_neededlib(&ctx->neededlibs);
 
@@ -301,11 +301,11 @@ void FreeBox64Context(box64context_t** context)
 
     if(cycle_log)
         for(int i=0; i<CYCLE_LOG; ++i) {
-            free(ctx->log_call[i]);
-            free(ctx->log_ret[i]);
+            box_free(ctx->log_call[i]);
+            box_free(ctx->log_ret[i]);
         }
 
-    free(ctx);
+    box_free(ctx);
 }
 
 int AddElfHeader(box64context_t* ctx, elfheader_t* head) {
@@ -313,7 +313,7 @@ int AddElfHeader(box64context_t* ctx, elfheader_t* head) {
     if(idx==ctx->elfcap) {
         // resize...
         ctx->elfcap += 16;
-        ctx->elfs = (elfheader_t**)realloc(ctx->elfs, sizeof(elfheader_t*) * ctx->elfcap);
+        ctx->elfs = (elfheader_t**)box_realloc(ctx->elfs, sizeof(elfheader_t*) * ctx->elfcap);
     }
     ctx->elfs[idx] = head;
     ctx->elfsize++;
@@ -324,7 +324,7 @@ int AddElfHeader(box64context_t* ctx, elfheader_t* head) {
 int AddTLSPartition(box64context_t* context, int tlssize) {
     int oldsize = context->tlssize;
     context->tlssize += tlssize;
-    context->tlsdata = realloc(context->tlsdata, context->tlssize);
+    context->tlsdata = box_realloc(context->tlsdata, context->tlssize);
     memmove(context->tlsdata+tlssize, context->tlsdata, oldsize);   // move to the top, using memmove as regions will probably overlap
     memset(context->tlsdata, 0, tlssize);           // fill new space with 0 (not mandatory)
     // clean GS segment for current emu
@@ -346,7 +346,7 @@ void add_neededlib(needed_libs_t* needed, library_t* lib)
             return;
     if(needed->size == needed->cap) {
         needed->cap += 8;
-        needed->libs = (library_t**)realloc(needed->libs, needed->cap*sizeof(library_t*));
+        needed->libs = (library_t**)box_realloc(needed->libs, needed->cap*sizeof(library_t*));
     }
     needed->libs[needed->size++] = lib;
 }
@@ -358,7 +358,7 @@ void free_neededlib(needed_libs_t* needed)
     needed->cap = 0;
     needed->size = 0;
     if(needed->libs)
-        free(needed->libs);
+        box_free(needed->libs);
     needed->libs = NULL;
 }
 
@@ -371,7 +371,7 @@ void add_dependedlib(needed_libs_t* depended, library_t* lib)
             return;
     if(depended->size == depended->cap) {
         depended->cap += 8;
-        depended->libs = (library_t**)realloc(depended->libs, depended->cap*sizeof(library_t*));
+        depended->libs = (library_t**)box_realloc(depended->libs, depended->cap*sizeof(library_t*));
     }
     depended->libs[depended->size++] = lib;
 }
@@ -383,6 +383,6 @@ void free_dependedlib(needed_libs_t* depended)
     depended->cap = 0;
     depended->size = 0;
     if(depended->libs)
-        free(depended->libs);
+        box_free(depended->libs);
     depended->libs = NULL;
 }
diff --git a/src/custommem.c b/src/custommem.c
index 691771ec..7d0d82f0 100644
--- a/src/custommem.c
+++ b/src/custommem.c
@@ -271,7 +271,7 @@ void* customMalloc(size_t size)
     }
     // add a new block
     int i = n_blocks++;
-    p_blocks = (blocklist_t*)realloc(p_blocks, n_blocks*sizeof(blocklist_t));
+    p_blocks = (blocklist_t*)box_realloc(p_blocks, n_blocks*sizeof(blocklist_t));
     size_t allocsize = MMAPSIZE;
     if(size+2*sizeof(blockmark_t)>allocsize)
         allocsize = size+2*sizeof(blockmark_t);
@@ -279,7 +279,7 @@ void* customMalloc(size_t size)
     void* p = mmap(NULL, allocsize, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
     memset(p, 0, allocsize);
     #else
-    void* p = calloc(1, allocsize);
+    void* p = box_calloc(1, allocsize);
     #endif
     p_blocks[i].block = p;
     p_blocks[i].size = allocsize;
@@ -410,7 +410,7 @@ uintptr_t AddNewDynarecMap(dynablock_t* db, size_t size)
     dynarec_log(LOG_DEBUG, "Ask for DynaRec Block Alloc #%zu/%zu\n", mmapsize, mmapcap);
     if(mmapsize>mmapcap) {
         mmapcap += 32;
-        mmaplist = (mmaplist_t*)realloc(mmaplist, mmapcap*sizeof(mmaplist_t));
+        mmaplist = (mmaplist_t*)box_realloc(mmaplist, mmapcap*sizeof(mmaplist_t));
     }
     #ifndef USE_MMAP
     void *p = NULL;
@@ -433,7 +433,7 @@ uintptr_t AddNewDynarecMap(dynablock_t* db, size_t size)
     mmaplist[i].locked = 1;
     mmaplist[i].block = p;
     mmaplist[i].size = MMAPSIZE;
-    mmaplist[i].helper = (uint8_t*)calloc(1, MMAPSIZE);
+    mmaplist[i].helper = (uint8_t*)box_calloc(1, MMAPSIZE);
     mmaplist[i].first = p;
     // setup marks
     blockmark_t* m = (blockmark_t*)p;
@@ -556,7 +556,7 @@ void FreeDynarecMap(dynablock_t* db, uintptr_t addr, size_t size)
         return;
     if(size>MMAPSIZE-2*sizeof(blockmark_t)) {
         #ifndef USE_MMAP
-        free((void*)addr);
+        box_free((void*)addr);
         #else
         munmap((void*)addr, size);
         #endif
@@ -597,14 +597,14 @@ void addDBFromAddressRange(uintptr_t addr, size_t size)
         int idx2 = (i>>16)&((1<<DYNAMAP_SHIFT)-1);
         int idx1 = (i    )&((1<<DYNAMAP_SHIFT)-1);
         if(!dynmap123[idx3]) {
-            dynablocklist_t*** p = (dynablocklist_t***)calloc(1<<DYNAMAP_SHIFT, sizeof(dynablocklist_t**));
+            dynablocklist_t*** p = (dynablocklist_t***)box_calloc(1<<DYNAMAP_SHIFT, sizeof(dynablocklist_t**));
             if(native_lock_storeifnull(&dynmap123[idx3], p)!=p)
-                free(p);
+                box_free(p);
         }
         if(!dynmap123[idx3][idx2]) {
-            dynablocklist_t** p = (dynablocklist_t**)calloc(1<<DYNAMAP_SHIFT, sizeof(dynablocklist_t*));
+            dynablocklist_t** p = (dynablocklist_t**)box_calloc(1<<DYNAMAP_SHIFT, sizeof(dynablocklist_t*));
             if(native_lock_storeifnull(&dynmap123[idx3][idx2], p)!=p)
-                free(p);
+                box_free(p);
         }
         if(!dynmap123[idx3][idx2][idx1]) {
             dynablocklist_t* p = NewDynablockList(i<<DYNAMAP_SHIFT, 1<<DYNAMAP_SHIFT, 0);
@@ -644,10 +644,10 @@ void cleanDBFromAddressRange(uintptr_t addr, size_t size, int destroy)
                                     dynablocklist_t*** p2 = dynmap123[idx3];
                                     if(dynmapempty((void**)p2)) {
                                         if(!native_lock_storeifref(&dynmap123[idx3], NULL, p2)) {
-                                            free(p2);
+                                            box_free(p2);
                                         }
                                     }
-                                    free(p);
+                                    box_free(p);
                                 }
                             }
                             FreeDynablockList(&dblist);
@@ -678,25 +678,25 @@ void addJumpTableIfDefault64(void* addr, void* jmp)
     idx1 = (((uintptr_t)addr)>>16)&0xffff;
     idx0 = (((uintptr_t)addr)    )&0xffff;
     if(box64_jmptbl3[idx3] == box64_jmptbldefault2) {
-        uintptr_t*** tbl = (uintptr_t***)malloc((1<<JMPTABL_SHIFT)*sizeof(uintptr_t**));
+        uintptr_t*** tbl = (uintptr_t***)box_malloc((1<<JMPTABL_SHIFT)*sizeof(uintptr_t**));
         for(int i=0; i<(1<<JMPTABL_SHIFT); ++i)
             tbl[i] = box64_jmptbldefault1;
         if(native_lock_storeifref(&box64_jmptbl3[idx3], tbl, box64_jmptbldefault2)!=tbl)
-            free(tbl);
+            box_free(tbl);
     }
     if(box64_jmptbl3[idx3][idx2] == box64_jmptbldefault1) {
-        uintptr_t** tbl = (uintptr_t**)malloc((1<<JMPTABL_SHIFT)*sizeof(uintptr_t*));
+        uintptr_t** tbl = (uintptr_t**)box_malloc((1<<JMPTABL_SHIFT)*sizeof(uintptr_t*));
         for(int i=0; i<(1<<JMPTABL_SHIFT); ++i)
             tbl[i] = box64_jmptbldefault0;
         if(native_lock_storeifref(&box64_jmptbl3[idx3][idx2], tbl, box64_jmptbldefault1)!=tbl)
-            free(tbl);
+            box_free(tbl);
     }
     if(box64_jmptbl3[idx3][idx2][idx1] == box64_jmptbldefault0) {
-        uintptr_t* tbl = (uintptr_t*)malloc((1<<JMPTABL_SHIFT)*sizeof(uintptr_t));
+        uintptr_t* tbl = (uintptr_t*)box_malloc((1<<JMPTABL_SHIFT)*sizeof(uintptr_t));
         for(int i=0; i<(1<<JMPTABL_SHIFT); ++i)
             tbl[i] = (uintptr_t)native_next;
         if(native_lock_storeifref(&box64_jmptbl3[idx3][idx2][idx1], tbl, box64_jmptbldefault0)!=tbl)
-            free(tbl);
+            box_free(tbl);
     }
 
     native_lock_storeifref(&box64_jmptbl3[idx3][idx2][idx1][idx0], jmp, native_next);
@@ -748,25 +748,25 @@ uintptr_t getJumpTableAddress64(uintptr_t addr)
     idx1 = ((addr)>>16)&0xffff;
     idx0 = ((addr)    )&0xffff;
     if(box64_jmptbl3[idx3] == box64_jmptbldefault2) {
-        uintptr_t*** tbl = (uintptr_t***)malloc((1<<JMPTABL_SHIFT)*sizeof(uintptr_t**));
+        uintptr_t*** tbl = (uintptr_t***)box_malloc((1<<JMPTABL_SHIFT)*sizeof(uintptr_t**));
         for(int i=0; i<(1<<JMPTABL_SHIFT); ++i)
             tbl[i] = box64_jmptbldefault1;
         if(native_lock_storeifref(&box64_jmptbl3[idx3], tbl, box64_jmptbldefault2)!=tbl)
-            free(tbl);
+            box_free(tbl);
     }
     if(box64_jmptbl3[idx3][idx2] == box64_jmptbldefault1) {
-        uintptr_t** tbl = (uintptr_t**)malloc((1<<JMPTABL_SHIFT)*sizeof(uintptr_t*));
+        uintptr_t** tbl = (uintptr_t**)box_malloc((1<<JMPTABL_SHIFT)*sizeof(uintptr_t*));
         for(int i=0; i<(1<<JMPTABL_SHIFT); ++i)
             tbl[i] = box64_jmptbldefault0;
         if(native_lock_storeifref(&box64_jmptbl3[idx3][idx2], tbl, box64_jmptbldefault1)!=tbl)
-            free(tbl);
+            box_free(tbl);
     }
     if(box64_jmptbl3[idx3][idx2][idx1] == box64_jmptbldefault0) {
-        uintptr_t* tbl = (uintptr_t*)malloc((1<<JMPTABL_SHIFT)*sizeof(uintptr_t));
+        uintptr_t* tbl = (uintptr_t*)box_malloc((1<<JMPTABL_SHIFT)*sizeof(uintptr_t));
         for(int i=0; i<(1<<JMPTABL_SHIFT); ++i)
             tbl[i] = (uintptr_t)native_next;
         if(native_lock_storeifref(&box64_jmptbl3[idx3][idx2][idx1], tbl, box64_jmptbldefault0)!=tbl)
-            free(tbl);
+            box_free(tbl);
     }
 
     return (uintptr_t)&box64_jmptbl3[idx3][idx2][idx1][idx0];
@@ -786,9 +786,9 @@ void protectDB(uintptr_t addr, uintptr_t size)
     int ret;
     for (uintptr_t i=(idx>>16); i<=(end>>16); ++i)
         if(memprot[i]==memprot_default) {
-            uint8_t* newblock = calloc(1<<16, sizeof(uint8_t));
+            uint8_t* newblock = box_calloc(1<<16, sizeof(uint8_t));
             /*if (native_lock_storeifref(&memprot[i], newblock, memprot_default) != newblock) {
-                free(newblock);
+                box_free(newblock);
             }*/
             memprot[i] = newblock;
         }
@@ -819,9 +819,9 @@ void unprotectDB(uintptr_t addr, size_t size)
     pthread_mutex_lock(&mutex_prot);
     for (uintptr_t i=(idx>>16); i<=(end>>16); ++i)
         if(memprot[i]==memprot_default) {
-            uint8_t* newblock = calloc(1<<16, sizeof(uint8_t));
+            uint8_t* newblock = box_calloc(1<<16, sizeof(uint8_t));
             /*if (native_lock_storeifref(&memprot[i], newblock, memprot_default) != newblock) {
-                free(newblock);
+                box_free(newblock);
             }*/
             memprot[i] = newblock;
         }
@@ -892,7 +892,7 @@ void addMapMem(uintptr_t begin, uintptr_t end)
         newm = m;
     } else {
     // create a new block
-        newm = (mapmem_t*)calloc(1, sizeof(mapmem_t));
+        newm = (mapmem_t*)box_calloc(1, sizeof(mapmem_t));
         newm->next = m->next;
         newm->begin = begin;
         newm->end = end;
@@ -904,7 +904,7 @@ void addMapMem(uintptr_t begin, uintptr_t end)
             newm->end = newm->next->end;
         mapmem_t* tmp = newm->next;
         newm->next = tmp->next;
-        free(tmp);
+        box_free(tmp);
     }
     // all done!
 }
@@ -932,9 +932,9 @@ void removeMapMem(uintptr_t begin, uintptr_t end)
                 m = mapmem;
                 prev = NULL;
             }
-            free(tmp);
+            box_free(tmp);
         } else if(begin>m->begin && end<m->end) { // the zone is totaly inside the block => split it!
-            mapmem_t* newm = (mapmem_t*)calloc(1, sizeof(mapmem_t));    // create a new "next"
+            mapmem_t* newm = (mapmem_t*)box_calloc(1, sizeof(mapmem_t));    // create a new "next"
             newm->end = m->end;
             m->end = begin - 1;
             newm->begin = end + 1;
@@ -965,10 +965,10 @@ void updateProtection(uintptr_t addr, size_t size, uint32_t prot)
     pthread_mutex_lock(&mutex_prot);
     for (uintptr_t i=(idx>>16); i<=(end>>16); ++i)
         if(memprot[i]==memprot_default) {
-            uint8_t* newblock = calloc(1<<16, sizeof(uint8_t));
+            uint8_t* newblock = box_calloc(1<<16, sizeof(uint8_t));
 #if 0 //def ARM64   //disabled for now, not usefull with the mutex
             if (native_lock_storeifref(&memprot[i], newblock, memprot_default) != newblock) {
-                free(newblock);
+                box_free(newblock);
             }
 #else
             memprot[i] = newblock;
@@ -995,10 +995,10 @@ void setProtection(uintptr_t addr, size_t size, uint32_t prot)
     pthread_mutex_lock(&mutex_prot);
     for (uintptr_t i=(idx>>16); i<=(end>>16); ++i)
         if(memprot[i]==memprot_default) {
-            uint8_t* newblock = calloc(MEMPROT_SIZE, sizeof(uint8_t));
+            uint8_t* newblock = box_calloc(MEMPROT_SIZE, sizeof(uint8_t));
 #if 0 //def ARM64   //disabled for now, not usefull with the mutex
             if (native_lock_storeifref(&memprot[i], newblock, memprot_default) != newblock) {
-                free(newblock);
+                box_free(newblock);
             }
 #else
             memprot[i] = newblock;
@@ -1022,10 +1022,10 @@ void allocProtection(uintptr_t addr, size_t size, uint32_t prot)
     pthread_mutex_lock(&mutex_prot);
     for (uintptr_t i=(idx>>16); i<=(end>>16); ++i)
         if(memprot[i]==memprot_default) {
-            uint8_t* newblock = calloc(1<<16, sizeof(uint8_t));
+            uint8_t* newblock = box_calloc(1<<16, sizeof(uint8_t));
 #if 0 //def ARM64   //disabled for now, not usefull with the mutex
             if (native_lock_storeifref(&memprot[i], newblock, memprot_default) != newblock) {
-                free(newblock);
+                box_free(newblock);
             }
 #else
             memprot[i] = newblock;
@@ -1103,16 +1103,16 @@ void freeProtection(uintptr_t addr, size_t size)
                         memprot[key][i] |= block[i];
                     }
                 }
-                if (block != memprot_default) free(block);
+                if (block != memprot_default) box_free(block);
             }
 #else
             if(start==0 && finish==MEMPROT_SIZE-1) {
                 memprot[key] = memprot_default;
-                free(block);
+                box_free(block);
             }
             /*else if(blockempty(block)) {
                 memprot[key] = memprot_default;
-                free(block);
+                box_free(block);
             }*/
 #endif
             i+=finish-start;    // +1 from the "for" loop
@@ -1244,7 +1244,7 @@ void init_custommem_helper(box64context_t* ctx)
 #endif
     pthread_atfork(NULL, NULL, atfork_child_custommem);
     // init mapmem list
-    mapmem = (mapmem_t*)calloc(1, sizeof(mapmem_t));
+    mapmem = (mapmem_t*)box_calloc(1, sizeof(mapmem_t));
     mapmem->begin = 0x0;
     mapmem->end = (uintptr_t)LOWEST - 1;
     loadProtectionFromMap();
@@ -1269,14 +1269,14 @@ void fini_custommem_helper(box64context_t *ctx)
                 #ifdef USE_MMAP
                 munmap(mmaplist[i].block, mmaplist[i].size);
                 #else
-                free(mmaplist[i].block);
+                box_free(mmaplist[i].block);
                 #endif
             if(mmaplist[i].dblist) {
                 kh_destroy(dynablocks, mmaplist[i].dblist);
                 mmaplist[i].dblist = NULL;
             }
             if(mmaplist[i].helper) {
-                free(mmaplist[i].helper);
+                box_free(mmaplist[i].helper);
                 mmaplist[i].helper = NULL;
             }
         }
@@ -1294,23 +1294,23 @@ void fini_custommem_helper(box64context_t *ctx)
                         for (uintptr_t idx1=0; idx1<=0xffff; ++idx1)
                             if(dynmap123[idx3][idx2][idx1])
                                 FreeDynablockList(&dynmap123[idx3][idx2][idx1]);
-                        free(dynmap123[idx3][idx2]);
+                        box_free(dynmap123[idx3][idx2]);
                     }
-                free(dynmap123[idx3]);
+                box_free(dynmap123[idx3]);
             }
 
-        free(mmaplist);
+        box_free(mmaplist);
         for (int i3=0; i3<(1<<DYNAMAP_SHIFT); ++i3)
             if(box64_jmptbl3[i3]!=box64_jmptbldefault2) {
                 for (int i2=0; i2<(1<<DYNAMAP_SHIFT); ++i2)
                     if(box64_jmptbl3[i3][i2]!=box64_jmptbldefault1) {
                         for (int i1=0; i1<(1<<DYNAMAP_SHIFT); ++i1)
                             if(box64_jmptbl3[i3][i2][i1]!=box64_jmptbldefault0) {
-                                free(box64_jmptbl3[i3][i2][i1]);
+                                box_free(box64_jmptbl3[i3][i2][i1]);
                             }
-                        free(box64_jmptbl3[i3][i2]);
+                        box_free(box64_jmptbl3[i3][i2]);
                     }
-                free(box64_jmptbl3[i3]);
+                box_free(box64_jmptbl3[i3]);
             }
     }
     kh_destroy(lockaddress, lockaddress);
@@ -1320,22 +1320,22 @@ void fini_custommem_helper(box64context_t *ctx)
     for(int i=0; i<(1<<20); ++i) {
         m = memprot[i];
         if(m!=memprot_default)
-            free(m);
+            box_free(m);
     }
 
     for(int i=0; i<n_blocks; ++i)
         #ifdef USE_MMAP
         munmap(p_blocks[i].block, p_blocks[i].size);
         #else
-        free(p_blocks[i].block);
+        box_free(p_blocks[i].block);
         #endif
-    free(p_blocks);
+    box_free(p_blocks);
     pthread_mutex_destroy(&mutex_prot);
     pthread_mutex_destroy(&mutex_blocks);
     while(mapmem) {
         mapmem_t *tmp = mapmem;
         mapmem = mapmem->next;
-        free(tmp);
+        box_free(tmp);
     }
 }
 
diff --git a/src/dynarec/arm64/dynarec_arm64_pass0.h b/src/dynarec/arm64/dynarec_arm64_pass0.h
index 87b256b5..26dcc9c7 100755
--- a/src/dynarec/arm64/dynarec_arm64_pass0.h
+++ b/src/dynarec/arm64/dynarec_arm64_pass0.h
@@ -23,7 +23,7 @@
 #define NEW_INST \
         ++dyn->size;                            \
         if(dyn->size+3>=dyn->cap) {             \
-                dyn->insts = (instruction_native_t*)realloc(dyn->insts, sizeof(instruction_native_t)*dyn->cap*2);     \
+                dyn->insts = (instruction_native_t*)box_realloc(dyn->insts, sizeof(instruction_native_t)*dyn->cap*2);\
                 memset(&dyn->insts[dyn->cap], 0, sizeof(instruction_native_t)*dyn->cap);   \
                 dyn->cap *= 2;                  \
         }                                       \
diff --git a/src/dynarec/dynablock.c b/src/dynarec/dynablock.c
index 02286442..0e5e4aa0 100755
--- a/src/dynarec/dynablock.c
+++ b/src/dynarec/dynablock.c
@@ -43,13 +43,13 @@ dynablocklist_t* NewDynablockList(uintptr_t text, int textsz, int direct)
         printf_log(LOG_NONE, "Error, creating a NULL sized Dynablock\n");
         return NULL;
     }
-    dynablocklist_t* ret = (dynablocklist_t*)calloc(1, sizeof(dynablocklist_t));
+    dynablocklist_t* ret = (dynablocklist_t*)box_calloc(1, sizeof(dynablocklist_t));
     ret->text = text;
     ret->textsz = textsz;
     ret->minstart = text;
     ret->maxend = text+textsz-1;
     if(direct && textsz) {
-        ret->direct = (dynablock_t**)calloc(textsz, sizeof(dynablock_t*));
+        ret->direct = (dynablock_t**)box_calloc(textsz, sizeof(dynablock_t*));
         if(!ret->direct) {printf_log(LOG_NONE, "Warning, fail to create direct block for dynablock @%p\n", (void*)text);}
     }
     dynarec_log(LOG_DEBUG, "New Dynablocklist %p, from %p->%p\n", ret, (void*)text, (void*)(text+textsz));
@@ -85,10 +85,10 @@ void FreeDynablock(dynablock_t* db, int need_lock)
         if(!db->father) {
             dynarec_log(LOG_DEBUG, " -- FreeDyrecMap(%p, %d)\n", db->block, db->size);
             FreeDynarecMap(db, (uintptr_t)db->block, db->size);
-            free(db->sons);
-            free(db->instsize);
+            box_free(db->sons);
+            box_free(db->instsize);
         }
-        free(db);
+        box_free(db);
         if(need_lock)
             pthread_mutex_unlock(&my_context->mutex_dyndump);
     }
@@ -106,11 +106,11 @@ void FreeDynablockList(dynablocklist_t** dynablocks)
             if((*dynablocks)->direct[i] && !(*dynablocks)->direct[i]->father) 
                 FreeDynablock((*dynablocks)->direct[i], 1);
         }
-        free((*dynablocks)->direct);
+        box_free((*dynablocks)->direct);
     }
     (*dynablocks)->direct = NULL;
 
-    free(*dynablocks);
+    box_free(*dynablocks);
     *dynablocks = NULL;
 }
 
@@ -271,21 +271,21 @@ dynablock_t *AddNewDynablock(dynablocklist_t* dynablocks, uintptr_t addr, int* c
     
     pthread_mutex_lock(&my_context->mutex_dyndump);
     if(!dynablocks->direct) {
-        dynablock_t** p = (dynablock_t**)calloc(dynablocks->textsz, sizeof(dynablock_t*));
+        dynablock_t** p = (dynablock_t**)box_calloc(dynablocks->textsz, sizeof(dynablock_t*));
         if(native_lock_storeifnull(&dynablocks->direct, p)!=p)
-            free(p);    // someone already create the direct array, too late...
+            box_free(p);    // someone already create the direct array, too late...
     }
 
     // create and add new block
     dynarec_log(LOG_VERBOSE, "Ask for DynaRec Block creation @%p\n", (void*)addr);
 
-    block = (dynablock_t*)calloc(1, sizeof(dynablock_t));
+    block = (dynablock_t*)box_calloc(1, sizeof(dynablock_t));
     block->parent = dynablocks; 
     dynablock_t* tmp = (dynablock_t*)native_lock_storeifnull(&dynablocks->direct[addr-dynablocks->text], block);
     if(tmp !=  block) {
         // a block appeard!
         pthread_mutex_unlock(&my_context->mutex_dyndump);
-        free(block);
+        box_free(block);
         *created = 0;
         return tmp;
     }
@@ -356,7 +356,7 @@ static dynablock_t* internalDBGetBlock(x64emu_t* emu, uintptr_t addr, uintptr_t
             dynarec_log(LOG_INFO, "Warning, a wild block appeared at %p: %p\n", (void*)addr, old);
             // doing nothing else, the block has not be writen
         }
-        free(block);
+        box_free(block);
         block = NULL;
     }
     // check size
diff --git a/src/dynarec/dynarec_native.c b/src/dynarec/dynarec_native.c
index c44c42be..260fb145 100755
--- a/src/dynarec/dynarec_native.c
+++ b/src/dynarec/dynarec_native.c
@@ -66,7 +66,7 @@ void add_next(dynarec_native_t *dyn, uintptr_t addr) {
             return;
     if(dyn->next_sz == dyn->next_cap) {
         dyn->next_cap += 16;
-        dyn->next = (uintptr_t*)realloc(dyn->next, dyn->next_cap*sizeof(uintptr_t));
+        dyn->next = (uintptr_t*)box_realloc(dyn->next, dyn->next_cap*sizeof(uintptr_t));
     }
     dyn->next[dyn->next_sz++] = addr;
 }
@@ -242,7 +242,7 @@ instsize_t* addInst(instsize_t* insts, size_t* size, size_t* cap, int x64_size,
         toadd = 1 + native_size/15;
     if((*size)+toadd>(*cap)) {
         *cap = (*size)+toadd;
-        insts = (instsize_t*)realloc(insts, (*cap)*sizeof(instsize_t));
+        insts = (instsize_t*)box_realloc(insts, (*cap)*sizeof(instsize_t));
     }
     while(toadd) {
         if(x64_size>15)
@@ -273,7 +273,7 @@ int Table64(dynarec_native_t *dyn, uint64_t val)
     if(idx==-1) {
         if(dyn->table64size == dyn->table64cap) {
             dyn->table64cap+=4;
-            dyn->table64 = (uint64_t*)realloc(dyn->table64, dyn->table64cap * sizeof(uint64_t));
+            dyn->table64 = (uint64_t*)box_realloc(dyn->table64, dyn->table64cap * sizeof(uint64_t));
         }
         idx = dyn->table64size++;
         dyn->table64[idx] = val;
@@ -301,7 +301,7 @@ static void fillPredecessors(dynarec_native_t* dyn)
             dyn->insts[i+1].pred_sz++;
         }
     }
-    dyn->predecessor = (int*)malloc(pred_sz*sizeof(int));
+    dyn->predecessor = (int*)box_malloc(pred_sz*sizeof(int));
     // fill pred pointer
     int* p = dyn->predecessor;
     for(int i=0; i<dyn->size; ++i) {
@@ -381,11 +381,11 @@ void CancelBlock64()
     current_helper = NULL;
     if(!helper)
         return;
-    free(helper->next);
-    free(helper->insts);
-    free(helper->table64);
-    free(helper->sons_x64);
-    free(helper->sons_native);
+    box_free(helper->next);
+    box_free(helper->insts);
+    box_free(helper->table64);
+    box_free(helper->sons_x64);
+    box_free(helper->sons_native);
     if(helper->dynablock && helper->dynablock->block)
         FreeDynarecMap(helper->dynablock, (uintptr_t)helper->dynablock->block, helper->dynablock->size);
 }
@@ -413,11 +413,11 @@ void* FillBlock64(dynablock_t* block, uintptr_t addr) {
     helper.start = addr;
     uintptr_t start = addr;
     helper.cap = 64; // needs epilog handling
-    helper.insts = (instruction_native_t*)calloc(helper.cap, sizeof(instruction_native_t));
+    helper.insts = (instruction_native_t*)box_calloc(helper.cap, sizeof(instruction_native_t));
     // pass 0, addresses, x64 jump addresses, overall size of the block
     uintptr_t end = native_pass0(&helper, addr);
     // no need for next anymore
-    free(helper.next);
+    box_free(helper.next);
     helper.next_sz = helper.next_cap = 0;
     helper.next = NULL;
     // basic checks
@@ -538,8 +538,8 @@ void* FillBlock64(dynablock_t* block, uintptr_t addr) {
     helper.native_start = (uintptr_t)p;
     helper.tablestart = helper.native_start + helper.native_size;
     if(helper.sons_size) {
-        helper.sons_x64 = (uintptr_t*)calloc(helper.sons_size, sizeof(uintptr_t));
-        helper.sons_native = (void**)calloc(helper.sons_size, sizeof(void*));
+        helper.sons_x64 = (uintptr_t*)box_calloc(helper.sons_size, sizeof(uintptr_t));
+        helper.sons_native = (void**)box_calloc(helper.sons_size, sizeof(void*));
     }
     // pass 3, emit (log emit native opcode)
     if(box64_dynarec_dump) {
@@ -578,15 +578,15 @@ void* FillBlock64(dynablock_t* block, uintptr_t addr) {
         for(int i=0; i<helper.size; ++i)
             cap += 1 + ((helper.insts[i].x64.size>helper.insts[i].size)?helper.insts[i].x64.size:helper.insts[i].size)/15;
         size_t size = 0;
-        block->instsize = (instsize_t*)calloc(cap, sizeof(instsize_t));
+        block->instsize = (instsize_t*)box_calloc(cap, sizeof(instsize_t));
         for(int i=0; i<helper.size; ++i)
             block->instsize = addInst(block->instsize, &size, &cap, helper.insts[i].x64.size, helper.insts[i].size/4);
         block->instsize = addInst(block->instsize, &size, &cap, 0, 0);    // add a "end of block" mark, just in case
     }
     // ok, free the helper now
-    free(helper.insts);
+    box_free(helper.insts);
     helper.insts = NULL;
-    free(helper.table64);
+    box_free(helper.table64);
     helper.table64 = NULL;
     block->size = sz;
     block->isize = helper.size;
@@ -611,7 +611,7 @@ void* FillBlock64(dynablock_t* block, uintptr_t addr) {
     dynablock_t** sons = NULL;
     int sons_size = 0;
     if(helper.sons_size) {
-        sons = (dynablock_t**)calloc(helper.sons_size, sizeof(dynablock_t*));
+        sons = (dynablock_t**)box_calloc(helper.sons_size, sizeof(dynablock_t*));
         for (int i=0; i<helper.sons_size; ++i) {
             int created = 1;
             dynablock_t *son = AddNewDynablock(block->parent, helper.sons_x64[i], &created);
@@ -633,11 +633,11 @@ void* FillBlock64(dynablock_t* block, uintptr_t addr) {
             block->sons = sons;
             block->sons_size = sons_size;
         } else
-            free(sons);
+            box_free(sons);
     }
-    free(helper.sons_x64);
+    box_free(helper.sons_x64);
     helper.sons_x64 = NULL;
-    free(helper.sons_native);
+    box_free(helper.sons_native);
     helper.sons_native = NULL;
     current_helper = NULL;
     //block->done = 1;
diff --git a/src/elfs/elfdwarf_private.c b/src/elfs/elfdwarf_private.c
index 84ee71dd..aeba39bf 100644
--- a/src/elfs/elfdwarf_private.c
+++ b/src/elfs/elfdwarf_private.c
@@ -393,17 +393,17 @@ uintptr_t get_parent_registers(dwarf_unwind_t *unwind, const elfheader_t *ehdr,
                 ++unwind_constr.reg_count;
                 size_t tablelen = unwind_constr.reg_count * sizeof(uint64_t);
                 size_t statuseslen = ((unwind_constr.reg_count+1) >> 1) * sizeof(uint8_t);
-                unwind_constr.table = (uint64_t*)malloc(tablelen);
-                unwind_constr.statuses = (uint8_t*)calloc((unwind_constr.reg_count+1) >> 1, sizeof(uint8_t));
+                unwind_constr.table = (uint64_t*)box_malloc(tablelen);
+                unwind_constr.statuses = (uint8_t*)box_calloc((unwind_constr.reg_count+1) >> 1, sizeof(uint8_t));
                 // ~~undefined is 0: no initialization needed~~ still initialize the first 17 to same_val
                 for (int i = 0; (i < 17) && (i <= unwind_constr.reg_count); ++i) {
                     SET_STATUS(unwind_constr, i, REGSTATUS_same_val);
                 }
 
                 curstacksize = 0;
-                uint64_t **table_stack = (uint64_t**)malloc(maxstacksize * sizeof(uint64_t*));
+                uint64_t **table_stack = (uint64_t**)box_malloc(maxstacksize * sizeof(uint64_t*));
                 for (uint64_t i = 0; i < maxstacksize; ++i) {
-                    table_stack[i] = (uint64_t*)malloc(tablelen);
+                    table_stack[i] = (uint64_t*)box_malloc(tablelen);
                 }
                 cur_inst = initCIEinstr;
                 uintptr_t cur_pointed_addr = pc_begin;
@@ -541,10 +541,10 @@ uintptr_t get_parent_registers(dwarf_unwind_t *unwind, const elfheader_t *ehdr,
     printf_log(LOG_DEBUG, "Trying to restore register 0x%02lX while in the intial CFIs\n", (uint64_t)reg); \
     FAILED
 #define FAILED \
-    free(unwind_constr.statuses);                                       \
-    free(unwind_constr.table);                                          \
-    for (uint64_t i = 0; i < maxstacksize; ++i) free(table_stack[i]);   \
-    free(table_stack);                                                  \
+    box_free(unwind_constr.statuses);                                       \
+    box_free(unwind_constr.table);                                          \
+    for (uint64_t i = 0; i < maxstacksize; ++i) box_free(table_stack[i]);   \
+    box_free(table_stack);                                                  \
     *success = 0; return 0;
                 while (cur_inst < endCIEinstr) {
                     unsigned char inst; READ_U1(inst, cur_inst);
@@ -553,21 +553,21 @@ uintptr_t get_parent_registers(dwarf_unwind_t *unwind, const elfheader_t *ehdr,
                 }
 #undef FAILED
 #undef RESTORE_REG
-                uint64_t *init_table = (uint64_t*)malloc(tablelen);
+                uint64_t *init_table = (uint64_t*)box_malloc(tablelen);
                 memcpy(init_table, unwind_constr.table, tablelen);
-                uint8_t *init_statuses = (uint8_t*)malloc(statuseslen);
+                uint8_t *init_statuses = (uint8_t*)box_malloc(statuseslen);
                 memcpy(init_statuses, unwind_constr.statuses, statuseslen);
                 cur_inst = cur_addr;
 #define RESTORE_REG(reg) \
     unwind_constr.table[reg] = init_table[reg]; \
     SET_STATUS(unwind_constr, (reg), ((init_statuses[(reg) >> 1] >> (((reg) & 1) << 2)) & 0xF));
 #define FAILED \
-    free(init_statuses);                                                \
-    free(init_table);                                                   \
-    free(unwind_constr.statuses);                                       \
-    free(unwind_constr.table);                                          \
-    for (uint64_t i = 0; i < maxstacksize; ++i) free(table_stack[i]);   \
-    free(table_stack);                                                  \
+    box_free(init_statuses);                                                \
+    box_free(init_table);                                                   \
+    box_free(unwind_constr.statuses);                                       \
+    box_free(unwind_constr.table);                                          \
+    for (uint64_t i = 0; i < maxstacksize; ++i) box_free(table_stack[i]);   \
+    box_free(table_stack);                                                  \
     *success = 0; return 0;
                 while ((cur_inst < next_addr) && (cur_pointed_addr <= addr)) {
                     unsigned char inst; READ_U1(inst, cur_inst);
@@ -577,16 +577,16 @@ uintptr_t get_parent_registers(dwarf_unwind_t *unwind, const elfheader_t *ehdr,
 #undef FAILED
 #undef RESTORE_REG
 #undef PARSE_INST
-                free(init_statuses);
-                free(init_table);
+                box_free(init_statuses);
+                box_free(init_table);
                 for (int i = 0; i < maxstacksize; ++i) {
-                    free(table_stack[i]);
+                    box_free(table_stack[i]);
                 }
-                free(table_stack);
+                box_free(table_stack);
 
                 dwarf_unwind_t new_unwind;
                 new_unwind.reg_count = unwind_constr.reg_count;
-                new_unwind.regs = calloc(unwind_constr.reg_count, sizeof(uint64_t));
+                new_unwind.regs = box_calloc(unwind_constr.reg_count, sizeof(uint64_t));
                 uintptr_t cfa = unwind->regs[cfa_reg];
                 if (cfa_signed) cfa += cfa_offset.soff;
                 else            cfa += cfa_offset.uoff;
@@ -600,9 +600,9 @@ uintptr_t get_parent_registers(dwarf_unwind_t *unwind, const elfheader_t *ehdr,
                     case REGSTATUS_same_val:
                         if (i >= unwind->reg_count) {
                             printf_log(LOG_DEBUG, "Invalid register status (value copied from register 0x%02lX)\n", i);
-                            free(unwind_constr.statuses);
-                            free(unwind_constr.table);
-                            free(new_unwind.regs);
+                            box_free(unwind_constr.statuses);
+                            box_free(unwind_constr.table);
+                            box_free(new_unwind.regs);
                             *success = 0;
                             return 0;
                         }
@@ -620,9 +620,9 @@ uintptr_t get_parent_registers(dwarf_unwind_t *unwind, const elfheader_t *ehdr,
                     case REGSTATUS_register:
                         if (unwind_constr.table[i] >= unwind->reg_count) {
                             printf_log(LOG_DEBUG, "Invalid register status (value copied from register 0x%02lX)\n", unwind_constr.table[i]);
-                            free(unwind_constr.statuses);
-                            free(unwind_constr.table);
-                            free(new_unwind.regs);
+                            box_free(unwind_constr.statuses);
+                            box_free(unwind_constr.table);
+                            box_free(new_unwind.regs);
                             *success = 0;
                             return 0;
                         }
@@ -632,10 +632,10 @@ uintptr_t get_parent_registers(dwarf_unwind_t *unwind, const elfheader_t *ehdr,
                     }
                 }
                 *success = (GET_STATUS(unwind_constr, return_addr_reg) == REGSTATUS_undefined) ? 0 : ((aug_fields & AUG_SIGHDLER) ? 2 : 1);
-                free(unwind_constr.statuses);
-                free(unwind_constr.table);
+                box_free(unwind_constr.statuses);
+                box_free(unwind_constr.table);
                 
-                free(unwind->regs);
+                box_free(unwind->regs);
                 unwind->reg_count = new_unwind.reg_count;
                 unwind->regs = new_unwind.regs;
 
@@ -658,9 +658,9 @@ uintptr_t get_parent_registers(dwarf_unwind_t *unwind, const elfheader_t *ehdr,
 }
 
 dwarf_unwind_t *init_dwarf_unwind_registers(x64emu_t *emu) {
-    dwarf_unwind_t *unwind_struct = (dwarf_unwind_t*)malloc(sizeof(dwarf_unwind_t));
+    dwarf_unwind_t *unwind_struct = (dwarf_unwind_t*)box_malloc(sizeof(dwarf_unwind_t));
     unwind_struct->reg_count = 17;
-    unwind_struct->regs = (uint64_t*)malloc(17*sizeof(uint64_t));
+    unwind_struct->regs = (uint64_t*)box_malloc(17*sizeof(uint64_t));
     /* x86_64-abi-0.99.pdf
      * Register Name                    | Number | Abbreviation
      * General Purpose Register RAX     | 0      | %rax
@@ -715,7 +715,7 @@ dwarf_unwind_t *init_dwarf_unwind_registers(x64emu_t *emu) {
 }
 
 void free_dwarf_unwind_registers(dwarf_unwind_t **unwind_struct) {
-    free((*unwind_struct)->regs);
-    free(*unwind_struct);
+    box_free((*unwind_struct)->regs);
+    box_free(*unwind_struct);
     *unwind_struct = NULL;
 }
diff --git a/src/elfs/elfloader.c b/src/elfs/elfloader.c
index fa82f2e3..938b6318 100755
--- a/src/elfs/elfloader.c
+++ b/src/elfs/elfloader.c
@@ -55,7 +55,7 @@ elfheader_t* LoadAndCheckElfHeader(FILE* f, const char* name, int exec)
         return NULL;
 
     if ((h->path = realpath(name, NULL)) == NULL) {
-        h->path = (char*)malloc(1);
+        h->path = (char*)box_malloc(1);
         h->path[0] = '\0';
     }
     return h;
@@ -72,19 +72,19 @@ void FreeElfHeader(elfheader_t** head)
         cleanDBFromAddressRange(my_context, h->text, h->textsz, 1);
     }*/ // will be free at the end, no need to free it now
 #endif
-    free(h->name);
-    free(h->path);
-    free(h->PHEntries);
-    free(h->SHEntries);
-    free(h->SHStrTab);
-    free(h->StrTab);
-    free(h->Dynamic);
-    free(h->DynStr);
-    free(h->SymTab);
-    free(h->DynSym);
+    box_free(h->name);
+    box_free(h->path);
+    box_free(h->PHEntries);
+    box_free(h->SHEntries);
+    box_free(h->SHStrTab);
+    box_free(h->StrTab);
+    box_free(h->Dynamic);
+    box_free(h->DynStr);
+    box_free(h->SymTab);
+    box_free(h->DynSym);
 
     FreeElfMemory(h);
-    free(h);
+    box_free(h);
 
     *head = NULL;
 }
@@ -169,9 +169,9 @@ int AllocElfMemory(box64context_t* context, elfheader_t* head, int mainbin)
         for (size_t i=0; i<head->numPHEntries; ++i) 
             if(head->PHEntries[i].p_type == PT_LOAD && head->PHEntries[i].p_flags)
                 ++head->multiblock_n;
-        head->multiblock_size = (uint64_t*)calloc(head->multiblock_n, sizeof(uint64_t));
-        head->multiblock_offs = (uintptr_t*)calloc(head->multiblock_n, sizeof(uintptr_t));
-        head->multiblock = (void**)calloc(head->multiblock_n, sizeof(void*));
+        head->multiblock_size = (uint64_t*)box_calloc(head->multiblock_n, sizeof(uint64_t));
+        head->multiblock_offs = (uintptr_t*)box_calloc(head->multiblock_n, sizeof(uintptr_t));
+        head->multiblock = (void**)box_calloc(head->multiblock_n, sizeof(void*));
         // and now, create all individual blocks
         head->memory = (char*)0xffffffffffffffff;
         int n = 0;
@@ -264,9 +264,9 @@ int AllocElfMemory(box64context_t* context, elfheader_t* head, int mainbin)
         printf_log(log_level, "Got %p (delta=%p)\n", p, (void*)head->delta);
 
         head->multiblock_n = 1;
-        head->multiblock_size = (uint64_t*)calloc(head->multiblock_n, sizeof(uint64_t));
-        head->multiblock_offs = (uintptr_t*)calloc(head->multiblock_n, sizeof(uintptr_t));
-        head->multiblock = (void**)calloc(head->multiblock_n, sizeof(void*));
+        head->multiblock_size = (uint64_t*)box_calloc(head->multiblock_n, sizeof(uint64_t));
+        head->multiblock_offs = (uintptr_t*)box_calloc(head->multiblock_n, sizeof(uintptr_t));
+        head->multiblock = (void**)box_calloc(head->multiblock_n, sizeof(void*));
         head->multiblock_size[0] = head->memsz;
         head->multiblock_offs[0] = (uintptr_t)p;
         head->multiblock[0] = p;
@@ -282,9 +282,9 @@ void FreeElfMemory(elfheader_t* head)
     if(head->multiblock_n) {
         for(int i=0; i<head->multiblock_n; ++i)
             munmap(head->multiblock[i], head->multiblock_size[i]);
-        free(head->multiblock);
-        free(head->multiblock_size);
-        free(head->multiblock_offs);
+        box_free(head->multiblock);
+        box_free(head->multiblock_size);
+        box_free(head->multiblock_offs);
     }
 }
 
@@ -1065,46 +1065,46 @@ int LoadNeededLibs(elfheader_t* h, lib_t *maplib, needed_libs_t* neededlibs, lib
             char *rpathref = h->DynStrTab+h->delta+h->Dynamic[i].d_un.d_val;
             char* rpath = rpathref;
             while(strstr(rpath, "$ORIGIN")) {
-                char* origin = strdup(h->path);
+                char* origin = box_strdup(h->path);
                 char* p = strrchr(origin, '/');
                 if(p) *p = '\0';    // remove file name to have only full path, without last '/'
-                char* tmp = (char*)calloc(1, strlen(rpath)-strlen("$ORIGIN")+strlen(origin)+1);
+                char* tmp = (char*)box_calloc(1, strlen(rpath)-strlen("$ORIGIN")+strlen(origin)+1);
                 p = strstr(rpath, "$ORIGIN");
                 memcpy(tmp, rpath, p-rpath);
                 strcat(tmp, origin);
                 strcat(tmp, p+strlen("$ORIGIN"));
                 if(rpath!=rpathref)
-                    free(rpath);
+                    box_free(rpath);
                 rpath = tmp;
-                free(origin);
+                box_free(origin);
             }
             while(strstr(rpath, "${ORIGIN}")) {
-                char* origin = strdup(h->path);
+                char* origin = box_strdup(h->path);
                 char* p = strrchr(origin, '/');
                 if(p) *p = '\0';    // remove file name to have only full path, without last '/'
-                char* tmp = (char*)calloc(1, strlen(rpath)-strlen("${ORIGIN}")+strlen(origin)+1);
+                char* tmp = (char*)box_calloc(1, strlen(rpath)-strlen("${ORIGIN}")+strlen(origin)+1);
                 p = strstr(rpath, "${ORIGIN}");
                 memcpy(tmp, rpath, p-rpath);
                 strcat(tmp, origin);
                 strcat(tmp, p+strlen("${ORIGIN}"));
                 if(rpath!=rpathref)
-                    free(rpath);
+                    box_free(rpath);
                 rpath = tmp;
-                free(origin);
+                box_free(origin);
             }
             while(strstr(rpath, "${PLATFORM}")) {
-                char* platform = strdup("x86_64");
+                char* platform = box_strdup("x86_64");
                 char* p = strrchr(platform, '/');
                 if(p) *p = '\0';    // remove file name to have only full path, without last '/'
-                char* tmp = (char*)calloc(1, strlen(rpath)-strlen("${PLATFORM}")+strlen(platform)+1);
+                char* tmp = (char*)box_calloc(1, strlen(rpath)-strlen("${PLATFORM}")+strlen(platform)+1);
                 p = strstr(rpath, "${PLATFORM}");
                 memcpy(tmp, rpath, p-rpath);
                 strcat(tmp, platform);
                 strcat(tmp, p+strlen("${PLATFORM}"));
                 if(rpath!=rpathref)
-                    free(rpath);
+                    box_free(rpath);
                 rpath = tmp;
-                free(platform);
+                box_free(platform);
             }
             if(strchr(rpath, '$')) {
                 printf_log(LOG_INFO, "BOX64: Warning, RPATH with $ variable not supported yet (%s)\n", rpath);
@@ -1113,7 +1113,7 @@ int LoadNeededLibs(elfheader_t* h, lib_t *maplib, needed_libs_t* neededlibs, lib
                 PrependList(&box64->box64_ld_lib, rpath, 1);
             }
             if(rpath!=rpathref)
-                free(rpath);
+                box_free(rpath);
         }
 
     if(!h->neededlibs && neededlibs)
@@ -1186,7 +1186,7 @@ void RunElfInit(elfheader_t* h, x64emu_t *emu)
     if(context->deferedInit) {
         if(context->deferedInitSz==context->deferedInitCap) {
             context->deferedInitCap += 4;
-            context->deferedInitList = (elfheader_t**)realloc(context->deferedInitList, context->deferedInitCap*sizeof(elfheader_t*));
+            context->deferedInitList = (elfheader_t**)box_realloc(context->deferedInitList, context->deferedInitCap*sizeof(elfheader_t*));
         }
         context->deferedInitList[context->deferedInitSz++] = h;
         return;
@@ -1225,7 +1225,7 @@ void RunDeferedElfInit(x64emu_t *emu)
     context->deferedInitCap = context->deferedInitSz = 0;
     for (int i=0; i<Sz; ++i)
         RunElfInit(List[i], emu);
-    free(List);
+    box_free(List);
 }
 
 void RunElfFini(elfheader_t* h, x64emu_t *emu)
diff --git a/src/elfs/elfparser.c b/src/elfs/elfparser.c
index af506749..9607efd9 100755
--- a/src/elfs/elfparser.c
+++ b/src/elfs/elfparser.c
@@ -23,7 +23,7 @@ int LoadSH(FILE *f, Elf64_Shdr *s, void** SH, const char* name, uint32_t type)
     if (type==SHT_SYMTAB && s->sh_size%sizeof(Elf64_Sym)) {
         printf_log(LOG_INFO, "Section Header \"%s\" (off=%ld, size=%ld) has size (not multiple of %ld)\n", name, s->sh_offset, s->sh_size, sizeof(Elf64_Sym));
     }
-    *SH = calloc(1, s->sh_size);
+    *SH = box_calloc(1, s->sh_size);
     fseeko64(f, s->sh_offset ,SEEK_SET);
     if(fread(*SH, s->sh_size, 1, f)!=1) {
             printf_log(LOG_INFO, "Cannot read Section Header \"%s\" (off=%ld, size=%ld)\n", name, s->sh_offset, s->sh_size);
@@ -119,8 +119,8 @@ elfheader_t* ParseElfHeader(FILE* f, const char* name, int exec)
         return NULL;
     }
 
-    elfheader_t *h = calloc(1, sizeof(elfheader_t));
-    h->name = strdup(name);
+    elfheader_t *h = box_calloc(1, sizeof(elfheader_t));
+    h->name = box_strdup(name);
     h->entrypoint = header.e_entry;
     h->numPHEntries = header.e_phnum;
     h->numSHEntries = header.e_shnum;
@@ -134,7 +134,7 @@ elfheader_t* ParseElfHeader(FILE* f, const char* name, int exec)
             fseeko64(f, header.e_shoff, SEEK_SET);
             Elf64_Shdr section;
             if(fread(&section, sizeof(Elf64_Shdr), 1, f)!=1) {
-                free(h);
+                box_free(h);
                 printf_log(LOG_INFO, "Cannot read Initial Section Header\n");
                 return NULL;
             }
@@ -142,7 +142,7 @@ elfheader_t* ParseElfHeader(FILE* f, const char* name, int exec)
         }
         // now read all section headers
         printf_dump(LOG_DEBUG, "Read %zu Section header\n", h->numSHEntries);
-        h->SHEntries = (Elf64_Shdr*)calloc(h->numSHEntries, sizeof(Elf64_Shdr));
+        h->SHEntries = (Elf64_Shdr*)box_calloc(h->numSHEntries, sizeof(Elf64_Shdr));
         fseeko64(f, header.e_shoff ,SEEK_SET);
         if(fread(h->SHEntries, sizeof(Elf64_Shdr), h->numSHEntries, f)!=h->numSHEntries) {
                 FreeElfHeader(&h);
@@ -158,7 +158,7 @@ elfheader_t* ParseElfHeader(FILE* f, const char* name, int exec)
     }
 
     printf_dump(LOG_DEBUG, "Read %zu Program header\n", h->numPHEntries);
-    h->PHEntries = (Elf64_Phdr*)calloc(h->numPHEntries, sizeof(Elf64_Phdr));
+    h->PHEntries = (Elf64_Phdr*)box_calloc(h->numPHEntries, sizeof(Elf64_Phdr));
     fseeko64(f, header.e_phoff ,SEEK_SET);
     if(fread(h->PHEntries, sizeof(Elf64_Phdr), h->numPHEntries, f)!=h->numPHEntries) {
             FreeElfHeader(&h);
diff --git a/src/emu/x64emu.c b/src/emu/x64emu.c
index daa79814..a91fe04b 100755
--- a/src/emu/x64emu.c
+++ b/src/emu/x64emu.c
@@ -89,7 +89,7 @@ x64emu_t *NewX64Emu(box64context_t *context, uintptr_t start, uintptr_t stack, i
 {
     printf_log(LOG_DEBUG, "Allocate a new X86_64 Emu, with EIP=%p and Stack=%p/0x%X\n", (void*)start, (void*)stack, stacksize);
 
-    x64emu_t *emu = (x64emu_t*)calloc(1, sizeof(x64emu_t));
+    x64emu_t *emu = (x64emu_t*)box_calloc(1, sizeof(x64emu_t));
 
     internalX64Setup(emu, context, start, stack, stacksize, ownstack);
 
@@ -135,7 +135,7 @@ void AddCleanup(x64emu_t *emu, void *p)
     
     if(my_context->clean_sz == my_context->clean_cap) {
         my_context->clean_cap += 4;
-        my_context->cleanups = (cleanup_t*)realloc(my_context->cleanups, sizeof(cleanup_t)*my_context->clean_cap);
+        my_context->cleanups = (cleanup_t*)box_realloc(my_context->cleanups, sizeof(cleanup_t)*my_context->clean_cap);
     }
     my_context->cleanups[my_context->clean_sz].arg = 0;
     my_context->cleanups[my_context->clean_sz].a = NULL;
@@ -148,7 +148,7 @@ void AddCleanup1Arg(x64emu_t *emu, void *p, void* a)
     
     if(my_context->clean_sz == my_context->clean_cap) {
         my_context->clean_cap += 4;
-        my_context->cleanups = (cleanup_t*)realloc(my_context->cleanups, sizeof(cleanup_t)*my_context->clean_cap);
+        my_context->cleanups = (cleanup_t*)box_realloc(my_context->cleanups, sizeof(cleanup_t)*my_context->clean_cap);
     }
     my_context->cleanups[my_context->clean_sz].arg = 1;
     my_context->cleanups[my_context->clean_sz].a = a;
@@ -178,7 +178,7 @@ void CallAllCleanup(x64emu_t *emu)
         RunFunctionWithEmu(emu, 0, (uintptr_t)(my_context->cleanups[i].f), my_context->cleanups[i].arg, my_context->cleanups[i].a );
     }
     my_context->clean_sz = 0;
-    free(my_context->cleanups);
+    box_free(my_context->cleanups);
     my_context->cleanups = NULL;
 }
 
@@ -197,7 +197,7 @@ void FreeX64Emu(x64emu_t **emu)
 
     internalFreeX64(*emu);
 
-    free(*emu);
+    box_free(*emu);
     *emu = NULL;
 }
 
diff --git a/src/emu/x64run_private.c b/src/emu/x64run_private.c
index 326c5a02..f84599eb 100755
--- a/src/emu/x64run_private.c
+++ b/src/emu/x64run_private.c
@@ -964,7 +964,7 @@ const char* getAddrFunctionName(uintptr_t addr)
     if(!sz) sz=0x100;   // arbitrary value...
     if(symbname && addr>=start && (addr<(start+sz) || !sz)) {
         if(symbname[0]=='\0')
-            sprintf(ret, "%s + 0x%lx", ElfName(elf), addr - (uintptr_t)GetBaseAddress(elf));
+            sprintf(ret, "%s + 0x%lx + 0x%lx", ElfName(elf), start - (uintptr_t)GetBaseAddress(elf), addr - start);
         else if(addr==start)
             sprintf(ret, "%s/%s", ElfName(elf), symbname);
         else
diff --git a/src/emu/x64trace.c b/src/emu/x64trace.c
index 0fb14c03..b5ddb5b7 100755
--- a/src/emu/x64trace.c
+++ b/src/emu/x64trace.c
@@ -41,7 +41,7 @@ int InitX64Trace(box64context_t *context)
 {
     if(context->zydis)
         return 0;
-    context->zydis = (zydis_t*)calloc(1, sizeof(zydis_t));
+    context->zydis = (zydis_t*)box_calloc(1, sizeof(zydis_t));
     if(!context->zydis)
         return 1;
     context->zydis->lib = dlopen("libZydis.so", RTLD_LAZY);
@@ -69,7 +69,7 @@ void DeleteX64Trace(box64context_t *context)
         return;
     if(context->zydis->lib)
         dlclose(context->zydis->lib);
-    free(context->zydis);
+    box_free(context->zydis);
     context->zydis = NULL;
 }
 
@@ -77,7 +77,7 @@ zydis_dec_t* InitX64TraceDecoder(box64context_t *context)
 {
     if(!context->zydis)
         return NULL;
-    zydis_dec_t *dec = (zydis_dec_t*)calloc(1, sizeof(zydis_dec_t));
+    zydis_dec_t *dec = (zydis_dec_t*)box_calloc(1, sizeof(zydis_dec_t));
     dec->ZydisDecoderDecodeBuffer = context->zydis->ZydisDecoderDecodeBuffer;
     dec->ZydisFormatterFormatInstruction = context->zydis->ZydisFormatterFormatInstruction;
     context->zydis->ZydisDecoderInit(&dec->decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_ADDRESS_WIDTH_64);
@@ -87,7 +87,7 @@ zydis_dec_t* InitX64TraceDecoder(box64context_t *context)
 }
 void DeleteX64TraceDecoder(zydis_dec_t **dec)
 {
-    free(*dec);
+    box_free(*dec);
     *dec = NULL;
 }
 
diff --git a/src/include/debug.h b/src/include/debug.h
index 48103d3a..d39ac369 100755
--- a/src/include/debug.h
+++ b/src/include/debug.h
@@ -73,4 +73,22 @@ extern FILE* ftrace;
 #define EXPORTDYN 
 #endif
 
+#ifdef ANDROID
+void init_malloc_hook();
+extern void*(*__libc_malloc)(size_t);
+extern void*(*__libc_realloc)(void*, size_t);
+extern void*(*__libc_calloc)(size_t, size_t);
+extern void (*__libc_free*)(void*);
+#else
+extern void* __libc_malloc(size_t);
+extern void* __libc_realloc(void*, size_t);
+extern void* __libc_calloc(size_t, size_t);
+extern void __libc_free(void*);
+#endif
+#define box_malloc  __libc_malloc
+#define box_realloc __libc_realloc
+#define box_calloc  __libc_calloc
+#define box_free    __libc_free
+extern char* box_strdup(const char* s);
+
 #endif //__DEBUG_H_
diff --git a/src/librarian/dictionnary.c b/src/librarian/dictionnary.c
index 9d343e93..9d5e375a 100755
--- a/src/librarian/dictionnary.c
+++ b/src/librarian/dictionnary.c
@@ -20,7 +20,7 @@ void FreeDictionnary(dic_t **d)
         return;

     kh_dic_t* dic = (kh_dic_t*)*d;

     const char* k;

-    kh_foreach_key(dic, k, free((void*)k));

+    kh_foreach_key(dic, k, box_free((void*)k));

     kh_destroy(dic, dic);

     *d = NULL;

 }

@@ -31,7 +31,7 @@ const char* AddDictionnary(dic_t* d, const char* s)
     khint_t k = kh_get(dic, dic, s);

     if(k!=kh_end(dic))

         return kh_key(dic, k);

-    char* n = strdup(s);

+    char* n = box_strdup(s);

     int ret;

     k = kh_put(dic, dic, n, &ret);

     return n;

diff --git a/src/librarian/librarian.c b/src/librarian/librarian.c
index 9921c06a..f8126479 100755
--- a/src/librarian/librarian.c
+++ b/src/librarian/librarian.c
@@ -19,7 +19,7 @@ KHASH_MAP_IMPL_INT(mapoffsets, cstr_t);
 
 lib_t *NewLibrarian(box64context_t* context, int ownlibs)
 {
-    lib_t *maplib = (lib_t*)calloc(1, sizeof(lib_t));
+    lib_t *maplib = (lib_t*)box_calloc(1, sizeof(lib_t));
     
     maplib->mapsymbols = NewMapSymbols();
     maplib->weaksymbols = NewMapSymbols();
@@ -74,7 +74,7 @@ void FreeLibrarian(lib_t **maplib, x64emu_t *emu)
     (*maplib)->owner = NULL;    // to avoid recursive free
     if((*maplib)->ownlibs && (*maplib)->libsz) {
         printf_log(LOG_DEBUG, "Closing %d libs from maplib %p\n", (*maplib)->libsz, *maplib);
-        char *freed = (char*)calloc((*maplib)->libsz, sizeof(char));
+        char *freed = (char*)box_calloc((*maplib)->libsz, sizeof(char));
         if (!freed) {
             printf_log(LOG_INFO, "Failed to malloc freed table, using old algorithm (a crash is likely)\n");
             for (int i=(*maplib)->libsz-1; i>=0; --i) 
@@ -88,10 +88,10 @@ void FreeLibrarian(lib_t **maplib, x64emu_t *emu)
             }
             memset((*maplib)->libraries, 0, (*maplib)->libsz*sizeof(library_t*)); // NULL = 0 anyway
             (*maplib)->libsz = 0;
-            free(freed);
+            box_free(freed);
         }
     }
-    free((*maplib)->libraries);
+    box_free((*maplib)->libraries);
     (*maplib)->libraries = NULL;
 
     FreeMapSymbols(&(*maplib)->mapsymbols);
@@ -106,7 +106,7 @@ void FreeLibrarian(lib_t **maplib, x64emu_t *emu)
     if((*maplib)->bridge)
         FreeBridge(&(*maplib)->bridge);
 
-    free(*maplib);
+    box_free(*maplib);
     *maplib = NULL;
 
 }
@@ -163,7 +163,7 @@ void MapLibAddLib(lib_t* maplib, library_t* lib)
         return;
     if (maplib->libsz == maplib->libcap) {
         maplib->libcap += 8;
-        maplib->libraries = (library_t**)realloc(maplib->libraries, maplib->libcap*sizeof(library_t*));
+        maplib->libraries = (library_t**)box_realloc(maplib->libraries, maplib->libcap*sizeof(library_t*));
     }
     maplib->libraries[maplib->libsz] = lib;
     ++maplib->libsz;
@@ -179,7 +179,7 @@ void MapLibAddMapLib(lib_t* dest, lib_t* src)
         MapLibAddLib(dest, lib);
         if(lib->maplib && src!=lib->maplib) {   //TODO: find why is src!=lib->maplib needed
             MapLibAddMapLib(dest, lib->maplib);
-            free(lib->maplib);
+            box_free(lib->maplib);
             lib->maplib = NULL;
         }
     }
@@ -246,7 +246,7 @@ int AddNeededLib_add(lib_t* maplib, needed_libs_t* neededlibs, library_t* deplib
             // for add the depending local libs...
             if(lib->maplib) {
                 MapLibAddMapLib(my_context->maplib, lib->maplib);
-                free(lib->maplib);
+                box_free(lib->maplib);
                 lib->maplib = NULL;
             }
             if(!libraryInMapLib(my_context->maplib, lib))
@@ -345,7 +345,7 @@ int AddNeededLib(lib_t* maplib, needed_libs_t* neededlibs, library_t* deplib, in
 {
     box64_mapclean = 0;
     if(!neededlibs) {
-        neededlibs = calloc(1, sizeof(needed_libs_t));
+        neededlibs = box_calloc(1, sizeof(needed_libs_t));
     }
     int idx = neededlibs->size;
     // Add libs and symbol
@@ -474,14 +474,14 @@ int GetGlobalSymbolStartEnd(lib_t *maplib, const char* name, uintptr_t* start, u
     if(GetGlobalSymbolStartEnd_internal(maplib, name, start, end, self, version, vername)) {
         if(start && end && *end==*start) {  // object is of 0 sized, try to see an "_END" object of null size
             uintptr_t start2, end2;
-            char* buff = (char*)malloc(strlen(name) + strlen("_END") + 1);
+            char* buff = (char*)box_malloc(strlen(name) + strlen("_END") + 1);
             strcpy(buff, name);
             strcat(buff, "_END");
             if(GetGlobalSymbolStartEnd_internal(maplib, buff, &start2, &end2, self, version, vername)) {
                 if(end2>*end && start2==end2)
                     *end = end2;
             }
-            free(buff);
+            box_free(buff);
         }
         return 1;
     }
diff --git a/src/librarian/library.c b/src/librarian/library.c
index bc4b0d7f..d12b4d3f 100755
--- a/src/librarian/library.c
+++ b/src/librarian/library.c
@@ -54,7 +54,7 @@ KHASH_MAP_IMPL_STR(datamap, uint64_t)
 
 char* Path2Name(const char* path)
 {
-    char* name = (char*)calloc(1, MAX_PATH);
+    char* name = (char*)box_calloc(1, MAX_PATH);
     char* p = strrchr(path, '/');
     strcpy(name, (p)?(p+1):path);
     // name should be libXXX.so.A(.BB.CCCC)
@@ -106,11 +106,11 @@ void NativeLib_FinishFini(library_t* lib)
         dlclose(lib->priv.w.lib);
     lib->priv.w.lib = NULL;
     if(lib->priv.w.altprefix)
-        free(lib->priv.w.altprefix);
+        box_free(lib->priv.w.altprefix);
     if(lib->priv.w.neededlibs) {
         for(int i=0; i<lib->priv.w.needed; ++i)
-            free(lib->priv.w.neededlibs[i]);
-        free(lib->priv.w.neededlibs);
+            box_free(lib->priv.w.neededlibs[i]);
+        box_free(lib->priv.w.neededlibs);
     }
     FreeBridge(&lib->priv.w.bridge);
 }
@@ -280,10 +280,10 @@ static int loadEmulatedLib(const char* libname, library_t *lib, box64context_t*
         lib->priv.n.localsymbols = kh_init(mapsymbols);
 
         if(lib->path && strcmp(lib->path, libname)) {
-            free(lib->path);
+            box_free(lib->path);
             lib->path = realpath(libname, NULL);
             if(!lib->path)
-                lib->path = strdup(libname);
+                lib->path = box_strdup(libname);
         }
 
         printf_log(LOG_INFO, "Using emulated %s\n", libname);
@@ -340,12 +340,12 @@ extern char* libGL;
 library_t *NewLibrary(const char* path, box64context_t* context)
 {
     printf_log(LOG_DEBUG, "Trying to load \"%s\"\n", path);
-    library_t *lib = (library_t*)calloc(1, sizeof(library_t));
+    library_t *lib = (library_t*)box_calloc(1, sizeof(library_t));
     lib->path = realpath(path, NULL);
     if(!lib->path)
-        lib->path = strdup(path);
+        lib->path = box_strdup(path);
     if(libGL && !strcmp(path, libGL))
-        lib->name = strdup("libGL.so.1");
+        lib->name = box_strdup("libGL.so.1");
     else
         lib->name = Path2Name(path);
     lib->nbdot = NbDot(lib->name);
@@ -354,17 +354,17 @@ library_t *NewLibrary(const char* path, box64context_t* context)
     printf_log(LOG_DEBUG, "Simplified name is \"%s\"\n", lib->name);
     if(box64_nopulse) {
         if(strstr(lib->name, "libpulse.so")==lib->name || strstr(lib->name, "libpulse-simple.so")==lib->name) {
-            free(lib->name);
-            free(lib->path);
-            free(lib);
+            box_free(lib->name);
+            box_free(lib->path);
+            box_free(lib);
             return NULL;
         }
     }
     if(box64_novulkan) {
         if(strstr(lib->name, "libvulkan.so")==lib->name) {
-            free(lib->name);
-            free(lib->path);
-            free(lib);
+            box_free(lib->name);
+            box_free(lib->path);
+            box_free(lib);
             return NULL;
         }
     }
@@ -391,9 +391,9 @@ library_t *NewLibrary(const char* path, box64context_t* context)
     // nothing loaded, so error...
     if(lib->type==-1)
     {
-        free(lib->name);
-        free(lib->path);
-        free(lib);
+        box_free(lib->name);
+        box_free(lib->path);
+        box_free(lib);
         return NULL;
     }
 
@@ -436,12 +436,12 @@ int FinalizeLibrary(library_t* lib, lib_t* local_maplib, int bindnow, x64emu_t*
             if (GetGlobalSymbolStartEnd(local_maplib, trace_func, &trace_start, &trace_end, elf_header, -1, NULL)) {
                 SetTraceEmu(trace_start, trace_end);
                 printf_log(LOG_INFO, "TRACE on %s only (%p-%p)\n", trace_func, (void*)trace_start, (void*)trace_end);
-                free(trace_func);
+                box_free(trace_func);
                 trace_func = NULL;
             } else if(GetLibLocalSymbolStartEnd(lib, trace_func, &trace_start, &trace_end, -1, NULL, 0)) {
                 SetTraceEmu(trace_start, trace_end);
                 printf_log(LOG_INFO, "TRACE on %s only (%p-%p)\n", trace_func, (void*)trace_start, (void*)trace_end);
-                free(trace_func);
+                box_free(trace_func);
                 trace_func = NULL;
             }
         }
@@ -516,14 +516,14 @@ void Free1Library(library_t **lib, x64emu_t* emu)
     if((*lib)->type!=-1 && (*lib)->fini) {
         (*lib)->fini(*lib);
     }
-    free((*lib)->name);
-    free((*lib)->path);
-    free((*lib)->altmy);
+    box_free((*lib)->name);
+    box_free((*lib)->path);
+    box_free((*lib)->altmy);
 
     if((*lib)->bridgemap) {
         bridged_t *br;
         kh_foreach_value_ref((*lib)->bridgemap, br,
-            free(br->name);
+            box_free(br->name);
         );
         kh_destroy(bridgemap, (*lib)->bridgemap);
     }
@@ -548,7 +548,7 @@ void Free1Library(library_t **lib, x64emu_t* emu)
     free_neededlib(&(*lib)->needed);
     free_neededlib(&(*lib)->depended);
 
-    free(*lib);
+    box_free(*lib);
     *lib = NULL;
 }
 
@@ -578,7 +578,7 @@ int IsSameLib(library_t* lib, const char* path)
                 ret=1;
     }
 
-    free(name);
+    box_free(name);
     return ret;
 }
 int GetLibSymbolStartEnd(library_t* lib, const char* name, uintptr_t* start, uintptr_t* end, int version, const char* vername, int local)
@@ -596,7 +596,7 @@ int GetLibSymbolStartEnd(library_t* lib, const char* name, uintptr_t* start, uin
     // get a new symbol
     if(lib->get(lib, name, start, end, version, vername, local)) {
         *end += *start;     // lib->get(...) gives size, not end
-        char* symbol = strdup(VersionnedName(name, version, vername));
+        char* symbol = box_strdup(VersionnedName(name, version, vername));
         int ret;
         k = kh_put(bridgemap, lib->bridgemap, symbol, &ret);
         kh_value(lib->bridgemap, k).name = symbol;
@@ -622,7 +622,7 @@ int GetLibNoWeakSymbolStartEnd(library_t* lib, const char* name, uintptr_t* star
             *end = kh_value(lib->bridgemap, k).end;
             return 1;
         }
-        char* symbol = strdup(VersionnedName(name, version, vername));
+        char* symbol = box_strdup(VersionnedName(name, version, vername));
         int ret;
         k = kh_put(bridgemap, lib->bridgemap, symbol, &ret);
         kh_value(lib->bridgemap, k).name = symbol;
@@ -648,7 +648,7 @@ int GetLibLocalSymbolStartEnd(library_t* lib, const char* name, uintptr_t* start
     // get a new symbol
     if(lib->getlocal(lib, name, start, end, version, vername, local)) {
         *end += *start;     // lib->get(...) gives size, not end
-        char* symbol = strdup(VersionnedName(name, version, vername));
+        char* symbol = box_strdup(VersionnedName(name, version, vername));
         int ret;
         k = kh_put(bridgemap, lib->bridgemap, symbol, &ret);
         kh_value(lib->bridgemap, k).name = symbol;
@@ -909,14 +909,14 @@ linkmap_t* getLinkMapLib(library_t* lib)
 linkmap_t* addLinkMapLib(library_t* lib)
 {
     if(!my_context->linkmap) {
-        my_context->linkmap = (linkmap_t*)calloc(1, sizeof(linkmap_t));
+        my_context->linkmap = (linkmap_t*)box_calloc(1, sizeof(linkmap_t));
         my_context->linkmap->l_lib = lib;
         return my_context->linkmap;
     }
     linkmap_t* lm = my_context->linkmap;
     while(lm->l_next)
         lm = lm->l_next;
-    lm->l_next = (linkmap_t*)calloc(1, sizeof(linkmap_t));
+    lm->l_next = (linkmap_t*)box_calloc(1, sizeof(linkmap_t));
     lm->l_next->l_lib = lib;
     lm->l_next->l_prev = lm;
     return lm->l_next;
@@ -929,7 +929,7 @@ void removeLinkMapLib(library_t* lib)
         lm->l_next->l_prev = lm->l_prev;
     if(lm->l_prev)
         lm->l_prev->l_next = lm->l_next;
-    free(lm);
+    box_free(lm);
 }
 
 void AddMainElfToLinkmap(elfheader_t* elf)
@@ -946,11 +946,11 @@ void setNeededLibs(library_t* lib, int n, ...)
     if(lib->type!=0 && lib->type!=-1)
         return;
     lib->priv.w.needed = n;
-    lib->priv.w.neededlibs = (char**)calloc(n, sizeof(char*));
+    lib->priv.w.neededlibs = (char**)box_calloc(n, sizeof(char*));
     va_list va;
     va_start (va, n);
     for (int i=0; i<n; ++i) {
-        lib->priv.w.neededlibs[i] = strdup(va_arg(va, char*));
+        lib->priv.w.neededlibs[i] = box_strdup(va_arg(va, char*));
     }
     va_end (va);
 }
\ No newline at end of file
diff --git a/src/librarian/symbols.c b/src/librarian/symbols.c
index adf6020a..8042c860 100644
--- a/src/librarian/symbols.c
+++ b/src/librarian/symbols.c
@@ -41,7 +41,7 @@ void FreeMapSymbols(kh_mapsymbols_t** map)
     if(!map || !(*map))
         return;
     versymbols_t *v;
-    kh_foreach_value_ref(*map, v, free(v->syms););
+    kh_foreach_value_ref(*map, v, box_free(v->syms););
 
     kh_destroy(mapsymbols, *map);
     *map = NULL;
@@ -152,7 +152,7 @@ void AddSymbol(kh_mapsymbols_t *mapsymbols, const char* name, uintptr_t addr, ui
     // add a new record
     if(v->sz == v->cap) {
         v->cap+=4;
-        v->syms = (versymbol_t*)realloc(v->syms, v->cap*sizeof(versymbol_t));
+        v->syms = (versymbol_t*)box_realloc(v->syms, v->cap*sizeof(versymbol_t));
     }
     int idx = v->sz++;
     v->syms[idx].version = ver;
@@ -189,7 +189,7 @@ void AddWeakSymbol(kh_mapsymbols_t *mapsymbols, const char* name, uintptr_t addr
     // add a new record
     if(v->sz == v->cap) {
         v->cap+=4;
-        v->syms = (versymbol_t*)realloc(v->syms, v->cap*sizeof(versymbol_t));
+        v->syms = (versymbol_t*)box_realloc(v->syms, v->cap*sizeof(versymbol_t));
     }
     int idx = v->sz++;
     v->syms[idx].version = ver;
diff --git a/src/libtools/signals.c b/src/libtools/signals.c
index adce486b..6bed019f 100755
--- a/src/libtools/signals.c
+++ b/src/libtools/signals.c
@@ -255,7 +255,7 @@ struct kernel_sigaction {
 static void sigstack_destroy(void* p)
 {
 	x64_stack_t *ss = (x64_stack_t*)p;
-    free(ss);
+    box_free(ss);
 }
 
 static pthread_key_t sigstack_key;
@@ -390,7 +390,7 @@ EXPORT int my_sigaltstack(x64emu_t* emu, const x64_stack_t* ss, x64_stack_t* oss
 
     if(ss->ss_flags==SS_DISABLE) {
         if(new_ss)
-            free(new_ss);
+            box_free(new_ss);
         pthread_setspecific(sigstack_key, NULL);
 
         return 0;
@@ -408,7 +408,7 @@ EXPORT int my_sigaltstack(x64emu_t* emu, const x64_stack_t* ss, x64_stack_t* oss
         }
     }
     if(!new_ss)
-        new_ss = (x64_stack_t*)calloc(1, sizeof(x64_stack_t));
+        new_ss = (x64_stack_t*)box_calloc(1, sizeof(x64_stack_t));
     new_ss->ss_sp = ss->ss_sp;
     new_ss->ss_size = ss->ss_size;
 
diff --git a/src/libtools/threads.c b/src/libtools/threads.c
index dd9b9382..6eb6305d 100755
--- a/src/libtools/threads.c
+++ b/src/libtools/threads.c
@@ -75,7 +75,7 @@ void CleanStackSize(box64context_t* context)
 	if(!context || !context->stacksizes)
 		return;
 	pthread_mutex_lock(&context->mutex_thread);
-	kh_foreach_value(context->stacksizes, ts, free(ts));
+	kh_foreach_value(context->stacksizes, ts, box_free(ts));
 	kh_destroy(threadstack, context->stacksizes);
 	context->stacksizes = NULL;
 	pthread_mutex_unlock(&context->mutex_thread);
@@ -86,7 +86,7 @@ void FreeStackSize(kh_threadstack_t* map, uintptr_t attr)
 	pthread_mutex_lock(&my_context->mutex_thread);
 	khint_t k = kh_get(threadstack, map, attr);
 	if(k!=kh_end(map)) {
-		free(kh_value(map, k));
+		box_free(kh_value(map, k));
 		kh_del(threadstack, map, k);
 	}
 	pthread_mutex_unlock(&my_context->mutex_thread);
@@ -98,7 +98,7 @@ void AddStackSize(kh_threadstack_t* map, uintptr_t attr, void* stack, size_t sta
 	int ret;
 	pthread_mutex_lock(&my_context->mutex_thread);
 	k = kh_put(threadstack, map, attr, &ret);
-	threadstack_t* ts = kh_value(map, k) = (threadstack_t*)calloc(1, sizeof(threadstack_t));
+	threadstack_t* ts = kh_value(map, k) = (threadstack_t*)box_calloc(1, sizeof(threadstack_t));
 	ts->stack = stack;
 	ts->stacksize = stacksize;
 	pthread_mutex_unlock(&my_context->mutex_thread);
@@ -136,7 +136,7 @@ static void FreeCancelThread(box64context_t* context)
 }
 static __pthread_unwind_buf_t* AddCancelThread(x64_unwind_buff_t* buff)
 {
-	__pthread_unwind_buf_t* r = (__pthread_unwind_buf_t*)calloc(1, sizeof(__pthread_unwind_buf_t));
+	__pthread_unwind_buf_t* r = (__pthread_unwind_buf_t*)box_calloc(1, sizeof(__pthread_unwind_buf_t));
 	buff->__pad[3] = r;
 	return r;
 }
@@ -149,7 +149,7 @@ static __pthread_unwind_buf_t* GetCancelThread(x64_unwind_buff_t* buff)
 static void DelCancelThread(x64_unwind_buff_t* buff)
 {
 	__pthread_unwind_buf_t* r = (__pthread_unwind_buf_t*)buff->__pad[3];
-	free(r);
+	box_free(r);
 	buff->__pad[3] = NULL;
 }
 
@@ -164,7 +164,7 @@ static void emuthread_destroy(void* p)
 	emuthread_t *et = (emuthread_t*)p;
 	if(et) {
 		FreeX64Emu(&et->emu);
-		free(et);
+		box_free(et);
 	}
 }
 
@@ -181,12 +181,12 @@ void thread_set_emu(x64emu_t* emu)
 	pthread_once(&thread_key_once, thread_key_alloc);
 	emuthread_t *et = (emuthread_t*)pthread_getspecific(thread_key);
 	if(!emu) {
-		if(et) free(et);
+		if(et) box_free(et);
 		pthread_setspecific(thread_key, NULL);
 		return;
 	}
 	if(!et) {
-		et = (emuthread_t*)calloc(1, sizeof(emuthread_t));
+		et = (emuthread_t*)box_calloc(1, sizeof(emuthread_t));
 	} else {
 		if(et->emu != emu)
 			FreeX64Emu(&et->emu);
@@ -277,7 +277,7 @@ pthread_attr_t* getAlignedAttrWithInit(pthread_attr_t* attr, int init)
 	aligned_attr_t* at = (aligned_attr_t*)attr;
 	if(init && at->sign==SIGN_ATTR)
 		return at->at;
-	pthread_attr_t* ret = (pthread_attr_t*)calloc(1, sizeof(pthread_attr_t));
+	pthread_attr_t* ret = (pthread_attr_t*)box_calloc(1, sizeof(pthread_attr_t));
 	at->sign = SIGN_ATTR;
 	at->at = ret;
 	if(init)
@@ -472,7 +472,7 @@ EXPORT int my_pthread_create(x64emu_t *emu, void* t, void* attr, void* start_rou
 		own = 1;
 	}
 
-	emuthread_t *et = (emuthread_t*)calloc(1, sizeof(emuthread_t));
+	emuthread_t *et = (emuthread_t*)box_calloc(1, sizeof(emuthread_t));
     x64emu_t *emuthread = NewX64Emu(my_context, (uintptr_t)start_routine, (uintptr_t)stack, stacksize, own);
 	SetupX64Emu(emuthread);
 	//SetFS(emuthread, GetFS(emu));
@@ -495,7 +495,7 @@ void* my_prepare_thread(x64emu_t *emu, void* f, void* arg, int ssize, void** pet
 {
 	int stacksize = (ssize)?ssize:(2*1024*1024);	//default stack size is 2Mo
 	void* stack = mmap(NULL, stacksize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_GROWSDOWN, -1, 0);
-	emuthread_t *et = (emuthread_t*)calloc(1, sizeof(emuthread_t));
+	emuthread_t *et = (emuthread_t*)box_calloc(1, sizeof(emuthread_t));
     x64emu_t *emuthread = NewX64Emu(emu->context, (uintptr_t)f, (uintptr_t)stack, stacksize, 1);
 	SetupX64Emu(emuthread);
 	//SetFS(emuthread, GetFS(emu));
@@ -809,7 +809,7 @@ static pthread_mutex_t mutex_mutexes = PTHREAD_MUTEX_INITIALIZER;
 
 static mutexes_block_t* NewMutexesBlock()
 {
-	mutexes_block_t* ret = (mutexes_block_t*)calloc(1, sizeof(mutexes_block_t));
+	mutexes_block_t* ret = (mutexes_block_t*)box_calloc(1, sizeof(mutexes_block_t));
 	ret->n_free = MUTEXES_SIZE;
 	return ret;
 }
@@ -859,7 +859,7 @@ void FreeAllMutexes(mutexes_block_t* m)
 		return;
 	FreeAllMutexes(m->next);
 	// should destroy all mutexes also?
-	free(m);
+	box_free(m);
 }
 
 pthread_mutex_t* GetMutex(int k)
@@ -1214,8 +1214,8 @@ static void emujmpbuf_destroy(void* p)
 {
 	emu_jmpbuf_t *ej = (emu_jmpbuf_t*)p;
 	if(ej) {
-		free(ej->jmpbuf);
-		free(ej);
+		box_free(ej->jmpbuf);
+		box_free(ej);
 	}
 }
 
@@ -1225,8 +1225,8 @@ emu_jmpbuf_t* GetJmpBuf()
 {
 	emu_jmpbuf_t *ejb = (emu_jmpbuf_t*)pthread_getspecific(jmpbuf_key);
 	if(!ejb) {
-		ejb = (emu_jmpbuf_t*)calloc(1, sizeof(emu_jmpbuf_t));
-		ejb->jmpbuf = calloc(1, sizeof(struct __jmp_buf_tag));
+		ejb = (emu_jmpbuf_t*)box_calloc(1, sizeof(emu_jmpbuf_t));
+		ejb->jmpbuf = box_calloc(1, sizeof(struct __jmp_buf_tag));
 		pthread_setspecific(jmpbuf_key, ejb);
 	}
 	return ejb;
diff --git a/src/main.c b/src/main.c
index 7baba053..4929b769 100755
--- a/src/main.c
+++ b/src/main.c
@@ -545,11 +545,11 @@ void LoadLogEnv()
     }
     p = getenv("BOX64_LIBGL");
     if(p)
-        libGL = strdup(p);
+        libGL = box_strdup(p);
     if(!libGL) {
         p = getenv("SDL_VIDEO_GL_DRIVER");
         if(p)
-            libGL = strdup(p);
+            libGL = box_strdup(p);
     }
     if(libGL) {
         printf_log(LOG_INFO, "BOX64 using \"%s\" as libGL.so.1\n", p);
@@ -669,19 +669,19 @@ int GatherEnv(char*** dest, char** env, char* prog)
     int ld_path = 0;
     while(*p) {
         if(strncmp(*p, "BOX64_PATH=", 11)==0) {
-            (*dest)[idx++] = strdup(*p+6);
+            (*dest)[idx++] = box_strdup(*p+6);
             path = 1;
         } else if(strncmp(*p, "BOX64_LD_LIBRARY_PATH=", 22)==0) {
-            (*dest)[idx++] = strdup(*p+6);
+            (*dest)[idx++] = box_strdup(*p+6);
             ld_path = 1;
         } else if(strncmp(*p, "_=", 2)==0) {
             /*int l = strlen(prog);
             char tmp[l+3];
             strcpy(tmp, "_=");
             strcat(tmp, prog);
-            (*dest)[idx++] = strdup(tmp);*/
+            (*dest)[idx++] = box_strdup(tmp);*/
         } else if(strncmp(*p, "BOX64_", 6)!=0) {
-            (*dest)[idx++] = strdup(*p);
+            (*dest)[idx++] = box_strdup(*p);
             /*if(!(strncmp(*p, "PATH=", 5)==0 || strncmp(*p, "LD_LIBRARY_PATH=", 16)==0)) {
             }*/
         }
@@ -689,10 +689,10 @@ int GatherEnv(char*** dest, char** env, char* prog)
     }
     // update the calloc of envv when adding new variables here
     if(!path) {
-        (*dest)[idx++] = strdup("BOX64_PATH=.:bin");
+        (*dest)[idx++] = box_strdup("BOX64_PATH=.:bin");
     }
     if(!ld_path) {
-        (*dest)[idx++] = strdup("BOX64_LD_LIBRARY_PATH=.:lib:lib64");
+        (*dest)[idx++] = box_strdup("BOX64_LD_LIBRARY_PATH=.:lib:lib64");
     }
     // add "_=prog" at the end...
     if(prog) {
@@ -700,7 +700,7 @@ int GatherEnv(char*** dest, char** env, char* prog)
         char tmp[l+3];
         strcpy(tmp, "_=");
         strcat(tmp, prog);
-        (*dest)[idx++] = strdup(tmp);
+        (*dest)[idx++] = box_strdup(tmp);
     }
     // and a final NULL
     (*dest)[idx++] = 0;
@@ -763,17 +763,17 @@ void addNewEnvVar(const char* s)
 {
     if(!s)
         return;
-    char* p = strdup(s);
+    char* p = box_strdup(s);
     char* e = strchr(p, '=');
     if(!e) {
         printf_log(LOG_INFO, "Invalid specific env. var. '%s'\n", s);
-        free(p);
+        box_free(p);
         return;
     }
     *e='\0';
     ++e;
     setenv(p, e, 1);
-    free(p);
+    box_free(p);
 }
 
 EXPORTDYN
@@ -938,7 +938,7 @@ void setupTrace()
             } else {
                 printf_log(LOG_NONE, "Warning, symbol to trace (\"%s\") not found, trying to set trace later\n", p);
                 SetTraceEmu(0, 1);  // disabling trace, mostly
-                trace_func = strdup(p);
+                trace_func = box_strdup(p);
             }
         }
     }
@@ -1003,7 +1003,7 @@ void endBox64()
     // all done, free context
     FreeBox64Context(&my_context);
     if(libGL) {
-        free(libGL);
+        box_free(libGL);
         libGL = NULL;
     }
 }
@@ -1012,12 +1012,15 @@ void endBox64()
 static void free_contextargv()
 {
     for(int i=0; i<my_context->argc; ++i)
-        free(my_context->argv[i]);
+        box_free(my_context->argv[i]);
 }
 
 void pressure_vessel(int argc, const char** argv, int nextarg);
 extern char** environ;
 int main(int argc, const char **argv, char **env) {
+    #ifdef ANDROID
+    init_malloc_hook();
+    #endif
 
     init_auxval(argc, argv, environ?environ:env);
     // trying to open and load 1st arg
@@ -1142,7 +1145,7 @@ int main(int argc, const char **argv, char **env) {
     my_context->envc = CountEnv(environ?environ:env);
     printf_log(LOG_INFO, "Counted %d Env var\n", my_context->envc);
     // allocate extra space for new environment variables such as BOX64_PATH
-    my_context->envv = (char**)calloc(my_context->envc+4, sizeof(char*));
+    my_context->envv = (char**)box_calloc(my_context->envc+4, sizeof(char*));
     GatherEnv(&my_context->envv, environ?environ:env, my_context->box64path);
     if(box64_dump || box64_log<=LOG_DEBUG) {
         for (int i=0; i<my_context->envc; ++i)
@@ -1184,11 +1187,11 @@ int main(int argc, const char **argv, char **env) {
     my_context->argv[0] = ResolveFile(prog, &my_context->box64_path);
     // check if box86 is present
     {
-        my_context->box86path = strdup(my_context->box64path);
+        my_context->box86path = box_strdup(my_context->box64path);
         char* p = strrchr(my_context->box86path, '6');  // get the 6 of box64
         p[0] = '8'; p[1] = '6'; // change 64 to 86
         if(!FileExist(my_context->box86path, IS_FILE)) {
-            free(my_context->box86path);
+            box_free(my_context->box86path);
             my_context->box86path = NULL;
         }
     }
@@ -1245,7 +1248,7 @@ int main(int argc, const char **argv, char **env) {
     }*/
 
     for(int i=1; i<my_context->argc; ++i) {
-        my_context->argv[i] = strdup(argv[i+nextarg]);
+        my_context->argv[i] = box_strdup(argv[i+nextarg]);
         printf_log(LOG_INFO, "argv[%i]=\"%s\"\n", i, my_context->argv[i]);
     }
 
@@ -1265,7 +1268,7 @@ int main(int argc, const char **argv, char **env) {
         return -1;
     }
     if(!(my_context->fullpath = realpath(my_context->argv[0], NULL)))
-        my_context->fullpath = strdup(my_context->argv[0]);
+        my_context->fullpath = box_strdup(my_context->argv[0]);
     FILE *f = fopen(my_context->argv[0], "rb");
     if(!f) {
         printf_log(LOG_NONE, "Error: Cannot open %s\n", my_context->argv[0]);
@@ -1283,14 +1286,14 @@ int main(int argc, const char **argv, char **env) {
         FreeCollection(&ld_preload);
         if(x86) {
             // duplicate the array and insert 1st arg as box86
-            const char** newargv = (const char**)calloc(my_context->argc+2, sizeof(char*));
+            const char** newargv = (const char**)box_calloc(my_context->argc+2, sizeof(char*));
             newargv[0] = my_context->box86path;
             for(int i=0; i<my_context->argc; ++i)
                 newargv[i+1] = my_context->argv[i];
             FreeBox64Context(&my_context);
             return execvp(newargv[0], (char * const*)newargv);
         } else {
-            const char** newargv = (const char**)calloc(my_context->argc+1, sizeof(char*));
+            const char** newargv = (const char**)box_calloc(my_context->argc+1, sizeof(char*));
             for(int i=0; i<my_context->argc; ++i)
                 newargv[i] = my_context->argv[i];
             FreeBox64Context(&my_context);
@@ -1337,10 +1340,10 @@ int main(int argc, const char **argv, char **env) {
             int nenv = 0;
             while(env[nenv]) nenv++;
             // alloc + "LD_PRELOAD" if needd + last NULL ending
-            char** newenv = (char**)calloc(nenv+1+((preload)?0:1), sizeof(char*));
+            char** newenv = (char**)box_calloc(nenv+1+((preload)?0:1), sizeof(char*));
             // copy strings
             for (int i=0; i<nenv; ++i)
-                newenv[i] = strdup(env[i]);
+                newenv[i] = box_strdup(env[i]);
             // add ld_preload
             if(preload) {
                 // find the line
@@ -1349,24 +1352,24 @@ int main(int argc, const char **argv, char **env) {
                     if(strstr(newenv[l], "LD_PRELOAD=")==newenv[l]) {
                         // found it!
                         char *old = newenv[l];
-                        newenv[l] = (char*)calloc(strlen(old)+strlen("libtcmalloc_minimal.so.4:")+1, sizeof(char));
+                        newenv[l] = (char*)box_calloc(strlen(old)+strlen("libtcmalloc_minimal.so.4:")+1, sizeof(char));
                         strcpy(newenv[l], "LD_PRELOAD=libtcmalloc_minimal.so.4:");
                         strcat(newenv[l], old + strlen("LD_PRELOAD="));
-                        free(old);
+                        box_free(old);
                         // done, end loop
                         l = nenv;
                     } else ++l;
                 }
             } else {
                 //move last one
-                newenv[nenv] = strdup(newenv[nenv-1]);
-                free(newenv[nenv-1]);
-                newenv[nenv-1] = strdup("LD_PRELOAD=libtcmalloc_minimal.so.4");
+                newenv[nenv] = box_strdup(newenv[nenv-1]);
+                box_free(newenv[nenv-1]);
+                newenv[nenv-1] = box_strdup("LD_PRELOAD=libtcmalloc_minimal.so.4");
             }
             // duplicate argv too
-            char** newargv = calloc(argc+1, sizeof(char*));
+            char** newargv = box_calloc(argc+1, sizeof(char*));
             int narg = 0;
-            while(argv[narg]) {newargv[narg] = strdup(argv[narg]); narg++;}
+            while(argv[narg]) {newargv[narg] = box_strdup(argv[narg]); narg++;}
             // launch with new env...
             if(execve(newargv[0], newargv, newenv)<0)
                 printf_log(LOG_NONE, "Failed to relaunch, error is %d/%s\n", errno, strerror(errno));
@@ -1494,7 +1497,7 @@ int main(int argc, const char **argv, char **env) {
 
 #ifdef HAVE_TRACE
     if(trace_func)  {
-        free(trace_func);
+        box_free(trace_func);
         trace_func = NULL;
     }
 #endif
diff --git a/src/mallochook.c b/src/mallochook.c
new file mode 100644
index 00000000..e5927b7f
--- /dev/null
+++ b/src/mallochook.c
@@ -0,0 +1,28 @@
+#define _GNU_SOURCE         /* See feature_test_macros(7) */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <dlfcn.h>
+
+#include "box64context.h"
+#include "debug.h"
+
+#ifdef ANDROID
+void*(*__libc_malloc)(size_t) = NULL;
+void*(*__libc_realloc)(size_t, void*) = NULL;
+void*(*__libc_calloc)(size_t, size_t) = NULL;
+void (*__libc_free*)(void*) = NULL;
+
+void init_malloc_hook() {
+    __libc_malloc = dlsym(RTLD_NEXT, "malloc");
+    __libc_realloc = dlsym(RTLD_NEXT, "realloc");
+    __libc_calloc = dlsym(RTLD_NEXT, "realloc");
+    __libc_free = dlsym(RTLD_NEXT, "free");
+}
+#endif
+
+char* box_strdup(const char* s) {
+    char* ret = box_calloc(1, strlen(s)+1);
+    memcpy(ret, s, strlen(s));
+    return ret;
+}
\ No newline at end of file
diff --git a/src/steam.c b/src/steam.c
index 83b37dac..bb616bb9 100644
--- a/src/steam.c
+++ b/src/steam.c
@@ -49,7 +49,7 @@ void pressure_vessel(int argc, const char** argv, int nextarg)
     int x86 = my_context->box86path?FileIsX86ELF(argv[nextarg]):0;
     int x64 = my_context->box64path?FileIsX64ELF(argv[nextarg]):0;
     // create the new argv array
-    const char** newargv = (const char**)calloc((argc-nextarg)+1+((x86 || x64)?1:0), sizeof(char*));
+    const char** newargv = (const char**)box_calloc((argc-nextarg)+1+((x86 || x64)?1:0), sizeof(char*));
     if(x86 || x64) {
         newargv[0] = x64?my_context->box64path:my_context->box86path;
         printf_log(LOG_DEBUG, "argv[%d]=\"%s\"\n", 0, newargv[0]);    
diff --git a/src/tools/box64stack.c b/src/tools/box64stack.c
index a820b983..a46fcbd6 100755
--- a/src/tools/box64stack.c
+++ b/src/tools/box64stack.c
@@ -98,7 +98,7 @@ void SetupInitialStack(x64emu_t *emu)
     for (int i=emu->context->argc-1; i>=0; --i) {
         PushString(emu, emu->context->argv[i]);
         p_argv[i] = R_RSP;
-        free(emu->context->argv[i]);
+        box_free(emu->context->argv[i]);
         emu->context->argv[i] = (char*)p_argv[i];
     }
     // align
@@ -176,12 +176,12 @@ void SetupInitialStack(x64emu_t *emu)
     Push(emu, 0);
     for (int i=emu->context->envc-1; i>=0; --i)
         Push(emu, p_envv[i]);
-    free(emu->context->envv);
+    box_free(emu->context->envv);
     emu->context->envv = (char**)R_RSP;
     Push(emu, 0);
     for (int i=emu->context->argc-1; i>=0; --i)
         Push(emu, p_argv[i]);
-    free(emu->context->argv);
+    box_free(emu->context->argv);
     emu->context->argv = (char**)R_RSP;
     Push(emu, emu->context->argc);
 }
diff --git a/src/tools/bridge.c b/src/tools/bridge.c
index b2e8d5ff..8f9c7dad 100755
--- a/src/tools/bridge.c
+++ b/src/tools/bridge.c
@@ -41,7 +41,7 @@ int my_munmap(x64emu_t* emu, void* addr, unsigned long length);
 
 brick_t* NewBrick()
 {
-    brick_t* ret = (brick_t*)calloc(1, sizeof(brick_t));
+    brick_t* ret = (brick_t*)box_calloc(1, sizeof(brick_t));
     void* ptr = my_mmap(thread_get_emu(), NULL, NBRICK * sizeof(onebridge_t), PROT_READ | PROT_WRITE, MAP_PRIVATE | 0x40 | MAP_ANONYMOUS, -1, 0); // 0x40 is MAP_32BIT
     if(ptr == MAP_FAILED) {
         printf_log(LOG_NONE, "Warning, cannot allocate 0x%lx aligned bytes for bridge, will probably crash later\n", NBRICK*sizeof(onebridge_t));
@@ -52,7 +52,7 @@ brick_t* NewBrick()
 
 bridge_t *NewBridge()
 {
-    bridge_t *b = (bridge_t*)calloc(1, sizeof(bridge_t));
+    bridge_t *b = (bridge_t*)box_calloc(1, sizeof(bridge_t));
     b->head = NewBrick();
     b->last = b->head;
     b->bridgemap = kh_init(bridgemap);
@@ -72,11 +72,11 @@ void FreeBridge(bridge_t** bridge)
             unprotectDB((uintptr_t)b->b, NBRICK*sizeof(onebridge_t));
         #endif
         my_munmap(emu, b->b, NBRICK*sizeof(onebridge_t));
-        free(b);
+        box_free(b);
         b = n;
     }
     kh_destroy(bridgemap, (*bridge)->bridgemap);
-    free(*bridge);
+    box_free(*bridge);
     *bridge = NULL;
 }
 
diff --git a/src/tools/fileutils.c b/src/tools/fileutils.c
index 5a6c0af8..5631134b 100755
--- a/src/tools/fileutils.c
+++ b/src/tools/fileutils.c
@@ -45,7 +45,7 @@ char* ResolveFile(const char* filename, path_collection_t* paths)
 {
     char p[MAX_PATH];
     if(filename[0]=='/')
-        return strdup(filename);
+        return box_strdup(filename);
     for (int i=0; i<paths->size; ++i) {
         if(paths->paths[i][0]!='/') {
             // not an absolute path...
@@ -60,7 +60,7 @@ char* ResolveFile(const char* filename, path_collection_t* paths)
             return realpath(p, NULL);
     }
 
-    return strdup(filename); //NULL;
+    return box_strdup(filename); //NULL;
 }
 
 int FileIsX64ELF(const char* filename)
diff --git a/src/tools/gtkclass.c b/src/tools/gtkclass.c
index 2a8ee921..633fa02f 100755
--- a/src/tools/gtkclass.c
+++ b/src/tools/gtkclass.c
@@ -2024,7 +2024,7 @@ void my_add_signal_offset(size_t itype, uint32_t offset, int n)
         }
     if(p->sz==p->cap) {
         p->cap+=4;
-        p->a = (sigoffset_t*)realloc(p->a, sizeof(sigoffset_t)*p->cap);
+        p->a = (sigoffset_t*)box_realloc(p->a, sizeof(sigoffset_t)*p->cap);
     }
     p->a[p->sz].offset = offset;
     p->a[p->sz++].n = n;
@@ -2281,7 +2281,7 @@ void FiniGTKClass()
         /*khint_t k;
         kh_foreach_key(my_signalmap, k,
             my_signal_t* p = (my_signal_t*)(uintptr_t)k;
-            free(p);
+            box_free(p);
         );*/ // lets assume all signals data is freed by gtk already
         kh_destroy(signalmap, my_signalmap);
         my_signalmap = NULL;
@@ -2289,7 +2289,7 @@ void FiniGTKClass()
     if(my_sigoffset) {
         sigoffset_array_t* p;
         kh_foreach_value_ref(my_sigoffset, p,
-            free(p->a);
+            box_free(p->a);
         );
         kh_destroy(sigoffset, my_sigoffset);
         my_sigoffset = NULL;
@@ -2324,7 +2324,7 @@ void SetGTypeName(void* f)
 
 my_signal_t* new_mysignal(void* f, void* data, void* destroy)
 {
-    my_signal_t* sig = (my_signal_t*)calloc(1, sizeof(my_signal_t));
+    my_signal_t* sig = (my_signal_t*)box_calloc(1, sizeof(my_signal_t));
     sig->sign = SIGN;
     sig->c_handler = (uintptr_t)f;
     sig->destroy = (uintptr_t)destroy;
@@ -2346,7 +2346,7 @@ void my_signal_delete(my_signal_t* sig)
         RunFunction(my_context, d, 1, sig->data);
     }
     printf_log(LOG_DEBUG, "gtk Data deleted, sig=%p, data=%p, destroy=%p\n", sig, sig->data, (void*)d);
-    free(sig);
+    box_free(sig);
 }
 int my_signal_is_valid(void* sig)
 {
diff --git a/src/tools/pathcoll.c b/src/tools/pathcoll.c
index d7e423bb..1ee55721 100755
--- a/src/tools/pathcoll.c
+++ b/src/tools/pathcoll.c
@@ -17,8 +17,8 @@ void FreeCollection(path_collection_t* collection)
         return;
     if(collection->cap) {
         for(int i=0; i<collection->size; ++i)
-            free(collection->paths[i]);
-        free(collection->paths);
+            box_free(collection->paths[i]);
+        box_free(collection->paths);
     }
     collection->paths = NULL;
     collection->size = 0;
@@ -42,7 +42,7 @@ void ParseList(const char* List, path_collection_t* collection, int folder)
     }
     // alloc empty strings
     collection->cap = cnt;
-    collection->paths = (char**)calloc(cnt, sizeof(char*));
+    collection->paths = (char**)box_calloc(cnt, sizeof(char*));
     // and now split the paths...
     char tmp[MAX_PATH];
     const char *p = List;
@@ -64,7 +64,7 @@ void ParseList(const char* List, path_collection_t* collection, int folder)
         if(l) {
             if(folder && tmp[l-1]!='/')
                 strcat(tmp, "/");
-            collection->paths[idx]  =strdup(tmp);
+            collection->paths[idx]  =box_strdup(tmp);
             collection->size=++idx;
         }
     }
@@ -73,10 +73,10 @@ void ParseList(const char* List, path_collection_t* collection, int folder)
 void CopyCollection(path_collection_t* to, path_collection_t* from)
 {
     to->cap = from->cap;
-    to->paths = (char**)calloc(to->cap, sizeof(char*));
+    to->paths = (char**)box_calloc(to->cap, sizeof(char*));
     to->size = from->size;
     for (int i=0; i<to->size; ++i)
-        to->paths[i] = strdup(from->paths[i]);
+        to->paths[i] = box_strdup(from->paths[i]);
 }
 
 void AddPath(const char* path, path_collection_t* collection, int folder)
@@ -90,9 +90,9 @@ void AddPath(const char* path, path_collection_t* collection, int folder)
             strcat(tmp, "/");
         if(collection->size==collection->cap) {
             collection->cap += 4;
-            collection->paths = (char**)realloc(collection->paths, collection->cap*sizeof(char*));
+            collection->paths = (char**)box_realloc(collection->paths, collection->cap*sizeof(char*));
         }
-        collection->paths[collection->size++]=strdup(tmp);
+        collection->paths[collection->size++]=box_strdup(tmp);
     }
 }
 void PrependPath(const char* path, path_collection_t* collection, int folder)
@@ -106,10 +106,10 @@ void PrependPath(const char* path, path_collection_t* collection, int folder)
             strcat(tmp, "/");
         if(collection->size==collection->cap) {
             collection->cap += 4;
-            collection->paths = (char**)realloc(collection->paths, collection->cap*sizeof(char*));
+            collection->paths = (char**)box_realloc(collection->paths, collection->cap*sizeof(char*));
         }
         memmove(collection->paths+1, collection->paths, sizeof(char*)*collection->size);
-        collection->paths[0]=strdup(tmp);
+        collection->paths[0]=box_strdup(tmp);
         ++collection->size;
     }
 }
@@ -149,12 +149,12 @@ void PrependList(path_collection_t* collection, const char* List, int folder)
         return;
         // and now split the paths...
     char tmp[MAX_PATH];
-    char *p = strdup(List);
+    char *p = box_strdup(List);
     while(p) {
         char *p2 = strrchr(p, ':');
         if(!p2) {
             strncpy(tmp, p, MAX_PATH - 1);
-            free(p);
+            box_free(p);
             p=NULL;
         } else {
             strncpy(tmp, p2+1, MAX_PATH - 1);
diff --git a/src/wrapped/wrappedgtkx112.c b/src/wrapped/wrappedgtkx112.c
index e5f40f74..c7d42e83 100755
--- a/src/wrapped/wrappedgtkx112.c
+++ b/src/wrapped/wrappedgtkx112.c
@@ -911,7 +911,7 @@ EXPORT void my_gtk_binding_entry_add_signal(x64emu_t* emu, void* binding, uint32
         my->gtk_binding_entry_add_signal(binding, keyval, mod, name, 0);
         return;
     }
-    // build the list
+    // build the list (using calloc and not box_calloc, it's program space...)
     my_GSList_t *list = calloc(n, sizeof(my_GSList_t));
     for(uint32_t i=0; i<n; ++i) {
         list[i].data = (void*)getVArgs(emu, 5, b, i);
diff --git a/src/wrapped/wrappedlibc.c b/src/wrapped/wrappedlibc.c
index 2eead1f2..60c294d2 100755
--- a/src/wrapped/wrappedlibc.c
+++ b/src/wrapped/wrappedlibc.c
@@ -1320,8 +1320,8 @@ void grabNCpu() {
     size_t dummy;
     if(f) {
         nCPU = 0;
-        size_t len = 0;
-        char* line = NULL;
+        size_t len = 500;
+        char* line[500] = {0};
         while ((dummy = getline(&line, &len, f)) != -1) {
             if(!strncmp(line, "processor\t", strlen("processor\t")))
                 ++nCPU;
@@ -1332,7 +1332,6 @@ void grabNCpu() {
                     bogoMips = tmp;
             }
         }
-        if(line) free(line);
         fclose(f);
         if(!nCPU) nCPU=1;
     }
@@ -1691,7 +1690,7 @@ EXPORT int32_t my_execv(x64emu_t* emu, const char* path, char* const argv[])
         // count argv...
         int n=skip_first;
         while(argv[n]) ++n;
-        const char** newargv = (const char**)calloc(n+2, sizeof(char*));
+        const char** newargv = (const char**)box_calloc(n+2, sizeof(char*));
         newargv[0] = x86?emu->context->box86path:emu->context->box64path;
         memcpy(newargv+1, argv+skip_first, sizeof(char*)*(n+1));
         if(self) newargv[1] = emu->context->fullpath; else newargv[1] = skip_first?argv[skip_first]:path;
@@ -1705,7 +1704,7 @@ EXPORT int32_t my_execv(x64emu_t* emu, const char* path, char* const argv[])
             ret = execve(newargv[0], (char* const*)newargv, envv);
         else
             ret = execv(newargv[0], (char* const*)newargv);
-        free(newargv);
+        box_free(newargv);
         return ret;
     }
     #endif
@@ -1730,13 +1729,13 @@ EXPORT int32_t my_execve(x64emu_t* emu, const char* path, char* const argv[], ch
         // count argv...
         int n=skip_first;
         while(argv[n]) ++n;
-        const char** newargv = (const char**)calloc(n+2, sizeof(char*));
+        const char** newargv = (const char**)box_calloc(n+2, sizeof(char*));
         newargv[0] = x86?emu->context->box86path:emu->context->box64path;
         memcpy(newargv+1, argv+skip_first, sizeof(char*)*(n+1));
         if(self) newargv[1] = emu->context->fullpath;
         printf_log(LOG_DEBUG, " => execve(\"%s\", %p [\"%s\", \"%s\", \"%s\"...:%d], %p)\n", newargv[0], newargv, newargv[0], n?newargv[1]:"", (n>1)?newargv[2]:"",n, envp);
         int ret = execve(newargv[0], (char* const*)newargv, envp);
-        free(newargv);
+        box_free(newargv);
         return ret;
     }
     #endif
@@ -1762,12 +1761,12 @@ EXPORT int32_t my_execvp(x64emu_t* emu, const char* path, char* const argv[])
     int x64 = FileIsX64ELF(fullpath);
     int x86 = my_context->box86path?FileIsX86ELF(path):0;
     printf_log(LOG_DEBUG, "execvp(\"%s\", %p), IsX86=%d / fullpath=\"%s\"\n", path, argv, x64, fullpath);
-    free(fullpath);
+    box_free(fullpath);
     if (x64 || x86 || self) {
         // count argv...
         int i=0;
         while(argv[i]) ++i;
-        char** newargv = (char**)calloc(i+2, sizeof(char*));
+        char** newargv = (char**)box_calloc(i+2, sizeof(char*));
         newargv[0] = x86?emu->context->box86path:emu->context->box64path;
         for (int j=0; j<i; ++j)
             newargv[j+1] = argv[j];
@@ -1782,7 +1781,7 @@ EXPORT int32_t my_execvp(x64emu_t* emu, const char* path, char* const argv[])
             ret = execvpe(newargv[0], newargv, envv);
         else
             ret = execvp(newargv[0], newargv);
-        free(newargv);
+        box_free(newargv);
         return ret;
     }
     if((!strcmp(path + strlen(path) - strlen("/uname"), "/uname") || !strcmp(path, "uname"))
@@ -1808,7 +1807,7 @@ EXPORT int32_t my_execl(x64emu_t* emu, const char* path)
     // count argv...
     int i=0;
     while(getVargN(emu, i+1)) ++i;
-    char** newargv = (char**)calloc(i+((x64 || self)?2:1), sizeof(char*));
+    char** newargv = (char**)box_calloc(i+((x64 || self)?2:1), sizeof(char*));
     int j=0;
     if ((x64 || x86 || self))
         newargv[j++] = x86?emu->context->box86path:emu->context->box64path;
@@ -1817,7 +1816,7 @@ EXPORT int32_t my_execl(x64emu_t* emu, const char* path)
     if(self) newargv[1] = emu->context->fullpath;
     printf_log(LOG_DEBUG, " => execl(\"%s\", %p [\"%s\", \"%s\"...:%d])\n", newargv[0], newargv, newargv[1], i?newargv[2]:"", i);
     int ret = execv(newargv[0], newargv);
-    free(newargv);
+    box_free(newargv);
     return ret;
 }
 
@@ -1830,11 +1829,11 @@ EXPORT int32_t my_execlp(x64emu_t* emu, const char* path)
     int x64 = FileIsX64ELF(fullpath);
     int x86 = my_context->box86path?FileIsX86ELF(path):0;
     printf_log(LOG_DEBUG, "execlp(\"%s\", ...), IsX86=%d / fullpath=\"%s\"\n", path, x64, fullpath);
-    free(fullpath);
+    box_free(fullpath);
     // count argv...
     int i=0;
     while(getVargN(emu, i+1)) ++i;
-    char** newargv = (char**)calloc(i+((x64 || self)?2:1), sizeof(char*));
+    char** newargv = (char**)box_calloc(i+((x64 || self)?2:1), sizeof(char*));
     int j=0;
     if ((x64 || x86 || self))
         newargv[j++] = x86?emu->context->box86path:emu->context->box64path;
@@ -1851,7 +1850,7 @@ EXPORT int32_t my_execlp(x64emu_t* emu, const char* path)
         ret = execvpe(newargv[0], newargv, envv);
     else
         ret = execvp(newargv[0], newargv);
-    free(newargv);
+    box_free(newargv);
     return ret;
 }
 
@@ -1866,7 +1865,7 @@ EXPORT int32_t my_posix_spawn(x64emu_t* emu, pid_t* pid, const char* fullpath,
         // count argv...
         int i=0;
         while(argv[i]) ++i;
-        char** newargv = (char**)calloc(i+2, sizeof(char*));
+        char** newargv = (char**)box_calloc(i+2, sizeof(char*));
         newargv[0] = x86?emu->context->box86path:emu->context->box64path;
         for (int j=0; j<i; ++j)
             newargv[j+1] = argv[j];
@@ -1874,7 +1873,7 @@ EXPORT int32_t my_posix_spawn(x64emu_t* emu, pid_t* pid, const char* fullpath,
         printf_log(/*LOG_DEBUG*/LOG_INFO, " => posix_spawn(%p, \"%s\", %p, %p, %p [\"%s\", \"%s\"...:%d], %p)\n", pid, newargv[0], actions, attrp, newargv, newargv[1], i?newargv[2]:"", i, envp);
         int ret = posix_spawn(pid, newargv[0], actions, attrp, newargv, envp);
         printf_log(/*LOG_DEBUG*/LOG_INFO, "posix_spawn returned %d\n", ret);
-        //free(newargv);
+        //box_free(newargv);
         return ret;
     }
     // fullpath is gone, so the search will only be on PATH, not on BOX64_PATH (is that an issue?)
@@ -1892,12 +1891,12 @@ EXPORT int32_t my_posix_spawnp(x64emu_t* emu, pid_t* pid, const char* path,
     int x64 = FileIsX64ELF(fullpath);
     int x86 = my_context->box86path?FileIsX86ELF(path):0;
     printf_log(/*LOG_DEBUG*/LOG_INFO, "posix_spawnp(%p, \"%s\", %p, %p, %p, %p), IsX86=%d / fullpath=\"%s\"\n", pid, path, actions, attrp, argv, envp, x64, fullpath);
-    free(fullpath);
+    box_free(fullpath);
     if (x64 || x86 || self) {
         // count argv...
         int i=0;
         while(argv[i]) ++i;
-        char** newargv = (char**)calloc(i+2, sizeof(char*));
+        char** newargv = (char**)box_calloc(i+2, sizeof(char*));
         newargv[0] = x86?emu->context->box86path:emu->context->box64path;
         for (int j=0; j<i; ++j)
             newargv[j+1] = argv[j];
@@ -1905,7 +1904,7 @@ EXPORT int32_t my_posix_spawnp(x64emu_t* emu, pid_t* pid, const char* path,
         printf_log(/*LOG_DEBUG*/LOG_INFO, " => posix_spawnp(%p, \"%s\", %p, %p, %p [\"%s\", \"%s\"...:%d], %p)\n", pid, newargv[0], actions, attrp, newargv, newargv[1], i?newargv[2]:"", i, envp);
         int ret = posix_spawnp(pid, newargv[0], actions, attrp, newargv, envp);
         printf_log(/*LOG_DEBUG*/LOG_INFO, "posix_spawnp returned %d\n", ret);
-        //free(newargv);
+        //box_free(newargv);
         return ret;
     }
     // fullpath is gone, so the search will only be on PATH, not on BOX64_PATH (is that an issue?)
@@ -1927,7 +1926,7 @@ EXPORT int32_t my___register_atfork(x64emu_t *emu, void* prepare, void* parent,
     // this is partly incorrect, because the emulated funcionts should be executed by actual fork and not by my_atfork...
     if(my_context->atfork_sz==my_context->atfork_cap) {
         my_context->atfork_cap += 4;
-        my_context->atforks = (atfork_fnc_t*)realloc(my_context->atforks, my_context->atfork_cap*sizeof(atfork_fnc_t));
+        my_context->atforks = (atfork_fnc_t*)box_realloc(my_context->atforks, my_context->atfork_cap*sizeof(atfork_fnc_t));
     }
     my_context->atforks[my_context->atfork_sz].prepare = (uintptr_t)prepare;
     my_context->atforks[my_context->atfork_sz].parent = (uintptr_t)parent;
@@ -2420,13 +2419,13 @@ static int my_cookie_close(void *p)
     int ret = 0;
     if(cookie->c)
         ret = RunFunction(my_context, cookie->c, 1, cookie->cookie);
-    free(cookie);
+    box_free(cookie);
     return ret;
 }
 EXPORT void* my_fopencookie(x64emu_t* emu, void* cookie, void* mode, void* read, void* write, void* seek, void* close)
 {
     cookie_io_functions_t io_funcs = {read?my_cookie_read:NULL, write?my_cookie_write:NULL, seek?my_cookie_seek:NULL, my_cookie_close};
-    my_cookie_t *cb = (my_cookie_t*)calloc(1, sizeof(my_cookie_t));
+    my_cookie_t *cb = (my_cookie_t*)box_calloc(1, sizeof(my_cookie_t));
     cb->r = (uintptr_t)read;
     cb->w = (uintptr_t)write;
     cb->s = (uintptr_t)seek;
@@ -2525,14 +2524,14 @@ EXPORT int my_renameat2(int olddirfd, void* oldpath, int newdirfd, void* newpath
     }
     if((flags&RENAME_EXCHANGE) && (olddirfd==-1) && (newdirfd==-1)) {
         // cannot do atomically...
-        char* tmp = (char*)malloc(strlen(oldpath)+10); // create a temp intermediary
+        char* tmp = (char*)box_malloc(strlen(oldpath)+10); // create a temp intermediary
         tmp = strcat(oldpath, ".tmp");
         int ret = renameat(-1, oldpath, -1, tmp);
         if(ret==-1) return -1;
         ret = renameat(-1, newpath, -1, oldpath);
         if(ret==-1) return -1;
         ret = renameat(-1, tmp, -1, newpath);
-        free(tmp);
+        box_free(tmp);
         return ret;
     }
     return -1; // unknown flags
@@ -2652,7 +2651,7 @@ EXPORT int my_backtrace(x64emu_t* emu, void** buffer, int size)
 EXPORT char** my_backtrace_symbols(x64emu_t* emu, uintptr_t* buffer, int size)
 {
     (void)emu;
-    char** ret = (char**)calloc(1, size*sizeof(char*) + size*200);  // capping each strings to 200 chars
+    char** ret = (char**)calloc(1, size*sizeof(char*) + size*200);  // capping each strings to 200 chars, not using box_calloc (program space)
     char* s = (char*)(ret+size);
     for (int i=0; i<size; ++i) {
         uintptr_t start = 0;
@@ -2769,7 +2768,7 @@ static int clone_fn(void* p)
     FreeX64Emu(&emu);
     if(arg->stack_clone_used)
         my_context->stack_clone_used = 0;
-    free(arg);
+    box_free(arg);
     /*if(exited)
         exit(ret);*/
     return ret;
@@ -2779,16 +2778,16 @@ EXPORT int my_clone(x64emu_t* emu, void* fn, void* stack, int flags, void* args,
 {
     printf_log(LOG_DEBUG, "my_clone(fn:%p(%s), stack:%p, 0x%x, args:%p, %p, %p, %p)", fn, getAddrFunctionName((uintptr_t)fn), stack, flags, args, parent, tls, child);
     void* mystack = NULL;
-    clone_arg_t* arg = (clone_arg_t*)calloc(1, sizeof(clone_arg_t));
+    clone_arg_t* arg = (clone_arg_t*)box_calloc(1, sizeof(clone_arg_t));
     x64emu_t * newemu = NewX64Emu(emu->context, R_RIP, (uintptr_t)stack, 0, 0);
     SetupX64Emu(newemu);
     CloneEmu(newemu, emu);
     if(my_context->stack_clone_used) {
         printf_log(LOG_DEBUG, " no free stack_clone ");
-        mystack = malloc(1024*1024);  // stack for own process... memory leak, but no practical way to remove it
+        mystack = box_malloc(1024*1024);  // stack for own process... memory leak, but no practical way to remove it
     } else {
         if(!my_context->stack_clone)
-            my_context->stack_clone = malloc(1024*1024);
+            my_context->stack_clone = box_malloc(1024*1024);
         mystack = my_context->stack_clone;
         printf_log(LOG_DEBUG, " using stack_clone ");
         my_context->stack_clone_used = 1;
diff --git a/src/wrapped/wrappedlibdl.c b/src/wrapped/wrappedlibdl.c
index 894eca69..6360cf34 100755
--- a/src/wrapped/wrappedlibdl.c
+++ b/src/wrapped/wrappedlibdl.c
@@ -29,12 +29,12 @@ typedef struct dlprivate_s {
 } dlprivate_t;
 
 dlprivate_t *NewDLPrivate() {
-    dlprivate_t* dl =  (dlprivate_t*)calloc(1, sizeof(dlprivate_t));
+    dlprivate_t* dl =  (dlprivate_t*)box_calloc(1, sizeof(dlprivate_t));
     return dl;
 }
 void FreeDLPrivate(dlprivate_t **lib) {
-    free((*lib)->last_error);
-    free(*lib);
+    box_free((*lib)->last_error);
+    box_free(*lib);
 }
 
 void* my_dlopen(x64emu_t* emu, void *filename, int flag) EXPORT;
@@ -50,7 +50,7 @@ int my_dlinfo(x64emu_t* emu, void* handle, int request, void* info) EXPORT;
 #define LIBNAME libdl
 const char* libdlName = "libdl.so.2";
 
-#define CLEARERR    if(dl->last_error) free(dl->last_error); dl->last_error = NULL;
+#define CLEARERR    if(dl->last_error) box_free(dl->last_error); dl->last_error = NULL;
 
 extern int box64_zoom;
 // Implementation
@@ -75,30 +75,30 @@ void* my_dlopen(x64emu_t* emu, void *filename, int flag)
         printf_dlsym(LOG_DEBUG, "Call to dlopen(\"%s\"/%p, %X)\n", rfilename, filename, flag);
         // Transform any ${...} that maight be present
         while(strstr(rfilename, "${ORIGIN}")) {
-            char* origin = strdup(my_context->fullpath);
+            char* origin = box_strdup(my_context->fullpath);
             char* p = strrchr(origin, '/');
             if(p) *p = '\0';    // remove file name to have only full path, without last '/'
-            char* tmp = (char*)calloc(1, strlen(rfilename)-strlen("${ORIGIN}")+strlen(origin)+1);
+            char* tmp = (char*)box_calloc(1, strlen(rfilename)-strlen("${ORIGIN}")+strlen(origin)+1);
             p = strstr(rfilename, "${ORIGIN}");
             memcpy(tmp, rfilename, p-rfilename);
             strcat(tmp, origin);
             strcat(tmp, p+strlen("${ORIGIN}"));
             strcpy(rfilename, tmp);
-            free(tmp);
-            free(origin);
+            box_free(tmp);
+            box_free(origin);
         }
         while(strstr(rfilename, "${PLATFORM}")) {
-            char* platform = strdup("x86_64");
+            char* platform = box_strdup("x86_64");
             char* p = strrchr(platform, '/');
             if(p) *p = '\0';    // remove file name to have only full path, without last '/'
-            char* tmp = (char*)calloc(1, strlen(rfilename)-strlen("${PLATFORM}")+strlen(platform)+1);
+            char* tmp = (char*)box_calloc(1, strlen(rfilename)-strlen("${PLATFORM}")+strlen(platform)+1);
             p = strstr(rfilename, "${PLATFORM}");
             memcpy(tmp, rfilename, p-rfilename);
             strcat(tmp, platform);
             strcat(tmp, p+strlen("${PLATFORM}"));
             strcpy(rfilename, tmp);
-            free(tmp);
-            free(platform);
+            box_free(tmp);
+            box_free(platform);
         }
         // check if alread dlopenned...
         for (size_t i=0; i<dl->lib_sz; ++i) {
@@ -132,7 +132,7 @@ void* my_dlopen(x64emu_t* emu, void *filename, int flag)
         if(AddNeededLib(NULL, NULL, NULL, is_local, bindnow, libs, 1, emu->context, emu)) {
             printf_dlsym(strchr(rfilename,'/')?LOG_DEBUG:LOG_INFO, "Warning: Cannot dlopen(\"%s\"/%p, %X)\n", rfilename, filename, flag);
             if(!dl->last_error)
-                dl->last_error = malloc(129);
+                dl->last_error = box_malloc(129);
             snprintf(dl->last_error, 129, "Cannot dlopen(\"%s\"/%p, %X)\n", rfilename, filename, flag);
             return NULL;
         }
@@ -152,9 +152,9 @@ void* my_dlopen(x64emu_t* emu, void *filename, int flag)
     
     if(dl->lib_sz == dl->lib_cap) {
         dl->lib_cap += 4;
-        dl->libs = (library_t**)realloc(dl->libs, sizeof(library_t*)*dl->lib_cap);
-        dl->count = (size_t*)realloc(dl->count, sizeof(size_t)*dl->lib_cap);
-        dl->dlopened = (size_t*)realloc(dl->dlopened, sizeof(size_t)*dl->lib_cap);
+        dl->libs = (library_t**)box_realloc(dl->libs, sizeof(library_t*)*dl->lib_cap);
+        dl->count = (size_t*)box_realloc(dl->count, sizeof(size_t)*dl->lib_cap);
+        dl->dlopened = (size_t*)box_realloc(dl->dlopened, sizeof(size_t)*dl->lib_cap);
         // memset count...
         memset(dl->count+dl->lib_sz, 0, (dl->lib_cap-dl->lib_sz)*sizeof(size_t));
     }
@@ -227,7 +227,7 @@ void* my_dlsym(x64emu_t* emu, void *handle, void *symbol)
             return (void*)start;
         }
         if(!dl->last_error)
-            dl->last_error = malloc(129);
+            dl->last_error = box_malloc(129);
         snprintf(dl->last_error, 129, "Symbol \"%s\" not found in %p)\n", rsymbol, handle);
         printf_dlsym(LOG_NEVER, "%p\n", NULL);
         return NULL;
@@ -240,7 +240,7 @@ void* my_dlsym(x64emu_t* emu, void *handle, void *symbol)
             return (void*)start;
         }
         if(!dl->last_error)
-            dl->last_error = malloc(129);
+            dl->last_error = box_malloc(129);
         snprintf(dl->last_error, 129, "Symbol \"%s\" not found in %p)\n", rsymbol, handle);
         printf_dlsym(LOG_NEVER, "%p\n", NULL);
         return NULL;
@@ -250,14 +250,14 @@ void* my_dlsym(x64emu_t* emu, void *handle, void *symbol)
     // size_t is unsigned
     if(nlib>=dl->lib_sz) {
         if(!dl->last_error)
-            dl->last_error = malloc(129);
+            dl->last_error = box_malloc(129);
         snprintf(dl->last_error, 129, "Bad handle %p)\n", handle);
         printf_dlsym(LOG_NEVER, "%p\n", NULL);
         return NULL;
     }
     if(dl->count[nlib]==0) {
         if(!dl->last_error)
-            dl->last_error = malloc(129);
+            dl->last_error = box_malloc(129);
         snprintf(dl->last_error, 129, "Bad handle %p (already closed))\n", handle);
         printf_dlsym(LOG_NEVER, "%p\n", (void*)NULL);
         return NULL;
@@ -268,7 +268,7 @@ void* my_dlsym(x64emu_t* emu, void *handle, void *symbol)
             printf_dlsym(LOG_NEVER, "%p\nCall to dlsym(%s, \"%s\") Symbol not found\n", NULL, GetNameLib(dl->libs[nlib]), rsymbol);
             printf_log(LOG_DEBUG, " Symbol not found\n");
             if(!dl->last_error)
-                dl->last_error = malloc(129);
+                dl->last_error = box_malloc(129);
             snprintf(dl->last_error, 129, "Symbol \"%s\" not found in %p(%s)", rsymbol, handle, GetNameLib(dl->libs[nlib]));
             return NULL;
         }
@@ -280,7 +280,7 @@ void* my_dlsym(x64emu_t* emu, void *handle, void *symbol)
             return (void*)start;
         }
         if(!dl->last_error)
-            dl->last_error = malloc(129);
+            dl->last_error = box_malloc(129);
         snprintf(dl->last_error, 129, "Symbol \"%s\" not found in %p)\n", rsymbol, handle);
         printf_dlsym(LOG_NEVER, "%p\n", NULL);
         return NULL;
@@ -299,14 +299,14 @@ int my_dlclose(x64emu_t* emu, void *handle)
     // size_t is unsigned
     if(nlib>=dl->lib_sz) {
         if(!dl->last_error)
-            dl->last_error = malloc(129);
+            dl->last_error = box_malloc(129);
         snprintf(dl->last_error, 129, "Bad handle %p)\n", handle);
         printf_dlsym(LOG_DEBUG, "dlclose: %s\n", dl->last_error);
         return -1;
     }
     if(dl->count[nlib]==0) {
         if(!dl->last_error)
-            dl->last_error = malloc(129);
+            dl->last_error = box_malloc(129);
         snprintf(dl->last_error, 129, "Bad handle %p (already closed))\n", handle);
         printf_dlsym(LOG_DEBUG, "dlclose: %s\n", dl->last_error);
         return -1;
@@ -363,7 +363,7 @@ void* my_dlvsym(x64emu_t* emu, void *handle, void *symbol, const char *vername)
             return (void*)start;
         }
         if(!dl->last_error)
-            dl->last_error = malloc(129);
+            dl->last_error = box_malloc(129);
         snprintf(dl->last_error, 129, "Symbol \"%s\" version %s not found in %p)\n", rsymbol, vername?vername:"(nil)", handle);
             printf_dlsym(LOG_NEVER, "%p\n", NULL);
         return NULL;
@@ -376,7 +376,7 @@ void* my_dlvsym(x64emu_t* emu, void *handle, void *symbol, const char *vername)
             return (void*)start;
         }
         if(!dl->last_error)
-            dl->last_error = malloc(129);
+            dl->last_error = box_malloc(129);
         snprintf(dl->last_error, 129, "Symbol \"%s\" version %s not found in %p)\n", rsymbol, vername?vername:"(nil)", handle);
             printf_dlsym(LOG_NEVER, "%p\n", NULL);
         return NULL;
@@ -386,14 +386,14 @@ void* my_dlvsym(x64emu_t* emu, void *handle, void *symbol, const char *vername)
     // size_t is unsigned
     if(nlib>=dl->lib_sz) {
         if(!dl->last_error)
-            dl->last_error = malloc(129);
+            dl->last_error = box_malloc(129);
         snprintf(dl->last_error, 129, "Bad handle %p)\n", handle);
             printf_dlsym(LOG_NEVER, "%p\n", NULL);
         return NULL;
     }
     if(dl->count[nlib]==0) {
         if(!dl->last_error)
-            dl->last_error = malloc(129);
+            dl->last_error = box_malloc(129);
         snprintf(dl->last_error, 129, "Bad handle %p (already closed))\n", handle);
             printf_dlsym(LOG_NEVER, "%p\n", (void*)NULL);
         return NULL;
@@ -404,7 +404,7 @@ void* my_dlvsym(x64emu_t* emu, void *handle, void *symbol, const char *vername)
                 printf_dlsym(LOG_NEVER, "%p\nCall to dlvsym(%s, \"%s\", %s) Symbol not found\n", NULL, GetNameLib(dl->libs[nlib]), rsymbol, vername?vername:"(nil)");
             printf_log(LOG_DEBUG, " Symbol not found\n");
             if(!dl->last_error)
-                dl->last_error = malloc(129);
+                dl->last_error = box_malloc(129);
             snprintf(dl->last_error, 129, "Symbol \"%s\" not found in %p(%s)", rsymbol, handle, GetNameLib(dl->libs[nlib]));
             return NULL;
         }
@@ -426,7 +426,7 @@ void* my_dlvsym(x64emu_t* emu, void *handle, void *symbol, const char *vername)
             printf_dlsym(LOG_NEVER, "%p\nCall to dlvsym(%s, \"%s\", %s) Symbol not found\n", NULL, "Self", rsymbol, vername?vername:"(nil)");
         printf_log(LOG_DEBUG, " Symbol not found\n");
         if(!dl->last_error)
-            dl->last_error = malloc(129);
+            dl->last_error = box_malloc(129);
         snprintf(dl->last_error, 129, "Symbol \"%s\" version %s not found in %p)\n", rsymbol, vername?vername:"(nil)", handle);
         return NULL;
     }
@@ -445,14 +445,14 @@ int my_dlinfo(x64emu_t* emu, void* handle, int request, void* info)
     // size_t is unsigned
     if(nlib>=dl->lib_sz) {
         if(!dl->last_error)
-            dl->last_error = malloc(129);
+            dl->last_error = box_malloc(129);
         snprintf(dl->last_error, 129, "Bad handle %p)\n", handle);
         printf_dlsym(LOG_DEBUG, "dlinfo: %s\n", dl->last_error);
         return -1;
     }
     if(dl->count[nlib]==0) {
         if(!dl->last_error)
-            dl->last_error = malloc(129);
+            dl->last_error = box_malloc(129);
         snprintf(dl->last_error, 129, "Bad handle %p (already closed))\n", handle);
         printf_dlsym(LOG_DEBUG, "dlinfo: %s\n", dl->last_error);
         return -1;
@@ -468,7 +468,7 @@ int my_dlinfo(x64emu_t* emu, void* handle, int request, void* info)
         default:
             printf_log(LOG_NONE, "Warning, unsupported call to dlinfo(%p, %d, %p)\n", handle, request, info);
         if(!dl->last_error)
-            dl->last_error = malloc(129);
+            dl->last_error = box_malloc(129);
         snprintf(dl->last_error, 129, "unsupported call to dlinfo request:%d\n", request);
     }
     return -1;