Skip to content

Commit 8719763

Browse files
ryuhei shimaryuhei shima
authored andcommitted
wasi: accept int32 ptr in clock_time_get
Fixes: #62671
1 parent a6e9e32 commit 8719763

5 files changed

Lines changed: 100 additions & 5 deletions

File tree

src/node_wasi.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,14 +486,17 @@ uint32_t WASI::ClockTimeGet(WASI& wasi,
486486
WasmMemory memory,
487487
uint32_t clock_id,
488488
uint64_t precision,
489-
uint32_t time_ptr) {
490-
Debug(wasi, "clock_time_get(%d, %d, %d)\n", clock_id, precision, time_ptr);
491-
CHECK_BOUNDS_OR_RETURN(memory.size, time_ptr, UVWASI_SERDES_SIZE_timestamp_t);
489+
int32_t time_ptr) {
490+
const auto time_ptr_u32 = static_cast<uint32_t>(time_ptr);
491+
Debug(
492+
wasi, "clock_time_get(%d, %d, %d)\n", clock_id, precision, time_ptr_u32);
493+
CHECK_BOUNDS_OR_RETURN(
494+
memory.size, time_ptr_u32, UVWASI_SERDES_SIZE_timestamp_t);
492495
uvwasi_timestamp_t time;
493496
uvwasi_errno_t err =
494497
uvwasi_clock_time_get(&wasi.uvw_, clock_id, precision, &time);
495498
if (err == UVWASI_ESUCCESS)
496-
uvwasi_serdes_write_timestamp_t(memory.data, time_ptr, time);
499+
uvwasi_serdes_write_timestamp_t(memory.data, time_ptr_u32, time);
497500

498501
return err;
499502
}

src/node_wasi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class WASI : public BaseObject,
3131
static uint32_t ArgsGet(WASI&, WasmMemory, uint32_t, uint32_t);
3232
static uint32_t ArgsSizesGet(WASI&, WasmMemory, uint32_t, uint32_t);
3333
static uint32_t ClockResGet(WASI&, WasmMemory, uint32_t, uint32_t);
34-
static uint32_t ClockTimeGet(WASI&, WasmMemory, uint32_t, uint64_t, uint32_t);
34+
static uint32_t ClockTimeGet(WASI&, WasmMemory, uint32_t, uint64_t, int32_t);
3535
static uint32_t EnvironGet(WASI&, WasmMemory, uint32_t, uint32_t);
3636
static uint32_t EnvironSizesGet(WASI&, WasmMemory, uint32_t, uint32_t);
3737
static uint32_t FdAdvise(
366 Bytes
Binary file not shown.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
(module
2+
(import "wasi_snapshot_preview1" "clock_time_get"
3+
(func $clock_time_get (param i32 i64 i32) (result i32)))
4+
5+
(memory (export "memory") 2)
6+
7+
(func (export "test_clock_time_get") (result i32)
8+
i32.const 0x80000000
9+
i64.const 1
10+
i32.const 0x80000000
11+
call $clock_time_get
12+
)
13+
14+
(func (export "test_clock_time_get_zero") (result i32)
15+
i32.const 0
16+
i64.const 1
17+
i32.const 0
18+
call $clock_time_get
19+
)
20+
21+
(func (export "test_clock_time_get_one") (result i32)
22+
i32.const 1
23+
i64.const 1
24+
i32.const 1
25+
call $clock_time_get
26+
)
27+
28+
(func (export "test_clock_time_get_int32_max") (result i32)
29+
i32.const 0
30+
i64.const 1
31+
i32.const 0x7fffffff
32+
call $clock_time_get
33+
)
34+
35+
(func (export "test_clock_time_get_uint32_min") (result i32)
36+
i32.const 0
37+
i64.const 1
38+
i32.const 0x80000000
39+
call $clock_time_get
40+
)
41+
42+
(func (export "test_clock_time_get_uint32_max") (result i32)
43+
i32.const 0
44+
i64.const 1
45+
i32.const 0xffffffff
46+
call $clock_time_get
47+
)
48+
49+
(func (export "_start"))
50+
)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const fs = require('fs');
5+
const path = require('path');
6+
const { WASI } = require('wasi');
7+
8+
const wasm = fs.readFileSync(
9+
path.join(__dirname, '..', 'fixtures', 'wasi-uint32-int32.wasm'),
10+
);
11+
12+
const kWasiUint32Expectations = [
13+
['test_clock_time_get', 61],
14+
['test_clock_time_get_zero', 0],
15+
['test_clock_time_get_one', 0],
16+
['test_clock_time_get_int32_max', 61],
17+
['test_clock_time_get_uint32_min', 61],
18+
['test_clock_time_get_uint32_max', 61],
19+
];
20+
21+
const kWasiUint32ExportNames = kWasiUint32Expectations.map(([name]) => name);
22+
23+
(async () => {
24+
const wasi = new WASI({ version: 'preview1', returnOnExit: true });
25+
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
26+
const { instance } = await WebAssembly.instantiate(wasm, importObject);
27+
28+
assert.deepStrictEqual(
29+
Object.keys(instance.exports).sort(),
30+
['_start', 'memory', ...kWasiUint32ExportNames].sort(),
31+
);
32+
33+
wasi.start(instance);
34+
35+
for (const [name, expectedErrno] of kWasiUint32Expectations) {
36+
assert.strictEqual(
37+
instance.exports[name](),
38+
expectedErrno,
39+
name,
40+
);
41+
}
42+
})().then(common.mustCall());

0 commit comments

Comments
 (0)