]> xenbits.xensource.com Git - unikraft/libs/musl.git/commitdiff
Avoid `uk_syscall_set_tid_address()` for main thread
authorDragos Iulian Argint <dragosargint21@gmail.com>
Mon, 28 Nov 2022 20:19:24 +0000 (22:19 +0200)
committerUnikraft <monkey@unikraft.io>
Tue, 29 Nov 2022 20:07:08 +0000 (20:07 +0000)
The uk_syscall_set_tid_address call should be used
to set the main thread's tid address within some
internal structures. At the moment, because it is
called before the initialization of the scheduler,
it fails causing the program to crash. A workaround
for this behavior is to set the tid to 0 if the
`__uk_sched_thread_current` variable is NULL, which
means that the scheduler is not initialized.

Signed-off-by: Dragos Iulian Argint <dragosargint21@gmail.com>
Reviewed-by: Razvan Deaconescu <razvand@unikraft.io>
Approved-by: Simon Kuenzer <simon@unikraft.io>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Closes: #29

__uk_init_tls.c

index 552d2b6b9573f19d8a1b35cb2a9b3c4a243842e5..df13b955eabb1185f481f41e753521b768da422a 100644 (file)
@@ -118,14 +118,26 @@ static int __uk_init_tp(void *p)
        /*
         * The original musl code will invoke here a `SYS_set_tid_address`
         * syscall, to set the tid user space address in the Kernel.
-        * FIXME: Currently this does not return the tid assigned for the caller,
-        * it returns an error code (-95, -ENOTSUP) because posix_process_init has not been
-        * called at this stage, but will be called via uk_late_initcall.
+        * FIXME: Currently this can fail in two ways. The first way is a
+        * crash, which we avoid by checking if the current thread is NULL.
+        * The reason why we introduced this check is that the scheduler is not
+        * initialized when this function gets executed.
+        *`uk_syscall_r_set_tid_address()` will call `uk_syscall_r_gettid()`
+        * which may use the current uk_thread. If the uk_thread is NULL then
+        * it will crash when trying to access an invalid address.
+        * The second way it can fail is by returning an invalid tid. The call
+        * can return an error code (-95, -ENOTSUP) because posix_process_init
+        * has not been called at this stage, but will be called via uk_late_initcall.
         * It is not a really big problem right now. Since this is the main thread,
         * nobody should ever wait for it, and we can just assume thread id 0.
+        * The workaround for the moment is to set the tid to 0 whenever an error
+        * might happen.
         */
-       td->tid = uk_syscall_r_set_tid_address(&td->tid);
-       if (td->tid < 0) {
+       if (uk_thread_current()) {
+               td->tid = uk_syscall_r_set_tid_address(&td->tid);
+               if (td->tid < 0)
+                       td->tid = 0;
+       } else {
                td->tid = 0;
        }
        td->locale = &libc.global_locale;