Skip to content

Add support for 'std::time::Instant' in Windows #1294

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 6 commits into from
Apr 3, 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
13 changes: 8 additions & 5 deletions src/shims/foreign_items/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,22 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let result = this.GetEnvironmentVariableW(args[0], args[1], args[2])?;
this.write_scalar(Scalar::from_u32(result), dest)?;
}

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

"GetEnvironmentStringsW" => {
let result = this.GetEnvironmentStringsW()?;
this.write_scalar(result, dest)?;
}

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

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

"SetCurrentDirectoryW" => {
let result = this.SetCurrentDirectoryW(args[0])?;
this.write_scalar(Scalar::from_i32(result), dest)?;
Expand Down Expand Up @@ -171,6 +166,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
"GetSystemTimeAsFileTime" => {
this.GetSystemTimeAsFileTime(args[0])?;
}
"QueryPerformanceCounter" => {
let result = this.QueryPerformanceCounter(args[0])?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"QueryPerformanceFrequency" => {
let result = this.QueryPerformanceFrequency(args[0])?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}

// Miscellaneous
"SystemFunction036" => {
Expand Down
32 changes: 32 additions & 0 deletions src/shims/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,38 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
Ok(())
}

#[allow(non_snake_case)]
fn QueryPerformanceCounter(&mut self, lpPerformanceCount_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();

this.assert_target_os("windows", "QueryPerformanceCounter");
this.check_no_isolation("QueryPerformanceCounter")?;

// QueryPerformanceCounter uses a hardware counter as its basis.
// Miri will emulate a counter with a resolution of 1 nanosecond.
let duration = Instant::now().duration_since(this.machine.time_anchor);
let qpc = i64::try_from(duration.as_nanos())
.map_err(|_| err_unsup_format!("programs running longer than 2^63 nanoseconds are not supported"))?;
this.write_scalar(Scalar::from_i64(qpc), this.deref_operand(lpPerformanceCount_op)?.into())?;
Ok(-1) // return non-zero on success
}

#[allow(non_snake_case)]
fn QueryPerformanceFrequency(&mut self, lpFrequency_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();

this.assert_target_os("windows", "QueryPerformanceFrequency");
this.check_no_isolation("QueryPerformanceFrequency")?;

// Retrieves the frequency of the hardware performance counter.
// The frequency of the performance counter is fixed at system boot and
// is consistent across all processors.
// Miri emulates a "hardware" performance counter with a resolution of 1ns,
// and thus 10^9 counts per second.
this.write_scalar(Scalar::from_i64(1_000_000_000), this.deref_operand(lpFrequency_op)?.into())?;
Ok(-1) // Return non-zero on success
}

fn mach_absolute_time(&self) -> InterpResult<'tcx, u64> {
let this = self.eval_context_ref();

Expand Down
29 changes: 13 additions & 16 deletions tests/run-pass/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,19 @@ fn main() {
assert!(2020 <= year && year < 2100);

// Check `Instant`.
#[cfg(not(windows))] // `Instant` shims not yet implemented on Windows
{
let now1 = Instant::now();
// Do some work to make time pass.
for _ in 0..10 { drop(vec![42]); }
let now2 = Instant::now();
assert!(now2 > now1);
let now1 = Instant::now();
// Do some work to make time pass.
for _ in 0..10 { drop(vec![42]); }
let now2 = Instant::now();
assert!(now2 > now1);

#[cfg(target_os = "linux")] // 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);
}
#[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);
}
}