@@ -9,10 +9,10 @@ use rustc_target::abi::{HasDataLayout, Size};
99
1010use crate :: * ;
1111
12- pub type MemoryExtra = RefCell < GlobalState > ;
12+ pub type GlobalState = RefCell < GlobalStateInner > ;
1313
1414#[ derive( Clone , Debug ) ]
15- pub struct GlobalState {
15+ pub struct GlobalStateInner {
1616 /// This is used as a map between the address of each allocation and its `AllocId`.
1717 /// It is always sorted
1818 int_to_ptr_map : Vec < ( u64 , AllocId ) > ,
@@ -29,9 +29,9 @@ pub struct GlobalState {
2929 strict_provenance : bool ,
3030}
3131
32- impl GlobalState {
32+ impl GlobalStateInner {
3333 pub fn new ( config : & MiriConfig ) -> Self {
34- GlobalState {
34+ GlobalStateInner {
3535 int_to_ptr_map : Vec :: default ( ) ,
3636 base_addr : FxHashMap :: default ( ) ,
3737 next_base_addr : STACK_ADDR ,
@@ -40,13 +40,10 @@ impl GlobalState {
4040 }
4141}
4242
43- impl < ' mir , ' tcx > GlobalState {
44- pub fn ptr_from_addr (
45- addr : u64 ,
46- memory : & Memory < ' mir , ' tcx , Evaluator < ' mir , ' tcx > > ,
47- ) -> Pointer < Option < Tag > > {
43+ impl < ' mir , ' tcx > GlobalStateInner {
44+ pub fn ptr_from_addr ( addr : u64 , ecx : & MiriEvalContext < ' mir , ' tcx > ) -> Pointer < Option < Tag > > {
4845 trace ! ( "Casting 0x{:x} to a pointer" , addr) ;
49- let global_state = memory . extra . intptrcast . borrow ( ) ;
46+ let global_state = ecx . machine . intptrcast . borrow ( ) ;
5047
5148 if global_state. strict_provenance {
5249 return Pointer :: new ( None , Size :: from_bytes ( addr) ) ;
@@ -64,7 +61,11 @@ impl<'mir, 'tcx> GlobalState {
6461 let offset = addr - glb;
6562 // If the offset exceeds the size of the allocation, don't use this `alloc_id`.
6663 if offset
67- <= memory. get_size_and_align ( alloc_id, AllocCheck :: MaybeDead ) . unwrap ( ) . 0 . bytes ( )
64+ <= ecx
65+ . get_alloc_size_and_align ( alloc_id, AllocCheck :: MaybeDead )
66+ . unwrap ( )
67+ . 0
68+ . bytes ( )
6869 {
6970 Some ( alloc_id)
7071 } else {
@@ -79,11 +80,8 @@ impl<'mir, 'tcx> GlobalState {
7980 )
8081 }
8182
82- fn alloc_base_addr (
83- memory : & Memory < ' mir , ' tcx , Evaluator < ' mir , ' tcx > > ,
84- alloc_id : AllocId ,
85- ) -> u64 {
86- let mut global_state = memory. extra . intptrcast . borrow_mut ( ) ;
83+ fn alloc_base_addr ( ecx : & MiriEvalContext < ' mir , ' tcx > , alloc_id : AllocId ) -> u64 {
84+ let mut global_state = ecx. machine . intptrcast . borrow_mut ( ) ;
8785 let global_state = & mut * global_state;
8886
8987 match global_state. base_addr . entry ( alloc_id) {
@@ -92,12 +90,12 @@ impl<'mir, 'tcx> GlobalState {
9290 // There is nothing wrong with a raw pointer being cast to an integer only after
9391 // it became dangling. Hence `MaybeDead`.
9492 let ( size, align) =
95- memory . get_size_and_align ( alloc_id, AllocCheck :: MaybeDead ) . unwrap ( ) ;
93+ ecx . get_alloc_size_and_align ( alloc_id, AllocCheck :: MaybeDead ) . unwrap ( ) ;
9694
9795 // This allocation does not have a base address yet, pick one.
9896 // Leave some space to the previous allocation, to give it some chance to be less aligned.
9997 let slack = {
100- let mut rng = memory . extra . rng . borrow_mut ( ) ;
98+ let mut rng = ecx . machine . rng . borrow_mut ( ) ;
10199 // This means that `(global_state.next_base_addr + slack) % 16` is uniformly distributed.
102100 rng. gen_range ( 0 ..16 )
103101 } ;
@@ -129,27 +127,21 @@ impl<'mir, 'tcx> GlobalState {
129127 }
130128
131129 /// Convert a relative (tcx) pointer to an absolute address.
132- pub fn rel_ptr_to_addr (
133- memory : & Memory < ' mir , ' tcx , Evaluator < ' mir , ' tcx > > ,
134- ptr : Pointer < AllocId > ,
135- ) -> u64 {
130+ pub fn rel_ptr_to_addr ( ecx : & MiriEvalContext < ' mir , ' tcx > , ptr : Pointer < AllocId > ) -> u64 {
136131 let ( alloc_id, offset) = ptr. into_parts ( ) ; // offset is relative
137- let base_addr = GlobalState :: alloc_base_addr ( memory , alloc_id) ;
132+ let base_addr = GlobalStateInner :: alloc_base_addr ( ecx , alloc_id) ;
138133
139134 // Add offset with the right kind of pointer-overflowing arithmetic.
140- let dl = memory . data_layout ( ) ;
135+ let dl = ecx . data_layout ( ) ;
141136 dl. overflowing_offset ( base_addr, offset. bytes ( ) ) . 0
142137 }
143138
144- pub fn abs_ptr_to_rel (
145- memory : & Memory < ' mir , ' tcx , Evaluator < ' mir , ' tcx > > ,
146- ptr : Pointer < Tag > ,
147- ) -> Size {
139+ pub fn abs_ptr_to_rel ( ecx : & MiriEvalContext < ' mir , ' tcx > , ptr : Pointer < Tag > ) -> Size {
148140 let ( tag, addr) = ptr. into_parts ( ) ; // addr is absolute
149- let base_addr = GlobalState :: alloc_base_addr ( memory , tag. alloc_id ) ;
141+ let base_addr = GlobalStateInner :: alloc_base_addr ( ecx , tag. alloc_id ) ;
150142
151143 // Wrapping "addr - base_addr"
152- let dl = memory . data_layout ( ) ;
144+ let dl = ecx . data_layout ( ) ;
153145 let neg_base_addr = ( base_addr as i64 ) . wrapping_neg ( ) ;
154146 Size :: from_bytes ( dl. overflowing_signed_offset ( addr. bytes ( ) , neg_base_addr) . 0 )
155147 }
@@ -170,7 +162,7 @@ mod tests {
170162
171163 #[ test]
172164 fn test_align_addr ( ) {
173- assert_eq ! ( GlobalState :: align_addr( 37 , 4 ) , 40 ) ;
174- assert_eq ! ( GlobalState :: align_addr( 44 , 4 ) , 44 ) ;
165+ assert_eq ! ( GlobalStateInner :: align_addr( 37 , 4 ) , 40 ) ;
166+ assert_eq ! ( GlobalStateInner :: align_addr( 44 , 4 ) , 44 ) ;
175167 }
176168}
0 commit comments