diff options
| author | ptitSeb <sebastien.chev@gmail.com> | 2021-03-07 12:40:17 +0100 |
|---|---|---|
| committer | ptitSeb <sebastien.chev@gmail.com> | 2021-03-07 12:40:17 +0100 |
| commit | e937df638c045ea7378e8eed8d19d9d81205e358 (patch) | |
| tree | 5ef09d09f3ded78ea7a8b3c610f2eb8c4c0a5301 /src | |
| parent | 12b177a1ec213d1efaf3150a1aac31f0f97cec2b (diff) | |
| download | box64-e937df638c045ea7378e8eed8d19d9d81205e358.tar.gz box64-e937df638c045ea7378e8eed8d19d9d81205e358.zip | |
Added longjmp and pthread_cancel handling (and test14 works)
Diffstat (limited to 'src')
| -rwxr-xr-x | src/libtools/threads.c | 303 | ||||
| -rw-r--r-- | src/wrapped/generated/functions_list.txt | 4 | ||||
| -rw-r--r-- | src/wrapped/generated/wrapper.c | 8 | ||||
| -rw-r--r-- | src/wrapped/generated/wrapper.h | 4 | ||||
| -rwxr-xr-x | src/wrapped/wrappedlibc.c | 60 | ||||
| -rwxr-xr-x | src/wrapped/wrappedlibc_private.h | 10 | ||||
| -rwxr-xr-x | src/wrapped/wrappedlibpthread_private.h | 100 |
7 files changed, 239 insertions, 250 deletions
diff --git a/src/libtools/threads.c b/src/libtools/threads.c index e23ad0d4..90fc8f96 100755 --- a/src/libtools/threads.c +++ b/src/libtools/threads.c @@ -27,10 +27,10 @@ #include "dynablock.h" #endif -//void _pthread_cleanup_push_defer(void* buffer, void* routine, void* arg); // declare hidden functions -//void _pthread_cleanup_pop_restore(void* buffer, int exec); -//void _pthread_cleanup_push(void* buffer, void* routine, void* arg); // declare hidden functions -//void _pthread_cleanup_pop(void* buffer, int exec); +void _pthread_cleanup_push_defer(void* buffer, void* routine, void* arg); // declare hidden functions +void _pthread_cleanup_pop_restore(void* buffer, int exec); +void _pthread_cleanup_push(void* buffer, void* routine, void* arg); // declare hidden functions +void _pthread_cleanup_pop(void* buffer, int exec); typedef struct threadstack_s { void* stack; @@ -38,31 +38,26 @@ typedef struct threadstack_s { } threadstack_t; // longjmp / setjmp -//typedef struct jump_buff_i386_s { -// uint32_t save_ebx; -// uint32_t save_esi; -// uint32_t save_edi; -// uint32_t save_ebp; -// uint32_t save_esp; -// uint32_t save_eip; -//} jump_buff_i386_t; - -//typedef struct __jmp_buf_tag_s { -// jump_buff_i386_t __jmpbuf; -// int __mask_was_saved; -// __sigset_t __saved_mask; -//} __jmp_buf_tag_t; - -//typedef struct x86_unwind_buff_s { -// struct { -// jump_buff_i386_t __cancel_jmp_buf; -// int __mask_was_saved; -// } __cancel_jmp_buf[1]; -// void *__pad[4]; -//} x86_unwind_buff_t __attribute__((__aligned__)); - -KHASH_MAP_INIT_INT(threadstack, threadstack_t*) -//KHASH_MAP_INIT_INT(cancelthread, __pthread_unwind_buf_t*) +typedef struct jump_buff_x64_s { + uint64_t save_reg[8]; +} jump_buff_x64_t; + +typedef struct __jmp_buf_tag_s { + jump_buff_x64_t __jmpbuf; + int __mask_was_saved; + __sigset_t __saved_mask; +} __jmp_buf_tag_t; + +typedef struct x64_unwind_buff_s { + struct { + jump_buff_x64_t __cancel_jmp_buf; + int __mask_was_saved; + } __cancel_jmp_buf[1]; + void *__pad[4]; +} x64_unwind_buff_t __attribute__((__aligned__)); + +KHASH_MAP_INIT_INT64(threadstack, threadstack_t*) +KHASH_MAP_INIT_INT64(cancelthread, __pthread_unwind_buf_t*) void CleanStackSize(box64context_t* context) { @@ -120,33 +115,33 @@ int GetStackSize(x64emu_t* emu, uintptr_t attr, void** stack, size_t* stacksize) return 0; } -//static void InitCancelThread() -//{ -//} +static void InitCancelThread() +{ +} -//static void FreeCancelThread(box64context_t* context) -//{ -// if(!context) -// return; -//} -//static __pthread_unwind_buf_t* AddCancelThread(x86_unwind_buff_t* buff) -//{ -// __pthread_unwind_buf_t* r = (__pthread_unwind_buf_t*)calloc(1, sizeof(__pthread_unwind_buf_t)); -// buff->__pad[1] = r; -// return r; -//} +static void FreeCancelThread(box64context_t* context) +{ + if(!context) + return; +} +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)); + buff->__pad[1] = r; + return r; +} -//static __pthread_unwind_buf_t* GetCancelThread(x86_unwind_buff_t* buff) -//{ -// return (__pthread_unwind_buf_t*)buff->__pad[1]; -//} +static __pthread_unwind_buf_t* GetCancelThread(x64_unwind_buff_t* buff) +{ + return (__pthread_unwind_buf_t*)buff->__pad[1]; +} -//static void DelCancelThread(x86_unwind_buff_t* buff) -//{ -// __pthread_unwind_buf_t* r = (__pthread_unwind_buf_t*)buff->__pad[1]; -// free(r); -// buff->__pad[1] = NULL; -//} +static void DelCancelThread(x64_unwind_buff_t* buff) +{ + __pthread_unwind_buf_t* r = (__pthread_unwind_buf_t*)buff->__pad[1]; + free(r); + buff->__pad[1] = NULL; +} typedef struct emuthread_s { uintptr_t fnc; @@ -325,62 +320,58 @@ void* my_prepare_thread(x64emu_t *emu, void* f, void* arg, int ssize, void** pet return pthread_routine; } -//void my_longjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p, int32_t __val); +void my_longjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p, int32_t __val); -//#define CANCEL_MAX 8 -//static __thread x64emu_t* cancel_emu[CANCEL_MAX] = {0}; -//static __thread x86_unwind_buff_t* cancel_buff[CANCEL_MAX] = {0}; -//static __thread int cancel_deep = 0; -//EXPORT void my___pthread_register_cancel(void* E, void* B) -//{ -// // get a stack local copy of the args, as may be live in some register depending the architecture (like ARM) -// if(cancel_deep<0) { -// printf_log(LOG_NONE/*LOG_INFO*/, "BOX86: Warning, inconsistant value in __pthread_register_cancel (%d)\n", cancel_deep); -// cancel_deep = 0; -// } -// if(cancel_deep!=CANCEL_MAX-1) -// ++cancel_deep; -// else -// {printf_log(LOG_NONE/*LOG_INFO*/, "BOX86: Warning, calling __pthread_register_cancel(...) too many time\n");} -// -// cancel_emu[cancel_deep] = (x64emu_t*)E; -// // on i386, the function as __cleanup_fct_attribute attribute: so 1st parameter is in register -// x86_unwind_buff_t* buff = cancel_buff[cancel_deep] = (x86_unwind_buff_t*)((x64emu_t*)E)->regs[_AX].dword[0]; -// __pthread_unwind_buf_t * pbuff = AddCancelThread(buff); -// if(__sigsetjmp((struct __jmp_buf_tag*)(void*)pbuff->__cancel_jmp_buf, 0)) { -// //DelCancelThread(cancel_buff); // no del here, it will be delete by unwind_next... -// int i = cancel_deep--; -// x64emu_t* emu = cancel_emu[i]; -// my_longjmp(emu, cancel_buff[i]->__cancel_jmp_buf, 1); -// DynaRun(emu); // resume execution -// return; -// } -// -// __pthread_register_cancel(pbuff); -//} +#define CANCEL_MAX 8 +static __thread x64emu_t* cancel_emu[CANCEL_MAX] = {0}; +static __thread x64_unwind_buff_t* cancel_buff[CANCEL_MAX] = {0}; +static __thread int cancel_deep = 0; +EXPORT void my___pthread_register_cancel(void* E, void* B) +{ + // get a stack local copy of the args, as may be live in some register depending the architecture (like ARM) + if(cancel_deep<0) { + printf_log(LOG_NONE/*LOG_INFO*/, "BOX86: Warning, inconsistant value in __pthread_register_cancel (%d)\n", cancel_deep); + cancel_deep = 0; + } + if(cancel_deep!=CANCEL_MAX-1) + ++cancel_deep; + else + {printf_log(LOG_NONE/*LOG_INFO*/, "BOX86: Warning, calling __pthread_register_cancel(...) too many time\n");} + + cancel_emu[cancel_deep] = (x64emu_t*)E; + + x64_unwind_buff_t* buff = cancel_buff[cancel_deep] = (x64_unwind_buff_t*)B; + __pthread_unwind_buf_t * pbuff = AddCancelThread(buff); + if(__sigsetjmp((struct __jmp_buf_tag*)(void*)pbuff->__cancel_jmp_buf, 0)) { + //DelCancelThread(cancel_buff); // no del here, it will be delete by unwind_next... + int i = cancel_deep--; + x64emu_t* emu = cancel_emu[i]; + my_longjmp(emu, cancel_buff[i]->__cancel_jmp_buf, 1); + DynaRun(emu); // resume execution + return; + } -//EXPORT void my___pthread_unregister_cancel(x64emu_t* emu, x86_unwind_buff_t* buff) -//{ -// // on i386, the function as __cleanup_fct_attribute attribute: so 1st parameter is in register -// buff = (x86_unwind_buff_t*)R_EAX; -// __pthread_unwind_buf_t * pbuff = GetCancelThread(buff); -// __pthread_unregister_cancel(pbuff); -// -// --cancel_deep; -// DelCancelThread(buff); -//} + __pthread_register_cancel(pbuff); +} -//EXPORT void my___pthread_unwind_next(x64emu_t* emu, void* p) -//{ -// // on i386, the function as __cleanup_fct_attribute attribute: so 1st parameter is in register -// x86_unwind_buff_t* buff = (x86_unwind_buff_t*)R_EAX; -// __pthread_unwind_buf_t pbuff = *GetCancelThread(buff); -// DelCancelThread(buff); -// // function is noreturn, putting stuff on the stack to have it auto-free (is that correct?) -// __pthread_unwind_next(&pbuff); -// // just in case it does return -// emu->quit = 1; -//} +EXPORT void my___pthread_unregister_cancel(x64emu_t* emu, x64_unwind_buff_t* buff) +{ + __pthread_unwind_buf_t * pbuff = GetCancelThread(buff); + __pthread_unregister_cancel(pbuff); + + --cancel_deep; + DelCancelThread(buff); +} + +EXPORT void my___pthread_unwind_next(x64emu_t* emu, x64_unwind_buff_t* buff) +{ + __pthread_unwind_buf_t pbuff = *GetCancelThread(buff); + DelCancelThread(buff); + // function is noreturn, putting stuff on the stack to have it auto-free (is that correct?) + __pthread_unwind_next(&pbuff); + // just in case it does return + emu->quit = 1; +} KHASH_MAP_INIT_INT(once, int) @@ -590,20 +581,14 @@ EXPORT int my_pthread_cond_wait(x64emu_t* emu, void* cond, void* mutex) pthread_cond_t * c = get_cond(cond); return pthread_cond_wait(c, getAlignedMutex((pthread_mutex_t*)mutex)); } -#if 0 -EXPORT int my_pthread_mutexattr_setkind_np(x64emu_t* emu, void* t, int kind) -{ - // does "kind" needs some type of translation? - return pthread_mutexattr_settype(t, kind); -} -EXPORT int my_pthread_attr_setscope(x64emu_t* emu, void* attr, int scope) -{ - if(scope!=PTHREAD_SCOPE_SYSTEM) printf_log(LOG_INFO, "Warning, scope of call to pthread_attr_setscope(...) changed from %d to PTHREAD_SCOPE_SYSTEM\n", scope); - return pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM); - //The scope is either PTHREAD_SCOPE_SYSTEM or PTHREAD_SCOPE_PROCESS - // but PTHREAD_SCOPE_PROCESS doesn't seem supported on ARM linux, and PTHREAD_SCOPE_SYSTEM is default -} +//EXPORT int my_pthread_attr_setscope(x64emu_t* emu, void* attr, int scope) +//{ +// if(scope!=PTHREAD_SCOPE_SYSTEM) printf_log(LOG_INFO, "Warning, scope of call to pthread_attr_setscope(...) changed from %d to PTHREAD_SCOPE_SYSTEM\n", scope); +// return pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM); +// //The scope is either PTHREAD_SCOPE_SYSTEM or PTHREAD_SCOPE_PROCESS +// // but PTHREAD_SCOPE_PROCESS doesn't seem supported on ARM linux, and PTHREAD_SCOPE_SYSTEM is default +//} EXPORT void my__pthread_cleanup_push_defer(x64emu_t* emu, void* buffer, void* routine, void* arg) { @@ -625,54 +610,36 @@ EXPORT void my__pthread_cleanup_pop(x64emu_t* emu, void* buffer, int exec) _pthread_cleanup_pop(buffer, exec); } -// getaffinity_np (pthread or attr) hav an "old" version (glibc-2.3.3) that only have 2 args, cpusetsize is omited -EXPORT int my_pthread_getaffinity_np(x64emu_t* emu, pthread_t thread, int cpusetsize, void* cpuset) -{ - if(cpusetsize>0x1000) { - // probably old version of the function, that didn't have cpusetsize.... - cpuset = (void*)cpusetsize; - cpusetsize = sizeof(cpu_set_t); - } - - int ret = pthread_getaffinity_np(thread, cpusetsize, cpuset); - if(ret<0) { - printf_log(LOG_INFO, "Warning, pthread_getaffinity_np(%p, %d, %p) errored, with errno=%d\n", (void*)thread, cpusetsize, cpuset, errno); - } - - return ret; -} - -EXPORT int my_pthread_setaffinity_np(x64emu_t* emu, pthread_t thread, int cpusetsize, void* cpuset) -{ - if(cpusetsize>0x1000) { - // probably old version of the function, that didn't have cpusetsize.... - cpuset = (void*)cpusetsize; - cpusetsize = sizeof(cpu_set_t); - } - - int ret = pthread_setaffinity_np(thread, cpusetsize, cpuset); - if(ret<0) { - printf_log(LOG_INFO, "Warning, pthread_setaffinity_np(%p, %d, %p) errored, with errno=%d\n", (void*)thread, cpusetsize, cpuset, errno); - } - - return ret; -} - -EXPORT int my_pthread_attr_setaffinity_np(x64emu_t* emu, void* attr, uint32_t cpusetsize, void* cpuset) -{ - if(cpusetsize>0x1000) { - // probably old version of the function, that didn't have cpusetsize.... - cpuset = (void*)cpusetsize; - cpusetsize = sizeof(cpu_set_t); - } +//EXPORT int my_pthread_getaffinity_np(x64emu_t* emu, pthread_t thread, int cpusetsize, void* cpuset) +//{ +// int ret = pthread_getaffinity_np(thread, cpusetsize, cpuset); +// if(ret<0) { +// printf_log(LOG_INFO, "Warning, pthread_getaffinity_np(%p, %d, %p) errored, with errno=%d\n", (void*)thread, cpusetsize, cpuset, errno); +// } +// +// return ret; +//} - int ret = pthread_attr_setaffinity_np(attr, cpusetsize, cpuset); - if(ret<0) { - printf_log(LOG_INFO, "Warning, pthread_attr_setaffinity_np(%p, %d, %p) errored, with errno=%d\n", attr, cpusetsize, cpuset, errno); - } +//EXPORT int my_pthread_setaffinity_np(x64emu_t* emu, pthread_t thread, int cpusetsize, void* cpuset) +//{ +// int ret = pthread_setaffinity_np(thread, cpusetsize, cpuset); +// if(ret<0) { +// printf_log(LOG_INFO, "Warning, pthread_setaffinity_np(%p, %d, %p) errored, with errno=%d\n", (void*)thread, cpusetsize, cpuset, errno); +// } +// +// return ret; +//} - return ret; -} +//EXPORT int my_pthread_attr_setaffinity_np(x64emu_t* emu, void* attr, uint32_t cpusetsize, void* cpuset) +//{ +// +// int ret = pthread_attr_setaffinity_np(attr, cpusetsize, cpuset); +// if(ret<0) { +// printf_log(LOG_INFO, "Warning, pthread_attr_setaffinity_np(%p, %d, %p) errored, with errno=%d\n", attr, cpusetsize, cpuset, errno); +// } +// +// return ret; +//} EXPORT int my_pthread_kill(x64emu_t* emu, void* thread, int sig) { @@ -687,7 +654,7 @@ EXPORT void my_pthread_exit(x64emu_t* emu, void* retval) emu->quit = 1; // to be safe pthread_exit(retval); } -#endif + #ifdef NOALIGN pthread_mutex_t* getAlignedMutex(pthread_mutex_t* m) { return m; @@ -780,7 +747,7 @@ emu_jmpbuf_t* GetJmpBuf() void init_pthread_helper() { -// InitCancelThread(); + InitCancelThread(); mapcond = kh_init(mapcond); pthread_key_create(&jmpbuf_key, emujmpbuf_destroy); #ifndef NOALIGN @@ -790,7 +757,7 @@ void init_pthread_helper() void fini_pthread_helper(box64context_t* context) { -// FreeCancelThread(context); + FreeCancelThread(context); CleanStackSize(context); pthread_cond_t *cond; kh_foreach_value(mapcond, cond, diff --git a/src/wrapped/generated/functions_list.txt b/src/wrapped/generated/functions_list.txt index 2a7ddd9e..dc8edcc7 100644 --- a/src/wrapped/generated/functions_list.txt +++ b/src/wrapped/generated/functions_list.txt @@ -8,10 +8,12 @@ #() iFu #() iFf #() iFd +#() iFL #() iFp #() IFf #() IFd #() IFp +#() uFu #() uFp #() UFV #() fFf @@ -49,6 +51,7 @@ #() vFEpu #() vFfpp #() vFdpp +#() iFEpi #() iFEpp #() iFEpV #() iFpiu @@ -58,6 +61,7 @@ #() fFffp #() dFddd #() dFddp +#() pFEip #() pFEpi #() pFEpp #() pFipp diff --git a/src/wrapped/generated/wrapper.c b/src/wrapped/generated/wrapper.c index 872238a6..7cd7cd1a 100644 --- a/src/wrapped/generated/wrapper.c +++ b/src/wrapped/generated/wrapper.c @@ -79,10 +79,12 @@ typedef int32_t (*iFi_t)(int32_t); typedef int32_t (*iFu_t)(uint32_t); typedef int32_t (*iFf_t)(float); typedef int32_t (*iFd_t)(double); +typedef int32_t (*iFL_t)(uintptr_t); typedef int32_t (*iFp_t)(void*); typedef int64_t (*IFf_t)(float); typedef int64_t (*IFd_t)(double); typedef int64_t (*IFp_t)(void*); +typedef uint32_t (*uFu_t)(uint32_t); typedef uint32_t (*uFp_t)(void*); typedef uint64_t (*UFV_t)(void*); typedef float (*fFf_t)(float); @@ -120,6 +122,7 @@ typedef void* (*pFpV_t)(void*, void*); typedef void (*vFEpu_t)(x64emu_t*, void*, uint32_t); typedef void (*vFfpp_t)(float, void*, void*); typedef void (*vFdpp_t)(double, void*, void*); +typedef int32_t (*iFEpi_t)(x64emu_t*, void*, int32_t); typedef int32_t (*iFEpp_t)(x64emu_t*, void*, void*); typedef int32_t (*iFEpV_t)(x64emu_t*, void*, void*); typedef int32_t (*iFpiu_t)(void*, int32_t, uint32_t); @@ -129,6 +132,7 @@ typedef float (*fFfff_t)(float, float, float); typedef float (*fFffp_t)(float, float, void*); typedef double (*dFddd_t)(double, double, double); typedef double (*dFddp_t)(double, double, void*); +typedef void* (*pFEip_t)(x64emu_t*, int32_t, void*); typedef void* (*pFEpi_t)(x64emu_t*, void*, int32_t); typedef void* (*pFEpp_t)(x64emu_t*, void*, void*); typedef void* (*pFipp_t)(int32_t, void*, void*); @@ -171,10 +175,12 @@ void iFi(x64emu_t *emu, uintptr_t fcn) { iFi_t fn = (iFi_t)fcn; R_RAX=fn((int32_ void iFu(x64emu_t *emu, uintptr_t fcn) { iFu_t fn = (iFu_t)fcn; R_RAX=fn((uint32_t)R_RDI); } void iFf(x64emu_t *emu, uintptr_t fcn) { iFf_t fn = (iFf_t)fcn; R_RAX=fn(emu->xmm[0].f[0]); } void iFd(x64emu_t *emu, uintptr_t fcn) { iFd_t fn = (iFd_t)fcn; R_RAX=fn(emu->xmm[0].d[0]); } +void iFL(x64emu_t *emu, uintptr_t fcn) { iFL_t fn = (iFL_t)fcn; R_RAX=fn((uintptr_t)R_RDI); } void iFp(x64emu_t *emu, uintptr_t fcn) { iFp_t fn = (iFp_t)fcn; R_RAX=fn((void*)R_RDI); } void IFf(x64emu_t *emu, uintptr_t fcn) { IFf_t fn = (IFf_t)fcn; R_RAX=(uint64_t)fn(emu->xmm[0].f[0]); } void IFd(x64emu_t *emu, uintptr_t fcn) { IFd_t fn = (IFd_t)fcn; R_RAX=(uint64_t)fn(emu->xmm[0].d[0]); } void IFp(x64emu_t *emu, uintptr_t fcn) { IFp_t fn = (IFp_t)fcn; R_RAX=(uint64_t)fn((void*)R_RDI); } +void uFu(x64emu_t *emu, uintptr_t fcn) { uFu_t fn = (uFu_t)fcn; R_RAX=(uint32_t)fn((uint32_t)R_RDI); } void uFp(x64emu_t *emu, uintptr_t fcn) { uFp_t fn = (uFp_t)fcn; R_RAX=(uint32_t)fn((void*)R_RDI); } void UFV(x64emu_t *emu, uintptr_t fcn) { UFV_t fn = (UFV_t)fcn; R_RAX=fn((void*)(R_RSP + 8)); } void fFf(x64emu_t *emu, uintptr_t fcn) { fFf_t fn = (fFf_t)fcn; emu->xmm[0].f[0]=fn(emu->xmm[0].f[0]); } @@ -212,6 +218,7 @@ void pFpV(x64emu_t *emu, uintptr_t fcn) { pFpV_t fn = (pFpV_t)fcn; R_RAX=(uintpt void vFEpu(x64emu_t *emu, uintptr_t fcn) { vFEpu_t fn = (vFEpu_t)fcn; fn(emu, (void*)R_RDI, (uint32_t)R_RSI); } void vFfpp(x64emu_t *emu, uintptr_t fcn) { vFfpp_t fn = (vFfpp_t)fcn; fn(emu->xmm[0].f[0], (void*)R_RDI, (void*)R_RSI); } void vFdpp(x64emu_t *emu, uintptr_t fcn) { vFdpp_t fn = (vFdpp_t)fcn; fn(emu->xmm[0].d[0], (void*)R_RDI, (void*)R_RSI); } +void iFEpi(x64emu_t *emu, uintptr_t fcn) { iFEpi_t fn = (iFEpi_t)fcn; R_RAX=fn(emu, (void*)R_RDI, (int32_t)R_RSI); } void iFEpp(x64emu_t *emu, uintptr_t fcn) { iFEpp_t fn = (iFEpp_t)fcn; R_RAX=fn(emu, (void*)R_RDI, (void*)R_RSI); } void iFEpV(x64emu_t *emu, uintptr_t fcn) { iFEpV_t fn = (iFEpV_t)fcn; R_RAX=fn(emu, (void*)R_RDI, (void*)(R_RSP + 8)); } void iFpiu(x64emu_t *emu, uintptr_t fcn) { iFpiu_t fn = (iFpiu_t)fcn; R_RAX=fn((void*)R_RDI, (int32_t)R_RSI, (uint32_t)R_RDX); } @@ -221,6 +228,7 @@ void fFfff(x64emu_t *emu, uintptr_t fcn) { fFfff_t fn = (fFfff_t)fcn; emu->xmm[0 void fFffp(x64emu_t *emu, uintptr_t fcn) { fFffp_t fn = (fFffp_t)fcn; emu->xmm[0].f[0]=fn(emu->xmm[0].f[0], emu->xmm[1].f[0], (void*)R_RDI); } void dFddd(x64emu_t *emu, uintptr_t fcn) { dFddd_t fn = (dFddd_t)fcn; emu->xmm[0].d[0]=fn(emu->xmm[0].d[0], emu->xmm[1].d[0], emu->xmm[2].d[0]); } void dFddp(x64emu_t *emu, uintptr_t fcn) { dFddp_t fn = (dFddp_t)fcn; emu->xmm[0].d[0]=fn(emu->xmm[0].d[0], emu->xmm[1].d[0], (void*)R_RDI); } +void pFEip(x64emu_t *emu, uintptr_t fcn) { pFEip_t fn = (pFEip_t)fcn; R_RAX=(uintptr_t)fn(emu, (int32_t)R_RDI, (void*)R_RSI); } void pFEpi(x64emu_t *emu, uintptr_t fcn) { pFEpi_t fn = (pFEpi_t)fcn; R_RAX=(uintptr_t)fn(emu, (void*)R_RDI, (int32_t)R_RSI); } void pFEpp(x64emu_t *emu, uintptr_t fcn) { pFEpp_t fn = (pFEpp_t)fcn; R_RAX=(uintptr_t)fn(emu, (void*)R_RDI, (void*)R_RSI); } void pFipp(x64emu_t *emu, uintptr_t fcn) { pFipp_t fn = (pFipp_t)fcn; R_RAX=(uintptr_t)fn((int32_t)R_RDI, (void*)R_RSI, (void*)R_RDX); } diff --git a/src/wrapped/generated/wrapper.h b/src/wrapped/generated/wrapper.h index b134b8fb..9b59212d 100644 --- a/src/wrapped/generated/wrapper.h +++ b/src/wrapped/generated/wrapper.h @@ -40,10 +40,12 @@ void iFi(x64emu_t *emu, uintptr_t fnc); void iFu(x64emu_t *emu, uintptr_t fnc); void iFf(x64emu_t *emu, uintptr_t fnc); void iFd(x64emu_t *emu, uintptr_t fnc); +void iFL(x64emu_t *emu, uintptr_t fnc); void iFp(x64emu_t *emu, uintptr_t fnc); void IFf(x64emu_t *emu, uintptr_t fnc); void IFd(x64emu_t *emu, uintptr_t fnc); void IFp(x64emu_t *emu, uintptr_t fnc); +void uFu(x64emu_t *emu, uintptr_t fnc); void uFp(x64emu_t *emu, uintptr_t fnc); void UFV(x64emu_t *emu, uintptr_t fnc); void fFf(x64emu_t *emu, uintptr_t fnc); @@ -81,6 +83,7 @@ void pFpV(x64emu_t *emu, uintptr_t fnc); void vFEpu(x64emu_t *emu, uintptr_t fnc); void vFfpp(x64emu_t *emu, uintptr_t fnc); void vFdpp(x64emu_t *emu, uintptr_t fnc); +void iFEpi(x64emu_t *emu, uintptr_t fnc); void iFEpp(x64emu_t *emu, uintptr_t fnc); void iFEpV(x64emu_t *emu, uintptr_t fnc); void iFpiu(x64emu_t *emu, uintptr_t fnc); @@ -90,6 +93,7 @@ void fFfff(x64emu_t *emu, uintptr_t fnc); void fFffp(x64emu_t *emu, uintptr_t fnc); void dFddd(x64emu_t *emu, uintptr_t fnc); void dFddp(x64emu_t *emu, uintptr_t fnc); +void pFEip(x64emu_t *emu, uintptr_t fnc); void pFEpi(x64emu_t *emu, uintptr_t fnc); void pFEpp(x64emu_t *emu, uintptr_t fnc); void pFipp(x64emu_t *emu, uintptr_t fnc); diff --git a/src/wrapped/wrappedlibc.c b/src/wrapped/wrappedlibc.c index b86980e7..ed3c7982 100755 --- a/src/wrapped/wrappedlibc.c +++ b/src/wrapped/wrappedlibc.c @@ -559,7 +559,7 @@ EXPORT uint32_t my__ITM_RU4(const uint32_t * a) { printf("warning _ITM_RU4 calle EXPORT uint64_t my__ITM_RU8(const uint64_t * a) { printf("warning _ITM_RU8 called\n"); return 0; } EXPORT void my__ITM_memcpyRtWn(void * a, const void * b, size_t c) {printf("warning _ITM_memcpyRtWn called\n"); } EXPORT void my__ITM_memcpyRnWt(void * a, const void * b, size_t c) {printf("warning _ITM_memcpyRtWn called\n"); } -#if 0 + EXPORT void my_longjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p, int32_t __val); EXPORT void my__longjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p, int32_t __val) __attribute__((alias("my_longjmp"))); EXPORT void my_siglongjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p, int32_t __val) __attribute__((alias("my_longjmp"))); @@ -568,7 +568,6 @@ EXPORT void my___longjmp_chk(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/voi EXPORT int32_t my_setjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p); EXPORT int32_t my__setjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p) __attribute__((alias("my_setjmp"))); EXPORT int32_t my___sigsetjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p) __attribute__((alias("my_setjmp"))); -#endif EXPORT int my_printf(x64emu_t *emu, void* fmt, void* b) { myStackAlign(emu, (const char*)fmt, b, emu->scratch, R_EAX, 1); @@ -2090,35 +2089,39 @@ EXPORT void* my___deregister_frame_info(void* a) } EXPORT void* my____brk_addr = NULL; -#if 0 + // longjmp / setjmp -typedef struct jump_buff_i386_s { - uint32_t save_ebx; - uint32_t save_esi; - uint32_t save_edi; - uint32_t save_ebp; - uint32_t save_esp; - uint32_t save_eip; -} jump_buff_i386_t; +typedef struct jump_buff_x64_s { + uint64_t save_rbx; + uint64_t save_rbp; + uint64_t save_r12; + uint64_t save_r13; + uint64_t save_r14; + uint64_t save_r15; + uint64_t save_rsp; + uint64_t save_rip; +} jump_buff_x64_t; typedef struct __jmp_buf_tag_s { - jump_buff_i386_t __jmpbuf; + jump_buff_x64_t __jmpbuf; int __mask_was_saved; __sigset_t __saved_mask; } __jmp_buf_tag_t; void EXPORT my_longjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p, int32_t __val) { - jump_buff_i386_t *jpbuff = &((__jmp_buf_tag_t*)p)->__jmpbuf; + jump_buff_x64_t *jpbuff = &((__jmp_buf_tag_t*)p)->__jmpbuf; //restore regs - R_EBX = jpbuff->save_ebx; - R_ESI = jpbuff->save_esi; - R_EDI = jpbuff->save_edi; - R_EBP = jpbuff->save_ebp; - R_ESP = jpbuff->save_esp; + R_RBX = jpbuff->save_rbx; + R_RBP = jpbuff->save_rbp; + R_R12 = jpbuff->save_r12; + R_R13 = jpbuff->save_r13; + R_R14 = jpbuff->save_r14; + R_R15 = jpbuff->save_r15; + R_RSP = jpbuff->save_rsp; // jmp to saved location, plus restore val to eax R_EAX = __val; - R_EIP = jpbuff->save_eip; + R_RIP = jpbuff->save_rip; if(emu->quitonlongjmp) { emu->longjmp = 1; emu->quit = 1; @@ -2127,14 +2130,17 @@ void EXPORT my_longjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p, EXPORT int32_t my_setjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p) { - jump_buff_i386_t *jpbuff = &((__jmp_buf_tag_t*)p)->__jmpbuf; + jump_buff_x64_t *jpbuff = &((__jmp_buf_tag_t*)p)->__jmpbuf; // save the buffer - jpbuff->save_ebx = R_EBX; - jpbuff->save_esi = R_ESI; - jpbuff->save_edi = R_EDI; - jpbuff->save_ebp = R_EBP; - jpbuff->save_esp = R_ESP+4; // include "return address" - jpbuff->save_eip = *(uint32_t*)(R_ESP); + jpbuff->save_rbx = R_RBX; + jpbuff->save_rbp = R_RBP; + jpbuff->save_r12 = R_R12; + jpbuff->save_r13 = R_R13; + jpbuff->save_r14 = R_R14; + jpbuff->save_r15 = R_R15; + jpbuff->save_rsp = R_RSP; + jpbuff->save_rsp = R_RSP+sizeof(uintptr_t); // include "return address" + jpbuff->save_rip = *(uintptr_t*)(R_RSP); // and that's it.. Nothing more for now return 0; } @@ -2152,7 +2158,7 @@ EXPORT void* my_realpath(x64emu_t* emu, void* path, void* resolved_path) } return realpath(path, resolved_path); } - +#if 0 EXPORT void* my_mmap(x64emu_t* emu, void *addr, unsigned long length, int prot, int flags, int fd, int offset) { if(prot&PROT_WRITE) diff --git a/src/wrapped/wrappedlibc_private.h b/src/wrapped/wrappedlibc_private.h index 1451abbb..eaef9a7a 100755 --- a/src/wrapped/wrappedlibc_private.h +++ b/src/wrapped/wrappedlibc_private.h @@ -1605,8 +1605,8 @@ GOW(puts, iFp) //GO(sethostname, //GO(setipv4sourcefilter, //GOW(setitimer, -//GO(_setjmp, -//GO(setjmp, +GOM(_setjmp, iFEp) +GOM(setjmp, iFEp) //GO(setlinebuf, //GO(setlocale, //GO(setlogin, @@ -1686,8 +1686,8 @@ GOW(puts, iFp) //GOW(sigqueue, //GO(sigrelse, //GOW(sigreturn, -//GO(sigset, -//GO(__sigsetjmp, +GOM(sigset, pFEip) +GOM(__sigsetjmp, iFEp) //GOW(sigsetmask, //GO(sigstack, //GO(__sigsuspend, @@ -1697,7 +1697,7 @@ GOW(puts, iFp) //GO(sigvec, //GOW(sigwait, //GOW(sigwaitinfo, -//GOW(sleep, +GOW(sleep, uFu) //GO(__snprintf, //GOW(snprintf, //GO(__snprintf_chk, diff --git a/src/wrapped/wrappedlibpthread_private.h b/src/wrapped/wrappedlibpthread_private.h index 92989389..aeccee66 100755 --- a/src/wrapped/wrappedlibpthread_private.h +++ b/src/wrapped/wrappedlibpthread_private.h @@ -23,11 +23,11 @@ GOM(pthread_attr_destroy, iFEp) //GO(pthread_attr_getinheritsched, iFpp) //GO(pthread_attr_getschedparam, iFpp) //GO(pthread_attr_getschedpolicy, iFpp) -//GO(pthread_attr_getscope, iFpp) +GO(pthread_attr_getscope, iFpp) //GOM(pthread_attr_getstack, iFEppp) //GO(pthread_attr_getstackaddr, iFpp) //GO(pthread_attr_getstacksize, iFpp) -//GO(pthread_attr_init, iFp) +GO(pthread_attr_init, iFp) //GOM(pthread_attr_setaffinity_np, iFEpup) //GO(pthread_attr_setdetachstate, iFpi) //GO(pthread_attr_setguardsize, iFpL) @@ -45,24 +45,24 @@ GOM(pthread_attr_destroy, iFEp) //GO(pthread_barrier_destroy, iFp) //GO(pthread_barrier_init, iFppu) //GO(pthread_barrier_wait, iFp) -//GO(pthread_cancel, iFi) +GO(pthread_cancel, iFL) //GOM(_pthread_cleanup_pop, vFEpi) //GOM(_pthread_cleanup_pop_restore, vFEpi) //GOM(_pthread_cleanup_push, vFEppp) //GOM(_pthread_cleanup_push_defer, vFEppp) // __pthread_cleanup_routine -//GO(pthread_condattr_destroy, iFp) -//GO(pthread_condattr_getclock, iFpp) -//GO(pthread_condattr_getpshared, iFpp) -//GO(pthread_condattr_init, iFp) -//GO(pthread_condattr_setclock, iFpp) -//GO(pthread_condattr_setpshared, iFpi) -//GOM(pthread_cond_broadcast, iFEp) -//GOM(pthread_cond_destroy, iFEp) -//GOM(pthread_cond_init, iFEpp) -//GOM(pthread_cond_signal, iFEp) -//GOM(pthread_cond_timedwait, iFEppp) -//GOM(pthread_cond_wait, iFEpp) +GO(pthread_condattr_destroy, iFp) +GO(pthread_condattr_getclock, iFpp) +GO(pthread_condattr_getpshared, iFpp) +GO(pthread_condattr_init, iFp) +GO(pthread_condattr_setclock, iFpp) +GO(pthread_condattr_setpshared, iFpi) +GOM(pthread_cond_broadcast, iFEp) +GOM(pthread_cond_destroy, iFEp) +GOM(pthread_cond_init, iFEpp) +GOM(pthread_cond_signal, iFEp) +GOM(pthread_cond_timedwait, iFEppp) +GOM(pthread_cond_wait, iFEpp) GOM(pthread_create, iFEpppp) //GO(pthread_detach, iFu) GO(pthread_equal, iFLL) @@ -80,54 +80,54 @@ GO(pthread_equal, iFLL) GO(pthread_join, iFLp) GOM(__pthread_key_create, iFEpp) GOM(pthread_key_create, iFEpp) -GO(pthread_key_delete, iFu) -//GO(pthread_kill, iFEpi) +GO(pthread_key_delete, iFL) +GO(pthread_kill, iFEpi) // pthread_kill_other_threads_np -//GO(__pthread_mutexattr_destroy, iFp) -//GO(pthread_mutexattr_destroy, iFp) +GO(__pthread_mutexattr_destroy, iFp) +GO(pthread_mutexattr_destroy, iFp) // pthread_mutexattr_getprioceiling // pthread_mutexattr_getprotocol // pthread_mutexattr_getpshared // pthread_mutexattr_getrobust_np -//GO(pthread_mutexattr_gettype, iFpp) -//GO(__pthread_mutexattr_init, iFp) -//GO(pthread_mutexattr_init, iFp) +GO(pthread_mutexattr_gettype, iFpp) +GO(__pthread_mutexattr_init, iFp) +GO(pthread_mutexattr_init, iFp) // pthread_mutexattr_setprioceiling -//GO(pthread_mutexattr_setprotocol, iFpp) -//GO(pthread_mutexattr_setpshared, iFpi) +GO(pthread_mutexattr_setprotocol, iFpp) +GO(pthread_mutexattr_setpshared, iFpi) // pthread_mutexattr_setrobust_np -//GO(__pthread_mutexattr_settype, iFpi) -//GO(pthread_mutexattr_settype, iFpi) +GO(__pthread_mutexattr_settype, iFpi) +GO(pthread_mutexattr_settype, iFpi) // pthread_mutex_consistent_np -//GO(__pthread_mutex_destroy, iFp) -//GO(pthread_mutex_destroy, iFp) +GO(__pthread_mutex_destroy, iFp) +GO(pthread_mutex_destroy, iFp) // pthread_mutex_getprioceiling -//GO(__pthread_mutex_init, iFpp) -//GO(pthread_mutex_init, iFpp) -//GO(__pthread_mutex_lock, iFp) -//GO(pthread_mutex_lock, iFp) +GO(__pthread_mutex_init, iFpp) +GO(pthread_mutex_init, iFpp) +GO(__pthread_mutex_lock, iFp) +GO(pthread_mutex_lock, iFp) // pthread_mutex_setprioceiling -//GO(pthread_mutex_timedlock, iFpp) -//GO(__pthread_mutex_trylock, iFp) -//GO(pthread_mutex_trylock, iFp) -//GO(__pthread_mutex_unlock, iFp) -//GO(pthread_mutex_unlock, iFp) +GO(pthread_mutex_timedlock, iFpp) +GO(__pthread_mutex_trylock, iFp) +GO(pthread_mutex_trylock, iFp) +GO(__pthread_mutex_unlock, iFp) +GO(pthread_mutex_unlock, iFp) GOM(pthread_once, iFEpp) GOM(__pthread_once, iFEpp) -//GOM(__pthread_register_cancel, vFEp) +GOM(__pthread_register_cancel, vFEp) // __pthread_register_cancel_defer -//GO(pthread_rwlockattr_destroy, vFp) -//GO(pthread_rwlockattr_getkind_np, iFpp) +GO(pthread_rwlockattr_destroy, vFp) +GO(pthread_rwlockattr_getkind_np, iFpp) // pthread_rwlockattr_getpshared -//GO(pthread_rwlockattr_init, iFp) -//GO(pthread_rwlockattr_setkind_np, iFpi) +GO(pthread_rwlockattr_init, iFp) +GO(pthread_rwlockattr_setkind_np, iFpi) // pthread_rwlockattr_setpshared // __pthread_rwlock_destroy -//GO(pthread_rwlock_destroy, iFp) -//GO(__pthread_rwlock_init, iFpp) -//GO(pthread_rwlock_init, iFpp) -//GO(__pthread_rwlock_rdlock, iFp) -//GO2(pthread_rwlock_rdlock, iFp, __pthread_rwlock_rdlock) // not always defined +GO(pthread_rwlock_destroy, iFp) +GO(__pthread_rwlock_init, iFpp) +GO(pthread_rwlock_init, iFpp) +GO(__pthread_rwlock_rdlock, iFp) +GO(pthread_rwlock_rdlock, iFp) // pthread_rwlock_timedrdlock // pthread_rwlock_timedwrlock // __pthread_rwlock_tryrdlock @@ -157,11 +157,11 @@ GO(pthread_self, LFv) //GO(pthread_testcancel, vFv) //GO(pthread_timedjoin_np, iFppp) //GO(pthread_tryjoin_np, iFpp) -//GOM(__pthread_unregister_cancel, vFEp) +GOM(__pthread_unregister_cancel, vFEp) // __pthread_unregister_cancel_restore // __pthread_unwind -//GOM(__pthread_unwind_next, vFEp) -//GO(pthread_yield, iFv) +GOM(__pthread_unwind_next, vFEp) +GO(pthread_yield, iFv) // raise // __res_state //GO(sem_close, iFp) |