Skip to content

Commit ff12ab2

Browse files
committed
correct overflows in the backslide case, add test
1 parent 7256a6a commit ff12ab2

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

library/std/src/time/monotonic.rs

+30-8
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ pub mod inner {
1212
use crate::sys::time;
1313
use crate::time::Duration;
1414

15-
const ZERO: time::Instant = time::Instant::zero();
15+
pub(in crate::time) const ZERO: time::Instant = time::Instant::zero();
1616

1717
// bits 30 and 31 are never used since the seconds part never exceeds 10^9
18-
const UNINITIALIZED: u64 = 0xff00_0000;
18+
const UNINITIALIZED: u64 = 0b11 << 30;
1919
static MONO: AtomicU64 = AtomicU64::new(UNINITIALIZED);
2020

2121
#[inline]
2222
pub(super) fn monotonize(raw: time::Instant) -> time::Instant {
23+
monotonize_impl(&MONO, raw)
24+
}
25+
26+
#[inline]
27+
pub(in crate::time) fn monotonize_impl(mono: &AtomicU64, raw: time::Instant) -> time::Instant {
2328
let delta = raw.checked_sub_instant(&ZERO).unwrap();
2429
let secs = delta.as_secs();
2530
// occupies no more than 30 bits (10^9 seconds)
@@ -32,16 +37,33 @@ pub mod inner {
3237
// This could be a problem for programs that call instants at intervals greater
3338
// than 68 years. Interstellar probes may want to ensure that actually_monotonic() is true.
3439
let packed = (secs << 32) | nanos;
35-
let old = MONO.load(Relaxed);
40+
let old = mono.load(Relaxed);
3641

3742
if old == UNINITIALIZED || packed.wrapping_sub(old) < u64::MAX / 2 {
38-
MONO.store(packed, Relaxed);
43+
mono.store(packed, Relaxed);
3944
raw
4045
} else {
41-
// Backslide occurred. We reconstruct monotonized time by assuming the clock will never
42-
// backslide more than 2`32 seconds which means we can reuse the upper 32bits from
43-
// the seconds.
44-
let secs = (secs & 0xffff_ffff_0000_0000) | old >> 32;
46+
// Backslide occurred. We reconstruct monotonized time from the upper 32 bit of the
47+
// passed in value and the 64bits loaded from the atomic
48+
let seconds_lower = old >> 32;
49+
let mut seconds_upper = secs & 0xffff_ffff_0000_0000;
50+
if secs & 0xffff_ffff > seconds_lower {
51+
// Backslide caused the lower 32bit of the seconds part to wrap.
52+
// This must be the case because the seconds part is larger even though
53+
// we are in the backslide branch, i.e. the seconds count should be smaller or equal.
54+
//
55+
// We assume that backslides are smaller than 2^32 seconds
56+
// which means we need to add 1 to the upper half to restore it.
57+
//
58+
// Example:
59+
// most recent observed time: 0xA1_0000_0000_0000_0000u128
60+
// bits stored in AtomicU64: 0x0000_0000_0000_0000u64
61+
// backslide by 1s
62+
// caller time is 0xA0_ffff_ffff_0000_0000u128
63+
// -> we can fix up the upper half time by adding 1 << 32
64+
seconds_upper = seconds_upper.wrapping_add(0x1_0000_0000);
65+
}
66+
let secs = seconds_upper | seconds_lower;
4567
let nanos = old as u32;
4668
ZERO.checked_add_duration(&Duration::new(secs, nanos)).unwrap()
4769
}

library/std/src/time/tests.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{Duration, Instant, SystemTime, UNIX_EPOCH};
2+
use core::sync::atomic::AtomicU64;
23
use test::{black_box, Bencher};
34

45
macro_rules! assert_almost_eq {
@@ -190,6 +191,23 @@ fn since_epoch() {
190191
assert!(a < hundred_twenty_years);
191192
}
192193

194+
#[cfg(all(target_has_atomic = "64", not(target_has_atomic = "128")))]
195+
#[test]
196+
fn monotonizer_wrapping_backslide() {
197+
use super::monotonic::inner::{monotonize_impl, ZERO};
198+
199+
let reference = AtomicU64::new(0);
200+
201+
let time = ZERO.checked_add_duration(&Duration::from_secs(0xffff_ffff)).unwrap();
202+
203+
let monotonized = monotonize_impl(&reference, time);
204+
let expected = ZERO.checked_add_duration(&Duration::from_secs(1 << 32)).unwrap();
205+
assert_eq!(
206+
monotonized, expected,
207+
"64bit monotonizer should handle overflows in the seconds part"
208+
);
209+
}
210+
193211
macro_rules! bench_instant_threaded {
194212
($bench_name:ident, $thread_count:expr) => {
195213
#[bench]

0 commit comments

Comments
 (0)