Skip to content

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

Merged
alexcrichton merged 7 commits into
bytecodealliance:mainfrom
SebTardif:fix/datetime-from-no-unwrap
Jul 20, 2026
Merged

wasi: do not panic on extreme file timestamps in DescriptorStat#13894
alexcrichton merged 7 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
Comment thread crates/wasi/tests/all/p1.rs
Comment thread crates/wasi/tests/all/p2/sync.rs
Comment thread crates/wasi/tests/all/p2/async_.rs
Comment thread crates/wasi/src/filesystem.rs Outdated
Comment thread crates/wasi/tests/all/p1.rs Outdated
Address review feedback:
- Implement run() via run_with_workspace_setup() in p1, p2-sync, and
  p2-async so the runners are not duplicated.
- Move host extreme-mtime workspace seeding into
  store::prepare_extreme_mtime_fixture for shared use.
- Drop the unit test module in filesystem.rs now that integration
  coverage exists.

cargo test -p wasmtime-wasi --features p1,p2 --test all p1_stat_extreme_host_mtime

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
@alexcrichton
alexcrichton added this pull request to the merge queue Jul 20, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 20, 2026
MinGW merge-queue CI panicked at construct extreme SystemTime because
FILETIME-based SystemTime cannot represent UNIX_EPOCH - (i64::MAX+1).
Prefer that far-past value when available; otherwise fall back to the
Windows epoch (~1601). Guest coverage still only requires that
path_filestat_get does not panic the host.

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

Copy link
Copy Markdown
Contributor Author

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-based SystemTime, 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 that path_filestat_get does not panic the host.

Happy to be re-queued when convenient.

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
@alexcrichton
alexcrichton enabled auto-merge July 20, 2026 22:12
@alexcrichton
alexcrichton added this pull request to the merge queue Jul 20, 2026
Merged via the queue into bytecodealliance:main with commit 8e88f80 Jul 20, 2026
53 checks passed
yagehu pushed a commit to yagehu/wasmtime-x that referenced this pull request Jul 26, 2026
…codealliance#13894)

* wasi: do not panic on extreme file timestamps in DescriptorStat

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>

* wasi: rustfmt import order in filesystem.rs

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

* wasi: address review on DescriptorStat timestamp conversion

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>

* wasi: exercise extreme host mtime via guest filestat

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>

* wasi: share extreme-mtime fixture and dedupe test runners

Address review feedback:
- Implement run() via run_with_workspace_setup() in p1, p2-sync, and
  p2-async so the runners are not duplicated.
- Move host extreme-mtime workspace seeding into
  store::prepare_extreme_mtime_fixture for shared use.
- Drop the unit test module in filesystem.rs now that integration
  coverage exists.

cargo test -p wasmtime-wasi --features p1,p2 --test all p1_stat_extreme_host_mtime

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

* wasi: make extreme-mtime fixture constructible on Windows

MinGW merge-queue CI panicked at construct extreme SystemTime because
FILETIME-based SystemTime cannot represent UNIX_EPOCH - (i64::MAX+1).
Prefer that far-past value when available; otherwise fall back to the
Windows epoch (~1601). Guest coverage still only requires that
path_filestat_get does not panic the host.

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

* wasi: rustfmt extreme_host_mtime fixture

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

---------

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
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