Skip to content

Commit 2f4514f

Browse files
loganekjohn-sharratt
authored andcommitted
threads: Retrieve default stack size from __heap_base/__data_end (WebAssembly#350)
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.
1 parent 0a2420c commit 2f4514f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

expected/wasm32-wasi-pthread/undefined-symbols.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
__addtf3
2+
__data_end
23
__divtf3
34
__eqtf2
45
__extenddftf2
@@ -9,6 +10,7 @@ __fixunstfsi
910
__floatsitf
1011
__floatunsitf
1112
__getf2
13+
__global_base
1214
__gttf2
1315
__heap_base
1416
__imported_wasix_32v1_args_get

libc-top-half/musl/src/env/__init_tls.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,43 @@
2020
volatile int __thread_list_lock;
2121

2222
#ifndef __wasilibc_unmodified_upstream
23+
24+
/* These symbols are generated by wasm-ld. __stack_high/__stack_low
25+
* symbols are only available in LLVM v16 and higher, therefore they're
26+
* defined as weak symbols and if not available, __heap_base/__data_end
27+
* is used instead.
28+
*
29+
* TODO: remove usage of __heap_base/__data_end for stack size calculation
30+
* once we drop support for LLVM v15 and older.
31+
*/
32+
extern unsigned char __heap_base;
33+
extern unsigned char __data_end;
34+
extern unsigned char __global_base;
35+
extern weak unsigned char __stack_high;
36+
extern weak unsigned char __stack_low;
37+
38+
static inline void setup_default_stack_size()
39+
{
40+
ptrdiff_t stack_size;
41+
42+
if (&__stack_high)
43+
stack_size = &__stack_high - &__stack_low;
44+
else {
45+
unsigned char *sp;
46+
__asm__(
47+
".globaltype __stack_pointer, i32\n"
48+
"global.get __stack_pointer\n"
49+
"local.set %0\n"
50+
: "=r"(sp));
51+
stack_size = sp > &__global_base ? &__heap_base - &__data_end : (ptrdiff_t)&__global_base;
52+
}
53+
54+
if (stack_size > __default_stacksize)
55+
__default_stacksize =
56+
stack_size < DEFAULT_STACK_MAX ?
57+
stack_size : DEFAULT_STACK_MAX;
58+
}
59+
2360
void __wasi_init_tp() {
2461
__init_tp((void *)__get_tp());
2562
}
@@ -35,6 +72,8 @@ int __init_tp(void *p)
3572
if (!r) libc.can_do_threads = 1;
3673
td->detach_state = DT_JOINABLE;
3774
td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock);
75+
#else
76+
setup_default_stack_size();
3877
#endif
3978
td->locale = &libc.global_locale;
4079
td->robust_list.head = &td->robust_list.head;

0 commit comments

Comments
 (0)