about summary refs log tree commit diff stats
path: root/src/libtools
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2024-09-23 19:45:13 +0200
committerptitSeb <sebastien.chev@gmail.com>2024-09-23 19:45:13 +0200
commit7317472b5f62316e5817f9e89ae2ddf99862412d (patch)
treefa07f9df10da8400097c372289ec58094de5f6e2 /src/libtools
parent9271684e5a9f356056a6038f652ec1c15ed16ed2 (diff)
downloadbox64-7317472b5f62316e5817f9e89ae2ddf99862412d.tar.gz
box64-7317472b5f62316e5817f9e89ae2ddf99862412d.zip
[BOX32][WRAPPER] More 32bits wrapped functions, and a few fixes on threads handling
Diffstat (limited to 'src/libtools')
-rw-r--r--src/libtools/libc_net32.c30
-rw-r--r--src/libtools/threads.c15
-rwxr-xr-xsrc/libtools/threads32.c68
3 files changed, 74 insertions, 39 deletions
diff --git a/src/libtools/libc_net32.c b/src/libtools/libc_net32.c
index 14101aea..6bdbce82 100644
--- a/src/libtools/libc_net32.c
+++ b/src/libtools/libc_net32.c
@@ -174,3 +174,33 @@ EXPORT void* my32___h_errno_location(x64emu_t* emu)
     emu->libc_herr = h_errno;
     return &emu->libc_herr;
 }
+
+struct protoent_32
+{
+  ptr_t p_name; //char*
+  ptr_t p_aliases;// char**
+  int p_proto;
+};
+
+EXPORT void* my32_getprotobyname(x64emu_t* emu, void* name)
+{
+    static struct protoent_32 my_protoent = {0};
+    static ptr_t strings[256];
+    struct protoent *ret = getprotobyname(name);
+    if(!ret)
+        return NULL;
+    my_protoent.p_name = to_cstring(ret->p_name);
+    my_protoent.p_proto = ret->p_proto;
+    if(ret->p_aliases) {
+        my_protoent.p_aliases = to_ptrv(&strings);
+        int i = 0;
+        while(ret->p_aliases[i]) {
+            strings[i] = to_cstring(ret->p_aliases[i]);
+            ++i;
+        }
+        strings[i] = 0;
+    } else 
+        my_protoent.p_aliases = 0;
+
+    return &my_protoent;
+}
\ No newline at end of file
diff --git a/src/libtools/threads.c b/src/libtools/threads.c
index f0632154..6ca80bba 100644
--- a/src/libtools/threads.c
+++ b/src/libtools/threads.c
@@ -129,13 +129,13 @@ void my_longjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p, int32_t
 
 static pthread_key_t thread_key;
 
-static void emuthread_destroy(void* p)
+void emuthread_destroy(void* p)
 {
 	emuthread_t *et = (emuthread_t*)p;
 	if(!et)
 		return;
 	#ifdef BOX32
-	if(!et->join && et->fnc)
+	if(et->is32bits && !et->join && et->fnc)
 		to_hash_d(et->self);
 	#endif
 	FreeX64Emu(&et->emu);
@@ -180,6 +180,7 @@ void thread_set_emu(x64emu_t* emu)
 	et->emu->type = EMUTYPE_MAIN;
 	#ifdef BOX32
 	if(box64_is32bits) {
+		et->is32bits = 1;
 		et->self = (uintptr_t)pthread_self();
 		et->hself = to_hash(et->self);
 	}
@@ -213,6 +214,16 @@ x64emu_t* thread_get_emu()
 	return et->emu;
 }
 
+emuthread_t* thread_get_et()
+{
+	return (emuthread_t*)pthread_getspecific(thread_key);
+}
+
+void thread_set_et(emuthread_t* et)
+{
+	pthread_setspecific(thread_key, et);
+}
+
 static void* pthread_routine(void* p)
 {
 	// free current emuthread if it exist
diff --git a/src/libtools/threads32.c b/src/libtools/threads32.c
index 5cc808a0..4d2f8cfb 100755
--- a/src/libtools/threads32.c
+++ b/src/libtools/threads32.c
@@ -69,14 +69,19 @@ typedef struct __jmp_buf_tag_s {
     sigset_t         __saved_mask;
 } __jmp_buf_tag_t;
 
-typedef struct x64_unwind_buff_s {
+typedef struct i386_unwind_buff_s {
 	struct {
 		jump_buff_i386_t	__cancel_jmp_buf;	
 		int					__mask_was_saved;
 	} __cancel_jmp_buf[1];
 	ptr_t __pad[2];
 	void* __pad3;
-} x64_unwind_buff_t __attribute__((__aligned__));
+} i386_unwind_buff_t __attribute__((__aligned__));
+
+// those are define in thread.c
+emuthread_t* thread_get_et();
+void thread_set_et(emuthread_t* et);
+void emuthread_destroy(void* p);
 
 static pthread_attr_t* get_attr(void* attr);
 static void del_attr(void* attr);
@@ -93,22 +98,8 @@ void FreeStackSize(kh_threadstack_t* map, uintptr_t attr);
 void AddStackSize(kh_threadstack_t* map, uintptr_t attr, void* stack, size_t stacksize);
 int GetStackSize(x64emu_t* emu, uintptr_t attr, void** stack, size_t* stacksize);
 
-static pthread_key_t thread_key;
-
 void my32_longjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p, int32_t __val);
 
-static void emuthread_destroy(void* p)
-{
-	emuthread_t *et = (emuthread_t*)p;
-	if(!et)
-		return;
-	// destroy thread emu and all
-	if(!et->join && et->fnc)	// if there is no function, that this thread was not built from a create_thread, don't touch the hash...
-		to_hash_d(et->self);
-	FreeX64Emu(&et->emu);
-	free(et);
-}
-
 static void emuthread_cancel(void* p)
 {
 	emuthread_t *et = (emuthread_t*)p;
@@ -117,7 +108,7 @@ static void emuthread_cancel(void* p)
 	// check cancels threads
 	for(int i=et->cancel_size-1; i>=0; --i) {
 		et->emu->flags.quitonlongjmp = 0;
-		my32_longjmp(et->emu, ((x64_unwind_buff_t*)et->cancels[i])->__cancel_jmp_buf, 1);
+		my32_longjmp(et->emu, ((i386_unwind_buff_t*)et->cancels[i])->__cancel_jmp_buf, 1);
 		DynaRun(et->emu);	// will return after a __pthread_unwind_next()
 	}
 	free(et->cancels);
@@ -129,16 +120,16 @@ static void* pthread_routine(void* p)
 {
 	// free current emuthread if it exist
 	{
-		void* t = pthread_getspecific(thread_key);
+		void* t = thread_get_et();
 		if(t) {
 			// not sure how this could happens
 			printf_log(LOG_INFO, "Clean of an existing ET for Thread %04d\n", GetTID());
 			emuthread_destroy(t);
 		}
 	}
-	pthread_setspecific(thread_key, p);
 	// call the function
 	emuthread_t *et = (emuthread_t*)p;
+	thread_set_et(et);
 	et->emu->type = EMUTYPE_MAIN;
 	et->self = (uintptr_t)pthread_self();
 	et->hself = to_hash(et->self);
@@ -244,7 +235,7 @@ EXPORT int my32_pthread_create(x64emu_t *emu, void* t, void* attr, void* start_r
 EXPORT int my32_pthread_detach(x64emu_t* emu, pthread_t p)
 {
 	if(pthread_equal(p ,pthread_self())) {
-		emuthread_t *et = (emuthread_t*)pthread_getspecific(thread_key);
+		emuthread_t *et = (emuthread_t*)thread_get_et();
 		et->join = 0;
 	}
 	return pthread_detach(p);
@@ -274,24 +265,24 @@ void* my32_prepare_thread(x64emu_t *emu, void* f, void* arg, int ssize, void** p
 
 void my32_longjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p, int32_t __val);
 
-EXPORT void my32___pthread_register_cancel(x64emu_t* emu, x64_unwind_buff_t* buff)
+EXPORT void my32___pthread_register_cancel(x64emu_t* emu, i386_unwind_buff_t* buff)
 {
-	buff = (x64_unwind_buff_t*)from_ptr(R_EAX);	// param is in fact on register
-	emuthread_t *et = (emuthread_t*)pthread_getspecific(thread_key);
+	buff = (i386_unwind_buff_t*)from_ptr(R_EAX);	// param is in fact on register
+	emuthread_t *et = (emuthread_t*)thread_get_et();
 	if(et->cancel_cap == et->cancel_size) {
 		et->cancel_cap+=8;
-		et->cancels = realloc(et->cancels, sizeof(x64_unwind_buff_t*)*et->cancel_cap);
+		et->cancels = realloc(et->cancels, sizeof(i386_unwind_buff_t*)*et->cancel_cap);
 	}
 	et->cancels[et->cancel_size++] = buff;
 }
 
-EXPORT void my32___pthread_unregister_cancel(x64emu_t* emu, x64_unwind_buff_t* buff)
+EXPORT void my32___pthread_unregister_cancel(x64emu_t* emu, i386_unwind_buff_t* buff)
 {
-	emuthread_t *et = (emuthread_t*)pthread_getspecific(thread_key);
+	emuthread_t *et = (emuthread_t*)thread_get_et();
 	for (int i=et->cancel_size-1; i>=0; --i) {
 		if(et->cancels[i] == buff) {
 			if(i!=et->cancel_size-1)
-				memmove(et->cancels+i, et->cancels+i+1, sizeof(x64_unwind_buff_t*)*(et->cancel_size-i-1));
+				memmove(et->cancels+i, et->cancels+i+1, sizeof(i386_unwind_buff_t*)*(et->cancel_size-i-1));
 			et->cancel_size--;
 		}
 	}
@@ -364,9 +355,9 @@ GO(29)
 
 // cleanup_routine
 #define GO(A)   \
-static uintptr_t my32_cleanup_routine_fct_##A = 0;  						\
-static void my32_cleanup_routine_##A(void* a)    							\
-{                                       								\
+static uintptr_t my32_cleanup_routine_fct_##A = 0;  				\
+static void my32_cleanup_routine_##A(void* a)    					\
+{                                       							\
     RunFunctionFmt(my32_cleanup_routine_fct_##A, "p", to_ptrv(a));	\
 }
 SUPER()
@@ -387,9 +378,9 @@ static void* findcleanup_routineFct(void* fct)
 
 // key_destructor
 #define GO(A)   \
-static uintptr_t my32_key_destructor_fct_##A = 0;  						\
-static void my32_key_destructor_##A(void* a)    							\
-{                                       								\
+static uintptr_t my32_key_destructor_fct_##A = 0;  					\
+static void my32_key_destructor_##A(void* a)    					\
+{                                       							\
     RunFunctionFmt(my32_key_destructor_fct_##A, "p", to_ptrv(a));	\
 }
 SUPER()
@@ -639,6 +630,11 @@ EXPORT int my32_pthread_attr_init(x64emu_t* emu, void* attr)
 	return pthread_attr_init(get_attr(attr));
 }
 
+EXPORT int my32_pthread_getattr_np(x64emu_t* emu, uintptr_t th, void* attr)
+{
+	return pthread_getattr_np(th, get_attr(attr));
+}
+
 EXPORT int my32_pthread_attr_getdetachstate(x64emu_t* emu, void* attr, void* p)
 {
 	return pthread_attr_getdetachstate(get_attr(attr), p);
@@ -920,16 +916,14 @@ void init_pthread_helper_32()
 
 	mapcond = kh_init(mapcond);
 	unaligned_mutex = kh_init(mutex);
-	pthread_key_create(&thread_key, emuthread_destroy);
-	pthread_setspecific(thread_key, NULL);
 }
 
 void clean_current_emuthread_32()
 {
-	emuthread_t *et = (emuthread_t*)pthread_getspecific(thread_key);
+	emuthread_t *et = (emuthread_t*)thread_get_et();
 	if(et) {
 		emuthread_destroy(et);
-		pthread_setspecific(thread_key, NULL);
+		thread_set_et(NULL);
 	}
 }