Add isolated time broker and raw Windows capture with Linux evidence
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -148,18 +148,13 @@ fn bracket_sample(
|
||||
base_error_ns > 0 && base_error_ns <= MAX_BASE_ERROR_NS,
|
||||
"TIME_NATIVE_CALIBRATION_REQUIRED"
|
||||
);
|
||||
let width = interrupt_after
|
||||
.checked_sub(interrupt_before)
|
||||
.and_then(|value| value.checked_mul(100))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_NATIVE_CAPTURE_COUNTER"))?;
|
||||
let unbiased_width = unbiased_after
|
||||
.checked_sub(unbiased_before)
|
||||
.and_then(|value| value.checked_mul(100))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_NATIVE_CAPTURE_COUNTER"))?;
|
||||
ensure!(
|
||||
width.abs_diff(unbiased_width) <= SECOND_NS,
|
||||
"TIME_NATIVE_CAPTURE_SUSPEND"
|
||||
);
|
||||
let width = raw_widths(
|
||||
interrupt_before,
|
||||
interrupt_after,
|
||||
unbiased_before,
|
||||
unbiased_after,
|
||||
)?
|
||||
.0;
|
||||
let capture_error_ns = width
|
||||
.checked_add(base_error_ns)
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_NATIVE_CAPTURE_OVERFLOW"))?;
|
||||
@@ -184,6 +179,215 @@ fn bracket_sample(
|
||||
})
|
||||
}
|
||||
|
||||
fn raw_widths(ib: u64, ia: u64, ub: u64, ua: u64) -> Result<(u64, u64)> {
|
||||
let width = ia
|
||||
.checked_sub(ib)
|
||||
.and_then(|value| value.checked_mul(100))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_NATIVE_CAPTURE_COUNTER"))?;
|
||||
let unbiased_width = ua
|
||||
.checked_sub(ub)
|
||||
.and_then(|value| value.checked_mul(100))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_NATIVE_CAPTURE_COUNTER"))?;
|
||||
ensure!(
|
||||
width.abs_diff(unbiased_width) <= SECOND_NS,
|
||||
"TIME_NATIVE_CAPTURE_SUSPEND"
|
||||
);
|
||||
Ok((width, unbiased_width))
|
||||
}
|
||||
/// Private raw capture, not a Sample and never clock authority.
|
||||
#[derive(Clone, Copy)]
|
||||
struct RawCapture {
|
||||
wall: u64,
|
||||
ib: u64,
|
||||
ub: u64,
|
||||
ua: u64,
|
||||
ia: u64,
|
||||
}
|
||||
impl RawCapture {
|
||||
fn widths(self) -> Result<(u64, u64)> {
|
||||
ensure!(
|
||||
self.wall > 116_444_736_000_000_000 && self.ib > 0 && self.ub > 0,
|
||||
"TIME_NATIVE_RAW_RANGE"
|
||||
);
|
||||
raw_widths(self.ib, self.ia, self.ub, self.ua)
|
||||
}
|
||||
fn row(self) -> [u64; 5] {
|
||||
[self.wall, self.ib, self.ub, self.ua, self.ia]
|
||||
}
|
||||
}
|
||||
/// Fixed read-only getter sequence shared by installed and diagnostic paths.
|
||||
#[cfg(windows)]
|
||||
fn raw_capture() -> Result<RawCapture> {
|
||||
#[link(name = "kernel32")]
|
||||
unsafe extern "system" {
|
||||
fn GetSystemTimePreciseAsFileTime(value: *mut u64);
|
||||
fn QueryInterruptTimePrecise(value: *mut u64);
|
||||
fn QueryUnbiasedInterruptTimePrecise(value: *mut u64);
|
||||
}
|
||||
let mut value = RawCapture {
|
||||
wall: 0,
|
||||
ib: 0,
|
||||
ub: 0,
|
||||
ua: 0,
|
||||
ia: 0,
|
||||
};
|
||||
// SAFETY: documented VOID getters write valid aligned u64 outputs. These
|
||||
// Windows10+ imports have no optional fallback or caller-selected target.
|
||||
unsafe {
|
||||
QueryInterruptTimePrecise(&mut value.ib);
|
||||
QueryUnbiasedInterruptTimePrecise(&mut value.ub);
|
||||
GetSystemTimePreciseAsFileTime(&mut value.wall);
|
||||
QueryUnbiasedInterruptTimePrecise(&mut value.ua);
|
||||
QueryInterruptTimePrecise(&mut value.ia);
|
||||
}
|
||||
value.widths()?;
|
||||
Ok(value)
|
||||
}
|
||||
const RAW_SAMPLES: usize = 128;
|
||||
const RAW_BUDGET_NS: u64 = 250_000_000;
|
||||
/// Numeric observations only. No Deserialize, calibrated Sample conversion or
|
||||
/// readiness flag; a report cannot open SignedClock or bypass its pin loader.
|
||||
#[derive(serde::Serialize)]
|
||||
pub struct CaptureDiagnostics {
|
||||
schema_version: u32,
|
||||
calibrated: bool,
|
||||
clock_authority: bool,
|
||||
columns_100ns: [&'static str; 5],
|
||||
samples_100ns: Vec<[u64; 5]>,
|
||||
sample_count: u32,
|
||||
elapsed_monotonic_ns: u64,
|
||||
elapsed_interrupt_ns: u64,
|
||||
max_bracket_ns: u64,
|
||||
max_internal_interrupt_unbiased_difference_ns: u64,
|
||||
max_wall_interrupt_difference_ns: u64,
|
||||
max_interrupt_unbiased_difference_ns: u64,
|
||||
}
|
||||
struct RawSeries {
|
||||
first: Option<RawCapture>,
|
||||
previous: Option<RawCapture>,
|
||||
rows: Vec<[u64; 5]>,
|
||||
max_bracket_ns: u64,
|
||||
max_internal_delta_ns: u64,
|
||||
max_wall_delta_ns: u64,
|
||||
max_unbiased_delta_ns: u64,
|
||||
}
|
||||
impl RawSeries {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
first: None,
|
||||
previous: None,
|
||||
rows: Vec::with_capacity(RAW_SAMPLES),
|
||||
max_bracket_ns: 0,
|
||||
max_internal_delta_ns: 0,
|
||||
max_wall_delta_ns: 0,
|
||||
max_unbiased_delta_ns: 0,
|
||||
}
|
||||
}
|
||||
fn push(&mut self, value: RawCapture) -> Result<()> {
|
||||
ensure!(self.rows.len() < RAW_SAMPLES, "TIME_RAW_SAMPLE_LIMIT");
|
||||
let (width, unbiased_width) = value.widths()?;
|
||||
ensure!(width <= RAW_BUDGET_NS, "TIME_RAW_CAPTURE_BUDGET");
|
||||
if let Some(previous) = self.previous {
|
||||
let wall = value
|
||||
.wall
|
||||
.checked_sub(previous.wall)
|
||||
.and_then(|v| v.checked_mul(100))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_RAW_WALL_BACKWARD"))?;
|
||||
let interrupt = value
|
||||
.ia
|
||||
.checked_sub(previous.ia)
|
||||
.and_then(|v| v.checked_mul(100))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_RAW_INTERRUPT_BACKWARD"))?;
|
||||
let unbiased = value
|
||||
.ua
|
||||
.checked_sub(previous.ua)
|
||||
.and_then(|v| v.checked_mul(100))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_RAW_UNBIASED_BACKWARD"))?;
|
||||
ensure!(
|
||||
value.ib >= previous.ia && value.ub >= previous.ua,
|
||||
"TIME_RAW_COUNTER_ORDER"
|
||||
);
|
||||
self.max_wall_delta_ns = self.max_wall_delta_ns.max(wall.abs_diff(interrupt));
|
||||
self.max_unbiased_delta_ns =
|
||||
self.max_unbiased_delta_ns.max(interrupt.abs_diff(unbiased));
|
||||
ensure!(
|
||||
wall.abs_diff(interrupt) <= SECOND_NS && interrupt.abs_diff(unbiased) <= SECOND_NS,
|
||||
"TIME_RAW_DISCONTINUITY"
|
||||
);
|
||||
}
|
||||
self.first.get_or_insert(value);
|
||||
self.previous = Some(value);
|
||||
self.max_bracket_ns = self.max_bracket_ns.max(width);
|
||||
self.max_internal_delta_ns = self
|
||||
.max_internal_delta_ns
|
||||
.max(width.abs_diff(unbiased_width));
|
||||
self.rows.push(value.row());
|
||||
Ok(())
|
||||
}
|
||||
fn finish(self, elapsed_monotonic_ns: u64) -> Result<CaptureDiagnostics> {
|
||||
ensure!(
|
||||
self.rows.len() == RAW_SAMPLES && elapsed_monotonic_ns <= RAW_BUDGET_NS,
|
||||
"TIME_RAW_INCOMPLETE_OR_EXPIRED"
|
||||
);
|
||||
let first = self
|
||||
.first
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_RAW_EMPTY"))?;
|
||||
let last = self
|
||||
.previous
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_RAW_EMPTY"))?;
|
||||
let elapsed_interrupt_ns = last
|
||||
.ia
|
||||
.checked_sub(first.ib)
|
||||
.and_then(|v| v.checked_mul(100))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_RAW_COUNTER_RANGE"))?;
|
||||
ensure!(
|
||||
elapsed_interrupt_ns <= RAW_BUDGET_NS,
|
||||
"TIME_RAW_INTERRUPT_BUDGET"
|
||||
);
|
||||
Ok(CaptureDiagnostics {
|
||||
schema_version: 1,
|
||||
calibrated: false,
|
||||
clock_authority: false,
|
||||
columns_100ns: [
|
||||
"wall_filetime",
|
||||
"interrupt_before",
|
||||
"unbiased_before",
|
||||
"unbiased_after",
|
||||
"interrupt_after",
|
||||
],
|
||||
samples_100ns: self.rows,
|
||||
sample_count: RAW_SAMPLES as u32,
|
||||
elapsed_monotonic_ns,
|
||||
elapsed_interrupt_ns,
|
||||
max_bracket_ns: self.max_bracket_ns,
|
||||
max_internal_interrupt_unbiased_difference_ns: self.max_internal_delta_ns,
|
||||
max_wall_interrupt_difference_ns: self.max_wall_delta_ns,
|
||||
max_interrupt_unbiased_difference_ns: self.max_unbiased_delta_ns,
|
||||
})
|
||||
}
|
||||
}
|
||||
/// Exactly128 bounded captures, spaced at fixed1ms target offsets. A missed
|
||||
/// 250ms own-monotonic budget aborts; no sleep/time-setting API is used.
|
||||
#[cfg(windows)]
|
||||
pub fn diagnostic_capture_series() -> Result<CaptureDiagnostics> {
|
||||
let started = std::time::Instant::now();
|
||||
let budget = std::time::Duration::from_nanos(RAW_BUDGET_NS);
|
||||
let mut series = RawSeries::new();
|
||||
for index in 0..RAW_SAMPLES {
|
||||
let due = std::time::Duration::from_millis(index as u64);
|
||||
loop {
|
||||
let elapsed = started.elapsed();
|
||||
ensure!(elapsed <= budget, "TIME_RAW_MONOTONIC_BUDGET");
|
||||
if elapsed >= due {
|
||||
break;
|
||||
}
|
||||
std::hint::spin_loop();
|
||||
}
|
||||
series.push(raw_capture()?)?;
|
||||
}
|
||||
series.finish(started.elapsed().as_nanos().try_into()?)
|
||||
}
|
||||
|
||||
#[cfg(all(windows, feature = "system-helper"))]
|
||||
mod installed {
|
||||
use super::*;
|
||||
@@ -239,24 +443,10 @@ mod installed {
|
||||
PROCESS.get_or_init(|| Mutex::new(Continuity::default()))
|
||||
}
|
||||
fn capture(base_error_ns: u64) -> Result<Sample> {
|
||||
#[link(name = "kernel32")]
|
||||
unsafe extern "system" {
|
||||
fn GetSystemTimePreciseAsFileTime(value: *mut u64);
|
||||
fn QueryInterruptTimePrecise(value: *mut u64);
|
||||
fn QueryUnbiasedInterruptTimePrecise(value: *mut u64);
|
||||
}
|
||||
let (mut ib, mut ia, mut ub, mut ua, mut wall) = (0, 0, 0, 0, 0);
|
||||
// SAFETY: documented VOID kernel getters with valid aligned u64
|
||||
// outputs. Windows 10+ is required; no optional API fallback exists.
|
||||
unsafe {
|
||||
QueryInterruptTimePrecise(&mut ib);
|
||||
QueryUnbiasedInterruptTimePrecise(&mut ub);
|
||||
GetSystemTimePreciseAsFileTime(&mut wall);
|
||||
QueryUnbiasedInterruptTimePrecise(&mut ua);
|
||||
QueryInterruptTimePrecise(&mut ia);
|
||||
}
|
||||
bracket_sample(wall, ib, ia, ub, ua, base_error_ns)
|
||||
let raw = raw_capture()?;
|
||||
bracket_sample(raw.wall, raw.ib, raw.ia, raw.ub, raw.ua, base_error_ns)
|
||||
}
|
||||
|
||||
fn observed(boot: Uuid, base_error_ns: u64) -> Result<(Sample, u64)> {
|
||||
let mut state = process()
|
||||
.lock()
|
||||
@@ -560,6 +750,63 @@ mod tests {
|
||||
capture_error_ns: 1_000_000,
|
||||
}
|
||||
}
|
||||
fn raw(index: u64) -> RawCapture {
|
||||
RawCapture {
|
||||
wall: 134_444_736_000_000_000 + index * 10_000,
|
||||
ib: 10_000_000 + index * 10_000,
|
||||
ia: 10_000_010 + index * 10_000,
|
||||
ub: 9_000_000 + index * 10_000,
|
||||
ua: 9_000_008 + index * 10_000,
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn raw_capture_denies_backward_wall_counters_and_incomplete_or_expired_series() {
|
||||
let mut series = RawSeries::new();
|
||||
series.push(raw(0)).unwrap();
|
||||
assert!(
|
||||
series
|
||||
.push(RawCapture {
|
||||
wall: raw(0).wall - 1,
|
||||
..raw(1)
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
let mut series = RawSeries::new();
|
||||
series.push(raw(0)).unwrap();
|
||||
assert!(
|
||||
series
|
||||
.push(RawCapture {
|
||||
ia: raw(0).ia - 1,
|
||||
ib: raw(0).ib - 1,
|
||||
..raw(1)
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
assert!(RawSeries::new().finish(0).is_err());
|
||||
let mut series = RawSeries::new();
|
||||
for index in 0..RAW_SAMPLES as u64 {
|
||||
series.push(raw(index)).unwrap();
|
||||
}
|
||||
assert!(series.finish(RAW_BUDGET_NS + 1).is_err());
|
||||
}
|
||||
#[test]
|
||||
fn complete_raw_report_is_bounded_non_authoritative_and_keeps_actual_brackets() {
|
||||
let mut series = RawSeries::new();
|
||||
for index in 0..RAW_SAMPLES as u64 {
|
||||
series.push(raw(index)).unwrap();
|
||||
}
|
||||
assert!(series.push(raw(RAW_SAMPLES as u64)).is_err());
|
||||
let report = series.finish(128_000_000).unwrap();
|
||||
assert_eq!(report.sample_count, 128);
|
||||
assert_eq!(report.max_bracket_ns, 1000);
|
||||
assert_eq!(report.elapsed_interrupt_ns, 127_001_000);
|
||||
assert_eq!(report.max_internal_interrupt_unbiased_difference_ns, 200);
|
||||
let value = serde_json::to_value(&report).unwrap();
|
||||
assert_eq!(value["calibrated"], false);
|
||||
assert_eq!(value["clock_authority"], false);
|
||||
assert!(value.get("capture_error_ns").is_none());
|
||||
assert!(serde_json::to_vec(&report).unwrap().len() < 16384);
|
||||
}
|
||||
#[test]
|
||||
fn job_key_cannot_be_reintroduced_as_a_time_key_with_different_hex_case() {
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user