Skip to content

Commit e623757

Browse files
committed
Add gettimeofday shim for macOS
1 parent 5971474 commit e623757

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/shims/foreign_items.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,58 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
559559
}
560560
}
561561

562+
"gettimeofday" => {
563+
if !this.machine.communicate {
564+
throw_unsup_format!("`gettimeofday` not available when isolation is enabled")
565+
} else {
566+
let tcx = &{ this.tcx.tcx };
567+
568+
let tz = this.read_scalar(args[1])?.not_undef()?;
569+
// Using tz is obsolete and should always be null
570+
if !this.is_null(tz)? {
571+
let einval = this.eval_libc("EINVAL")?;
572+
this.set_last_error(einval)?;
573+
this.write_scalar(Scalar::from_int(-1i32, dest.layout.size), dest)?;
574+
} else {
575+
let tv = this.force_ptr(this.read_scalar(args[0])?.not_undef()?)?;
576+
577+
let time_t = this.resolve_path(&["libc", "time_t"])?.ty(*tcx);
578+
let suseconds_t = this.resolve_path(&["libc", "suseconds_t"])?.ty(*tcx);
579+
580+
let tv_sec_size = this.layout_of(time_t)?.size;
581+
let tv_usec_size = this.layout_of(suseconds_t)?.size;
582+
583+
let allocation = this.memory_mut().get_mut(tv.alloc_id)?;
584+
585+
let mut sign = 1;
586+
587+
let duration = std::time::SystemTime::now()
588+
.duration_since(std::time::SystemTime::UNIX_EPOCH)
589+
.unwrap_or_else(|e| {
590+
sign = -1;
591+
e.duration()
592+
});
593+
594+
allocation.write_scalar(
595+
tcx,
596+
tv,
597+
Scalar::from_int(sign * (duration.as_secs() as i64), tv_sec_size)
598+
.into(),
599+
tv_sec_size,
600+
)?;
601+
602+
allocation.write_scalar(
603+
tcx,
604+
tv.offset(tv_sec_size, tcx)?,
605+
Scalar::from_int(duration.subsec_micros() as i64, tv_usec_size).into(),
606+
tv_usec_size,
607+
)?;
608+
609+
this.write_scalar(Scalar::from_int(0i32, dest.layout.size), dest)?;
610+
}
611+
}
612+
}
613+
562614
"strlen" => {
563615
let ptr = this.read_scalar(args[0])?.not_undef()?;
564616
let n = this.memory().read_c_str(ptr)?.len();

0 commit comments

Comments
 (0)