Skip to content

Commit bca7fcc

Browse files
author
Ellen Arteca
committed
avoid infinite recursion into pointers in intptrcast alloc_base_addr (see: recursive_static test case)
1 parent 7d73854 commit bca7fcc

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

src/intptrcast.rs

+27-19
Original file line numberDiff line numberDiff line change
@@ -157,36 +157,44 @@ impl<'mir, 'tcx> GlobalStateInner {
157157
}
158158

159159
fn alloc_base_addr(ecx: &MiriEvalContext<'mir, 'tcx>, alloc_id: AllocId) -> u64 {
160-
// TODO avoid leaked address hack
161-
let mut is_global = false;
162-
let base_addr: u64 = match ecx.get_alloc_base_addr(alloc_id) {
163-
Ok(addr) => {
164-
assert!(addr.bytes() % 16 == 0);
165-
addr.bytes()
166-
}
167-
// Grabbing u128 for max alignment
160+
// With our hack, base_addr should always be fully aligned
161+
let mut global_state = match ecx.machine.intptrcast.try_borrow_mut() {
162+
Ok(gstate) => gstate,
168163
Err(_) => {
169-
is_global = true;
170-
Box::leak(Box::new(0u128)) as *const u128 as u64
164+
// we're recursing
165+
let new_addr = Box::leak(Box::new(0u128)) as *const u128 as u64;
166+
unsafe {
167+
(*ecx.machine.intptrcast.as_ptr()).base_addr.insert(alloc_id, new_addr);
168+
(*ecx.machine.intptrcast.as_ptr()).int_to_ptr_map.insert(new_addr, alloc_id);
169+
}
170+
trace!(
171+
"Recursive case: Assigning base address {:#x} to allocation {:?}",
172+
new_addr,
173+
alloc_id,
174+
);
175+
return new_addr;
171176
}
172177
};
173-
// With our hack, base_addr should always be fully aligned
174-
let mut global_state = ecx.machine.intptrcast.borrow_mut();
175178
let global_state = &mut *global_state;
176179

177180
match global_state.base_addr.entry(alloc_id) {
178181
Entry::Occupied(entry) => *entry.get(),
179182
Entry::Vacant(entry) => {
183+
let base_addr = match ecx.get_alloc_base_addr(alloc_id) {
184+
Ok(addr) => {
185+
assert!(addr.bytes() % 16 == 0);
186+
addr.bytes()
187+
}
188+
// Grabbing u128 for max alignment
189+
Err(_) => {
190+
// TODO avoid leaked address hack
191+
Box::leak(Box::new(0u128)) as *const u128 as u64
192+
}
193+
};
180194
// There is nothing wrong with a raw pointer being cast to an integer only after
181195
// it became dangling. Hence we allow dead allocations.
182196
let (size, align, _kind) = ecx.get_alloc_info(alloc_id);
183197

184-
// println!("REE: {:?}, {:?}, {:?}", align, base_addr % align.bytes(), is_global);
185-
let what = Self::align_addr(base_addr, align.bytes());
186-
if (what != base_addr) {
187-
// println!("REEE: {:?}, {:?}, {:?}", what, base_addr, alloc_id);
188-
}
189-
190198
// This allocation does not have a base address yet, assign its bytes base.
191199
entry.insert(base_addr);
192200
trace!(
@@ -200,7 +208,7 @@ impl<'mir, 'tcx> GlobalStateInner {
200208
// Map has no duplicates so no need to remove copies.
201209
// Map is always sorted.
202210
global_state.int_to_ptr_map.insert(base_addr, alloc_id);
203-
211+
204212
base_addr
205213
}
206214
}

src/machine.rs

-7
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ pub struct AllocExtra {
224224
/// Weak memory emulation via the use of store buffers,
225225
/// this is only added if it is enabled.
226226
pub weak_memory: Option<weak_memory::AllocExtra>,
227-
pub real_pointer: *const u8,
228227
}
229228

230229
/// Precomputed layouts of primitive types
@@ -690,10 +689,6 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
690689
alloc: Cow<'b, Allocation>,
691690
kind: Option<MemoryKind<Self::MemoryKind>>,
692691
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra>>> {
693-
let size = alloc.size();
694-
let alloc_range = AllocRange{ start: rustc_target::abi::Size::ZERO, size: size};
695-
let alloc_bytes_ptr = alloc.get_bytes_with_uninit_and_ptr(ecx, alloc_range).unwrap().as_ptr();
696-
697692
let kind = kind.expect("we set our STATIC_KIND so this cannot be None");
698693
if ecx.machine.tracked_alloc_ids.contains(&id) {
699694
register_diagnostic(NonHaltingDiagnostic::CreatedAlloc(
@@ -723,14 +718,12 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
723718
)
724719
});
725720
let buffer_alloc = ecx.machine.weak_memory.then(weak_memory::AllocExtra::new_allocation);
726-
// println!("yup -- {:?}", id);
727721
let alloc: Allocation<Provenance, Self::AllocExtra> = alloc.adjust_from_tcx(
728722
&ecx.tcx,
729723
AllocExtra {
730724
stacked_borrows: stacks.map(RefCell::new),
731725
data_race: race_alloc,
732726
weak_memory: buffer_alloc,
733-
real_pointer: alloc_bytes_ptr,
734727
},
735728
|ptr| ecx.global_base_pointer(ptr),
736729
)?;

0 commit comments

Comments
 (0)