Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasi: avoid hard crash if start() is never called #33184

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/node_wasi.cc
Expand Up @@ -1791,11 +1791,11 @@ uvwasi_errno_t WASI::backingStore(char** store, size_t* byte_length) {
Local<Object> memory = PersistentToLocal::Strong(this->memory_);
Local<Value> prop;

if (!memory->Get(env->context(), env->buffer_string()).ToLocal(&prop))
return UVWASI_EINVAL;

if (!prop->IsArrayBuffer())
if (this->memory_.IsEmpty() ||
!memory->Get(env->context(), env->buffer_string()).ToLocal(&prop) ||
!prop->IsArrayBuffer()) {
return UVWASI_EINVAL;
}

Local<ArrayBuffer> ab = prop.As<ArrayBuffer>();
std::shared_ptr<BackingStore> backing_store = ab->GetBackingStore();
Expand Down
35 changes: 35 additions & 0 deletions test/wasi/test-no-start-call.js
@@ -0,0 +1,35 @@
'use strict';
const common = require('../common');

if (process.argv[2] === 'child') {
(async () => {
const fs = require('fs');
const path = require('path');
const { WASI } = require('wasi');
const wasmDir = path.join(__dirname, 'wasm');
const modulePath = path.join(wasmDir, 'main_args.wasm');
const buffer = fs.readFileSync(modulePath);
const wasi = new WASI();
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
const { instance } = await WebAssembly.instantiate(buffer, importObject);

// Verify that calling the _start() export instead of start() does not
// crash. The application will fail with a non-zero exit code.
instance.exports._start();
})().then(common.mustCall());
} else {
const assert = require('assert');
const { spawnSync } = require('child_process');
const child = spawnSync(process.execPath, [
'--experimental-wasi-unstable-preview1',
'--experimental-wasm-bigint',
'--no-warnings',
__filename,
'child'
]);

assert.strictEqual(child.stdout.toString(), '');
assert.strictEqual(child.stderr.toString(), '');
assert.strictEqual(child.signal, null);
assert.notStrictEqual(child.status, 0);
}