Skip to content

Implement mach_timebase_info for macOS #1331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/shims/foreign_items/posix/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_scalar(Scalar::from_u64(result), dest)?;
}

"mach_timebase_info" => {
let result = this.mach_timebase_info(args[0])?;
this.write_scalar(Scalar::from_i32(result), dest)?;
},

// Access to command-line arguments
"_NSGetArgc" => {
this.write_scalar(this.machine.argc.expect("machine must be initialized"), dest)?;
Expand Down
21 changes: 21 additions & 0 deletions src/shims/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn system_time_to_duration<'tcx>(time: &SystemTime) -> InterpResult<'tcx, Du
.map_err(|_| err_unsup_format!("times before the Unix epoch are not supported").into())
}


impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
fn clock_gettime(
Expand Down Expand Up @@ -159,4 +160,24 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
u64::try_from(duration.as_nanos())
.map_err(|_| err_unsup_format!("programs running longer than 2^64 nanoseconds are not supported").into())
}

fn mach_timebase_info(&mut self, info_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();

this.assert_target_os("macos", "mach_timebase_info");
this.check_no_isolation("mach_timebase_info")?;

let info = this.deref_operand(info_op)?;

// Since our emulated ticks in `mach_absolute_time` *are* nanoseconds,
// no scaling needs to happen.
let (numer, denom) = (1,1);
let imms = [
immty_from_int_checked(numer, this.machine.layouts.u32)?,
immty_from_int_checked(denom, this.machine.layouts.u32)?
];

this.write_packed_immediates(info, &imms)?;
Ok(0) // KERN_SUCCESS
}
}
16 changes: 6 additions & 10 deletions tests/run-pass/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ fn main() {
for _ in 0..10 { drop(vec![42]); }
let now2 = Instant::now();
assert!(now2 > now1);

#[cfg(not(target_os = "macos"))] // TODO: macOS does not support Instant subtraction
{
let diff = now2.duration_since(now1);
assert_eq!(now1 + diff, now2);
assert_eq!(now2 - diff, now1);
// Sanity-check the difference we got.
assert!(diff.as_micros() > 1);
assert!(diff.as_micros() < 1_000_000);
}
let diff = now2.duration_since(now1);
assert_eq!(now1 + diff, now2);
assert_eq!(now2 - diff, now1);
// Sanity-check the difference we got.
assert!(diff.as_micros() > 1);
assert!(diff.as_micros() < 1_000_000);
}