]> git.lizzy.rs Git - rust.git/commitdiff
move gen_random to helpers
authorRalf Jung <post@ralfj.de>
Sun, 30 Jun 2019 21:28:24 +0000 (23:28 +0200)
committerRalf Jung <post@ralfj.de>
Sat, 6 Jul 2019 08:07:21 +0000 (10:07 +0200)
src/helpers.rs
src/shims/foreign_items.rs

index 3503af43690f996fc3e02ba57dfe51840bac2153..b2be90ca999e1769d5814a7eec8de9b5c244362c 100644 (file)
@@ -3,6 +3,8 @@
 use rustc::ty::{self, layout::{self, Size}};
 use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
 
+use rand::RngCore;
+
 use crate::*;
 
 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
@@ -65,6 +67,40 @@ fn test_null(&self, val: Scalar<Tag>) -> InterpResult<'tcx, Option<Scalar<Tag>>>
         })
     }
 
+    /// Generate some random bytes, and write them to `dest`.
+    fn gen_random(
+        &mut self,
+        len: usize,
+        dest: Scalar<Tag>,
+    ) -> InterpResult<'tcx>  {
+        if len == 0 {
+            // Nothing to do
+            return Ok(());
+        }
+        let this = self.eval_context_mut();
+        let ptr = dest.to_ptr()?;
+
+        let data = match &mut this.memory_mut().extra.rng {
+            Some(rng) => {
+                let mut rng = rng.borrow_mut();
+                let mut data = vec![0; len];
+                rng.fill_bytes(&mut data);
+                data
+            }
+            None => {
+                return err!(Unimplemented(
+                    "miri does not support gathering system entropy in deterministic mode!
+                    Use '-Zmiri-seed=<seed>' to enable random number generation.
+                    WARNING: Miri does *not* generate cryptographically secure entropy -
+                    do not use Miri to run any program that needs secure random number generation".to_owned(),
+                ));
+            }
+        };
+        let tcx = &{this.tcx.tcx};
+        this.memory_mut().get_mut(ptr.alloc_id)?
+            .write_bytes(tcx, ptr, &data)
+    }
+
     /// Visits the memory covered by `place`, sensitive to freezing: the 3rd parameter
     /// will be true if this is frozen, false if this is in an `UnsafeCell`.
     fn visit_freeze_sensitive(
index e31a34a601589616149f34f372bf526415540ec8..7ab97c87e305650c3c8f2b67e42f221f5b0515b1 100644 (file)
@@ -4,8 +4,6 @@
 use syntax::attr;
 use syntax::symbol::sym;
 
-use rand::RngCore;
-
 use crate::*;
 
 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
@@ -986,37 +984,4 @@ fn eval_path_scalar(&mut self, path: &[&str]) -> InterpResult<'tcx, Option<Scala
         }
         return Ok(None);
     }
-
-    fn gen_random(
-        &mut self,
-        len: usize,
-        dest: Scalar<Tag>,
-    ) -> InterpResult<'tcx>  {
-        if len == 0 {
-            // Nothing to do
-            return Ok(());
-        }
-        let this = self.eval_context_mut();
-        let ptr = dest.to_ptr()?;
-
-        let data = match &mut this.memory_mut().extra.rng {
-            Some(rng) => {
-                let mut rng = rng.borrow_mut();
-                let mut data = vec![0; len];
-                rng.fill_bytes(&mut data);
-                data
-            }
-            None => {
-                return err!(Unimplemented(
-                    "miri does not support gathering system entropy in deterministic mode!
-                    Use '-Zmiri-seed=<seed>' to enable random number generation.
-                    WARNING: Miri does *not* generate cryptographically secure entropy -
-                    do not use Miri to run any program that needs secure random number generation".to_owned(),
-                ));
-            }
-        };
-        let tcx = &{this.tcx.tcx};
-        this.memory_mut().get_mut(ptr.alloc_id)?
-            .write_bytes(tcx, ptr, &data)
-    }
 }
\ No newline at end of file