Skip to content

wasi: do not panic on extreme file timestamps in DescriptorStat#13894

Open
SebTardif wants to merge 4 commits into
bytecodealliance:mainfrom
SebTardif:fix/datetime-from-no-unwrap
Open

wasi: do not panic on extreme file timestamps in DescriptorStat#13894
SebTardif wants to merge 4 commits into
bytecodealliance:mainfrom
SebTardif:fix/datetime-from-no-unwrap

Conversation

@SebTardif

@SebTardif SebTardif commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Summary

DescriptorStat::new converted host SystemTime values to WASI Datetime with unwrap(). When conversion fails (time outside the representable i64 second range), that panics the host while building file stats.

Map conversion failure to None (missing timestamp) on the existing Option<Datetime> fields.

Failure scenario

Host Metadata can surface SystemTime values that do not fit WASI Datetime (for example extreme far-past times representable as SystemTime but not as i64 seconds). A subsequent guest or host stat path that builds DescriptorStat would panic instead of returning stats with missing timestamp fields.

Guest set_times is not the reproducer for the overflow itself: those APIs already take Datetime values within range. The panic is on host-side conversion of SystemTime into Datetime.

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 extreme SystemTime values fail Datetime::try_from and map to None without panicking.

Integration coverage of the surrounding set_times/stat stack already exists as p1_fd_filestat_set in crates/test-programs / crates/wasi/tests. A guest program cannot construct the overflowing SystemTime via 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

Datetime::try_from can fail when SystemTime is outside the i64-second
range. datetime_from used unwrap(), so a guest that set extreme
timestamps on a preopened path could crash the host on the next stat.
Map conversion failure to a missing timestamp (Option::None) instead.

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
@SebTardif
SebTardif requested a review from a team as a code owner July 17, 2026 15:24
@SebTardif
SebTardif requested review from pchickey and removed request for a team July 17, 2026 15:24
Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>

@alexcrichton alexcrichton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread crates/wasi/src/filesystem.rs Outdated
Comment thread crates/wasi/src/filesystem.rs Outdated
Comment thread crates/wasi/src/filesystem.rs Outdated
@github-actions github-actions Bot added the wasi Issues pertaining to WASI label Jul 17, 2026
Inline try_from mapping, drop narrative comments, and keep a small
unit test under mod test for SystemTime values outside Datetime's
range. Guest set_times cannot construct those values because they
already use Datetime; host Metadata is the overflow path.

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
@SebTardif

Copy link
Copy Markdown
Contributor Author

@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 test without narrative comments, and explained why a guest test-program cannot reproduce the host SystemTime overflow (WASI timestamps are already Datetime). Happy to adjust further if you want the unit test dropped entirely and only rely on existing p1_fd_filestat_set coverage.

Prepare a host file with an extreme mtime and have the guest
path_filestat_get and read it (p1 core module and p2 component
runners). Also treat wall-clock conversions that fail for
negative/out-of-range i64 timestamps as missing fields so the
whole stat does not return OVERFLOW on platforms that clamp far-
past times into i64.

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
@SebTardif
SebTardif requested a review from a team as a code owner July 18, 2026 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasi Issues pertaining to WASI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants