---
# System prepended metadata

title: 'Musl, July 25, 2022'
tags: [' musl', ' clone', unikraft]

---

---
title: Musl, July 25, 2022
tags: unikraft, musl, clone
datetime: 2022-07-25T10:00:00+02:00
location: Online, Discord (https://bit.ly/UnikraftDiscord), the `#monkey-business` voice channel
teams:
- TODO
participants:
- TODO
---

## :dart: Agenda

- clone() status
- TLS status
- next steps

## :closed_book: Discussions

DA: Another tls (aka `uktls`) is still allocated beside the one that musl creates and passes it as a pointer to `clone()`.
This shouldn't happen.

SK: You are right, this thing needs to be changed.
The tricky part is that when we use `clone()` in the binary compatibility mode, we need this extra tls, while when we use musl and use a function call instead of a binary system call we don't need it.
Couldn't we make it use only the tls allocated by unikraft?

DA: We could, that's the way I thought about it initially.
The problem is that it will be a little cumbersome.
Clone expects some things to be initialised in the `pthread` structure that is part of this tls that musl allocates.
If we are going to use the callbacks like `uktcb_init()` this initialization would happen after the `clone()` is called, and where do you get the info that you need to fillup in the `pthread` structure, such the function that the user provides and the argument for it.
If we have access to the child's stack in this callback, we can place some pointers onto the stack before `clone()` is called and from there we can `memcpy()`, but this will not be easy and will need further changes.

SK: Right, the cleanest way is to get rid of the extra tls. But I need to know in the `clone()` implementation if it was reached by a binary system call or by a function call. When we'll reach `clone()` via a function call we know that musl is used, not binary compatibility mode and we can get rid of the extra tls.

DA: There are also a few changes that correct some errors that I suggest:

1. Posix_Futex: `uk_thread_wake()` vs `uk_thread_wakeup()`

1. In `pprocess_parent_setid()` and `pprocess_child_setid()` the call to `ukthread2pid()` needs to be changed to `ukthread2tid()`. 

1. In the implementation of clone:

   ```
       static int _clone()
       {
           ....
           uk_sched_yield()
           /*
            * Since the child will execute from here, and
            * no preemption will take place, this will surely result in
            * receiving a wrong value for the tid.
            */
           return ukthread2tid(child); // Use after free
       }
       | | | | | | | | | | | | |
       V V V V V V V V V V V V V
       static int _clone()
       {
           ctid = ukthread2tid(child)
           ....
           uk_sched_yield()
   
           return ctid;
       }
   ```
1. Setting the Child tid to 0 doesn't happen when the thread exits.
   This must happen as a result of registering the callback for the `CLONE_CHILD_CLEARTID` flag.
   Moreover, when giving this flag from musl the `clone()` syscall returns `-95` (errno:95 => Operation not supported) and the creation of the child thread doesn't happen.

## :wrench: TODOs and Decisions

SK: Look into DA's suggestion and solve bugs.

DA: Look into the way we initialise musl without the use of `__libc_start_main()`
