Skip to content

Commit 2b55c41

Browse files
committed
move gen_random to helpers
1 parent 347f194 commit 2b55c41

File tree

2 files changed

+36
-35
lines changed

2 files changed

+36
-35
lines changed

src/helpers.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::mem;
33
use rustc::ty::{self, layout::{self, Size}};
44
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
55

6+
use rand::RngCore;
7+
68
use crate::*;
79

810
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
@@ -43,6 +45,40 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
4345
})
4446
}
4547

48+
/// Generate some random bytes, and write them to `dest`.
49+
fn gen_random(
50+
&mut self,
51+
len: usize,
52+
dest: Scalar<Tag>,
53+
) -> InterpResult<'tcx> {
54+
if len == 0 {
55+
// Nothing to do
56+
return Ok(());
57+
}
58+
let this = self.eval_context_mut();
59+
let ptr = dest.to_ptr()?;
60+
61+
let data = match &mut this.memory_mut().extra.rng {
62+
Some(rng) => {
63+
let mut rng = rng.borrow_mut();
64+
let mut data = vec![0; len];
65+
rng.fill_bytes(&mut data);
66+
data
67+
}
68+
None => {
69+
return err!(Unimplemented(
70+
"miri does not support gathering system entropy in deterministic mode!
71+
Use '-Zmiri-seed=<seed>' to enable random number generation.
72+
WARNING: Miri does *not* generate cryptographically secure entropy -
73+
do not use Miri to run any program that needs secure random number generation".to_owned(),
74+
));
75+
}
76+
};
77+
let tcx = &{this.tcx.tcx};
78+
this.memory_mut().get_mut(ptr.alloc_id)?
79+
.write_bytes(tcx, ptr, &data)
80+
}
81+
4682
/// Visits the memory covered by `place`, sensitive to freezing: the 3rd parameter
4783
/// will be true if this is frozen, false if this is in an `UnsafeCell`.
4884
fn visit_freeze_sensitive(

src/shims/foreign_items.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use rustc::mir;
44
use syntax::attr;
55
use syntax::symbol::sym;
66

7-
use rand::RngCore;
8-
97
use crate::*;
108

119
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
@@ -967,37 +965,4 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
967965
}
968966
return Ok(None);
969967
}
970-
971-
fn gen_random(
972-
&mut self,
973-
len: usize,
974-
dest: Scalar<Tag>,
975-
) -> InterpResult<'tcx> {
976-
if len == 0 {
977-
// Nothing to do
978-
return Ok(());
979-
}
980-
let this = self.eval_context_mut();
981-
let ptr = dest.to_ptr()?;
982-
983-
let data = match &mut this.memory_mut().extra.rng {
984-
Some(rng) => {
985-
let mut rng = rng.borrow_mut();
986-
let mut data = vec![0; len];
987-
rng.fill_bytes(&mut data);
988-
data
989-
}
990-
None => {
991-
return err!(Unimplemented(
992-
"miri does not support gathering system entropy in deterministic mode!
993-
Use '-Zmiri-seed=<seed>' to enable random number generation.
994-
WARNING: Miri does *not* generate cryptographically secure entropy -
995-
do not use Miri to run any program that needs secure random number generation".to_owned(),
996-
));
997-
}
998-
};
999-
let tcx = &{this.tcx.tcx};
1000-
this.memory_mut().get_mut(ptr.alloc_id)?
1001-
.write_bytes(tcx, ptr, &data)
1002-
}
1003968
}

0 commit comments

Comments
 (0)