From b587b6554556853a031a56ba32198c843b7d1a98 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Thu, 29 Oct 2026 22:52:27 -0700 Subject: [PATCH 1/2] vmcore: map VM times beyond the host clock to its latest time TimerState::timestamp maps a VM time to the host time at which VM time reaches it, by adding the time's circular distance from the last start to the host time of that start. set_next arms the time source's host timer with that host time, and VmTimeAccess::host_time returns it. Two kinds of VM time break it: - Beyond the host clock. pal_async's Instant counts nanoseconds in 64 bits, about 584 years, and its Add panics on overflow. A timeout more than about 584 years after the last start therefore panics whichever thread maps it: a VmTimeAccess caller in set_timeout, poll_timeout or host_time, or the time source's task. microsoft/nvx#1038 arms a timeout while VM time is stopped and restores VM time to a time that the timeout follows by 2.66e18 units, so that VmTimeKeeper::start panics the time source's task with "duration too large". - Nearly half the ring ahead. Once VM time has run for d since the last start, a time in start + [2^63, 2^63 + d) follows now in VM time's circular order but precedes the start, so timestamp maps it to the host time of the start, in the past. TimerState::wake compares timeouts with now and keeps it pending, so the host timer fires at once, wake re-arms the same deadline, and TimerState::poll loops forever with the timer state's write lock held. No VM time timeout fires again, and VmTimeKeeper::stop hangs. microsoft/nvx#1069 reports this spin. A guest can reach both where the Hyper-V synthetic timers run on VM time: a one-shot synthetic timer may expire up to half the ring after the current reference time, and WHP's user-mode synic emulation, HVF, and OpenHCL's VmTimeGuestTimer add that distance to the current VM time. Make timestamp add the distance with Instant::saturating_add, so that a time beyond the host clock maps to the host clock's latest time, which no host timer reaches. Map a time that precedes the start but follows now to that latest time too. timestamp reads the current time only for a time before the start, so the common case costs nothing more. A time before the start and not after now is in the past, and still maps to the host time of the start. test_timeout_beyond_host_clock arms a timeout 1,000 years ahead, and test_timeout_beyond_host_clock_after_restore follows microsoft/nvx#1038's sequence. test_timeout_nearly_half_the_ring_ahead sets a timeout at the latest time that follows now, now + 2^63 - 1, after VM time has run for 10 ms, and checks that a past time before the start still maps to the start. Each test checks that the far timeout keeps waiting and that an earlier timeout still fires. Without the fix, the first two panic with "duration too large", and the third fails its check of host_time. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> (cherry picked from commit 65c517f03ff90cd82ec46f89d1f1122027e8ca40) --- vm/vmcore/src/vmtime.rs | 142 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 134 insertions(+), 8 deletions(-) diff --git a/vm/vmcore/src/vmtime.rs b/vm/vmcore/src/vmtime.rs index 26514141fe4..d2ca6d84122 100644 --- a/vm/vmcore/src/vmtime.rs +++ b/vm/vmcore/src/vmtime.rs @@ -210,14 +210,24 @@ impl TimerState { /// Returns the timestamp corresponding to the given VM time. /// - /// If the VM time is before the last start time, then the timestamp is at - /// the host time when the VM last started. + /// If the VM time is in the past and before the last start time, then the + /// timestamp is at the host time when the VM last started. If the VM time + /// is in the future, beyond what the host clock can represent, then the + /// timestamp is at the host clock's latest time. fn timestamp(&self, time: VmTime) -> Option { let start_time = self.time.start_time()?; - let since = time - .checked_sub(start_time.vmtime) - .unwrap_or(Duration::ZERO); - Some(Timestamp::new(time, start_time.os_time() + since)) + let os_time = if let Some(since) = time.checked_sub(start_time.vmtime) { + start_time.os_time().saturating_add(since) + } else if time.is_after(self.now(Instant::now()).vmtime) { + // Once VM time has run since the start, a time nearly half the ring + // after now is more than half the ring after the start, which VM + // time's circular order puts before it. Such a time is still in + // the future, far beyond the host clock. + Instant::from_nanos(u64::MAX) + } else { + start_time.os_time() + }; + Some(Timestamp::new(time, os_time)) } /// Returns the current guest time given a host time. @@ -809,8 +819,10 @@ impl VmTimeAccess { /// Returns the host time corresponding to a guest time. /// - /// If the guest time is before the VM last resumed, then returns the time - /// the VM last resumed. + /// If the guest time is in the past and before the VM last resumed, then + /// returns the time the VM last resumed. If the guest time is in the + /// future, beyond what the host clock can represent, then returns the host + /// clock's latest time. /// /// If the VM is not running, returns `None`. pub fn host_time(&self, time: VmTime) -> Option { @@ -964,11 +976,14 @@ impl VmTimerPeriodic { #[cfg(test)] mod tests { + use super::SavedState; use super::VmTime; + use super::VmTimeAccess; use super::VmTimeKeeper; use futures::FutureExt; use pal_async::DefaultDriver; use pal_async::async_test; + use pal_async::timer::Instant; use pal_async::timer::PolledTimer; use std::future::poll_fn; use std::time::Duration; @@ -1065,4 +1080,115 @@ mod tests { assert_eq!(acc1.now(), zero); assert_eq!(acc2.now(), zero); } + + /// Checks that `access`, waiting for a timeout that the host clock cannot + /// represent, keeps waiting, and that an earlier timeout still fires. + async fn check_unrepresentable_timeout(driver: &DefaultDriver, access: &mut VmTimeAccess) { + let mut timer = PolledTimer::new(driver); + futures::select! { + _ = timer.sleep(Duration::from_millis(50)).fuse() => {} + _ = poll_fn(|cx| access.poll_timeout(cx)).fuse() => panic!("unexpected wait completion"), + } + + let deadline = access.now().wrapping_add(Duration::from_millis(10)); + access.set_timeout(deadline); + futures::select! { + _ = timer.sleep(Duration::from_millis(1000)).fuse() => panic!("unexpected timeout"), + now = poll_fn(|cx| access.poll_timeout(cx)).fuse() => { + assert!(now.is_after(deadline)); + } + } + } + + #[async_test] + async fn test_timeout_beyond_host_clock(driver: DefaultDriver) { + let mut keeper = VmTimeKeeper::new(&driver, VmTime::from_100ns(0)); + let mut access = keeper + .builder() + .build(&driver) + .await + .unwrap() + .access("test"); + keeper.start().await; + + // The host clock counts nanoseconds in 64 bits, about 584 years, so it + // cannot represent a time 1000 years ahead. + let timeout = access + .now() + .wrapping_add(Duration::from_secs(1000 * 365 * 24 * 60 * 60)); + assert_eq!( + access.host_time(timeout), + Some(Instant::from_nanos(u64::MAX)) + ); + access.set_timeout(timeout); + check_unrepresentable_timeout(&driver, &mut access).await; + keeper.stop().await; + } + + #[async_test] + async fn test_timeout_beyond_host_clock_after_restore(driver: DefaultDriver) { + let mut keeper = VmTimeKeeper::new(&driver, VmTime::from_100ns(0)); + let mut access = keeper + .builder() + .build(&driver) + .await + .unwrap() + .access("test"); + + // Arm a timeout while VM time is stopped, and poll once to register + // it. + let timeout = VmTime::from_100ns(18_000_000_000_000_000); + access.set_timeout(timeout); + assert!( + poll_fn(|cx| access.poll_timeout(cx)) + .now_or_never() + .is_none() + ); + + // Restore VM time to a time that the timeout follows, in VM time's + // circular order, by more nanoseconds than the host clock can count. + // The timeout stays armed, so starting VM time arms the host timer for + // it. + let restored = VmTime::from_100ns(15_800_000_000_000_000_000); + assert!(timeout.checked_sub(restored).unwrap().as_nanos() > u64::MAX.into()); + keeper.restore(SavedState::from_vmtime(restored)).await; + keeper.start().await; + check_unrepresentable_timeout(&driver, &mut access).await; + keeper.stop().await; + } + + #[async_test] + async fn test_timeout_nearly_half_the_ring_ahead(driver: DefaultDriver) { + let start = VmTime::from_100ns(0); + let mut keeper = VmTimeKeeper::new(&driver, start); + let mut access = keeper + .builder() + .build(&driver) + .await + .unwrap() + .access("test"); + keeper.start().await; + PolledTimer::new(&driver) + .sleep(Duration::from_millis(10)) + .await; + + // The latest time that follows now in VM time's circular order. As VM + // time has run since the start, that order puts it before the start. + let now = access.now(); + let timeout = VmTime::from_100ns(now.as_100ns().wrapping_add(i64::MAX as u64)); + assert!(timeout.is_after(now)); + assert!(timeout.is_before(start)); + assert_eq!( + access.host_time(timeout), + Some(Instant::from_nanos(u64::MAX)) + ); + // A time in the past before the start still maps to the start. + assert_eq!( + access.host_time(VmTime::from_100ns(u64::MAX)), + access.host_time(start) + ); + access.set_timeout(timeout); + check_unrepresentable_timeout(&driver, &mut access).await; + keeper.stop().await; + } } From a5b823b01332ef81b0f5c3a55cba1d7aa260b5a8 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Fri, 6 Nov 2026 18:06:58 -0800 Subject: [PATCH 2/2] vmcore: add a bounded test of a timeout half the ring after the start Add microsoft/nvx#1069's reproduction as a test of the earliest time of the window that the previous commit fixed. After VM time has run for 10 ms from a start at 0, the test sets a timeout at the start + 2^63, which follows now in VM time's circular order but precedes the start. It checks that the timeout keeps waiting while an earlier timeout fires, that VmTimeAccess::host_time maps it to the host clock's latest time, and that VmTimeKeeper::stop completes. test_timeout_nearly_half_the_ring_ahead covers the window's latest time, now + 2^63 - 1, but runs its executor on the test's thread, which a time source task that never yields blocks for good. This test runs its executor on a thread of its own, so such a task fails it at a 10-second bound instead of hanging the test run. Without the previous commit's change to TimerState::timestamp, it fails at that bound while the time source's task spins. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- vm/vmcore/src/vmtime.rs | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/vm/vmcore/src/vmtime.rs b/vm/vmcore/src/vmtime.rs index d2ca6d84122..0ab8974d1de 100644 --- a/vm/vmcore/src/vmtime.rs +++ b/vm/vmcore/src/vmtime.rs @@ -1157,6 +1157,54 @@ mod tests { keeper.stop().await; } + #[test] + fn test_timeout_half_the_ring_after_start() { + // A time source task that never yields blocks its executor's thread for + // good, so run the executor on a thread of its own, and fail the test + // if it does not finish in time. + let (send, recv) = std::sync::mpsc::channel(); + let thread = std::thread::spawn(move || { + pal_async::DefaultPool::run_with(async |driver| { + let start = VmTime::from_100ns(0); + let mut keeper = VmTimeKeeper::new(&driver, start); + let mut access = keeper + .builder() + .build(&driver) + .await + .unwrap() + .access("test"); + keeper.start().await; + PolledTimer::new(&driver) + .sleep(Duration::from_millis(10)) + .await; + + // Half the ring after the start, the earliest time that VM + // time's circular order puts before the start. As VM time has + // run since the start, that order puts it after now. + let timeout = VmTime::from_100ns(start.as_100ns().wrapping_add(1 << 63)); + assert!(timeout.is_after(access.now())); + assert!(timeout.is_before(start)); + access.set_timeout_if_before(timeout); + check_unrepresentable_timeout(&driver, &mut access).await; + assert_eq!( + access.host_time(timeout), + Some(Instant::from_nanos(u64::MAX)) + ); + keeper.stop().await; + }); + let _ = send.send(()); + }); + if let Err(std::sync::mpsc::RecvTimeoutError::Timeout) = + recv.recv_timeout(Duration::from_secs(10)) + { + panic!("the test did not finish within 10 seconds"); + } + // The thread finished, or it panicked and dropped `send`. + thread + .join() + .unwrap_or_else(|panic| std::panic::resume_unwind(panic)); + } + #[async_test] async fn test_timeout_nearly_half_the_ring_ahead(driver: DefaultDriver) { let start = VmTime::from_100ns(0);