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

オープン
#701 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
38/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
c, wasm

調査の方向性

まず、wasm32-wasip1-threads 上で提供された main.c のコマンドを使って問題を再現し、スタックサイズ 2047、2048、およびデフォルトを比較します。デフォルトの pthread 属性の経路を含め、pthread_attr_setstacksize と PTHREAD_STACK_MIN がどのように使われているかを追跡します。最小サイズとデフォルトスタックの動作が意図された pthread セマンティクスと一致し、回帰がテストでカバーされれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
C
スター
1k
フォーク
268
平均マージ
1日 3時間
マージ済み PR(30日)
20

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

WebAssembly/wasi-libc のほかの issue

WebAssembly/wasi-libc の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。