Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/node_wasi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ using v8::FastApiCallbackOptions;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Int32;
using v8::Integer;
using v8::Isolate;
using v8::Local;
Expand Down Expand Up @@ -296,13 +297,17 @@ static bool CheckType(Local<Value> v);
template <typename VT>
static VT ConvertType(Local<Value> V);

// Wasm i32 values with the high bit set arrive as negative Int32 numbers.
template <>
bool CheckType<uint32_t>(Local<Value> value) {
return value->IsUint32();
return value->IsUint32() || value->IsInt32();
}

template <>
uint32_t ConvertType(Local<Value> value) {
if (value->IsInt32()) {
return static_cast<uint32_t>(value.As<Int32>()->Value());
}
return value.As<Uint32>()->Value();
}

Expand Down
Binary file added test/fixtures/wasi-high-bit-ptr.wasm
Binary file not shown.
22 changes: 22 additions & 0 deletions test/fixtures/wasi-high-bit-ptr.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
;; Build: wasm-tools parse wasi-high-bit-ptr.wat -o wasi-high-bit-ptr.wasm
(module
(import "wasi_snapshot_preview1" "clock_time_get"
(func $clock_time_get (param i32 i64 i32) (result i32)))
(import "wasi_snapshot_preview1" "args_sizes_get"
(func $args_sizes_get (param i32 i32) (result i32)))
(memory (export "memory") 1)
(func (export "_start"))
(func (export "clock_time_get_high_ptr") (result i32)
i32.const 0
i64.const 1
i32.const 0x80000000
call $clock_time_get)
(func (export "clock_time_get_low_ptr") (result i32)
i32.const 0
i64.const 1
i32.const 8
call $clock_time_get)
(func (export "args_sizes_get_high_ptr") (result i32)
i32.const 0x80000000
i32.const 0xfffffff0
call $args_sizes_get))
24 changes: 24 additions & 0 deletions test/wasi/test-wasi-high-bit-ptr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
// Wasm i32 arguments with the high bit set arrive in JS as negative Int32
// numbers. WASI functions must reinterpret them as uint32 pointers rather
// than rejecting them with EINVAL.
const common = require('../common');
const assert = require('assert');
const fixtures = require('../common/fixtures');
const { WASI } = require('wasi');

const UVWASI_EOVERFLOW = 61;

(async () => {
const wasi = new WASI({ version: 'preview1', returnOnExit: true });
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
const { instance } = await WebAssembly.instantiate(
fixtures.readSync('wasi-high-bit-ptr.wasm'), importObject);
wasi.start(instance);

assert.strictEqual(instance.exports.clock_time_get_low_ptr(), 0);
assert.strictEqual(instance.exports.clock_time_get_high_ptr(),
UVWASI_EOVERFLOW);
assert.strictEqual(instance.exports.args_sizes_get_high_ptr(),
UVWASI_EOVERFLOW);
})().then(common.mustCall());
Loading