Calling `pthread_create` with a stack size of 2047 (or at the default) results in an "out of memory" error, but 2048 works

Aperta
#701 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
38/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
c, wasm

Direzione di ricerca

Inizia riproducendo il problema con i comandi main.c forniti in wasm32-wasip1-threads, confrontando le dimensioni dello stack 2047, 2048 e quella predefinita. Traccia l’uso di pthread_attr_setstacksize e PTHREAD_STACK_MIN, incluso il percorso degli attributi pthread predefiniti. Il lavoro è completo quando il comportamento della dimensione minima e dello stack predefinito è coerente con la semantica pthread prevista e la regressione è coperta da un test.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
C
Stelle
1k
Fork
268
Merge medio
1g 3h
PR unite (30g)
20

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di WebAssembly/wasi-libc

Tutte le issue di WebAssembly/wasi-libc

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.