@@ -3,6 +3,8 @@ use std::mem;
33use rustc:: ty:: { self , layout:: { self , Size } } ;
44use rustc:: hir:: def_id:: { DefId , CRATE_DEF_INDEX } ;
55
6+ use rand:: RngCore ;
7+
68use crate :: * ;
79
810impl < ' 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 (
0 commit comments