]> xenbits.xensource.com Git - unikraft/libs/musl.git/commitdiff
Initialize main thread pthread structure
authorAleksandr Iashchenko <Aleksandr.Iashchenko@opensynergy.com>
Thu, 29 Jun 2023 15:31:59 +0000 (17:31 +0200)
committerUnikraft <monkey@unikraft.io>
Thu, 10 Aug 2023 18:42:03 +0000 (18:42 +0000)
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 <Aleksandr.Iashchenko@opensynergy.com>
Reviewed-by: Simon Kuenzer <simon@unikraft.io>
Reviewed-by: Andrei Tatar <andrei@unikraft.io>
Reviewed-by: Robert Kuban <robert.kuban@opensynergy.com>
Approved-by: Razvan Deaconescu <razvand@unikraft.io>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Closes: #59

__uk_init_tls.c

index 5f4f2c6c1a3cf98e0cc8722b59eee1409f5ef0d2..21a94f874a3ae81a1a3cf765bb351ef851ed016a 100644 (file)
@@ -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