From: Aleksandr Iashchenko Date: Thu, 29 Jun 2023 15:31:59 +0000 (+0200) Subject: Initialize main thread pthread structure X-Git-Tag: RELEASE-0.14.0~2 X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=46ae9e33aa03afd54a9d2e2712e74b060a12ff92;p=unikraft%2Flibs%2Fmusl.git Initialize main thread pthread structure It seems some steps were missing in pthread structure initialization. This patch does the following: - zeroes pthread memory (previously potentially contained garbage after uk_mem_align allocation) - initializes tsd - sets detach_state to joinable Signed-off-by: Aleksandr Iashchenko Reviewed-by: Simon Kuenzer Reviewed-by: Andrei Tatar Reviewed-by: Robert Kuban Approved-by: Razvan Deaconescu Tested-by: Unikraft CI GitHub-Closes: #59 --- diff --git a/__uk_init_tls.c b/__uk_init_tls.c index 5f4f2c6..21a94f8 100644 --- a/__uk_init_tls.c +++ b/__uk_init_tls.c @@ -84,6 +84,7 @@ * map + size ---------------------------------------------------------- */ +static const size_t __uk_tsd_size = sizeof(void *) * PTHREAD_KEYS_MAX; void *__uk_copy_tls(unsigned char *mem) { @@ -107,6 +108,28 @@ static int __uk_init_tp(void *p) { pthread_t td = p; + /* + * Clean and initialize pthread structure for init thread. + * FIXME: Some fields from pthread are not initialized yet: + * - information about stack + * - canary + */ + memset(td, 0, sizeof(*td)); + + td->tsd = (void *)uk_memalign(uk_alloc_get_default(), __PAGE_SIZE, + __uk_tsd_size); + if (!td->tsd) + UK_CRASH("Failed to initialize init thread tsd\n"); + + memset(td->tsd, 0, __uk_tsd_size); + + /* + * The initial thread in the new image shall be joinable, as if + * created with the detachstate attribute set to + * PTHREAD_CREATE_JOINABLE. + */ + td->detach_state = DT_JOINABLE; + /* Musl maintains a circular doubly linked list for threads. */ td->self = td->next = td->prev = td; /* @@ -170,8 +193,6 @@ static void __uk_init_libc(void) libc.page_size = __PAGE_SIZE; } -static const size_t __uk_tsd_size = sizeof(void *) * PTHREAD_KEYS_MAX; - /* * This callback will only be called for threads that are NOT * created with the pthread API but we still want them to be