1
Fork 0

Permit int->ptr->int roundtrip

This commit is contained in:
Ralf Jung 2017-06-04 10:42:02 -07:00
parent cfff91ba3e
commit 6197f4fac9
3 changed files with 25 additions and 2 deletions

View file

@ -1438,6 +1438,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
panic!("Failed to access local: {:?}", err);
}
Ok(Value::ByRef(ptr)) => {
write!(msg, " by ref:").unwrap();
allocs.push(ptr.alloc_id);
}
Ok(Value::ByVal(val)) => {

View file

@ -605,7 +605,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
if size == 0 {
return Ok(&[]);
}
if self.relocations(ptr, size)?.count() != 0 {
if self.has_non_int_relocations(ptr, size)? {
return Err(EvalError::ReadPointerAsBytes);
}
self.check_defined(ptr, size)?;
@ -703,7 +703,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
let offset = ptr.offset as usize;
match alloc.bytes[offset..].iter().position(|&c| c == 0) {
Some(size) => {
if self.relocations(ptr, (size + 1) as u64)?.count() != 0 {
if self.has_non_int_relocations(ptr, (size + 1) as u64)? {
return Err(EvalError::ReadPointerAsBytes);
}
self.check_defined(ptr, (size + 1) as u64)?;
@ -887,6 +887,12 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
Ok(self.get(ptr.alloc_id)?.relocations.range(start..end))
}
fn has_non_int_relocations(&self, ptr: Pointer, size: u64)
-> EvalResult<'tcx, bool>
{
Ok(self.relocations(ptr, size)?.any(|(_, &alloc_id)| alloc_id != NEVER_ALLOC_ID))
}
fn clear_relocations(&mut self, ptr: Pointer, size: u64) -> EvalResult<'tcx> {
// Find all relocations overlapping the given range.
let keys: Vec<_> = self.relocations(ptr, size)?.map(|(&k, _)| k).collect();

View file

@ -0,0 +1,16 @@
// fn eq_ref<T>(x: &T, y: &T) -> bool {
// x as *const _ == y as *const _
// }
fn main() {
// int-ptr-int
assert_eq!(1 as *const i32 as usize, 1);
// TODO
// { // ptr-int-ptr
// let x = 13;
// let y = &x as *const _ as usize;
// let y = y as *const _;
// assert!(eq_ref(&x, unsafe { &*y }));
// }
}