Skip to content

Commit c933c53

Browse files
committed
Return negative times when the current time is before the unix epoch
1 parent bfdc031 commit c933c53

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/shims/foreign_items.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -524,21 +524,30 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
524524

525525
let allocation = this.memory_mut().get_mut(tp.alloc_id)?;
526526

527+
let mut sign = 1;
528+
527529
let duration = std::time::SystemTime::now()
528530
.duration_since(std::time::SystemTime::UNIX_EPOCH)
529-
.unwrap_or_else(|_| bug!("Clock went backwards"));
531+
.unwrap_or_else(|e| {
532+
sign = -1;
533+
e.duration()
534+
});
535+
536+
let tv_sec_size = Size::from_bits(64);
537+
let tv_nsec_size = Size::from_bits(64);
530538

531539
allocation.write_scalar(
532540
tcx,
533541
tp,
534-
Scalar::from_u64(duration.as_secs()).into(),
535-
Size::from_bits(64),
542+
Scalar::from_int(sign * (duration.as_secs() as i64), tv_sec_size).into(),
543+
tv_sec_size,
536544
)?;
545+
537546
allocation.write_scalar(
538547
tcx,
539-
tp.offset(Size::from_bits(64), tcx)?,
540-
Scalar::from_u32(duration.subsec_nanos()).into(),
541-
Size::from_bits(32),
548+
tp.offset(tv_sec_size, tcx)?,
549+
Scalar::from_int(duration.subsec_nanos() as i64, tv_nsec_size).into(),
550+
tv_nsec_size,
542551
)?;
543552

544553
this.write_scalar(Scalar::from_int(0i32, dest.layout.size), dest)?;

0 commit comments

Comments
 (0)