@@ -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 > { }
@@ -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 (
0 commit comments