Skip to content

Commit e3c5c3d

Browse files
committed
Auto merge of rust-lang#104616 - RalfJung:ctfe-alignment, r=oli-obk,RalfJung
always check alignment during CTFE We originally disabled alignment checks because they got in the way -- there are some things we do with the interpreter during CTFE which does not correspond to actually running user-written code, but is purely administrative, and we didn't want alignment checks there, so we just disabled them entirely. But with `-Zextra-const-ub-checks` we anyway had to figure out how to disable those alignment checks while doing checks in regular code. So now it is easy to enable CTFE alignment checking by default. Let's see what the perf consequences of that are. r? `@oli-obk`
2 parents 53003af + 21b99ba commit e3c5c3d

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/machine.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ use rustc_middle::{
2222
};
2323
use rustc_span::def_id::{CrateNum, DefId};
2424
use rustc_span::Symbol;
25-
use rustc_target::abi::Size;
25+
use rustc_target::abi::{Size, Align};
2626
use rustc_target::spec::abi::Abi;
27+
use rustc_const_eval::const_eval::CheckAlignment;
2728

2829
use crate::{
2930
concurrency::{data_race, weak_memory},
@@ -752,15 +753,28 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
752753
const PANIC_ON_ALLOC_FAIL: bool = false;
753754

754755
#[inline(always)]
755-
fn enforce_alignment(ecx: &MiriInterpCx<'mir, 'tcx>) -> bool {
756-
ecx.machine.check_alignment != AlignmentCheck::None
756+
fn enforce_alignment(ecx: &MiriInterpCx<'mir, 'tcx>) -> CheckAlignment {
757+
if ecx.machine.check_alignment == AlignmentCheck::None {
758+
CheckAlignment::No
759+
} else {
760+
CheckAlignment::Error
761+
}
757762
}
758763

759764
#[inline(always)]
760765
fn use_addr_for_alignment_check(ecx: &MiriInterpCx<'mir, 'tcx>) -> bool {
761766
ecx.machine.check_alignment == AlignmentCheck::Int
762767
}
763768

769+
fn alignment_check_failed(
770+
_ecx: &InterpCx<'mir, 'tcx, Self>,
771+
has: Align,
772+
required: Align,
773+
_check: CheckAlignment,
774+
) -> InterpResult<'tcx, ()> {
775+
throw_ub!(AlignmentCheckFailed { has, required })
776+
}
777+
764778
#[inline(always)]
765779
fn enforce_validity(ecx: &MiriInterpCx<'mir, 'tcx>) -> bool {
766780
ecx.machine.validate

0 commit comments

Comments
 (0)