C++, no exceptions, and OOM in `operator new`

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

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

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

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
cpp, wasm

調査の方向性

提供された C++ の例、clang++ コマンド、wasmtime の呼び出しで失敗を再現します。まず -fno-exceptions 下での operator new と __cxa_throw との相互作用を追跡し、次に WebAssembly に対する標準準拠の失敗動作を決定します。OOM ケースが無限ループしなくなり、制限されたメモリの例を対象とするリグレッションテストがあれば完了です。

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

説明

This example program is intended to allocate 4 wasm pages of memory:

#include <vector>
#include <stdio.h>

int main() {
  std::vector<int> v;
  for (int i = 0; i < 65536; i++) {
    v.push_back(4);
  }
}

This can be compiled to have a max wasm memory size of 2 pages, though, which will cause the program to OOM at runtime:

$ clang++ foo.cpp -o foo.wasm -O2 -Wl,--stack-first,--max-memory=131072 -fno-exceptions

which produces foo.wasm.gz.

When run:

$ wasmtime run ./foo.wasm

this program will (currently for me) infinite loop.

The issue seems to be that when compiled with -fno-exceptions, which I believe is currently required of C++ code compiled with wasi-libc to wasm since __cxa_throw isn't otherwise defined, then the operator new operator is defined to return NULL for failed allocations. For a native platform this is fine because it probably faults quickly when accessing this address. For Wasm, however, this is a valid address so the program keeps going.

This was, for me, an unfortunately slow way for me to discover that the original program I was working with was OOM-ing and needed a --max-memory argument to be passed that was larger than the base. I'm not sure how best to improve this, though, since I suspect aborting on failure would not be spec-compliant with C++.

主要言語
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 を短くまとめたダイジェスト。