Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uk_init_tls: Initialize the TSD to the value expected by musl #82

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from

Conversation

shym
Copy link

@shym shym commented Oct 16, 2024

Even if the TSD has been initialized already, the first call to pthread_create will (re)set it to point to the statically-allocated __pthread_tsd_main (see (1)). So, before this patch, the memory allocated for the TSD leaked and, worse, the following sequence:

pthread_key_create(&key, NULL);
pthread_setspecific(key, value);
assert(pthread_getspecific(key) == value);
pthread_create(&th, NULL, &thrd, NULL);
assert(pthread_getspecific(key) == value);

ended up with the final assertion failing.

I’ve tested this example without and with the modification in CI:

(1) src/thread/pthread_create.c line 255 in musl v1.2.3

This was discovered investigating a segfault in the OCaml runtime, where some information is stored in the TSD in all cases, even for the initial thread. Accessing that information after a new thread has been created triggered the segfault.

Even if the TSD has been initialized already, the first call to
`pthread_create` will (re)set it to point to the statically-allocated
`__pthread_tsd_main` (see (1)). So, before this patch, the memory
allocated for the TSD leaked and, worse, the following sequence:

```
pthread_key_create(&key, NULL);
pthread_setspecific(key, value);
assert(pthread_getspecific(key) == value);
pthread_create(&th, NULL, &thrd, NULL);
assert(pthread_getspecific(key) == value);
```

ended up with the final assertion failing.

(1) src/thread/pthread_create.c line 255 in musl v1.2.3

Signed-off-by: Samuel Hym <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant