Skip to content

Commit

Permalink
ensure that integers cast to pointers will never point at a valid all…
Browse files Browse the repository at this point in the history
…oc, not even the zst alloc
  • Loading branch information
oli-obk committed Nov 10, 2016
1 parent 2d4301e commit 921f5af
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'tcx> Error for EvalError<'tcx> {
EvalError::DanglingPointerDeref =>
"dangling pointer was dereferenced",
EvalError::InvalidFunctionPointer =>
"tried to use a pointer as a function pointer",
"tried to use an integer pointer as a function pointer",
EvalError::InvalidBool =>
"invalid boolean value read",
EvalError::InvalidDiscriminant =>
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter/terminator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
};
let drop_fn = self.memory.read_ptr(vtable)?;
// some values don't need to call a drop impl, so the value is null
if !drop_fn.points_to_zst() {
if drop_fn != Pointer::from_int(0) {
let (def_id, substs, ty) = self.memory.get_fn(drop_fn.alloc_id)?;
let fn_sig = self.tcx.erase_late_bound_regions_and_normalize(&ty.sig);
let real_ty = fn_sig.inputs[0];
Expand Down
6 changes: 3 additions & 3 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Pointer {
// FIXME(solson): Integer pointers should use u64, not usize. Target pointers can be larger
// than host usize.
pub fn from_int(i: usize) -> Self {
Pointer::new(ZST_ALLOC_ID, i)
Pointer::new(NEVER_ALLOC_ID, i)
}

pub fn zst_ptr() -> Self {
Expand Down Expand Up @@ -290,7 +290,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
Some(alloc) => Ok(alloc),
None => match self.functions.get(&id) {
Some(_) => Err(EvalError::DerefFunctionPointer),
None if id == ZST_ALLOC_ID => Err(EvalError::InvalidMemoryAccess),
None if id == NEVER_ALLOC_ID || id == ZST_ALLOC_ID => Err(EvalError::InvalidMemoryAccess),
None => Err(EvalError::DanglingPointerDeref),
}
}
Expand All @@ -302,7 +302,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
Some(alloc) => Ok(alloc),
None => match self.functions.get(&id) {
Some(_) => Err(EvalError::DerefFunctionPointer),
None if id == ZST_ALLOC_ID => Err(EvalError::InvalidMemoryAccess),
None if id == NEVER_ALLOC_ID || id == ZST_ALLOC_ID => Err(EvalError::InvalidMemoryAccess),
None => Err(EvalError::DanglingPointerDeref),
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/run-pass/assume_bug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
vec![()].into_iter();
}

0 comments on commit 921f5af

Please sign in to comment.