@@ -25,7 +25,7 @@ mod memory;
25
25
26
26
use std:: collections:: HashMap ;
27
27
use std:: borrow:: Cow ;
28
- use std:: cell:: RefCell ;
28
+ use std:: cell:: Cell ;
29
29
use std:: rc:: Rc ;
30
30
31
31
use rand:: rngs:: StdRng ;
@@ -83,9 +83,11 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
83
83
let mut ecx = InterpretCx :: new (
84
84
tcx. at ( syntax:: source_map:: DUMMY_SP ) ,
85
85
ty:: ParamEnv :: reveal_all ( ) ,
86
- Evaluator :: new ( config. validate , config . seed ) ,
86
+ Evaluator :: new ( config. validate ) ,
87
87
) ;
88
88
89
+ ecx. memory_mut ( ) . extra . rng = config. seed . map ( StdRng :: seed_from_u64) ;
90
+
89
91
let main_instance = ty:: Instance :: mono ( ecx. tcx . tcx , main_id) ;
90
92
let main_mir = ecx. load_mir ( main_instance. def ) ?;
91
93
@@ -209,9 +211,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
209
211
cur_ptr = cur_ptr. offset ( char_size, tcx) ?;
210
212
}
211
213
}
212
-
213
- ecx. memory_mut ( ) . extra . seed = config. seed . clone ( ) ;
214
-
214
+
215
215
assert ! ( args. next( ) . is_none( ) , "start lang item has more arguments than expected" ) ;
216
216
217
217
Ok ( ecx)
@@ -347,14 +347,10 @@ pub struct Evaluator<'tcx> {
347
347
348
348
/// Whether to enforce the validity invariant.
349
349
pub ( crate ) validate : bool ,
350
-
351
- /// The random number generator to use if Miri
352
- /// is running in non-deterministic mode
353
- pub ( crate ) rng : Option < StdRng >
354
350
}
355
351
356
352
impl < ' tcx > Evaluator < ' tcx > {
357
- fn new ( validate : bool , seed : Option < u64 > ) -> Self {
353
+ fn new ( validate : bool ) -> Self {
358
354
Evaluator {
359
355
env_vars : HashMap :: default ( ) ,
360
356
argc : None ,
@@ -363,7 +359,6 @@ impl<'tcx> Evaluator<'tcx> {
363
359
last_error : 0 ,
364
360
tls : TlsData :: default ( ) ,
365
361
validate,
366
- rng : seed. map ( |s| StdRng :: seed_from_u64 ( s) )
367
362
}
368
363
}
369
364
}
@@ -543,7 +538,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
543
538
mutability : alloc. mutability ,
544
539
extra : AllocExtra {
545
540
stacks : extra,
546
- base_addr : RefCell :: new ( None ) ,
541
+ base_addr : Cell :: new ( None ) ,
547
542
} ,
548
543
} ;
549
544
( Cow :: Owned ( alloc) , base_tag)
@@ -598,20 +593,20 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
598
593
return err ! ( InvalidNullPointerUsage ) ;
599
594
}
600
595
601
- if memory. extra . seed . is_none ( ) {
596
+ if memory. extra . rng . is_none ( ) {
602
597
return err ! ( ReadBytesAsPointer ) ;
603
598
}
604
599
605
600
let extra = memory. extra . intptrcast . borrow ( ) ;
606
601
607
- match extra. vec . binary_search_by_key ( & int, |( int, _) | * int) {
602
+ match extra. int_to_ptr_map . binary_search_by_key ( & int, |( int, _) | * int) {
608
603
Ok ( pos) => {
609
- let ( _, alloc_id) = extra. vec [ pos] ;
604
+ let ( _, alloc_id) = extra. int_to_ptr_map [ pos] ;
610
605
Ok ( Pointer :: new_with_tag ( alloc_id, Size :: from_bytes ( 0 ) , Tag :: Untagged ) )
611
606
}
612
607
Err ( pos) => {
613
608
if pos > 0 {
614
- let ( glb, alloc_id) = extra. vec [ pos - 1 ] ;
609
+ let ( glb, alloc_id) = extra. int_to_ptr_map [ pos - 1 ] ;
615
610
let offset = int - glb;
616
611
if offset <= memory. get ( alloc_id) ?. bytes . len ( ) as u64 {
617
612
Ok ( Pointer :: new_with_tag ( alloc_id, Size :: from_bytes ( offset) , Tag :: Untagged ) )
@@ -629,36 +624,32 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
629
624
ptr : Pointer < Self :: PointerTag > ,
630
625
memory : & Memory < ' mir , ' tcx , Self > ,
631
626
) -> InterpResult < ' tcx , u64 > {
632
- if memory. extra . seed . is_none ( ) {
627
+ if memory. extra . rng . is_none ( ) {
633
628
return err ! ( ReadPointerAsBytes ) ;
634
629
}
635
630
636
631
let mut extra = memory. extra . intptrcast . borrow_mut ( ) ;
637
632
638
633
let alloc = memory. get ( ptr. alloc_id ) ?;
639
634
640
- let mut base_addr = alloc. extra . base_addr . borrow_mut ( ) ;
641
-
642
- let addr = match * base_addr {
643
- Some ( addr) => addr,
635
+ let base_addr = match alloc. extra . base_addr . get ( ) {
636
+ Some ( base_addr) => base_addr,
644
637
None => {
645
- let addr = extra. addr ;
646
- extra. addr += alloc. bytes . len ( ) as u64 ;
638
+ let base_addr = extra. next_base_addr ;
639
+ extra. next_base_addr += alloc. bytes . len ( ) as u64 ;
647
640
648
- * base_addr = Some ( addr ) ;
641
+ alloc . extra . base_addr . set ( Some ( base_addr ) ) ;
649
642
650
- let elem = ( addr , ptr. alloc_id ) ;
643
+ let elem = ( base_addr , ptr. alloc_id ) ;
651
644
652
- if let Err ( pos) = extra. vec . binary_search ( & elem) {
653
- extra. vec . insert ( pos, elem) ;
654
- } else {
655
- return err ! ( Unreachable ) ;
656
- }
645
+ // Given that `next_base_addr` increases in each allocation, pushing the
646
+ // corresponding tuple keeps `int_to_ptr_map` sorted
647
+ extra. int_to_ptr_map . push ( elem) ;
657
648
658
- addr
649
+ base_addr
659
650
}
660
651
} ;
661
652
662
- Ok ( addr + ptr. offset . bytes ( ) )
653
+ Ok ( base_addr + ptr. offset . bytes ( ) )
663
654
}
664
655
}
0 commit comments