Skip to content

Commit e56728b

Browse files
committed
move gen_random to helpers
1 parent 73fce77 commit e56728b

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
@@ -5,8 +5,6 @@ use rustc::mir;
55
use syntax::attr;
66
use syntax::symbol::sym;
77

8-
use rand::RngCore;
9-
108
use crate::*;
119

1210
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
@@ -992,37 +990,4 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
992990
}
993991
return Ok(None);
994992
}
995-
996-
fn gen_random(
997-
&mut self,
998-
len: usize,
999-
dest: Scalar<Tag>,
1000-
) -> InterpResult<'tcx> {
1001-
if len == 0 {
1002-
// Nothing to do
1003-
return Ok(());
1004-
}
1005-
let this = self.eval_context_mut();
1006-
let ptr = dest.to_ptr()?;
1007-
1008-
let data = match &mut this.memory_mut().extra.rng {
1009-
Some(rng) => {
1010-
let mut rng = rng.borrow_mut();
1011-
let mut data = vec![0; len];
1012-
rng.fill_bytes(&mut data);
1013-
data
1014-
}
1015-
None => {
1016-
return err!(Unimplemented(
1017-
"miri does not support gathering system entropy in deterministic mode!
1018-
Use '-Zmiri-seed=<seed>' to enable random number generation.
1019-
WARNING: Miri does *not* generate cryptographically secure entropy -
1020-
do not use Miri to run any program that needs secure random number generation".to_owned(),
1021-
));
1022-
}
1023-
};
1024-
let tcx = &{this.tcx.tcx};
1025-
this.memory_mut().get_mut(ptr.alloc_id)?
1026-
.write_bytes(tcx, ptr, &data)
1027-
}
1028993
}

0 commit comments

Comments
 (0)