address comments
This commit is contained in:
parent
594f1d79da
commit
3d9588332f
@ -39,7 +39,6 @@ pub struct Pointer {
|
||||
|
||||
impl Pointer {
|
||||
pub fn offset(self, i: isize) -> Self {
|
||||
// FIXME: prevent offsetting ZST ptrs in tracing mode
|
||||
Pointer { offset: (self.offset as isize + i) as usize, ..self }
|
||||
}
|
||||
pub fn points_to_zst(&self) -> bool {
|
||||
@ -47,7 +46,7 @@ impl Pointer {
|
||||
}
|
||||
fn zst_ptr() -> Self {
|
||||
Pointer {
|
||||
alloc_id: AllocId(0),
|
||||
alloc_id: ZST_ALLOC_ID,
|
||||
offset: 0,
|
||||
}
|
||||
}
|
||||
@ -76,6 +75,8 @@ pub struct Memory<'a, 'tcx> {
|
||||
pub layout: &'a TargetDataLayout,
|
||||
}
|
||||
|
||||
const ZST_ALLOC_ID: AllocId = AllocId(0);
|
||||
|
||||
impl<'a, 'tcx> Memory<'a, 'tcx> {
|
||||
pub fn new(layout: &'a TargetDataLayout) -> Self {
|
||||
let mut mem = Memory {
|
||||
@ -86,16 +87,16 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
|
||||
layout: layout,
|
||||
};
|
||||
// alloc id 0 is reserved for ZSTs, this is an optimization to prevent ZST
|
||||
// (e.g. function pointers, (), [], ...) from requiring memory
|
||||
// (e.g. function items, (), [], ...) from requiring memory
|
||||
let alloc = Allocation {
|
||||
bytes: Vec::new(),
|
||||
relocations: BTreeMap::new(),
|
||||
undef_mask: UndefMask::new(0),
|
||||
};
|
||||
mem.alloc_map.insert(AllocId(0), alloc);
|
||||
mem.alloc_map.insert(ZST_ALLOC_ID, alloc);
|
||||
// check that additional zst allocs work
|
||||
debug_assert!(mem.allocate(0).points_to_zst());
|
||||
debug_assert!(mem.get(AllocId(0)).is_ok());
|
||||
debug_assert!(mem.get(ZST_ALLOC_ID).is_ok());
|
||||
mem
|
||||
}
|
||||
|
||||
@ -147,17 +148,13 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
|
||||
// TODO(solson): Track which allocations were returned from __rust_allocate and report an error
|
||||
// when reallocating/deallocating any others.
|
||||
pub fn reallocate(&mut self, ptr: Pointer, new_size: usize) -> EvalResult<'tcx, Pointer> {
|
||||
if ptr.points_to_zst() {
|
||||
if new_size != 0 {
|
||||
return Ok(self.allocate(new_size));
|
||||
} else {
|
||||
return Ok(ptr);
|
||||
}
|
||||
}
|
||||
if ptr.offset != 0 {
|
||||
// TODO(solson): Report error about non-__rust_allocate'd pointer.
|
||||
return Err(EvalError::Unimplemented(format!("bad pointer offset: {}", ptr.offset)));
|
||||
}
|
||||
if ptr.points_to_zst() {
|
||||
return Ok(self.allocate(new_size));
|
||||
}
|
||||
|
||||
let size = self.get_mut(ptr.alloc_id)?.bytes.len();
|
||||
|
||||
@ -187,10 +184,12 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
|
||||
}
|
||||
|
||||
if self.alloc_map.remove(&ptr.alloc_id).is_none() {
|
||||
debug!("deallocated a pointer twice: {}", ptr.alloc_id);
|
||||
// TODO(solson): Report error about erroneous free. This is blocked on properly tracking
|
||||
// already-dropped state since this if-statement is entered even in safe code without
|
||||
// it.
|
||||
}
|
||||
debug!("deallocated : {}", ptr.alloc_id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
fn main() {
|
||||
let v: Vec<u8> = vec![1, 2];
|
||||
let x = unsafe { *v.get_unchecked(5) }; //~ ERROR: memory access of 5..6 outside bounds of allocation 31 which has size 2
|
||||
let x = unsafe { *v.get_unchecked(5) }; //~ ERROR: which has size 2
|
||||
panic!("this should never print: {}", x);
|
||||
}
|
||||
|
5
tests/compile-fail/out_of_bounds_read2.rs
Normal file
5
tests/compile-fail/out_of_bounds_read2.rs
Normal file
@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
let v: Vec<u8> = vec![1, 2];
|
||||
let x = unsafe { *v.get_unchecked(5) }; //~ ERROR: memory access of 5..6 outside bounds of allocation
|
||||
panic!("this should never print: {}", x);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user