dotnet / dotnet-wasi-sdk

An SDK for building .NET projects as standalone WASI-compliant modules
MIT License
279 stars 12 forks source link

'RangeError: Maximum call stack size exceeded' when using generated .wasm module with node.js #18

Closed ChristianWeyer closed 1 year ago

ChristianWeyer commented 1 year ago

Compiled a very simple hello world to .wasm:

using System.Runtime.InteropServices;

Console.WriteLine("Hello, World!");
Console.WriteLine($"Running on {RuntimeInformation.OSArchitecture}"); 

Then used a node.js module to host the .wasm module:

import { readFile } from 'node:fs/promises';
import { WASI } from 'node:wasi';
import { argv, env } from 'node:process';

const wasi = new WASI({
    version: 'preview1',
    args: argv,
    env,
    preopens: {
        '/': './'
    },
});

const importObject = { wasi_snapshot_preview1: wasi.wasiImport };

const wasm = await WebAssembly.compile(
    await readFile(new URL('./HelloWASI.wasm', import.meta.url)),
);
const instance = await WebAssembly.instantiate(wasm, importObject);

wasi.start(instance);

Executing this with node --experimental-wasi-unstable-preview1 --no-warnings wasi-host.mjs gives the following error:

wasm://wasm/021e4d4e:1

RangeError: Maximum call stack size exceeded
    at wasm://wasm/021e4d4e:wasm-function[176]:0x50df1
    at wasm://wasm/021e4d4e:wasm-function[94]:0x357c0
    at wasm://wasm/021e4d4e:wasm-function[76]:0x15e2a
    at wasm://wasm/021e4d4e:wasm-function[186]:0x52131
    at wasm://wasm/021e4d4e:wasm-function[94]:0x37241
    at wasm://wasm/021e4d4e:wasm-function[76]:0x15e2a
    at wasm://wasm/021e4d4e:wasm-function[186]:0x52131
    at wasm://wasm/021e4d4e:wasm-function[94]:0x37241
    at wasm://wasm/021e4d4e:wasm-function[76]:0x15e2a
    at wasm://wasm/021e4d4e:wasm-function[186]:0x52131

Any idea what could be the issue here? .cc @SteveSandersonMS


.NET 7.0 Wasi.Sdk 0.1.4-preview.10020 nodejs 19.8.1 macOS 13.3.1 Apple ARM M1

SteveSandersonMS commented 1 year ago

With Wasmtime and most other WebAssembly runners you can specify an arbitrary stack size. I'm not sure if Node.js has an API for that.

Another thing to try is running wasm-opt -Oz yourmodule.wasm -o yourmodule.wasm and see if the optimized version has lower stack size requirements (https://www.npmjs.com/package/wasm-opt).

ChristianWeyer commented 1 year ago

Hey Steve,

that did it - but I had to add --enable-bulk-memory:

wasm-opt -Oz --enable-bulk-memory HelloWASI.wasm -o HelloWASI-o.wasm

Thanks!