From adfa2eb062bf825bb100dc2cfd7ab7df075f1824 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 7 Oct 2019 16:22:53 -0500 Subject: [PATCH] Return negative times when the current time is before the unix epoch --- src/shims/foreign_items.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index c106a296dcd..c403afacd89 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -524,21 +524,30 @@ fn emulate_foreign_item( let allocation = this.memory_mut().get_mut(tp.alloc_id)?; + let mut sign = 1; + let duration = std::time::SystemTime::now() .duration_since(std::time::SystemTime::UNIX_EPOCH) - .unwrap_or_else(|_| bug!("Clock went backwards")); + .unwrap_or_else(|e| { + sign = -1; + e.duration() + }); + + let tv_sec_size = Size::from_bits(64); + let tv_nsec_size = Size::from_bits(64); allocation.write_scalar( tcx, tp, - Scalar::from_u64(duration.as_secs()).into(), - Size::from_bits(64), + Scalar::from_int(sign * (duration.as_secs() as i64), tv_sec_size).into(), + tv_sec_size, )?; + allocation.write_scalar( tcx, - tp.offset(Size::from_bits(64), tcx)?, - Scalar::from_u32(duration.subsec_nanos()).into(), - Size::from_bits(32), + tp.offset(tv_sec_size, tcx)?, + Scalar::from_int(duration.subsec_nanos() as i64, tv_nsec_size).into(), + tv_nsec_size, )?; this.write_scalar(Scalar::from_int(0i32, dest.layout.size), dest)?; -- 2.44.0