Skip to content

Commit 8093a59

Browse files
committed
move gen_random to helpers
1 parent b3c3c33 commit 8093a59

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> {}
@@ -65,6 +67,40 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6567
})
6668
}
6769

70+
/// Generate some random bytes, and write them to `dest`.
71+
fn gen_random(
72+
&mut self,
73+
len: usize,
74+
dest: Scalar<Tag>,
75+
) -> InterpResult<'tcx> {
76+
if len == 0 {
77+
// Nothing to do
78+
return Ok(());
79+
}
80+
let this = self.eval_context_mut();
81+
let ptr = dest.to_ptr()?;
82+
83+
let data = match &mut this.memory_mut().extra.rng {
84+
Some(rng) => {
85+
let mut rng = rng.borrow_mut();
86+
let mut data = vec![0; len];
87+
rng.fill_bytes(&mut data);
88+
data
89+
}
90+
None => {
91+
return err!(Unimplemented(
92+
"miri does not support gathering system entropy in deterministic mode!
93+
Use '-Zmiri-seed=<seed>' to enable random number generation.
94+
WARNING: Miri does *not* generate cryptographically secure entropy -
95+
do not use Miri to run any program that needs secure random number generation".to_owned(),
96+
));
97+
}
98+
};
99+
let tcx = &{this.tcx.tcx};
100+
this.memory_mut().get_mut(ptr.alloc_id)?
101+
.write_bytes(tcx, ptr, &data)
102+
}
103+
68104
/// Visits the memory covered by `place`, sensitive to freezing: the 3rd parameter
69105
/// will be true if this is frozen, false if this is in an `UnsafeCell`.
70106
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> {}
@@ -986,37 +984,4 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
986984
}
987985
return Ok(None);
988986
}
989-
990-
fn gen_random(
991-
&mut self,
992-
len: usize,
993-
dest: Scalar<Tag>,
994-
) -> InterpResult<'tcx> {
995-
if len == 0 {
996-
// Nothing to do
997-
return Ok(());
998-
}
999-
let this = self.eval_context_mut();
1000-
let ptr = dest.to_ptr()?;
1001-
1002-
let data = match &mut this.memory_mut().extra.rng {
1003-
Some(rng) => {
1004-
let mut rng = rng.borrow_mut();
1005-
let mut data = vec![0; len];
1006-
rng.fill_bytes(&mut data);
1007-
data
1008-
}
1009-
None => {
1010-
return err!(Unimplemented(
1011-
"miri does not support gathering system entropy in deterministic mode!
1012-
Use '-Zmiri-seed=<seed>' to enable random number generation.
1013-
WARNING: Miri does *not* generate cryptographically secure entropy -
1014-
do not use Miri to run any program that needs secure random number generation".to_owned(),
1015-
));
1016-
}
1017-
};
1018-
let tcx = &{this.tcx.tcx};
1019-
this.memory_mut().get_mut(ptr.alloc_id)?
1020-
.write_bytes(tcx, ptr, &data)
1021-
}
1022987
}

0 commit comments

Comments
 (0)