Skip to content

Commit cbf47a1

Browse files
committed
avoid computing misalignment if we won't act on it
1 parent f3f9b79 commit cbf47a1

File tree

1 file changed

+11
-6
lines changed
  • compiler/rustc_const_eval/src/interpret

1 file changed

+11
-6
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
424424
throw_ub!(DanglingIntPointer(addr, msg));
425425
}
426426
// Must be aligned.
427-
self.check_misalign(Self::offset_misalignment(addr, align))?;
427+
if M::enforce_alignment(self) && align.bytes() > 1 {
428+
self.check_misalign(Self::offset_misalignment(addr, align))?;
429+
}
428430
None
429431
}
430432
Ok((alloc_id, offset, prov)) => {
@@ -446,7 +448,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
446448
}
447449
// Test align. Check this last; if both bounds and alignment are violated
448450
// we want the error to be about the bounds.
449-
self.check_misalign(self.alloc_misalignment(ptr, offset, align, alloc_align))?;
451+
if M::enforce_alignment(self) && align.bytes() > 1 {
452+
self.check_misalign(self.alloc_misalignment(ptr, offset, align, alloc_align))?;
453+
}
450454

451455
// We can still be zero-sized in this branch, in which case we have to
452456
// return `None`.
@@ -457,10 +461,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
457461

458462
#[inline(always)]
459463
pub(super) fn check_misalign(&self, misaligned: Option<Misalignment>) -> InterpResult<'tcx> {
460-
if M::enforce_alignment(self) {
461-
if let Some(misaligned) = misaligned {
462-
throw_ub!(AlignmentCheckFailed(misaligned))
463-
}
464+
if let Some(misaligned) = misaligned {
465+
throw_ub!(AlignmentCheckFailed(misaligned))
464466
}
465467
Ok(())
466468
}
@@ -502,6 +504,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
502504
ptr: Pointer<Option<M::Provenance>>,
503505
align: Align,
504506
) -> Option<Misalignment> {
507+
if !M::enforce_alignment(self) {
508+
return None;
509+
}
505510
match self.ptr_try_get_alloc_id(ptr) {
506511
Err(addr) => Self::offset_misalignment(addr, align),
507512
Ok((alloc_id, offset, _prov)) => {

0 commit comments

Comments
 (0)