From 5f6d250b303d839896ead366153997d0e8e013e0 Mon Sep 17 00:00:00 2001 From: Samrat Man Singh Date: Mon, 13 Apr 2020 21:18:34 +0530 Subject: [PATCH 1/4] [macOS] Implement `mach_timebase_info` Since we return nanoseceonds instead of ticks from `mach_absolute_time`, we don't need to scale the absolute time --- src/shims/foreign_items/posix/macos.rs | 5 +++++ src/shims/time.rs | 22 ++++++++++++++++++++++ tests/run-pass/time.rs | 15 ++++++--------- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/shims/foreign_items/posix/macos.rs b/src/shims/foreign_items/posix/macos.rs index 9810a77ffde1..e7baacf7274d 100644 --- a/src/shims/foreign_items/posix/macos.rs +++ b/src/shims/foreign_items/posix/macos.rs @@ -60,6 +60,11 @@ fn emulate_foreign_item_by_name( 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)?; diff --git a/src/shims/time.rs b/src/shims/time.rs index 0d7a4929e4e0..adcca21fb4c0 100644 --- a/src/shims/time.rs +++ b/src/shims/time.rs @@ -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( @@ -159,4 +160,25 @@ fn mach_absolute_time(&self) -> InterpResult<'tcx, u64> { 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 we return nanoseceonds instead of ticks from + // `mach_absolute_time`, we don't need to scale the absolute + // time. + let (numer, denom) = (1,1); + let imms = [ + immty_from_int_checked(numer, this.libc_ty_layout("uint32_t")?)?, + immty_from_int_checked(denom, this.libc_ty_layout("uint32_t")?)? + ]; + + this.write_packed_immediates(info, &imms)?; + Ok(0) + } } diff --git a/tests/run-pass/time.rs b/tests/run-pass/time.rs index aa02ac15388e..9ae64fbae42a 100644 --- a/tests/run-pass/time.rs +++ b/tests/run-pass/time.rs @@ -25,13 +25,10 @@ fn main() { 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); } From f6bb8111f280653c86f220847bd4eb04fa9bebca Mon Sep 17 00:00:00 2001 From: Samrat Man Singh Date: Tue, 14 Apr 2020 09:40:40 +0530 Subject: [PATCH 2/4] Use pre-defined u32 layout Also fix typo and remove newline --- src/shims/time.rs | 7 ++++--- tests/run-pass/time.rs | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/shims/time.rs b/src/shims/time.rs index adcca21fb4c0..835541f9a957 100644 --- a/src/shims/time.rs +++ b/src/shims/time.rs @@ -169,13 +169,14 @@ fn mach_timebase_info(&mut self, info_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, let info = this.deref_operand(info_op)?; - // Since we return nanoseceonds instead of ticks from + // Since we return nanoseconds instead of ticks from // `mach_absolute_time`, we don't need to scale the absolute // time. let (numer, denom) = (1,1); + let uint32_layout = this.layout_of(this.tcx.types.u32)?; let imms = [ - immty_from_int_checked(numer, this.libc_ty_layout("uint32_t")?)?, - immty_from_int_checked(denom, this.libc_ty_layout("uint32_t")?)? + immty_from_int_checked(numer, uint32_layout)?, + immty_from_int_checked(denom, uint32_layout)? ]; this.write_packed_immediates(info, &imms)?; diff --git a/tests/run-pass/time.rs b/tests/run-pass/time.rs index 9ae64fbae42a..2c9b579f7e29 100644 --- a/tests/run-pass/time.rs +++ b/tests/run-pass/time.rs @@ -24,7 +24,6 @@ fn main() { for _ in 0..10 { drop(vec![42]); } let now2 = Instant::now(); assert!(now2 > now1); - let diff = now2.duration_since(now1); assert_eq!(now1 + diff, now2); assert_eq!(now2 - diff, now1); From 90729bb0394d64ee93026410480e8155c92ab7e5 Mon Sep 17 00:00:00 2001 From: Samrat Man Singh Date: Tue, 14 Apr 2020 13:31:24 +0530 Subject: [PATCH 3/4] Use precomputed TyLayout from `machine.layouts` And add comment documenting successful return value from `mach_timebase_info`. --- src/shims/time.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/shims/time.rs b/src/shims/time.rs index 835541f9a957..c22ac9ca1a50 100644 --- a/src/shims/time.rs +++ b/src/shims/time.rs @@ -173,13 +173,12 @@ fn mach_timebase_info(&mut self, info_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, // `mach_absolute_time`, we don't need to scale the absolute // time. let (numer, denom) = (1,1); - let uint32_layout = this.layout_of(this.tcx.types.u32)?; let imms = [ - immty_from_int_checked(numer, uint32_layout)?, - immty_from_int_checked(denom, uint32_layout)? + 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) + Ok(0) // KERN_SUCCESS } } From fff45b77adc7a08b1ee2d3c277e74f2ec7027ea2 Mon Sep 17 00:00:00 2001 From: Samrat Man Singh Date: Tue, 14 Apr 2020 13:59:43 +0530 Subject: [PATCH 4/4] Reword comment in mach_timebase_info Co-Authored-By: Ralf Jung --- src/shims/time.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/shims/time.rs b/src/shims/time.rs index c22ac9ca1a50..a87db9878202 100644 --- a/src/shims/time.rs +++ b/src/shims/time.rs @@ -169,9 +169,8 @@ fn mach_timebase_info(&mut self, info_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, let info = this.deref_operand(info_op)?; - // Since we return nanoseconds instead of ticks from - // `mach_absolute_time`, we don't need to scale the absolute - // time. + // 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)?,