]> xenbits.xensource.com Git - unikraft/libs/musl.git/commitdiff
Initialize pthread structure fields for non-pthread threads
authorMarco Schlumpp <marco@unikraft.io>
Tue, 18 Apr 2023 12:55:18 +0000 (14:55 +0200)
committerUnikraft <monkey@unikraft.io>
Thu, 4 May 2023 14:30:06 +0000 (14:30 +0000)
If these fields are not initialized as expected by musl, then calls
to pthread functions such `pthread_getspecific` can return unexpected
values or even crash.

Signed-off-by: Marco Schlumpp <marco@unikraft.io>
Reviewed-by: Eduard Vintilă <eduard.vintila47@gmail.com>
Reviewed-by: Sergiu Moga <sergiu.moga@protonmail.com>
Approved-by: Razvan Deaconescu <razvand@unikraft.io>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Closes: #41

__uk_init_tls.c

index 01ff96d185c7457eef212e1a54c8c6459dfc654e..68e0fc5ded7d00f18381ef1370fb5dc59f997f04 100644 (file)
@@ -176,6 +176,7 @@ static const size_t __uk_tsd_size = sizeof(void *) * PTHREAD_KEYS_MAX;
  */
 int uk_thread_uktcb_init(struct uk_thread *thread, void *tcb)
 {
+       struct pthread *self = pthread_self();
        struct pthread *td = (struct pthread *) tcb;
 
        uk_pr_debug("%s uk_thread %p, tcb %p\n", __func__, thread, tcb);
@@ -187,7 +188,15 @@ int uk_thread_uktcb_init(struct uk_thread *thread, void *tcb)
                uk_alloc_get_default(),
                __PAGE_SIZE,
                __uk_tsd_size);
+       /* musl expects that the tsd area is zero-initialized and will not
+        * zero the pointers on pthread_key_create.
+        */
+       memset(td->tsd, 0, __uk_tsd_size);
        td->locale = &libc.global_locale;
+       td->next = self->next;
+       td->prev = self;
+       td->next->prev = td;
+       td->prev->next = td;
 
        return 0;
 }