@@ -36,25 +36,25 @@ impl Default for GlobalState {
36
36
37
37
impl < ' mir , ' tcx > GlobalState {
38
38
pub fn int_to_ptr (
39
- base_addr : u64 ,
39
+ int : u64 ,
40
40
memory : & Memory < ' mir , ' tcx , Evaluator < ' tcx > > ,
41
41
) -> InterpResult < ' tcx , Pointer < Tag > > {
42
42
let global_state = memory. extra . intptrcast . borrow ( ) ;
43
43
44
- match global_state. int_to_ptr_map . binary_search_by_key ( & base_addr , |( addr, _) | * addr) {
44
+ match global_state. int_to_ptr_map . binary_search_by_key ( & int , |( addr, _) | * addr) {
45
45
Ok ( pos) => {
46
46
let ( _, alloc_id) = global_state. int_to_ptr_map [ pos] ;
47
- // `base_addr ` is the starting address for an allocation, the offset should be
47
+ // `int ` is equal to the starting address for an allocation, the offset should be
48
48
// zero. The pointer is untagged because it was created from a cast
49
49
Ok ( Pointer :: new_with_tag ( alloc_id, Size :: from_bytes ( 0 ) , Tag :: Untagged ) )
50
50
} ,
51
51
Err ( 0 ) => err ! ( DanglingPointerDeref ) ,
52
52
Err ( pos) => {
53
- // This is the gargest of the adresses smaller than `base_addr `,
53
+ // This is the largest of the adresses smaller than `int `,
54
54
// i.e. the greatest lower bound (glb)
55
55
let ( glb, alloc_id) = global_state. int_to_ptr_map [ pos - 1 ] ;
56
- // This never overflows because `base_addr >= glb`
57
- let offset = base_addr - glb;
56
+ // This never overflows because `int >= glb`
57
+ let offset = int - glb;
58
58
// If the offset exceeds the size of the allocation, this access is illegal
59
59
if offset <= memory. get ( alloc_id) ?. bytes . len ( ) as u64 {
60
60
// This pointer is untagged because it was created from a cast
@@ -77,21 +77,24 @@ impl<'mir, 'tcx> GlobalState {
77
77
let base_addr = match alloc. extra . intptrcast . base_addr . get ( ) {
78
78
Some ( base_addr) => base_addr,
79
79
None => {
80
- let base_addr = global_state . next_base_addr ;
81
- global_state. next_base_addr += alloc. bytes . len ( ) as u64 ;
82
-
80
+ // This allocation does not have a base address yet, pick one.
81
+ let base_addr = Self :: align_addr ( global_state. next_base_addr , alloc. align . bytes ( ) ) ;
82
+ global_state . next_base_addr = base_addr + alloc . bytes . len ( ) as u64 ;
83
83
alloc. extra . intptrcast . base_addr . set ( Some ( base_addr) ) ;
84
-
85
- let elem = ( base_addr, ptr. alloc_id ) ;
86
-
87
84
// Given that `next_base_addr` increases in each allocation, pushing the
88
85
// corresponding tuple keeps `int_to_ptr_map` sorted
89
- global_state. int_to_ptr_map . push ( elem ) ;
86
+ global_state. int_to_ptr_map . push ( ( base_addr , ptr . alloc_id ) ) ;
90
87
91
88
base_addr
92
89
}
93
90
} ;
94
91
95
92
Ok ( base_addr + ptr. offset . bytes ( ) )
96
93
}
94
+
95
+ /// Shifts `addr` to make it aligned with `align` by rounding `addr` to the smallest multiple
96
+ /// of `align` that is strictly larger to `addr`
97
+ fn align_addr ( addr : u64 , align : u64 ) -> u64 {
98
+ addr + align - addr % align
99
+ }
97
100
}
0 commit comments