@@ -25,7 +25,6 @@ mod memory;
25
25
26
26
use std:: collections:: HashMap ;
27
27
use std:: borrow:: Cow ;
28
- use std:: cell:: Cell ;
29
28
use std:: rc:: Rc ;
30
29
31
30
use rand:: rngs:: StdRng ;
@@ -387,7 +386,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
387
386
type MemoryKinds = MiriMemoryKind ;
388
387
389
388
type FrameExtra = stacked_borrows:: CallId ;
390
- type MemoryExtra = memory:: MemoryState ;
389
+ type MemoryExtra = memory:: MemoryExtra ;
391
390
type AllocExtra = memory:: AllocExtra ;
392
391
type PointerTag = Tag ;
393
392
@@ -513,17 +512,17 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
513
512
) -> ( Cow < ' b , Allocation < Self :: PointerTag , Self :: AllocExtra > > , Self :: PointerTag ) {
514
513
let kind = kind. expect ( "we set our STATIC_KIND so this cannot be None" ) ;
515
514
let alloc = alloc. into_owned ( ) ;
516
- let ( extra , base_tag) = Stacks :: new_allocation (
515
+ let ( stacks , base_tag) = Stacks :: new_allocation (
517
516
id,
518
517
Size :: from_bytes ( alloc. bytes . len ( ) as u64 ) ,
519
- Rc :: clone ( & memory. extra . stacked ) ,
518
+ Rc :: clone ( & memory. extra . stacked_borrows ) ,
520
519
kind,
521
520
) ;
522
521
if kind != MiriMemoryKind :: Static . into ( ) {
523
522
assert ! ( alloc. relocations. is_empty( ) , "Only statics can come initialized with inner pointers" ) ;
524
523
// Now we can rely on the inner pointers being static, too.
525
524
}
526
- let mut memory_extra = memory. extra . stacked . borrow_mut ( ) ;
525
+ let mut memory_extra = memory. extra . stacked_borrows . borrow_mut ( ) ;
527
526
let alloc: Allocation < Tag , Self :: AllocExtra > = Allocation {
528
527
bytes : alloc. bytes ,
529
528
relocations : Relocations :: from_presorted (
@@ -537,8 +536,8 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
537
536
align : alloc. align ,
538
537
mutability : alloc. mutability ,
539
538
extra : AllocExtra {
540
- stacks : extra ,
541
- base_addr : Cell :: new ( None ) ,
539
+ stacked_borrows : stacks ,
540
+ intptrcast : Default :: default ( ) ,
542
541
} ,
543
542
} ;
544
543
( Cow :: Owned ( alloc) , base_tag)
@@ -549,7 +548,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
549
548
id : AllocId ,
550
549
memory : & Memory < ' mir , ' tcx , Self > ,
551
550
) -> Self :: PointerTag {
552
- memory. extra . stacked . borrow_mut ( ) . static_base_ptr ( id)
551
+ memory. extra . stacked_borrows . borrow_mut ( ) . static_base_ptr ( id)
553
552
}
554
553
555
554
#[ inline( always) ]
@@ -574,49 +573,27 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
574
573
fn stack_push (
575
574
ecx : & mut InterpretCx < ' mir , ' tcx , Self > ,
576
575
) -> InterpResult < ' tcx , stacked_borrows:: CallId > {
577
- Ok ( ecx. memory ( ) . extra . stacked . borrow_mut ( ) . new_call ( ) )
576
+ Ok ( ecx. memory ( ) . extra . stacked_borrows . borrow_mut ( ) . new_call ( ) )
578
577
}
579
578
580
579
#[ inline( always) ]
581
580
fn stack_pop (
582
581
ecx : & mut InterpretCx < ' mir , ' tcx , Self > ,
583
582
extra : stacked_borrows:: CallId ,
584
583
) -> InterpResult < ' tcx > {
585
- Ok ( ecx. memory ( ) . extra . stacked . borrow_mut ( ) . end_call ( extra) )
584
+ Ok ( ecx. memory ( ) . extra . stacked_borrows . borrow_mut ( ) . end_call ( extra) )
586
585
}
587
586
588
587
fn int_to_ptr (
589
588
int : u64 ,
590
589
memory : & Memory < ' mir , ' tcx , Self > ,
591
590
) -> InterpResult < ' tcx , Pointer < Self :: PointerTag > > {
592
591
if int == 0 {
593
- return err ! ( InvalidNullPointerUsage ) ;
594
- }
595
-
596
- if memory. extra . rng . is_none ( ) {
597
- return err ! ( ReadBytesAsPointer ) ;
598
- }
599
-
600
- let extra = memory. extra . intptrcast . borrow ( ) ;
601
-
602
- match extra. int_to_ptr_map . binary_search_by_key ( & int, |( int, _) | * int) {
603
- Ok ( pos) => {
604
- let ( _, alloc_id) = extra. int_to_ptr_map [ pos] ;
605
- Ok ( Pointer :: new_with_tag ( alloc_id, Size :: from_bytes ( 0 ) , Tag :: Untagged ) )
606
- }
607
- Err ( pos) => {
608
- if pos > 0 {
609
- let ( glb, alloc_id) = extra. int_to_ptr_map [ pos - 1 ] ;
610
- let offset = int - glb;
611
- if offset <= memory. get ( alloc_id) ?. bytes . len ( ) as u64 {
612
- Ok ( Pointer :: new_with_tag ( alloc_id, Size :: from_bytes ( offset) , Tag :: Untagged ) )
613
- } else {
614
- return err ! ( DanglingPointerDeref ) ;
615
- }
616
- } else {
617
- return err ! ( DanglingPointerDeref ) ;
618
- }
619
- }
592
+ err ! ( InvalidNullPointerUsage )
593
+ } else if memory. extra . rng . is_none ( ) {
594
+ err ! ( ReadBytesAsPointer )
595
+ } else {
596
+ intptrcast:: GlobalState :: int_to_ptr ( int, memory)
620
597
}
621
598
}
622
599
@@ -625,31 +602,9 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
625
602
memory : & Memory < ' mir , ' tcx , Self > ,
626
603
) -> InterpResult < ' tcx , u64 > {
627
604
if memory. extra . rng . is_none ( ) {
628
- return err ! ( ReadPointerAsBytes ) ;
605
+ err ! ( ReadPointerAsBytes )
606
+ } else {
607
+ intptrcast:: GlobalState :: ptr_to_int ( ptr, memory)
629
608
}
630
-
631
- let mut extra = memory. extra . intptrcast . borrow_mut ( ) ;
632
-
633
- let alloc = memory. get ( ptr. alloc_id ) ?;
634
-
635
- let base_addr = match alloc. extra . base_addr . get ( ) {
636
- Some ( base_addr) => base_addr,
637
- None => {
638
- let base_addr = extra. next_base_addr ;
639
- extra. next_base_addr += alloc. bytes . len ( ) as u64 ;
640
-
641
- alloc. extra . base_addr . set ( Some ( base_addr) ) ;
642
-
643
- let elem = ( base_addr, ptr. alloc_id ) ;
644
-
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) ;
648
-
649
- base_addr
650
- }
651
- } ;
652
-
653
- Ok ( base_addr + ptr. offset . bytes ( ) )
654
609
}
655
610
}
0 commit comments