1
Fork 0

avoid computing misalignment if we won't act on it

This commit is contained in:
Ralf Jung 2023-09-05 21:54:14 +02:00
parent f3f9b795bd
commit cbf47a17d2

View file

@ -424,7 +424,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
throw_ub!(DanglingIntPointer(addr, msg)); throw_ub!(DanglingIntPointer(addr, msg));
} }
// Must be aligned. // Must be aligned.
self.check_misalign(Self::offset_misalignment(addr, align))?; if M::enforce_alignment(self) && align.bytes() > 1 {
self.check_misalign(Self::offset_misalignment(addr, align))?;
}
None None
} }
Ok((alloc_id, offset, prov)) => { Ok((alloc_id, offset, prov)) => {
@ -446,7 +448,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
} }
// Test align. Check this last; if both bounds and alignment are violated // Test align. Check this last; if both bounds and alignment are violated
// we want the error to be about the bounds. // we want the error to be about the bounds.
self.check_misalign(self.alloc_misalignment(ptr, offset, align, alloc_align))?; if M::enforce_alignment(self) && align.bytes() > 1 {
self.check_misalign(self.alloc_misalignment(ptr, offset, align, alloc_align))?;
}
// We can still be zero-sized in this branch, in which case we have to // We can still be zero-sized in this branch, in which case we have to
// return `None`. // return `None`.
@ -457,10 +461,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
#[inline(always)] #[inline(always)]
pub(super) fn check_misalign(&self, misaligned: Option<Misalignment>) -> InterpResult<'tcx> { pub(super) fn check_misalign(&self, misaligned: Option<Misalignment>) -> InterpResult<'tcx> {
if M::enforce_alignment(self) { if let Some(misaligned) = misaligned {
if let Some(misaligned) = misaligned { throw_ub!(AlignmentCheckFailed(misaligned))
throw_ub!(AlignmentCheckFailed(misaligned))
}
} }
Ok(()) Ok(())
} }
@ -502,6 +504,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
ptr: Pointer<Option<M::Provenance>>, ptr: Pointer<Option<M::Provenance>>,
align: Align, align: Align,
) -> Option<Misalignment> { ) -> Option<Misalignment> {
if !M::enforce_alignment(self) {
return None;
}
match self.ptr_try_get_alloc_id(ptr) { match self.ptr_try_get_alloc_id(ptr) {
Err(addr) => Self::offset_misalignment(addr, align), Err(addr) => Self::offset_misalignment(addr, align),
Ok((alloc_id, offset, _prov)) => { Ok((alloc_id, offset, _prov)) => {