| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
#![allow(clippy::upper_case_acronyms)] |
| 8 |
|
| 9 |
use crate::nss_prelude::PRInt64; |
| 10 |
use crate::err::{Error, Res}; |
| 11 |
//use crate::prio::PRFileDesc; |
| 12 |
//use crate::ssl::SSLTimeFunc; |
| 13 |
|
| 14 |
use once_cell::sync::OnceCell; |
| 15 |
//use std::boxed::Box; |
| 16 |
use std::convert::{TryFrom, TryInto}; |
| 17 |
use std::ops::Deref; |
| 18 |
//use std::os::raw::c_void; |
| 19 |
//use std::pin::Pin; |
| 20 |
use std::time::{Duration, Instant}; |
| 21 |
|
| 22 |
include!(concat!(env!("OUT_DIR"), "/nspr_time.rs")); |
| 23 |
|
| 24 |
/* TODO move to exp module |
| 25 |
experimental_api!(SSL_SetTimeFunc( |
| 26 |
fd: *mut PRFileDesc, |
| 27 |
cb: SSLTimeFunc, |
| 28 |
arg: *mut c_void, |
| 29 |
)); |
| 30 |
*/ |
| 31 |
|
| 32 |
/// This struct holds the zero time used for converting between `Instant` and `PRTime`. |
| 33 |
#[derive(Debug)] |
| 34 |
struct TimeZero { |
| 35 |
instant: Instant, |
| 36 |
prtime: PRTime, |
| 37 |
} |
| 38 |
|
| 39 |
impl TimeZero { |
| 40 |
/// This function sets a baseline from an instance of `Instant`. |
| 41 |
/// This allows for the possibility that code that uses these APIs will create |
| 42 |
/// instances of `Instant` before any of this code is run. If `Instant`s older than |
| 43 |
/// `BASE_TIME` are used with these conversion functions, they will fail. |
| 44 |
/// To avoid that, we make sure that this sets the base time using the first value |
| 45 |
/// it sees if it is in the past. If it is not, then use `Instant::now()` instead. |
| 46 |
pub fn baseline(t: Instant) -> Self { |
| 47 |
let now = Instant::now(); |
| 48 |
let prnow = unsafe { PR_Now() }; |
| 49 |
|
| 50 |
if now <= t { |
| 51 |
// `t` is in the future, just use `now`. |
| 52 |
Self { |
| 53 |
instant: now, |
| 54 |
prtime: prnow, |
| 55 |
} |
| 56 |
} else { |
| 57 |
let elapsed = Interval::from(now.duration_since(now)); |
| 58 |
// An error from these unwrap functions would require |
| 59 |
// ridiculously long application running time. |
| 60 |
let prelapsed: PRTime = elapsed.try_into().unwrap(); |
| 61 |
Self { |
| 62 |
instant: t, |
| 63 |
prtime: prnow.checked_sub(prelapsed).unwrap(), |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
static BASE_TIME: OnceCell<TimeZero> = OnceCell::new(); |
| 70 |
|
| 71 |
fn get_base() -> &'static TimeZero { |
| 72 |
BASE_TIME.get_or_init(|| TimeZero { |
| 73 |
instant: Instant::now(), |
| 74 |
prtime: unsafe { PR_Now() }, |
| 75 |
}) |
| 76 |
} |
| 77 |
|
| 78 |
pub fn init() { |
| 79 |
let _ = get_base(); |
| 80 |
} |
| 81 |
|
| 82 |
/// Time wraps Instant and provides conversion functions into `PRTime`. |
| 83 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 84 |
pub struct Time { |
| 85 |
t: Instant, |
| 86 |
} |
| 87 |
|
| 88 |
impl Deref for Time { |
| 89 |
type Target = Instant; |
| 90 |
fn deref(&self) -> &Self::Target { |
| 91 |
&self.t |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
impl From<Instant> for Time { |
| 96 |
/// Convert from an Instant into a Time. |
| 97 |
fn from(t: Instant) -> Self { |
| 98 |
// Call `TimeZero::baseline(t)` so that time zero can be set. |
| 99 |
BASE_TIME.get_or_init(|| TimeZero::baseline(t)); |
| 100 |
Self { t } |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
impl TryFrom<PRTime> for Time { |
| 105 |
type Error = Error; |
| 106 |
fn try_from(prtime: PRTime) -> Res<Self> { |
| 107 |
let base = get_base(); |
| 108 |
if let Some(delta) = prtime.checked_sub(base.prtime) { |
| 109 |
let d = Duration::from_micros(delta.try_into()?); |
| 110 |
base.instant |
| 111 |
.checked_add(d) |
| 112 |
.map_or(Err(Error::TimeTravelError), |t| Ok(Self { t })) |
| 113 |
} else { |
| 114 |
Err(Error::TimeTravelError) |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
impl TryInto<PRTime> for Time { |
| 120 |
type Error = Error; |
| 121 |
fn try_into(self) -> Res<PRTime> { |
| 122 |
let base = get_base(); |
| 123 |
let delta = self |
| 124 |
.t |
| 125 |
.checked_duration_since(base.instant) |
| 126 |
.ok_or(Error::TimeTravelError)?; |
| 127 |
if let Ok(d) = PRTime::try_from(delta.as_micros()) { |
| 128 |
d.checked_add(base.prtime).ok_or(Error::TimeTravelError) |
| 129 |
} else { |
| 130 |
Err(Error::TimeTravelError) |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
impl From<Time> for Instant { |
| 136 |
#[must_use] |
| 137 |
fn from(t: Time) -> Self { |
| 138 |
t.t |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/// Interval wraps Duration and provides conversion functions into `PRTime`. |
| 143 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 144 |
pub struct Interval { |
| 145 |
d: Duration, |
| 146 |
} |
| 147 |
|
| 148 |
impl Deref for Interval { |
| 149 |
type Target = Duration; |
| 150 |
fn deref(&self) -> &Self::Target { |
| 151 |
&self.d |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
impl TryFrom<PRTime> for Interval { |
| 156 |
type Error = Error; |
| 157 |
fn try_from(prtime: PRTime) -> Res<Self> { |
| 158 |
Ok(Self { |
| 159 |
d: Duration::from_micros(u64::try_from(prtime)?), |
| 160 |
}) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
impl From<Duration> for Interval { |
| 165 |
fn from(d: Duration) -> Self { |
| 166 |
Self { d } |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
impl TryInto<PRTime> for Interval { |
| 171 |
type Error = Error; |
| 172 |
fn try_into(self) -> Res<PRTime> { |
| 173 |
Ok(PRTime::try_from(self.d.as_micros())?) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/* TODO: restore, experimental only. |
| 178 |
/// `TimeHolder` maintains a `PRTime` value in a form that is accessible to the TLS stack. |
| 179 |
#[derive(Debug)] |
| 180 |
pub struct TimeHolder { |
| 181 |
t: Pin<Box<PRTime>>, |
| 182 |
} |
| 183 |
|
| 184 |
impl TimeHolder { |
| 185 |
unsafe extern "C" fn time_func(arg: *mut c_void) -> PRTime { |
| 186 |
let p = arg as *const PRTime; |
| 187 |
*p.as_ref().unwrap() |
| 188 |
} |
| 189 |
|
| 190 |
pub fn bind(&mut self, fd: *mut PRFileDesc) -> Res<()> { |
| 191 |
unsafe { SSL_SetTimeFunc(fd, Some(Self::time_func), &mut *self.t as *mut _ as *mut c_void) } |
| 192 |
} |
| 193 |
|
| 194 |
pub fn set(&mut self, t: Instant) -> Res<()> { |
| 195 |
*self.t = Time::from(t).try_into()?; |
| 196 |
Ok(()) |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
impl Default for TimeHolder { |
| 201 |
fn default() -> Self { |
| 202 |
TimeHolder { t: Box::pin(0) } |
| 203 |
} |
| 204 |
} |
| 205 |
*/ |
| 206 |
|
| 207 |
#[cfg(test)] |
| 208 |
mod test { |
| 209 |
use super::{get_base, init, Interval, PRTime, Time}; |
| 210 |
use crate::err::Res; |
| 211 |
use std::convert::{TryFrom, TryInto}; |
| 212 |
use std::time::{Duration, Instant}; |
| 213 |
|
| 214 |
#[test] |
| 215 |
fn convert_stable() { |
| 216 |
init(); |
| 217 |
let now = Time::from(Instant::now()); |
| 218 |
let pr: PRTime = now.try_into().expect("convert to PRTime with truncation"); |
| 219 |
let t2 = Time::try_from(pr).expect("convert to Instant"); |
| 220 |
let pr2: PRTime = t2.try_into().expect("convert to PRTime again"); |
| 221 |
assert_eq!(pr, pr2); |
| 222 |
let t3 = Time::try_from(pr2).expect("convert to Instant again"); |
| 223 |
assert_eq!(t2, t3); |
| 224 |
} |
| 225 |
|
| 226 |
#[test] |
| 227 |
fn past_time() { |
| 228 |
init(); |
| 229 |
let base = get_base(); |
| 230 |
assert!(Time::try_from(base.prtime - 1).is_err()); |
| 231 |
} |
| 232 |
|
| 233 |
#[test] |
| 234 |
fn negative_time() { |
| 235 |
init(); |
| 236 |
assert!(Time::try_from(-1).is_err()); |
| 237 |
} |
| 238 |
|
| 239 |
#[test] |
| 240 |
fn negative_interval() { |
| 241 |
init(); |
| 242 |
assert!(Interval::try_from(-1).is_err()); |
| 243 |
} |
| 244 |
|
| 245 |
#[test] |
| 246 |
// We allow replace_consts here because |
| 247 |
// std::u64::max_value() isn't available |
| 248 |
// in all of our targets |
| 249 |
fn overflow_interval() { |
| 250 |
init(); |
| 251 |
let interval = Interval::from(Duration::from_micros(u64::max_value())); |
| 252 |
let res: Res<PRTime> = interval.try_into(); |
| 253 |
assert!(res.is_err()); |
| 254 |
} |
| 255 |
} |