Skip to content

Conversation

@shym
Copy link
Contributor

@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 <samuel@tarides.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant