Skip to content

Commit 8b04b09

Browse files
committed
Move alignment checks out of Allocation
1 parent 1c08ced commit 8b04b09

File tree

6 files changed

+43
-85
lines changed

6 files changed

+43
-85
lines changed

src/librustc/mir/interpret/allocation.rs

Lines changed: 12 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use super::{
1515
truncate,
1616
};
1717

18-
use ty::layout::{self, Size, Align};
18+
use ty::layout::{Size, Align};
1919
use syntax::ast::Mutability;
2020
use std::iter;
2121
use mir;
@@ -103,10 +103,8 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
103103
cx: &impl HasDataLayout,
104104
ptr: Pointer<Tag>,
105105
size: Size,
106-
align: Align,
107106
check_defined_and_ptr: bool,
108107
) -> EvalResult<'tcx, &[u8]> {
109-
self.check_align(ptr.into(), align)?;
110108
self.check_bounds(cx, ptr, size)?;
111109

112110
if check_defined_and_ptr {
@@ -126,14 +124,13 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
126124
}
127125

128126
#[inline]
129-
fn get_bytes(
127+
pub fn get_bytes(
130128
&self,
131129
cx: &impl HasDataLayout,
132130
ptr: Pointer<Tag>,
133131
size: Size,
134-
align: Align
135132
) -> EvalResult<'tcx, &[u8]> {
136-
self.get_bytes_internal(cx, ptr, size, align, true)
133+
self.get_bytes_internal(cx, ptr, size, true)
137134
}
138135

139136
/// It is the caller's responsibility to handle undefined and pointer bytes.
@@ -144,9 +141,8 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
144141
cx: &impl HasDataLayout,
145142
ptr: Pointer<Tag>,
146143
size: Size,
147-
align: Align
148144
) -> EvalResult<'tcx, &[u8]> {
149-
self.get_bytes_internal(cx, ptr, size, align, false)
145+
self.get_bytes_internal(cx, ptr, size, false)
150146
}
151147

152148
/// Just calling this already marks everything as defined and removes relocations,
@@ -156,10 +152,8 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
156152
cx: &impl HasDataLayout,
157153
ptr: Pointer<Tag>,
158154
size: Size,
159-
align: Align,
160155
) -> EvalResult<'tcx, &mut [u8]> {
161156
assert_ne!(size.bytes(), 0, "0-sized accesses should never even get a `Pointer`");
162-
self.check_align(ptr.into(), align)?;
163157
self.check_bounds(cx, ptr, size)?;
164158

165159
self.mark_definedness(ptr, size, true)?;
@@ -201,9 +195,8 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
201195
size: Size,
202196
allow_ptr_and_undef: bool,
203197
) -> EvalResult<'tcx> {
204-
let align = Align::from_bytes(1).unwrap();
205198
// Check bounds, align and relocations on the edges
206-
self.get_bytes_with_undef_and_ptr(cx, ptr, size, align)?;
199+
self.get_bytes_with_undef_and_ptr(cx, ptr, size)?;
207200
// Check undef and ptr
208201
if !allow_ptr_and_undef {
209202
self.check_defined(ptr, size)?;
@@ -212,26 +205,13 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
212205
Ok(())
213206
}
214207

215-
pub fn read_bytes(
216-
&self,
217-
cx: &impl HasDataLayout,
218-
ptr: Pointer<Tag>,
219-
size: Size,
220-
) -> EvalResult<'tcx, &[u8]> {
221-
let align = Align::from_bytes(1).unwrap();
222-
self.get_bytes(cx, ptr, size, align)
223-
}
224-
225208
pub fn write_bytes(
226209
&mut self,
227210
cx: &impl HasDataLayout,
228211
ptr: Pointer<Tag>,
229212
src: &[u8],
230213
) -> EvalResult<'tcx> {
231-
let align = Align::from_bytes(1).unwrap();
232-
let bytes = self.get_bytes_mut(
233-
cx, ptr, Size::from_bytes(src.len() as u64), align,
234-
)?;
214+
let bytes = self.get_bytes_mut(cx, ptr, Size::from_bytes(src.len() as u64))?;
235215
bytes.clone_from_slice(src);
236216
Ok(())
237217
}
@@ -243,8 +223,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
243223
val: u8,
244224
count: Size
245225
) -> EvalResult<'tcx> {
246-
let align = Align::from_bytes(1).unwrap();
247-
let bytes = self.get_bytes_mut(cx, ptr, count, align)?;
226+
let bytes = self.get_bytes_mut(cx, ptr, count)?;
248227
for b in bytes {
249228
*b = val;
250229
}
@@ -256,13 +235,10 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
256235
&self,
257236
cx: &impl HasDataLayout,
258237
ptr: Pointer<Tag>,
259-
ptr_align: Align,
260238
size: Size
261239
) -> EvalResult<'tcx, ScalarMaybeUndef<Tag>> {
262-
// get_bytes_unchecked tests alignment and relocation edges
263-
let bytes = self.get_bytes_with_undef_and_ptr(
264-
cx, ptr, size, ptr_align.min(int_align(cx, size))
265-
)?;
240+
// get_bytes_unchecked tests relocation edges
241+
let bytes = self.get_bytes_with_undef_and_ptr(cx, ptr, size)?;
266242
// Undef check happens *after* we established that the alignment is correct.
267243
// We must not return Ok() for unaligned pointers!
268244
if self.check_defined(ptr, size).is_err() {
@@ -293,17 +269,15 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
293269
&self,
294270
cx: &impl HasDataLayout,
295271
ptr: Pointer<Tag>,
296-
ptr_align: Align
297272
) -> EvalResult<'tcx, ScalarMaybeUndef<Tag>> {
298-
self.read_scalar(cx, ptr, ptr_align, cx.data_layout().pointer_size)
273+
self.read_scalar(cx, ptr, cx.data_layout().pointer_size)
299274
}
300275

301276
/// Write a *non-ZST* scalar
302277
pub fn write_scalar(
303278
&mut self,
304279
cx: &impl HasDataLayout,
305280
ptr: Pointer<Tag>,
306-
ptr_align: Align,
307281
val: ScalarMaybeUndef<Tag>,
308282
type_size: Size,
309283
) -> EvalResult<'tcx> {
@@ -327,9 +301,8 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
327301
};
328302

329303
{
330-
// get_bytes_mut checks alignment
331304
let endian = cx.data_layout().endian;
332-
let dst = self.get_bytes_mut(cx, ptr, type_size, ptr_align)?;
305+
let dst = self.get_bytes_mut(cx, ptr, type_size)?;
333306
write_target_uint(endian, dst, bytes).unwrap();
334307
}
335308

@@ -351,31 +324,13 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
351324
&mut self,
352325
cx: &impl HasDataLayout,
353326
ptr: Pointer<Tag>,
354-
ptr_align: Align,
355327
val: ScalarMaybeUndef<Tag>
356328
) -> EvalResult<'tcx> {
357329
let ptr_size = cx.data_layout().pointer_size;
358-
self.write_scalar(cx, ptr.into(), ptr_align, val, ptr_size)
330+
self.write_scalar(cx, ptr.into(), val, ptr_size)
359331
}
360332
}
361333

362-
fn int_align(
363-
cx: &impl HasDataLayout,
364-
size: Size,
365-
) -> Align {
366-
// We assume pointer-sized integers have the same alignment as pointers.
367-
// We also assume signed and unsigned integers of the same size have the same alignment.
368-
let ity = match size.bytes() {
369-
1 => layout::I8,
370-
2 => layout::I16,
371-
4 => layout::I32,
372-
8 => layout::I64,
373-
16 => layout::I128,
374-
_ => bug!("bad integer size: {}", size.bytes()),
375-
};
376-
ity.align(cx).abi
377-
}
378-
379334
/// Relocations
380335
impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
381336
/// Return all relocations overlapping with the given ptr-offset pair.

src/librustc_mir/interpret/memory.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
578578
Ok(&[])
579579
} else {
580580
let ptr = ptr.to_ptr()?;
581-
self.get(ptr.alloc_id)?.read_bytes(self, ptr, size)
581+
self.get(ptr.alloc_id)?.get_bytes(self, ptr, size)
582582
}
583583
}
584584
}
@@ -656,10 +656,10 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
656656
length: u64,
657657
nonoverlapping: bool,
658658
) -> EvalResult<'tcx> {
659+
self.check_align(src, src_align)?;
660+
self.check_align(dest, dest_align)?;
659661
if size.bytes() == 0 {
660662
// Nothing to do for ZST, other than checking alignment and non-NULLness.
661-
self.check_align(src, src_align)?;
662-
self.check_align(dest, dest_align)?;
663663
return Ok(());
664664
}
665665
let src = src.to_ptr()?;
@@ -689,12 +689,12 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
689689

690690
let tcx = self.tcx.tcx;
691691

692-
// This also checks alignment, and relocation edges on the src.
692+
// This checks relocation edges on the src.
693693
let src_bytes = self.get(src.alloc_id)?
694-
.get_bytes_with_undef_and_ptr(&tcx, src, size, src_align)?
694+
.get_bytes_with_undef_and_ptr(&tcx, src, size)?
695695
.as_ptr();
696696
let dest_bytes = self.get_mut(dest.alloc_id)?
697-
.get_bytes_mut(&tcx, dest, size * length, dest_align)?
697+
.get_bytes_mut(&tcx, dest, size * length)?
698698
.as_mut_ptr();
699699

700700
// SAFE: The above indexing would have panicked if there weren't at least `size` bytes

src/librustc_mir/interpret/operand.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,14 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
275275
return Ok(Some(Immediate::Scalar(Scalar::zst().into())));
276276
}
277277

278+
// check for integer pointers before alignment to report better errors
278279
let ptr = ptr.to_ptr()?;
280+
self.memory.check_align(ptr.into(), ptr_align)?;
279281
match mplace.layout.abi {
280282
layout::Abi::Scalar(..) => {
281283
let scalar = self.memory
282284
.get(ptr.alloc_id)?
283-
.read_scalar(self, ptr, ptr_align, mplace.layout.size)?;
285+
.read_scalar(self, ptr, mplace.layout.size)?;
284286
Ok(Some(Immediate::Scalar(scalar)))
285287
}
286288
layout::Abi::ScalarPair(ref a, ref b) => {
@@ -289,13 +291,14 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
289291
let a_ptr = ptr;
290292
let b_offset = a_size.align_to(b.align(self).abi);
291293
assert!(b_offset.bytes() > 0); // we later use the offset to test which field to use
292-
let b_ptr = ptr.offset(b_offset, self)?.into();
294+
let b_ptr = ptr.offset(b_offset, self)?;
293295
let a_val = self.memory
294296
.get(ptr.alloc_id)?
295-
.read_scalar(self, a_ptr, ptr_align, a_size)?;
297+
.read_scalar(self, a_ptr, a_size)?;
298+
self.memory.check_align(b_ptr.into(), b.align(self))?;
296299
let b_val = self.memory
297300
.get(ptr.alloc_id)?
298-
.read_scalar(self, b_ptr, ptr_align, b_size)?;
301+
.read_scalar(self, b_ptr, b_size)?;
299302
Ok(Some(Immediate::ScalarPair(a_val, b_val)))
300303
}
301304
_ => Ok(None),

src/librustc_mir/interpret/place.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -713,11 +713,12 @@ where
713713

714714
// Nothing to do for ZSTs, other than checking alignment
715715
if dest.layout.is_zst() {
716-
self.memory.check_align(ptr, ptr_align)?;
717-
return Ok(());
716+
return self.memory.check_align(ptr, ptr_align);
718717
}
719718

719+
// check for integer pointers before alignment to report better errors
720720
let ptr = ptr.to_ptr()?;
721+
self.memory.check_align(ptr.into(), ptr_align)?;
721722
let tcx = &*self.tcx;
722723
// FIXME: We should check that there are dest.layout.size many bytes available in
723724
// memory. The code below is not sufficient, with enough padding it might not
@@ -729,9 +730,8 @@ where
729730
_ => bug!("write_immediate_to_mplace: invalid Scalar layout: {:#?}",
730731
dest.layout)
731732
}
732-
733733
self.memory.get_mut(ptr.alloc_id)?.write_scalar(
734-
tcx, ptr, ptr_align.min(dest.layout.align.abi), scalar, dest.layout.size
734+
tcx, ptr, scalar, dest.layout.size
735735
)
736736
}
737737
Immediate::ScalarPair(a_val, b_val) => {
@@ -741,20 +741,22 @@ where
741741
dest.layout)
742742
};
743743
let (a_size, b_size) = (a.size(self), b.size(self));
744-
let (a_align, b_align) = (a.align(self).abi, b.align(self).abi);
744+
let b_align = b.align(self).abi;
745745
let b_offset = a_size.align_to(b_align);
746746
let b_ptr = ptr.offset(b_offset, self)?;
747747

748+
self.memory.check_align(b_ptr.into(), ptr_align.min(b_align))?;
749+
748750
// It is tempting to verify `b_offset` against `layout.fields.offset(1)`,
749751
// but that does not work: We could be a newtype around a pair, then the
750752
// fields do not match the `ScalarPair` components.
751753

752754
self.memory
753755
.get_mut(ptr.alloc_id)?
754-
.write_scalar(tcx, ptr, ptr_align.min(a_align), a_val, a_size)?;
756+
.write_scalar(tcx, ptr, a_val, a_size)?;
755757
self.memory
756758
.get_mut(b_ptr.alloc_id)?
757-
.write_scalar(tcx, b_ptr, ptr_align.min(b_align), b_val, b_size)
759+
.write_scalar(tcx, b_ptr, b_val, b_size)
758760
}
759761
}
760762
}

src/librustc_mir/interpret/terminator.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,12 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
401401
// cannot use the shim here, because that will only result in infinite recursion
402402
ty::InstanceDef::Virtual(_, idx) => {
403403
let ptr_size = self.pointer_size();
404-
let ptr_align = self.tcx.data_layout.pointer_align.abi;
405404
let ptr = self.deref_operand(args[0])?;
406405
let vtable = ptr.vtable()?;
406+
self.memory.check_align(vtable.into(), self.tcx.data_layout.pointer_align.abi)?;
407407
let fn_ptr = self.memory.get(vtable.alloc_id)?.read_ptr_sized(
408408
self,
409409
vtable.offset(ptr_size * (idx as u64 + 3), self)?,
410-
ptr_align
411410
)?.to_ptr()?;
412411
let instance = self.memory.get_fn(fn_ptr)?;
413412

src/librustc_mir/interpret/traits.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,16 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
6161
let drop = self.memory.create_fn_alloc(drop).with_default_tag();
6262
self.memory
6363
.get_mut(vtable.alloc_id)?
64-
.write_ptr_sized(tcx, vtable, ptr_align, Scalar::Ptr(drop).into())?;
64+
.write_ptr_sized(tcx, vtable, Scalar::Ptr(drop).into())?;
6565

6666
let size_ptr = vtable.offset(ptr_size, self)?;
6767
self.memory
6868
.get_mut(size_ptr.alloc_id)?
69-
.write_ptr_sized(tcx, size_ptr, ptr_align, Scalar::from_uint(size, ptr_size).into())?;
69+
.write_ptr_sized(tcx, size_ptr, Scalar::from_uint(size, ptr_size).into())?;
7070
let align_ptr = vtable.offset(ptr_size * 2, self)?;
7171
self.memory
7272
.get_mut(align_ptr.alloc_id)?
73-
.write_ptr_sized(tcx, align_ptr, ptr_align, Scalar::from_uint(align, ptr_size).into())?;
73+
.write_ptr_sized(tcx, align_ptr, Scalar::from_uint(align, ptr_size).into())?;
7474

7575
for (i, method) in methods.iter().enumerate() {
7676
if let Some((def_id, substs)) = *method {
@@ -79,7 +79,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
7979
let method_ptr = vtable.offset(ptr_size * (3 + i as u64), self)?;
8080
self.memory
8181
.get_mut(method_ptr.alloc_id)?
82-
.write_ptr_sized(tcx, method_ptr, ptr_align, Scalar::Ptr(fn_ptr).into())?;
82+
.write_ptr_sized(tcx, method_ptr, Scalar::Ptr(fn_ptr).into())?;
8383
}
8484
}
8585

@@ -95,10 +95,10 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
9595
vtable: Pointer<M::PointerTag>,
9696
) -> EvalResult<'tcx, (ty::Instance<'tcx>, ty::Ty<'tcx>)> {
9797
// we don't care about the pointee type, we just want a pointer
98-
let pointer_align = self.tcx.data_layout.pointer_align.abi;
98+
self.memory.check_align(vtable.into(), self.tcx.data_layout.pointer_align.abi)?;
9999
let drop_fn = self.memory
100100
.get(vtable.alloc_id)?
101-
.read_ptr_sized(self, vtable, pointer_align)?
101+
.read_ptr_sized(self, vtable)?
102102
.to_ptr()?;
103103
let drop_instance = self.memory.get_fn(drop_fn)?;
104104
trace!("Found drop fn: {:?}", drop_instance);
@@ -114,14 +114,13 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
114114
vtable: Pointer<M::PointerTag>,
115115
) -> EvalResult<'tcx, (Size, Align)> {
116116
let pointer_size = self.pointer_size();
117-
let pointer_align = self.tcx.data_layout.pointer_align.abi;
117+
self.memory.check_align(vtable.into(), self.tcx.data_layout.pointer_align.abi)?;
118118
let alloc = self.memory.get(vtable.alloc_id)?;
119-
let size = alloc.read_ptr_sized(self, vtable.offset(pointer_size, self)?, pointer_align)?
119+
let size = alloc.read_ptr_sized(self, vtable.offset(pointer_size, self)?)?
120120
.to_bits(pointer_size)? as u64;
121121
let align = alloc.read_ptr_sized(
122122
self,
123123
vtable.offset(pointer_size * 2, self)?,
124-
pointer_align
125124
)?.to_bits(pointer_size)? as u64;
126125
Ok((Size::from_bytes(size), Align::from_bytes(align).unwrap()))
127126
}

0 commit comments

Comments
 (0)