Skip to content

Commit 957c711

Browse files
authoredDec 19, 2022
threads: Retrieve default stack size from __heap_base/__data_end (#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 dfad6fe commit 957c711

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
 

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

+2
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_wasi_snapshot_preview1_args_get

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

+39
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,43 @@
1616
volatile int __thread_list_lock;
1717

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

0 commit comments

Comments
 (0)
Please sign in to comment.