You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] Segfault on combo of global ctors, ostream, wasi #33175
This is an issue for node's WASI integration with emscripten.
both global ctors need to be called, and ostream being used in the WASM standalone mode (emsdk latest upstream).
I have a case where I need to call the global static initializers(need to call _start before running other functions), but also have ostream in the code.
What was happening is that when ostream is being used in the code and it somehow triggers certain things to be added to global ctors, and then calling global ctors resulted in a segfault. So far I only get this error on node14, and I am not sure if it is related to the use of WASI.
C++
#include<emscripten.h>
#include<vector>
#include<sstream>// static intializer, need to call _startstatic std::vector<int> x = {1, 2, 3};
extern"C" {
EMSCRIPTEN_KEEPALIVEintGetX(int i) {
// use of ostream somehow makes _start fail.
std::ostringstream os;
os << "x";
return x[i];
}
}
const{WASI}=require('wasi');constwasi=newWASI({args: process.argv,env: process.env});constbinary=require('fs').readFileSync('build/wasm_test.wasm');WebAssembly.instantiate(binary,{env: {},wasi_snapshot_preview1: wasi.wasiImport}).then(({ instance })=>{// trigger ctorsinstance.exports._start();// test the static vars are correctly initialized.console.log(instance.exports.GetX(0));});
Using wasi.start(instance); instead of instance.exports._start(); fixes this, but I agree that this shouldn’t crash. Essentially, https://github.com/nodejs/node/blob/master/src/node_wasi.cc#L1794-L1798 is missing another check that verifies that memory is not empty (i.e. that _setMemory() has been called at some point).
I’ve attached the generated WASM here so that if somebody wants to pick this up, they don’t have to install emscripten to reproduce this.
Activity
[-]Segfault on combo of global ctors, ostream, wasi[/-][+][BUG] Segfault on combo of global ctors, ostream, wasi[/+]tqchen commentedon Apr 30, 2020
Backtrace, note that the same code works for other wasi runtime.
addaleax commentedon Apr 30, 2020
Using
wasi.start(instance);instead ofinstance.exports._start();fixes this, but I agree that this shouldn’t crash. Essentially, https://github.com/nodejs/node/blob/master/src/node_wasi.cc#L1794-L1798 is missing another check that verifies thatmemoryis not empty (i.e. that_setMemory()has been called at some point).I’ve attached
the generated WASM here so that if somebody wants to pick this up, they don’t have to install emscripten to reproduce this.
gengjiawen commentedon Sep 29, 2020
Should be fixed by #33184. Please reopen if this persists.