GeezerVison is showing github.com — wasi: accept wasm i32 pointers with high bit set · nodejs/node@817eee0
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: src/node_wasi.cc +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,7 @@ using v8::FastApiCallbackOptions; |
36 | 36 | using v8::FunctionCallbackInfo; |
37 | 37 | using v8::FunctionTemplate; |
38 | 38 | using v8::HandleScope; |
| 39 | +using v8::Int32; |
39 | 40 | using v8::Integer; |
40 | 41 | using v8::Isolate; |
41 | 42 | using v8::Local; |
@@ -296,13 +297,17 @@ static bool CheckType(Local<Value> v); |
296 | 297 | template <typename VT> |
297 | 298 | static VT ConvertType(Local<Value> V); |
298 | 299 |
|
| 300 | +// Wasm i32 values with the high bit set arrive as negative Int32 numbers. |
299 | 301 | template <> |
300 | 302 | bool CheckType<uint32_t>(Local<Value> value) { |
301 | | - return value->IsUint32(); |
| 303 | + return value->IsUint32() || value->IsInt32(); |
302 | 304 | } |
303 | 305 |
|
304 | 306 | template <> |
305 | 307 | uint32_t ConvertType(Local<Value> value) { |
| 308 | + if (value->IsInt32()) { |
| 309 | + return static_cast<uint32_t>(value.As<Int32>()->Value()); |
| 310 | + } |
306 | 311 | return value.As<Uint32>()->Value(); |
307 | 312 | } |
308 | 313 |
|
|
Collapse file
Copy file name to clipboard Collapse file
Copy file name to clipboard +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +;; Build: wasm-tools parse wasi-high-bit-ptr.wat -o wasi-high-bit-ptr.wasm |
| 2 | +(module |
| 3 | + (import "wasi_snapshot_preview1" "clock_time_get" |
| 4 | + (func $clock_time_get (param i32 i64 i32) (result i32))) |
| 5 | + (import "wasi_snapshot_preview1" "args_sizes_get" |
| 6 | + (func $args_sizes_get (param i32 i32) (result i32))) |
| 7 | + (memory (export "memory") 1) |
| 8 | + (func (export "_start")) |
| 9 | + (func (export "clock_time_get_high_ptr") (result i32) |
| 10 | + i32.const 0 |
| 11 | + i64.const 1 |
| 12 | + i32.const 0x80000000 |
| 13 | + call $clock_time_get) |
| 14 | + (func (export "clock_time_get_low_ptr") (result i32) |
| 15 | + i32.const 0 |
| 16 | + i64.const 1 |
| 17 | + i32.const 8 |
| 18 | + call $clock_time_get) |
| 19 | + (func (export "args_sizes_get_high_ptr") (result i32) |
| 20 | + i32.const 0x80000000 |
| 21 | + i32.const 0xfffffff0 |
| 22 | + call $args_sizes_get)) |
Collapse file
Copy file name to clipboard +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +'use strict'; |
| 2 | +// Wasm i32 arguments with the high bit set arrive in JS as negative Int32 |
| 3 | +// numbers. WASI functions must reinterpret them as uint32 pointers rather |
| 4 | +// than rejecting them with EINVAL. |
| 5 | +const common = require('../common'); |
| 6 | +const assert = require('assert'); |
| 7 | +const fixtures = require('../common/fixtures'); |
| 8 | +const { WASI } = require('wasi'); |
| 9 | + |
| 10 | +const UVWASI_EOVERFLOW = 61; |
| 11 | + |
| 12 | +(async () => { |
| 13 | + const wasi = new WASI({ version: 'preview1', returnOnExit: true }); |
| 14 | + const importObject = { wasi_snapshot_preview1: wasi.wasiImport }; |
| 15 | + const { instance } = await WebAssembly.instantiate( |
| 16 | + fixtures.readSync('wasi-high-bit-ptr.wasm'), importObject); |
| 17 | + wasi.start(instance); |
| 18 | + |
| 19 | + assert.strictEqual(instance.exports.clock_time_get_low_ptr(), 0); |
| 20 | + assert.strictEqual(instance.exports.clock_time_get_high_ptr(), |
| 21 | + UVWASI_EOVERFLOW); |
| 22 | + assert.strictEqual(instance.exports.args_sizes_get_high_ptr(), |
| 23 | + UVWASI_EOVERFLOW); |
| 24 | +})().then(common.mustCall()); |