Skip to content

Preserve the full WASI timestamp range when building TimeSpec #755

Closed

Description

@LunaStev

Problem

The WASI clock provider rejects a successful u64 nanosecond timestamp above i64::MAX before dividing it into seconds and nanoseconds. These timestamps are representable in TimeSpec; the early check unnecessarily loses half of the host range.

Starting points: std/sys/wasm/time.wave.

Reproduction

Tested at 6257ba3eb4b9f1ab6c247a920c6e0c4b5d699331 on Linux amd64 with LLVM 21.1.8 and a compiler built from that checkout. Build it with LLVM_SYS_211_PREFIX=/usr/lib64/llvm21 cargo build --locked --jobs 2. Node.js 22.22.1 and wasm-ld were used. This is an executed Wave WASI module with controlled host imports, not an assertion about how frequently a production host returns these values.

For a self-contained test against this checkout's std, create a temporary alias (leaving installed std unchanged):

python3 - <<'PYSETUP'
from pathlib import Path
import shutil
p = Path('/tmp/wave-issue-std')
shutil.copytree('std', p, dirs_exist_ok=True)
for f in p.rglob('*.wave'):
    f.write_text(f.read_text().replace('"std::', '"checkout_std::'))
PYSETUP

Save as repro.wave:

import("checkout_std::sys::time")::{TimeSpec, clock_gettime};
pub fun audit() -> i64 { var value: TimeSpec; return clock_gettime(0, &value); }
fun main() -> i32 { return 0; }
target/debug/wavec build repro.wave --dep checkout_std=/tmp/wave-issue-std --target wasm32-wasip1 -O0 --emit=obj --out-dir /tmp/wave-issue-wasi
wasm-ld --no-entry --export=audit --export-memory /tmp/wave-issue-wasi/repro.o -o /tmp/wave-issue-wasi/test.wasm

Save as repro.cjs and run node repro.cjs:

const fs = require('fs');
(async () => {
const module = await WebAssembly.compile(fs.readFileSync('/tmp/wave-issue-wasi/test.wasm'));
const imports = {}; let instance; let calls = 0;
for (const i of WebAssembly.Module.imports(module)) {
  imports[i.module] ??= {};
  imports[i.module][i.name] = () => 0;
}
imports.wasi_snapshot_preview1.clock_time_get = (clock, precision, output) => {
  new DataView(instance.exports.memory.buffer).setBigUint64(output, 9223372036854775808n, true);
  return 0;
};
instance = await WebAssembly.instantiate(module, imports);
console.log(String(instance.exports.audit()), 'read calls:', calls);
})().catch(e => { console.error(e); process.exit(1); });

Observed: -75 read calls: 0, despite a successful host clock result that fits TimeSpec. Repeat compilation with -O2; the same behavior was observed.

The official WASI Preview 1 header defines EINTR as 27 and timestamps as u64 nanoseconds.

Scope

Split the unsigned timestamp before conversion and validate the actual destination fields. Preserve host failures and pointer validation.

Difficulty: easy.

Completion criteria

  • Check 0, i64::MAX, i64::MAX+1 and u64::MAX timestamps.
  • For 2^63 ns, return seconds=9223372036 and nanoseconds=854775808 successfully.
  • Keep nanoseconds normalized and test null output/host error handling at O0/O2.

Activity

  1. added
    bug
    good first issue
    needs testing
    os compatibility
    on Sep 25, 2026
  2. self-assigned this
    on Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

buggood first issueneeds testingos compatibility

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions