Skip to content

Commit bbc8424

Browse files
authored
Merge pull request rust-lang#41 from oli-obk/master
optimize all ZST allocations into one single allocation
2 parents 1720b1f + 3d95883 commit bbc8424

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

src/interpreter/terminator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
423423
"__rust_reallocate" => {
424424
let ptr = self.memory.read_ptr(args[0])?;
425425
let size = self.memory.read_usize(args[2])?;
426-
self.memory.reallocate(ptr, size as usize)?;
427-
self.memory.write_ptr(dest, ptr)?;
426+
let new_ptr = self.memory.reallocate(ptr, size as usize)?;
427+
self.memory.write_ptr(dest, new_ptr)?;
428428
}
429429

430430
"memcmp" => {

src/memory.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ impl Pointer {
4141
pub fn offset(self, i: isize) -> Self {
4242
Pointer { offset: (self.offset as isize + i) as usize, ..self }
4343
}
44+
pub fn points_to_zst(&self) -> bool {
45+
self.alloc_id.0 == 0
46+
}
47+
fn zst_ptr() -> Self {
48+
Pointer {
49+
alloc_id: ZST_ALLOC_ID,
50+
offset: 0,
51+
}
52+
}
4453
}
4554

4655
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
@@ -66,15 +75,29 @@ pub struct Memory<'a, 'tcx> {
6675
pub layout: &'a TargetDataLayout,
6776
}
6877

78+
const ZST_ALLOC_ID: AllocId = AllocId(0);
79+
6980
impl<'a, 'tcx> Memory<'a, 'tcx> {
7081
pub fn new(layout: &'a TargetDataLayout) -> Self {
71-
Memory {
82+
let mut mem = Memory {
7283
alloc_map: HashMap::new(),
7384
functions: HashMap::new(),
7485
function_alloc_cache: HashMap::new(),
75-
next_id: AllocId(0),
86+
next_id: AllocId(1),
7687
layout: layout,
77-
}
88+
};
89+
// alloc id 0 is reserved for ZSTs, this is an optimization to prevent ZST
90+
// (e.g. function items, (), [], ...) from requiring memory
91+
let alloc = Allocation {
92+
bytes: Vec::new(),
93+
relocations: BTreeMap::new(),
94+
undef_mask: UndefMask::new(0),
95+
};
96+
mem.alloc_map.insert(ZST_ALLOC_ID, alloc);
97+
// check that additional zst allocs work
98+
debug_assert!(mem.allocate(0).points_to_zst());
99+
debug_assert!(mem.get(ZST_ALLOC_ID).is_ok());
100+
mem
78101
}
79102

80103
pub fn allocations<'b>(&'b self) -> ::std::collections::hash_map::Iter<'b, AllocId, Allocation> {
@@ -105,6 +128,9 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
105128
}
106129

107130
pub fn allocate(&mut self, size: usize) -> Pointer {
131+
if size == 0 {
132+
return Pointer::zst_ptr();
133+
}
108134
let alloc = Allocation {
109135
bytes: vec![0; size],
110136
relocations: BTreeMap::new(),
@@ -121,11 +147,14 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
121147

122148
// TODO(solson): Track which allocations were returned from __rust_allocate and report an error
123149
// when reallocating/deallocating any others.
124-
pub fn reallocate(&mut self, ptr: Pointer, new_size: usize) -> EvalResult<'tcx, ()> {
150+
pub fn reallocate(&mut self, ptr: Pointer, new_size: usize) -> EvalResult<'tcx, Pointer> {
125151
if ptr.offset != 0 {
126152
// TODO(solson): Report error about non-__rust_allocate'd pointer.
127153
return Err(EvalError::Unimplemented(format!("bad pointer offset: {}", ptr.offset)));
128154
}
155+
if ptr.points_to_zst() {
156+
return Ok(self.allocate(new_size));
157+
}
129158

130159
let size = self.get_mut(ptr.alloc_id)?.bytes.len();
131160

@@ -141,21 +170,26 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
141170
alloc.undef_mask.truncate(new_size);
142171
}
143172

144-
Ok(())
173+
Ok(ptr)
145174
}
146175

147176
// TODO(solson): See comment on `reallocate`.
148177
pub fn deallocate(&mut self, ptr: Pointer) -> EvalResult<'tcx, ()> {
178+
if ptr.points_to_zst() {
179+
return Ok(());
180+
}
149181
if ptr.offset != 0 {
150182
// TODO(solson): Report error about non-__rust_allocate'd pointer.
151183
return Err(EvalError::Unimplemented(format!("bad pointer offset: {}", ptr.offset)));
152184
}
153185

154186
if self.alloc_map.remove(&ptr.alloc_id).is_none() {
187+
debug!("deallocated a pointer twice: {}", ptr.alloc_id);
155188
// TODO(solson): Report error about erroneous free. This is blocked on properly tracking
156189
// already-dropped state since this if-statement is entered even in safe code without
157190
// it.
158191
}
192+
debug!("deallocated : {}", ptr.alloc_id);
159193

160194
Ok(())
161195
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
22
let v: Vec<u8> = vec![1, 2];
3-
let x = unsafe { *v.get_unchecked(5) }; //~ ERROR: memory access of 5..6 outside bounds of allocation 31 which has size 2
3+
let x = unsafe { *v.get_unchecked(5) }; //~ ERROR: which has size 2
44
panic!("this should never print: {}", x);
55
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let v: Vec<u8> = vec![1, 2];
3+
let x = unsafe { *v.get_unchecked(5) }; //~ ERROR: memory access of 5..6 outside bounds of allocation
4+
panic!("this should never print: {}", x);
5+
}

tests/run-pass/zst.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// the following flag prevents this test from running on the host machine
2+
// this should only be run on miri, because rust doesn't (yet?) optimize ZSTs of different types
3+
// into the same memory location
4+
// ignore-test
5+
6+
17
#[derive(PartialEq, Debug)]
28
struct A;
39

@@ -13,4 +19,6 @@ fn use_zst() -> A {
1319
fn main() {
1420
assert_eq!(zst_ret(), A);
1521
assert_eq!(use_zst(), A);
22+
assert_eq!(&A as *const A as *const (), &() as *const _);
23+
assert_eq!(&A as *const A, &A as *const A);
1624
}

0 commit comments

Comments
 (0)