Skip to content

Commit 51269b8

Browse files
author
Ellen Arteca
committed
using the align parameter to properly align allocations
1 parent e2ed272 commit 51269b8

File tree

2 files changed

+32
-37
lines changed

2 files changed

+32
-37
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -447,11 +447,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
447447
if let Some(align) = align {
448448
if M::force_int_for_alignment_check(self) {
449449
// `force_int_for_alignment_check` can only be true if `OFFSET_IS_ADDR` is true.
450-
// TODO do a proper check here
451-
// now, addr().bytes() will not give the size of the allocation, it'll be the address
452-
// instead, get the allocation corresponding to this `AllocId` and get the size from it
453-
let alloc = self.get_alloc_raw(alloc_id)?;
454-
check_offset_align(alloc.size().bytes(), align)?;
450+
check_offset_align(ptr.addr().bytes(), align)?;
455451
} else {
456452
// Check allocation alignment and offset alignment.
457453
if alloc_align.bytes() < align.bytes() {

compiler/rustc_middle/src/mir/interpret/allocation.rs

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -209,21 +209,18 @@ impl<Prov> Allocation<Prov> {
209209
) -> Self {
210210
let slice: Cow<'a, [u8]> = slice.into();
211211
let size = Size::from_bytes(slice.len());
212-
// TODO come up with a cleaner way to align the box based on the alignment parameter
213-
let count128 = (slice.len() / 16) + 1;
214-
// println!("ALIGN: {:?}, {:?}", (align.bytes())+1, count128);
215-
let mut vec128: Vec<u128> = Vec::with_capacity(count128);
212+
let align_usize: usize = align.bytes().try_into().unwrap();
213+
let count_align = ((slice.len() / align_usize) + 1)*align_usize;
214+
215+
let mut vec_align: Vec<u8> = Vec::with_capacity(count_align);
216+
vec_align.resize(count_align, 0);
216217
// TODO avoid excess initialization
217-
vec128.resize(count128, 0);
218-
let (buf, _len, _capacity) = vec128.into_raw_parts();
219-
assert!((buf as u64) % 16 == 0);
220-
let vec8: Vec<u8> = unsafe {Vec::from_raw_parts(buf as *mut u8, size.bytes_usize(), size.bytes_usize())};
221-
assert!(vec8.as_ptr() as u64 % 16 == 0);
222-
//let mut vec8: Vec<u8> = unsafe {Vec::from_raw_parts(buf as *mut u8, len * 16, capacity * 16)};
223-
//vec8.truncate(size.bytes_usize());
224-
let mut bytes = vec8.into_boxed_slice();
225-
assert!(bytes.as_ptr() as u64 % 16 == 0);
226-
bytes.copy_from_slice(&slice);
218+
let (buf, _len, _capacity) = vec_align.into_raw_parts();
219+
vec_align = unsafe {Vec::from_raw_parts(buf as *mut u8, size.bytes_usize(), size.bytes_usize())};
220+
221+
let mut bytes = vec_align.into_boxed_slice();
222+
assert!(bytes.as_ptr() as u64 % align.bytes() == 0);
223+
bytes[..slice.len()].copy_from_slice(&slice);
227224

228225
Self {
229226
bytes,
@@ -244,16 +241,16 @@ impl<Prov> Allocation<Prov> {
244241
///
245242
/// If `panic_on_fail` is true, this will never return `Err`.
246243
pub fn uninit<'tcx>(size: Size, align: Align, panic_on_fail: bool) -> InterpResult<'tcx, Self> {
247-
let count128 = (size.bytes_usize() / 16) + 1;
244+
let align_usize: usize = align.bytes().try_into().unwrap();
245+
let count_align = ((size.bytes_usize() / align_usize) + 1)*align_usize;
246+
248247
// TODO this one is supposed to handle allocation failure, so do so.
249-
let mut vec128: Vec<u128> = Vec::with_capacity(count128);
250-
vec128.resize(count128, 0);
251-
let (buf, _len, _capacity) = vec128.into_raw_parts();
252-
assert!((buf as u64) % 16 == 0);
253-
let vec8: Vec<u8> = unsafe {Vec::from_raw_parts(buf as *mut u8, size.bytes_usize(), size.bytes_usize())};
254-
assert!(vec8.as_ptr() as u64 % 16 == 0);
255-
let bytes = vec8.into_boxed_slice();
256-
assert!(bytes.as_ptr() as u64 % 16 == 0);
248+
let mut vec_align: Vec<u8> = Vec::with_capacity(count_align);
249+
vec_align.resize(count_align, 0);
250+
let (buf, _len, _capacity) = vec_align.into_raw_parts();
251+
vec_align = unsafe {Vec::from_raw_parts(buf as *mut u8, size.bytes_usize(), size.bytes_usize())};
252+
253+
let bytes = vec_align.into_boxed_slice();
257254
Ok(()).map_err(|_: ()| {
258255
// This results in an error that can happen non-deterministically, since the memory
259256
// available to the compiler can change between runs. Normally queries are always
@@ -290,14 +287,15 @@ impl Allocation {
290287
) -> Result<Allocation<Prov, Extra>, Err> {
291288
// Compute new pointer provenance, which also adjusts the bytes.
292289
// Realign the pointer
293-
// println!("adjusting: {:?}, {:?}", self.align, self.bytes.len());
294-
let count128 = (self.bytes.len() / 16) + 1;
295-
let mut vec128: Vec<u128> = Vec::with_capacity(count128);
296-
vec128.resize(count128, 0);
297-
let (buf, _len, _capacity) = vec128.into_raw_parts();
298-
let mut vec8: Vec<u8> = unsafe {Vec::from_raw_parts(buf as *mut u8, self.bytes.len(), self.bytes.len())};
299-
vec8[..self.bytes.len()].copy_from_slice(&self.bytes);
300-
let mut bytes = vec8.into_boxed_slice();
290+
let align_usize: usize = self.align.bytes().try_into().unwrap();
291+
let count_align = ((self.bytes.len() / align_usize) + 1)*align_usize;
292+
293+
let mut vec_align: Vec<u8> = Vec::with_capacity(count_align);
294+
vec_align.resize(count_align, 0);
295+
assert!(vec_align.as_ptr() as u64 % self.align.bytes() == 0);
296+
297+
vec_align[..self.bytes.len()].copy_from_slice(&self.bytes);
298+
let mut bytes = vec_align.into_boxed_slice();
301299
let mut new_relocations = Vec::with_capacity(self.relocations.0.len());
302300
let ptr_size = cx.data_layout().pointer_size.bytes_usize();
303301
let endian = cx.data_layout().endian;
@@ -310,7 +308,8 @@ impl Allocation {
310308
write_target_uint(endian, ptr_bytes, ptr_offset.bytes().into()).unwrap();
311309
new_relocations.push((offset, ptr_prov));
312310
}
313-
assert!(bytes.as_ptr() as u64 % 16 == 0);
311+
assert!(bytes.as_ptr() as u64 % self.align.bytes() == 0);
312+
314313
// Create allocation.
315314
Ok(Allocation {
316315
bytes,

0 commit comments

Comments
 (0)