Skip to content

Commit

Permalink
threads: Retrieve default stack size from __heap_base/__data_end (#350)
Browse files Browse the repository at this point in the history
When compiling with `-z stack-size` flag, only the main thread's stack
size is set to the specified value and other threads use musl's default value.
That's inconsistent with LLD's `-Wl,-stack_size`.

I think we can make it similar to MUSL's behavior, where thread's stack
size can be set via `PT_GNU_STACK` program header (via `-Wl,-z,stack-size`
flag).

Configuring stack size through `pthread_attr_t` still work as expected and
overrides the defaults ([pthread_create.c](https://github.com/WebAssembly/wasi-libc/blob/be1ffd6a9eba1704085987482557c2a32724227f/libc-top-half/musl/src/thread/pthread_create.c#L362))
default settings.
  • Loading branch information
loganek committed Dec 19, 2022
1 parent dfad6fe commit 957c711
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions expected/wasm32-wasi-pthread/undefined-symbols.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__addtf3
__data_end
__divtf3
__eqtf2
__extenddftf2
Expand All @@ -9,6 +10,7 @@ __fixunstfsi
__floatsitf
__floatunsitf
__getf2
__global_base
__gttf2
__heap_base
__imported_wasi_snapshot_preview1_args_get
Expand Down
39 changes: 39 additions & 0 deletions libc-top-half/musl/src/env/__init_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,43 @@
volatile int __thread_list_lock;

#ifndef __wasilibc_unmodified_upstream

/* These symbols are generated by wasm-ld. __stack_high/__stack_low
* symbols are only available in LLVM v16 and higher, therefore they're
* defined as weak symbols and if not available, __heap_base/__data_end
* is used instead.
*
* TODO: remove usage of __heap_base/__data_end for stack size calculation
* once we drop support for LLVM v15 and older.
*/
extern unsigned char __heap_base;
extern unsigned char __data_end;
extern unsigned char __global_base;
extern weak unsigned char __stack_high;
extern weak unsigned char __stack_low;

static inline void setup_default_stack_size()
{
ptrdiff_t stack_size;

if (&__stack_high)
stack_size = &__stack_high - &__stack_low;
else {
unsigned char *sp;
__asm__(
".globaltype __stack_pointer, i32\n"
"global.get __stack_pointer\n"
"local.set %0\n"
: "=r"(sp));
stack_size = sp > &__global_base ? &__heap_base - &__data_end : (ptrdiff_t)&__global_base;
}

if (stack_size > __default_stacksize)
__default_stacksize =
stack_size < DEFAULT_STACK_MAX ?
stack_size : DEFAULT_STACK_MAX;
}

void __wasi_init_tp() {
__init_tp((void *)__get_tp());
}
Expand All @@ -31,6 +68,8 @@ int __init_tp(void *p)
if (!r) libc.can_do_threads = 1;
td->detach_state = DT_JOINABLE;
td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock);
#else
setup_default_stack_size();
#endif
td->locale = &libc.global_locale;
td->robust_list.head = &td->robust_list.head;
Expand Down

0 comments on commit 957c711

Please sign in to comment.