Skip to content

Commit 73e56de

Browse files
authored
Rollup merge of rust-lang#71101 - RalfJung:miri-alignment-check, r=ecstatic-morse
Miri: let machine hook dynamically decide about alignment checks This is needed for rust-lang/miri#1332.
2 parents eb49f7f + 28e6b1a commit 73e56de

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

src/librustc_mir/const_eval/machine.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,12 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter {
179179

180180
const GLOBAL_KIND: Option<!> = None; // no copying of globals from `tcx` to machine memory
181181

182-
// We do not check for alignment to avoid having to carry an `Align`
183-
// in `ConstValue::ByRef`.
184-
const CHECK_ALIGN: bool = false;
182+
#[inline(always)]
183+
fn enforce_alignment(_memory_extra: &Self::MemoryExtra) -> bool {
184+
// We do not check for alignment to avoid having to carry an `Align`
185+
// in `ConstValue::ByRef`.
186+
false
187+
}
185188

186189
#[inline(always)]
187190
fn enforce_validity(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {

src/librustc_mir/interpret/machine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
118118
const GLOBAL_KIND: Option<Self::MemoryKind>;
119119

120120
/// Whether memory accesses should be alignment-checked.
121-
const CHECK_ALIGN: bool;
121+
fn enforce_alignment(memory_extra: &Self::MemoryExtra) -> bool;
122122

123123
/// Whether to enforce the validity invariant
124124
fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;

src/librustc_mir/interpret/memory.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,12 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
323323
size: Size,
324324
align: Align,
325325
) -> InterpResult<'tcx, Option<Pointer<M::PointerTag>>> {
326-
let align = M::CHECK_ALIGN.then_some(align);
326+
let align = M::enforce_alignment(&self.extra).then_some(align);
327327
self.check_ptr_access_align(sptr, size, align, CheckInAllocMsg::MemoryAccessTest)
328328
}
329329

330330
/// Like `check_ptr_access`, but *definitely* checks alignment when `align`
331-
/// is `Some` (overriding `M::CHECK_ALIGN`). Also lets the caller control
331+
/// is `Some` (overriding `M::enforce_alignment`). Also lets the caller control
332332
/// the error message for the out-of-bounds case.
333333
pub fn check_ptr_access_align(
334334
&self,

src/librustc_mir/transform/const_prop.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
173173

174174
const GLOBAL_KIND: Option<!> = None; // no copying of globals from `tcx` to machine memory
175175

176-
const CHECK_ALIGN: bool = false;
176+
#[inline(always)]
177+
fn enforce_alignment(_memory_extra: &Self::MemoryExtra) -> bool {
178+
false
179+
}
177180

178181
#[inline(always)]
179182
fn enforce_validity(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {

0 commit comments

Comments
 (0)