]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #1290 - RalfJung:rustup, r=RalfJung
authorbors <bors@rust-lang.org>
Wed, 1 Apr 2020 17:59:33 +0000 (17:59 +0000)
committerbors <bors@rust-lang.org>
Wed, 1 Apr 2020 17:59:33 +0000 (17:59 +0000)
Rustup

Also remove ICEing `breakpoint` intrinsic shim.

src/helpers.rs
src/shims/env.rs
src/shims/foreign_items/windows.rs
src/shims/time.rs
tests/run-pass/time.rs

index e95b0d850b68f970f9f78f029b9fa6618b8579e6..568e9c925d55a28e6bfc114e82a3ddd395687866 100644 (file)
@@ -73,17 +73,23 @@ fn eval_libc(&mut self, name: &str) -> InterpResult<'tcx, Scalar<Tag>> {
             .not_undef()
     }
 
+    /// Helper function to get a `libc` constant as an `i32`.
+    fn eval_libc_i32(&mut self, name: &str) -> InterpResult<'tcx, i32> {
+        // TODO: Cache the result.
+        self.eval_libc(name)?.to_i32()
+    }
+
     /// Helper function to get a `windows` constant as a `Scalar`.
-    fn eval_windows(&mut self, name: &str) -> InterpResult<'tcx, Scalar<Tag>> {
+    fn eval_windows(&mut self, module: &str, name: &str) -> InterpResult<'tcx, Scalar<Tag>> {
         self.eval_context_mut()
-            .eval_path_scalar(&["std", "sys", "windows", "c", name])?
+            .eval_path_scalar(&["std", "sys", "windows", module, name])?
             .not_undef()
     }
 
-    /// Helper function to get a `libc` constant as an `i32`.
-    fn eval_libc_i32(&mut self, name: &str) -> InterpResult<'tcx, i32> {
+    /// Helper function to get a `windows` constant as an `u64`.
+    fn eval_windows_u64(&mut self, module: &str, name: &str) -> InterpResult<'tcx, u64> {
         // TODO: Cache the result.
-        self.eval_libc(name)?.to_i32()
+        self.eval_windows(module, name)?.to_u64()
     }
 
     /// Helper function to get the `TyAndLayout` of a `libc` type
@@ -93,6 +99,13 @@ fn libc_ty_layout(&mut self, name: &str) -> InterpResult<'tcx, TyAndLayout<'tcx>
         this.layout_of(ty)
     }
 
+    /// Helper function to get the `TyAndLayout` of a `windows` type
+    fn windows_ty_layout(&mut self, name: &str) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
+        let this = self.eval_context_mut();
+        let ty = this.resolve_path(&["std", "sys", "windows", "c", name]).monomorphic_ty(*this.tcx);
+        this.layout_of(ty)
+    }
+
     /// Write a 0 of the appropriate size to `dest`.
     fn write_null(&mut self, dest: PlaceTy<'tcx, Tag>) -> InterpResult<'tcx> {
         self.eval_context_mut().write_scalar(Scalar::from_int(0, dest.layout.size), dest)
@@ -350,7 +363,7 @@ fn visit_union(&mut self, v: MPlaceTy<'tcx, Tag>, fields: usize) -> InterpResult
         }
     }
 
-    // Writes several `ImmTy`s contiguosly into memory. This is useful when you have to pack
+    // Writes several `ImmTy`s contiguously into memory. This is useful when you have to pack
     // different values into a struct.
     fn write_packed_immediates(
         &mut self,
@@ -439,7 +452,7 @@ fn set_last_error_from_io_error(&mut self, e: std::io::Error) -> InterpResult<'t
             })?
         } else if target_os == "windows" {
             // FIXME: we have to finish implementing the Windows equivalent of this.
-            this.eval_windows(match e.kind() {
+            this.eval_windows("c", match e.kind() {
                 NotFound => "ERROR_FILE_NOT_FOUND",
                 _ => throw_unsup_format!("io error {} cannot be transformed into a raw os error", e)
             })?
index d30e953cf56a138b3be9b885ecd9d185df4affe5..3ffe4fc421f99ecee3858daf46d790669abab32d 100644 (file)
@@ -143,7 +143,7 @@ fn GetEnvironmentVariableW(
                 windows_check_buffer_size(this.write_os_str_to_wide_str(&var, buf_ptr, buf_size)?)
             }
             None => {
-                let envvar_not_found = this.eval_windows("ERROR_ENVVAR_NOT_FOUND")?;
+                let envvar_not_found = this.eval_windows("c", "ERROR_ENVVAR_NOT_FOUND")?;
                 this.set_last_error(envvar_not_found)?;
                 0 // return zero upon failure
             }
index f39d477f8278e367212b2d24093e165dabcbbb1c..ee39773d71f8374cc68d955eb4d8b1129c1c63ff 100644 (file)
@@ -167,6 +167,11 @@ fn emulate_foreign_item_by_name(
                 )?;
             }
 
+            // Time related shims
+            "GetSystemTimeAsFileTime" => {
+                this.GetSystemTimeAsFileTime(args[0])?;
+            }
+
             // Miscellaneous
             "SystemFunction036" => {
                 // The actual name of 'RtlGenRandom'
index 58db60e516883f31d2e74c744fb683ad6e6535c1..d501fa8a0fdb88f32ed55f4253fd3be2efe69223 100644 (file)
@@ -3,7 +3,9 @@
 
 use crate::stacked_borrows::Tag;
 use crate::*;
-use helpers::immty_from_int_checked;
+use helpers::{immty_from_int_checked, immty_from_uint_checked};
+
+use rustc_middle::ty::layout::LayoutOf;
 
 /// Returns the time elapsed between the provided time and the unix epoch as a `Duration`.
 pub fn system_time_to_duration<'tcx>(time: &SystemTime) -> InterpResult<'tcx, Duration> {
@@ -85,6 +87,36 @@ fn gettimeofday(
         Ok(0)
     }
 
+    #[allow(non_snake_case)]
+    fn GetSystemTimeAsFileTime(&mut self, LPFILETIME_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx> {
+        let this = self.eval_context_mut();
+
+        this.assert_target_os("windows", "GetSystemTimeAsFileTime");
+        this.check_no_isolation("GetSystemTimeAsFileTime")?;
+
+        let NANOS_PER_SEC = this.eval_windows_u64("time", "NANOS_PER_SEC")?;
+        let INTERVALS_PER_SEC = this.eval_windows_u64("time", "INTERVALS_PER_SEC")?;
+        let INTERVALS_TO_UNIX_EPOCH = this.eval_windows_u64("time", "INTERVALS_TO_UNIX_EPOCH")?;
+        let NANOS_PER_INTERVAL = NANOS_PER_SEC / INTERVALS_PER_SEC;
+        let SECONDS_TO_UNIX_EPOCH = INTERVALS_TO_UNIX_EPOCH / INTERVALS_PER_SEC;
+
+        let duration = system_time_to_duration(&SystemTime::now())?
+            .checked_add(Duration::from_secs(SECONDS_TO_UNIX_EPOCH))
+            .unwrap();        
+        let duration_ticks = u64::try_from(duration.as_nanos() / u128::from(NANOS_PER_INTERVAL))
+            .map_err(|_| err_unsup_format!("programs running longer than 2^64 ticks are not supported"))?;
+
+        let dwLowDateTime = u32::try_from(duration_ticks & 0x00000000FFFFFFFF).unwrap();
+        let dwHighDateTime = u32::try_from((duration_ticks & 0xFFFFFFFF00000000) >> 32).unwrap();
+        let DWORD_tylayout = this.layout_of(this.tcx.types.u32)?;
+        let imms = [
+            immty_from_uint_checked(dwLowDateTime, DWORD_tylayout)?,
+            immty_from_uint_checked(dwHighDateTime, DWORD_tylayout)?,
+        ];
+        this.write_packed_immediates(this.deref_operand(LPFILETIME_op)?, &imms)?;
+        Ok(())
+    }
+
     fn mach_absolute_time(&self) -> InterpResult<'tcx, u64> {
         let this = self.eval_context_ref();
 
index ca16f5ed5217b3c22d29e2ce3aeea034917e0da0..fc2059fa25612862628b462c01a905911c1e6168 100644 (file)
@@ -1,4 +1,3 @@
-// ignore-windows: TODO clock shims are not implemented on Windows
 // compile-flags: -Zmiri-disable-isolation
 
 use std::time::{SystemTime, Instant};
@@ -14,17 +13,20 @@ fn main() {
     assert_eq!(now1 + diff, now2);
     assert_eq!(now2 - diff, 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
+    #[cfg(not(windows))] // `Instant` shims not yet implemented on Windows
     {
-        let diff = now2.duration_since(now1);
-        assert!(diff.as_micros() > 0);
-        assert_eq!(now1 + diff, now2);
-        assert_eq!(now2 - diff, 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!(diff.as_micros() > 0);
+            assert_eq!(now1 + diff, now2);
+            assert_eq!(now2 - diff, now1);
+        }
     }
 }