Skip to content

Commit 7fbf8e5

Browse files
committed
Fix alignment of base addresses
1 parent 792d665 commit 7fbf8e5

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/intptrcast.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,25 @@ impl Default for GlobalState {
3636

3737
impl<'mir, 'tcx> GlobalState {
3838
pub fn int_to_ptr(
39-
base_addr: u64,
39+
int: u64,
4040
memory: &Memory<'mir, 'tcx, Evaluator<'tcx>>,
4141
) -> InterpResult<'tcx, Pointer<Tag>> {
4242
let global_state = memory.extra.intptrcast.borrow();
4343

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) {
4545
Ok(pos) => {
4646
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
4848
// zero. The pointer is untagged because it was created from a cast
4949
Ok(Pointer::new_with_tag(alloc_id, Size::from_bytes(0), Tag::Untagged))
5050
},
5151
Err(0) => err!(DanglingPointerDeref),
5252
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`,
5454
// i.e. the greatest lower bound (glb)
5555
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;
5858
// If the offset exceeds the size of the allocation, this access is illegal
5959
if offset <= memory.get(alloc_id)?.bytes.len() as u64 {
6060
// This pointer is untagged because it was created from a cast
@@ -77,21 +77,24 @@ impl<'mir, 'tcx> GlobalState {
7777
let base_addr = match alloc.extra.intptrcast.base_addr.get() {
7878
Some(base_addr) => base_addr,
7979
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;
8383
alloc.extra.intptrcast.base_addr.set(Some(base_addr));
84-
85-
let elem = (base_addr, ptr.alloc_id);
86-
8784
// Given that `next_base_addr` increases in each allocation, pushing the
8885
// 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));
9087

9188
base_addr
9289
}
9390
};
9491

9592
Ok(base_addr + ptr.offset.bytes())
9693
}
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+
}
97100
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
8585
Evaluator::new(config.validate),
8686
);
8787

88+
// FIXME: InterpretCx::new should take an initial MemoryExtra
8889
ecx.memory_mut().extra.rng = config.seed.map(StdRng::seed_from_u64);
8990

9091
let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id);
File renamed without changes.

0 commit comments

Comments
 (0)