From 342887c9981b9bc437055ed853a0053effb8b181 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: saturate the host time of a VM time beyond the host clock VmTime is a wrapping 64-bit count of 100 ns units, which is_before and checked_sub order circularly: a time less than half the ring, 2^63 units or about 29,000 years, after another follows it. The host clock, pal_async's Instant, counts nanoseconds in 64 bits, about 584 years. TimerState::timestamp maps a VM time to the host time at which VM time reaches it: it adds the time's circular distance from the last start to the host time of that start, with Instant's Add, which panics when the sum overflows the host clock, so for any time more than about 584 years, less the host's uptime at the start, after the start. Every timeout passes through timestamp: set_next arms the keeper's host timer with it whenever a timeout becomes the earliest that is pending, and VmTimeAccess::host_time returns it. A timeout that far ahead therefore panics the task that arms it. microsoft/nvx#1038 reaches this with public calls alone: a timeout at 1.8e16 is armed while VM time is stopped, and VM time is restored to 1.58e19, which the timeout follows by 2.66e18 units, so that start() panics the time source's task (SecondaryKeeper::run) in TimerState::start. A device that sets a timeout 1,000 years ahead panics its own task. A guest can choose such a timeout where OpenVMM emulates the Hyper-V synthetic timers, as WHP does when it does not offload the enlightenments: a one-shot timer expires at the absolute reference time that the guest writes, which virt_whp turns into a VM-time timeout. Saturate the sum at the host clock's latest time, with Instant::saturating_add, since the host clock never reaches a time that it cannot represent. Every timer backend already accepts that deadline: a timeout just short of overflowing reached it before, and the epoll, kqueue, IOCP, thread pool, io_uring, and local backends saturate their own conversions of it. A timeout that far ahead thus keeps its waiter waiting, and an earlier timeout still replaces it. The documentation of timestamp and host_time states the new bound. Saturating the distance itself at a day instead, as the issue suggests after TimerState::poll's default deadline, would stop the panic but make the keeper spin: once VM time has run for a day since the start, the deadline of a timeout further ahead is in the past, so the host timer fires, wake re-arms it with the same past deadline, and poll loops with the timer state's lock held. The new tests arm a timeout 1,000 years ahead of a running keeper, and the issue's sequence of calls. They check that host_time returns the host clock's latest time, that the timeout keeps waiting, and that an earlier one still fires. Both panic with "duration too large" without the fix. Part of microsoft/nvx#1038. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- vm/vmcore/src/vmtime.rs | 91 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 3 deletions(-) diff --git a/vm/vmcore/src/vmtime.rs b/vm/vmcore/src/vmtime.rs index 88e31817eb0..40caa7a772e 100644 --- a/vm/vmcore/src/vmtime.rs +++ b/vm/vmcore/src/vmtime.rs @@ -212,13 +212,18 @@ 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. + /// the host time when the VM last started. If the VM time is after it, + /// 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)) + Some(Timestamp::new( + time, + start_time.os_time().saturating_add(since), + )) } /// Returns the current guest time given a host time. @@ -884,7 +889,8 @@ 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. + /// the VM last resumed. If the guest time is after it, 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 { @@ -1038,11 +1044,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; @@ -1132,4 +1141,80 @@ 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; + } } From 5e0ab772eeea14f4d0f5ed05474856569358f80e Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Thu, 29 Oct 2026 22:58:18 -0700 Subject: [PATCH 2/2] vmcore: map a VM time nearly half the ring ahead beyond the host clock TimerState::timestamp maps a VM time to the host time at which VM time reaches it, from the time's circular distance after the last start, and maps a time that VM time's circular order puts before the start to the host time of that start. The keeper's waiters, though, compare their timeouts with the current time: poll_timeout waits for a timeout that is not before now, and wake re-arms the host timer for one after now. Once VM time has run since the start, the two disagree about a time nearly half the ring, about 29,000 years, after now: it follows now, but it is more than half the ring after the start, which the circular order puts before it. Such a timeout spins the keeper. set_next arms the host timer with the host time of the start, which has passed, so the timer fires at once. TimerState::poll then wakes, finds the timeout still after now, re-arms the same deadline, and loops without returning, with the timer state's write lock held, so every VmTimeAccess of the time source blocks. A test that sets such a timeout spends 9.99 s of user CPU time in 10 s, on Linux, until it is killed. VmTimeAccess::host_time returns the same past host time, which OpenHCL's VP loop uses as the deadline of its own timer. As with a timeout beyond the host clock, a guest can choose such a timeout where OpenVMM emulates the Hyper-V synthetic timers, since a one-shot timer may expire up to half the ring after the current reference time. Map a time that the circular order puts before the start, but after the current time, to the host clock's latest time instead: it is in the future, further ahead than the host clock can represent. 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. The documentation of timestamp and host_time says which times each bound covers. The new test sets a timeout at the latest time that follows now, after VM time has run for 10 ms since the start. It checks that host_time returns the host clock's latest time, that a time in the past before the start still maps to the start, that the timeout keeps waiting, and that an earlier one still fires. Without the fix, host_time returns the host time of the start, and without that check, the wait never ends. Part of microsoft/nvx#1038. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- vm/vmcore/src/vmtime.rs | 69 ++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 14 deletions(-) diff --git a/vm/vmcore/src/vmtime.rs b/vm/vmcore/src/vmtime.rs index 40caa7a772e..7a5e82b6c0b 100644 --- a/vm/vmcore/src/vmtime.rs +++ b/vm/vmcore/src/vmtime.rs @@ -211,19 +211,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 after it, - /// beyond what the host clock can represent, then the timestamp is at the - /// host clock's latest time. + /// 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().saturating_add(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. @@ -888,9 +893,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 after it, beyond what the host - /// clock can represent, then returns the host clock's latest time. + /// 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 { @@ -1217,4 +1223,39 @@ mod tests { 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; + } }