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

Initialize TLS of the main thread #372

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions libc-top-half/musl/src/env/__init_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <limits.h>
#ifdef __wasilibc_unmodified_upstream
#include <sys/mman.h>
#else
#include <stdlib.h>
#endif
#include <string.h>
#include <stddef.h>
Expand Down Expand Up @@ -53,7 +55,29 @@ static inline void setup_default_stack_size()
stack_size : DEFAULT_STACK_MAX;
}

static uintptr_t round_up(uintptr_t p, uintptr_t align)
{
return (p + align - 1) & (-align);
}

extern void __wasm_init_tls(void*);

static void __wasi_init_tls() {
size_t size = __builtin_wasm_tls_size();
if (size == 0) {
return;
}
size_t align = __builtin_wasm_tls_align();
size = round_up(size, align);
void *tls = aligned_alloc(align, size);
if (tls == NULL) {
a_crash();
}
__wasm_init_tls(tls);
}

void __wasi_init_tp() {
__wasi_init_tls();
__init_tp((void *)__get_tp());
}
#endif
Expand Down Expand Up @@ -90,10 +114,6 @@ static struct builtin_tls {
static struct tls_module main_tls;
#endif

#ifndef __wasilibc_unmodified_upstream
extern void __wasm_init_tls(void*);
#endif

void *__copy_tls(unsigned char *mem)
{
#ifdef __wasilibc_unmodified_upstream
Expand Down