SebTardif opened PR #13894 from SebTardif:fix/datetime-from-no-unwrap to bytecodealliance:main:
Summary
DescriptorStat::newconvertedSystemTimeto WASIDatetimewithDatetime::try_from(t).unwrap(). When a timestamp falls outside the representablei64second range, conversion returnsErrand the host panics while building file stats.This change maps conversion failure to
None(missing timestamp), which matches the existingOption<Datetime>fields onDescriptorStatand the prior "metadata access failed" path (now combined viaok().and_then(...)).Failure scenario
- Guest has write access to a preopened directory.
- Guest sets extreme atime/mtime via
set_times/path_filestat_set_times(or the host FS already has extreme mtimes).- Guest (or host code) stats the path.
- Host panics in
datetime_frominstead of returning a stat with missing timestamp fields.Bug origin
The
unwrappath was introduced with the cap-primitives migration in #13872 (Alex Crichton, 2026-07), including a FIXME that conversion should be infallible or handle errors properly.Test
cargo test -p wasmtime-wasi --lib datetime_from_testsUnit test constructs a far-past
SystemTimethat failsDatetime::try_fromand asserts.ok()isNone(the old.unwrap()would panic on the same value).Related
- Audit finding F004
- Previous Wasmtime merges from this fork: #13662, #13704
SebTardif requested pchickey for a review on PR #13894.
SebTardif requested wasmtime-wasi-reviewers for a review on PR #13894.
SebTardif updated PR #13894.
:memo: alexcrichton submitted PR review:
Thanks for the PR, but please be sure you review our AI tool usage policy. You own the code and you own the responsibility for being correct here, and for example the analysis of when this was introduced was wrong. The PR referred there was a refactor and did not introduce this.
:speech_balloon: alexcrichton created PR review comment:
Given how small this is now, could this get inlined directly?
:speech_balloon: alexcrichton created PR review comment:
Conventionally these tests are in
mod testas opposed to something more specific. Additionally we prefer to lean more heavily on integration tests exercise far more of the stack and ensure more complete fixes. Would it be possible to to model this as a test incrates/test-programs/src/bin/*.rs?
:speech_balloon: alexcrichton created PR review comment:
Please go through the output of your AI and consider rewording/removing comments like this. Comments like this about the state of a previous version of the tree are basically irrelevant noise as soon as the patch lands. The test here is self-explanatory and needs no comments.
github-actions[bot] added the label wasi on PR #13894.
SebTardif updated PR #13894.
SebTardif edited PR #13894:
Summary
DescriptorStat::newconverted hostSystemTimevalues to WASIDatetimewithunwrap(). When conversion fails (time outside the representablei64second range), that panics the host while building file stats.Map conversion failure to
None(missing timestamp) on the existingOption<Datetime>fields.Failure scenario
Host
Metadatacan surfaceSystemTimevalues that do not fit WASIDatetime(for example extreme far-past times representable asSystemTimebut not asi64seconds). A subsequent guest or hoststatpath that buildsDescriptorStatwould panic instead of returning stats with missing timestamp fields.Guest
set_timesis not the reproducer for the overflow itself: those APIs already takeDatetimevalues within range. The panic is on host-side conversion ofSystemTimeintoDatetime.Origin
The previous description incorrectly attributed introduction to the cap-primitives migration (#13872). That PR refactored the conversion site; the unwrap-style failure mode is not claimed as newly introduced there. This change is a small hardening of the conversion.
Test
Unit test under
crates/wasi/src/filesystem.rs(mod test) asserts that extremeSystemTimevalues failDatetime::try_fromand map toNonewithout panicking.Integration coverage of the surrounding set_times/stat stack already exists as
p1_fd_filestat_setincrates/test-programs/crates/wasi/tests. A guest program cannot construct the overflowingSystemTimevia the public WASI timestamp APIs, so the edge case stays as a unit test on the conversion.cargo test -p wasmtime-wasi --lib test::datetime_try_from
:memo: SebTardif submitted PR review.
:speech_balloon: SebTardif created PR review comment:
Inlined the
Datetime::try_from(...).ok()mapping at the three call sites.
:memo: SebTardif submitted PR review.
:speech_balloon: SebTardif created PR review comment:
Renamed to
mod testand dropped the narrative comments.On modeling this in
crates/test-programs/src/bin/*.rs: guestset_times/ filestat APIs already take WASIDatetime(i64seconds), so a guest cannot construct aSystemTimeoutside that range through the public ABI. The panic path is hostMetadata->Datetimeconversion. Existing integration coverage for the surrounding stack isp1_fd_filestat_set. The remaining unit test only covers the overflow mapping that guests cannot reach.
:memo: SebTardif submitted PR review.
:speech_balloon: SebTardif created PR review comment:
Removed. Left a comment-free unit test that only asserts the conversion edge.
SebTardif commented on PR #13894:
@alexcrichton
please be sure you review our AI tool usage policy... the analysis of when this was introduced was wrong. The PR referred there was a refactor and did not introduce this.
Thanks for the review. I re-read the AI tool use policy and corrected the PR description: #13872 was a refactor of the conversion site, not the introduction of this failure mode. Sorry for the incorrect origin claim.
Addressed the inline notes in the latest commit: inlined the conversion,
mod testwithout narrative comments, and explained why a guest test-program cannot reproduce the hostSystemTimeoverflow (WASI timestamps are alreadyDatetime). Happy to adjust further if you want the unit test dropped entirely and only rely on existingp1_fd_filestat_setcoverage.
:memo: alexcrichton submitted PR review.
:speech_balloon: alexcrichton created PR review comment:
Yes this can't be driven by purely guest-defined behavior, but it should be possible to prepare a filesystem on the host where there's a file with an extremely old timestamp in it which the guest attempts to stat and read. This can then assert that there's no panic and additionally the behavior of the timestamp basically just disappearing.
The test here is additionally overly-verbose and AI-generated where it checks
is_err()andok().is_none(). Please review all code generated by an AI, including tests.
SebTardif requested wasmtime-core-reviewers for a review on PR #13894.
SebTardif updated PR #13894.
:memo: SebTardif submitted PR review.
:speech_balloon: SebTardif created PR review comment:
Done. Host prepares a file with an extreme mtime before the guest runs; the guest does
path_filestat_getand reads the contents (p1_stat_extreme_host_mtimeundercrates/test-programs, wired through the p1 and p2 runners).On macOS the OS clamps that extreme time into a far-past value that still fits
i64seconds, soDatetime::try_fromsucceeds. The remaining failure mode was converting that negative time into wasi:clocks/wall-clock (u64seconds), which used to make the whole stat returnOVERFLOW. Unrepresentable wall-clock times are now treated as missing timestamps so filestat succeeds and the file remains readable.
:memo: alexcrichton submitted PR review.
:speech_balloon: alexcrichton created PR review comment:
Can you redefine the above function in terms of this one? Otherwise this looks like duplicated functionality.
:speech_balloon: alexcrichton created PR review comment:
It's ok to drop this custom test module now that it's integration-tested with the additional in-wasm test
:speech_balloon: alexcrichton created PR review comment:
Can the above function be implemented in terms of this one instead of duplicating it?
:speech_balloon: alexcrichton created PR review comment:
I might recommend extracting this to a helper shared function in
store.rsto avoid the need to duplicate this across p1/p2-sync/p2-async
:speech_balloon: alexcrichton created PR review comment:
Can the above function be deduplicated with this one?
:memo: SebTardif submitted PR review.
:speech_balloon: SebTardif created PR review comment:
@alexcrichton
Can you redefine the above function in terms of this one?
Done.
runis nowrun_with_workspace_setup(path, |_| Ok(()), with_builder).
SebTardif updated PR #13894.
:memo: SebTardif submitted PR review.
:speech_balloon: SebTardif created PR review comment:
@alexcrichton
Can the above function be implemented in terms of this one?
Done for p2-sync as well:
rundelegates torun_with_workspace_setupwith a no-op setup.
:speech_balloon: SebTardif created PR review comment:
@alexcrichton
Can the above function be deduplicated with this one?
Done for p2-async the same way.
:memo: SebTardif submitted PR review.
:memo: SebTardif submitted PR review.
:speech_balloon: SebTardif created PR review comment:
@alexcrichton
It's ok to drop this custom test module now that it's integration-tested
Removed the
#[cfg(test)] mod testblock fromfilesystem.rs.
:memo: SebTardif submitted PR review.
:speech_balloon: SebTardif created PR review comment:
@alexcrichton
extract this to a helper shared function in
store.rsAdded
store::prepare_extreme_mtime_fixtureand wired p1 / p2-sync / p2-asyncp1_stat_extreme_host_mtimethrough it.
:thumbs_up: alexcrichton submitted PR review.
alexcrichton added PR #13894 wasi: do not panic on extreme file timestamps in DescriptorStat to the merge queue.
github-merge-queue[bot] removed PR #13894 wasi: do not panic on extreme file timestamps in DescriptorStat from the merge queue.
SebTardif updated PR #13894.
SebTardif commented on PR #13894:
Pushed a MinGW fix for the merge-queue failure on
Test MinGW x86_64 (wasmtime-wasi).The extreme-mtime fixture used
SystemTime::UNIX_EPOCH - (i64::MAX+1), which is constructible on Unix but not on Windows FILETIME-basedSystemTime, so setup panicked before the guest ran. The fixture now prefers that far-past value when available and falls back to the Windows epoch (~1601) otherwise. The guest still only asserts thatpath_filestat_getdoes not panic the host.Happy to be re-queued when convenient.
SebTardif updated PR #13894.
alexcrichton has enabled auto merge for PR #13894.
alexcrichton added PR #13894 wasi: do not panic on extreme file timestamps in DescriptorStat to the merge queue.
:check: alexcrichton merged PR #13894.
alexcrichton removed PR #13894 wasi: do not panic on extreme file timestamps in DescriptorStat from the merge queue.
Last updated: Jul 29 2026 at 05:03 UTC