Skip to content

Commit 669191b

Browse files
committed
Auto merge of #1331 - samrat:macos-mach-timebase-info, r=RalfJung
Implement `mach_timebase_info` for macOS Since we return nanoseceonds instead of ticks from `mach_absolute_time`, we don't need to scale the absolute time Fixes #1288
2 parents 4ed3d48 + fff45b7 commit 669191b

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

src/shims/foreign_items/posix/macos.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6060
this.write_scalar(Scalar::from_u64(result), dest)?;
6161
}
6262

63+
"mach_timebase_info" => {
64+
let result = this.mach_timebase_info(args[0])?;
65+
this.write_scalar(Scalar::from_i32(result), dest)?;
66+
},
67+
6368
// Access to command-line arguments
6469
"_NSGetArgc" => {
6570
this.write_scalar(this.machine.argc.expect("machine must be initialized"), dest)?;

src/shims/time.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub fn system_time_to_duration<'tcx>(time: &SystemTime) -> InterpResult<'tcx, Du
1313
.map_err(|_| err_unsup_format!("times before the Unix epoch are not supported").into())
1414
}
1515

16+
1617
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
1718
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
1819
fn clock_gettime(
@@ -159,4 +160,24 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
159160
u64::try_from(duration.as_nanos())
160161
.map_err(|_| err_unsup_format!("programs running longer than 2^64 nanoseconds are not supported").into())
161162
}
163+
164+
fn mach_timebase_info(&mut self, info_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
165+
let this = self.eval_context_mut();
166+
167+
this.assert_target_os("macos", "mach_timebase_info");
168+
this.check_no_isolation("mach_timebase_info")?;
169+
170+
let info = this.deref_operand(info_op)?;
171+
172+
// Since our emulated ticks in `mach_absolute_time` *are* nanoseconds,
173+
// no scaling needs to happen.
174+
let (numer, denom) = (1,1);
175+
let imms = [
176+
immty_from_int_checked(numer, this.machine.layouts.u32)?,
177+
immty_from_int_checked(denom, this.machine.layouts.u32)?
178+
];
179+
180+
this.write_packed_immediates(info, &imms)?;
181+
Ok(0) // KERN_SUCCESS
182+
}
162183
}

tests/run-pass/time.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@ fn main() {
2424
for _ in 0..10 { drop(vec![42]); }
2525
let now2 = Instant::now();
2626
assert!(now2 > now1);
27-
28-
#[cfg(not(target_os = "macos"))] // TODO: macOS does not support Instant subtraction
29-
{
30-
let diff = now2.duration_since(now1);
31-
assert_eq!(now1 + diff, now2);
32-
assert_eq!(now2 - diff, now1);
33-
// Sanity-check the difference we got.
34-
assert!(diff.as_micros() > 1);
35-
assert!(diff.as_micros() < 1_000_000);
36-
}
27+
let diff = now2.duration_since(now1);
28+
assert_eq!(now1 + diff, now2);
29+
assert_eq!(now2 - diff, now1);
30+
// Sanity-check the difference we got.
31+
assert!(diff.as_micros() > 1);
32+
assert!(diff.as_micros() < 1_000_000);
3733
}

0 commit comments

Comments
 (0)