WebAssembly / WebAssembly/wasi-libc
Calling `pthread_create` with a stack size of 2047 (or at the default) results in an "out of memory" error, but 2048 works
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 1k
- Forks
- 251
- Avg merge
- 7h 15m
- Merged PRs (30d)
- 3
Description
Here's my example program:
#include <pthread.h>
#include <stdio.h>
void *thread_func(void *arg) {
printf("The thread is running!\n");
return NULL;
}
int main() {
printf("Starting\n");
pthread_t thread;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 2047); // <- 2048 (and above) works, 2047 (and below) does NOT work
if (pthread_create(&thread, &attr, thread_func, NULL) != 0) {
perror("pthread_create");
return 1;
}
pthread_join(thread, NULL);
printf("Done\n");
return 0;
}
Here's main.c as a WAT file: main.wat.txt
I'm compiling and running with the following commands:
# With a stack size of 2048 or greater
$ /opt/homebrew/opt/llvm/bin/clang -Wl,--import-memory,--export-memory -g --target=wasm32-wasip1-threads --sysroot=~/Downloads/wasi-sdk-29.0-arm64-macos/share/wasi-sysroot main.c -o main.wasm
$ wasmtime run --wasi threads=y --wasm threads=y main.wasm
Starting
The thread is running!
Done
# With a stack size of 2047 or less
$ /opt/homebrew/opt/llvm/bin/clang -Wl,--import-memory,--export-memory -g --target=wasm32-wasip1-threads --sysroot=~/Downloads/wasi-sdk-29.0-arm64-macos/share/wasi-sysroot main.c -o main.wasm
$ wasmtime run --wasi threads=y --wasm threads=y main.wasm
Starting
pthread_create: Out of memory
It also fails if you leave the stack size as the default.
Platform: I'm using an macOS 14.6.1 on an M1, with Homebrew Clang, a pre-built WASI sysroot, and wasm32-wasip1-threads, though it also repros with wasm32-wasi-threads.
Looking through the code, I see a PTHREAD_STACK_MIN config variable set to 2048 which.. would seem to explain this behavior, though I can't find any place outside of pthread_attr_setstack where it ever gets read. It also seems strange that the default behavior of pthread would result in a stack that's too small.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the issue with the supplied main.c commands under wasm32-wasip1-threads, comparing stack sizes of 2047, 2048, and the default. Trace how pthread_attr_setstacksize and PTHREAD_STACK_MIN are used, including the default pthread attributes path. Done means the minimum-size and default-stack behavior is consistent with the intended pthread semantics and the regression is covered by a test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, wasm
- Domain
- operating-systems
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100