1
Fork 0

more precise error for 'based on misaligned pointer' case

This commit is contained in:
Ralf Jung 2023-09-26 16:25:05 +02:00
parent cbf47a17d2
commit e24835c6e0
31 changed files with 112 additions and 91 deletions

View file

@ -1,11 +1,15 @@
const_eval_address_space_full = const_eval_address_space_full =
there are no more free addresses in the address space there are no more free addresses in the address space
const_eval_align_check_failed = accessing memory with alignment {$has}, but alignment {$required} is required
const_eval_align_offset_invalid_align = const_eval_align_offset_invalid_align =
`align_offset` called with non-power-of-two align: {$target_align} `align_offset` called with non-power-of-two align: {$target_align}
const_eval_alignment_check_failed = const_eval_alignment_check_failed =
accessing memory with alignment {$has}, but alignment {$required} is required {$msg ->
[AccessedPtr] accessing memory
*[other] accessing memory based on pointer
} with alignment {$has}, but alignment {$required} is required
const_eval_already_reported = const_eval_already_reported =
an error has already been reported elsewhere (this should not usually be printed) an error has already been reported elsewhere (this should not usually be printed)
const_eval_assume_false = const_eval_assume_false =

View file

@ -390,15 +390,6 @@ pub struct LiveDrop<'tcx> {
pub dropped_at: Option<Span>, pub dropped_at: Option<Span>,
} }
#[derive(LintDiagnostic)]
#[diag(const_eval_align_check_failed)]
pub struct AlignmentCheckFailed {
pub has: u64,
pub required: u64,
#[subdiagnostic]
pub frames: Vec<FrameNote>,
}
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(const_eval_error, code = "E0080")] #[diag(const_eval_error, code = "E0080")]
pub struct ConstEvalError { pub struct ConstEvalError {
@ -568,9 +559,10 @@ impl<'a> ReportErrorExt for UndefinedBehaviorInfo<'a> {
builder.set_arg("bad_pointer_message", bad_pointer_message(msg, handler)); builder.set_arg("bad_pointer_message", bad_pointer_message(msg, handler));
} }
AlignmentCheckFailed(Misalignment { required, has }) => { AlignmentCheckFailed(Misalignment { required, has }, msg) => {
builder.set_arg("required", required.bytes()); builder.set_arg("required", required.bytes());
builder.set_arg("has", has.bytes()); builder.set_arg("has", has.bytes());
builder.set_arg("msg", format!("{msg:?}"));
} }
WriteToReadOnly(alloc) | DerefFunctionPointer(alloc) | DerefVTablePointer(alloc) => { WriteToReadOnly(alloc) | DerefFunctionPointer(alloc) | DerefVTablePointer(alloc) => {
builder.set_arg("allocation", alloc); builder.set_arg("allocation", alloc);

View file

@ -21,8 +21,8 @@ use rustc_target::abi::{Align, HasDataLayout, Size};
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use super::{ use super::{
alloc_range, AllocBytes, AllocId, AllocMap, AllocRange, Allocation, CheckInAllocMsg, alloc_range, AllocBytes, AllocId, AllocMap, AllocRange, Allocation, CheckAlignMsg,
GlobalAlloc, InterpCx, InterpResult, Machine, MayLeak, Misalignment, Pointer, CheckInAllocMsg, GlobalAlloc, InterpCx, InterpResult, Machine, MayLeak, Misalignment, Pointer,
PointerArithmetic, Provenance, Scalar, PointerArithmetic, Provenance, Scalar,
}; };
@ -425,7 +425,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
} }
// Must be aligned. // Must be aligned.
if M::enforce_alignment(self) && align.bytes() > 1 { if M::enforce_alignment(self) && align.bytes() > 1 {
self.check_misalign(Self::offset_misalignment(addr, align))?; self.check_misalign(
Self::offset_misalignment(addr, align),
CheckAlignMsg::AccessedPtr,
)?;
} }
None None
} }
@ -449,7 +452,10 @@ 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.
if M::enforce_alignment(self) && align.bytes() > 1 { if M::enforce_alignment(self) && align.bytes() > 1 {
self.check_misalign(self.alloc_misalignment(ptr, offset, align, alloc_align))?; self.check_misalign(
self.alloc_misalignment(ptr, offset, align, alloc_align),
CheckAlignMsg::AccessedPtr,
)?;
} }
// 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
@ -460,9 +466,13 @@ 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>,
msg: CheckAlignMsg,
) -> InterpResult<'tcx> {
if let Some(misaligned) = misaligned { if let Some(misaligned) = misaligned {
throw_ub!(AlignmentCheckFailed(misaligned)) throw_ub!(AlignmentCheckFailed(misaligned, msg))
} }
Ok(()) Ok(())
} }

View file

@ -15,9 +15,9 @@ use rustc_middle::ty::Ty;
use rustc_target::abi::{Abi, Align, FieldIdx, HasDataLayout, Size, FIRST_VARIANT}; use rustc_target::abi::{Abi, Align, FieldIdx, HasDataLayout, Size, FIRST_VARIANT};
use super::{ use super::{
alloc_range, mir_assign_valid_types, AllocId, AllocRef, AllocRefMut, ImmTy, Immediate, alloc_range, mir_assign_valid_types, AllocId, AllocRef, AllocRefMut, CheckAlignMsg, ImmTy,
InterpCx, InterpResult, Machine, MemoryKind, Misalignment, OffsetMode, OpTy, Operand, Pointer, Immediate, InterpCx, InterpResult, Machine, MemoryKind, Misalignment, OffsetMode, OpTy,
PointerArithmetic, Projectable, Provenance, Readable, Scalar, Operand, Pointer, PointerArithmetic, Projectable, Provenance, Readable, Scalar,
}; };
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)] #[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]
@ -461,7 +461,7 @@ where
// We check alignment separately, and *after* checking everything else. // We check alignment separately, and *after* checking everything else.
// If an access is both OOB and misaligned, we want to see the bounds error. // If an access is both OOB and misaligned, we want to see the bounds error.
let a = self.get_ptr_alloc(mplace.ptr(), size, Align::ONE)?; let a = self.get_ptr_alloc(mplace.ptr(), size, Align::ONE)?;
self.check_misalign(mplace.mplace.misaligned)?; self.check_misalign(mplace.mplace.misaligned, CheckAlignMsg::BasedOn)?;
Ok(a) Ok(a)
} }
@ -477,7 +477,7 @@ where
// We check alignment separately, and raise that error *after* checking everything else. // We check alignment separately, and raise that error *after* checking everything else.
// If an access is both OOB and misaligned, we want to see the bounds error. // If an access is both OOB and misaligned, we want to see the bounds error.
// However we have to call `check_misalign` first to make the borrow checker happy. // However we have to call `check_misalign` first to make the borrow checker happy.
let misalign_err = self.check_misalign(mplace.mplace.misaligned); let misalign_err = self.check_misalign(mplace.mplace.misaligned, CheckAlignMsg::BasedOn);
let a = self.get_ptr_alloc_mut(mplace.ptr(), size, Align::ONE)?; let a = self.get_ptr_alloc_mut(mplace.ptr(), size, Align::ONE)?;
misalign_err?; misalign_err?;
Ok(a) Ok(a)
@ -881,8 +881,8 @@ where
dest_size, dest_size,
/*nonoverlapping*/ true, /*nonoverlapping*/ true,
)?; )?;
self.check_misalign(src.mplace.misaligned)?; self.check_misalign(src.mplace.misaligned, CheckAlignMsg::BasedOn)?;
self.check_misalign(dest.mplace.misaligned)?; self.check_misalign(dest.mplace.misaligned, CheckAlignMsg::BasedOn)?;
Ok(()) Ok(())
} }

View file

@ -385,7 +385,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
CheckInAllocMsg::InboundsTest, // will anyway be replaced by validity message CheckInAllocMsg::InboundsTest, // will anyway be replaced by validity message
), ),
self.path, self.path,
Ub(AlignmentCheckFailed(Misalignment { required, has })) => UnalignedPtr { Ub(AlignmentCheckFailed(Misalignment { required, has }, _msg)) => UnalignedPtr {
ptr_kind, ptr_kind,
required_bytes: required.bytes(), required_bytes: required.bytes(),
found_bytes: has.bytes() found_bytes: has.bytes()

View file

@ -216,7 +216,7 @@ pub enum InvalidProgramInfo<'tcx> {
} }
/// Details of why a pointer had to be in-bounds. /// Details of why a pointer had to be in-bounds.
#[derive(Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)] #[derive(Debug, Copy, Clone)]
pub enum CheckInAllocMsg { pub enum CheckInAllocMsg {
/// We are access memory. /// We are access memory.
MemoryAccessTest, MemoryAccessTest,
@ -228,7 +228,16 @@ pub enum CheckInAllocMsg {
InboundsTest, InboundsTest,
} }
#[derive(Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)] /// Details of which pointer is not aligned.
#[derive(Debug, Copy, Clone)]
pub enum CheckAlignMsg {
/// The accessed pointer did not have proper alignment.
AccessedPtr,
/// The access ocurred with a place that was based on a misaligned pointer.
BasedOn,
}
#[derive(Debug, Copy, Clone)]
pub enum InvalidMetaKind { pub enum InvalidMetaKind {
/// Size of a `[T]` is too big /// Size of a `[T]` is too big
SliceTooBig, SliceTooBig,
@ -329,7 +338,7 @@ pub enum UndefinedBehaviorInfo<'tcx> {
/// Using an integer as a pointer in the wrong way. /// Using an integer as a pointer in the wrong way.
DanglingIntPointer(u64, CheckInAllocMsg), DanglingIntPointer(u64, CheckInAllocMsg),
/// Used a pointer with bad alignment. /// Used a pointer with bad alignment.
AlignmentCheckFailed(Misalignment), AlignmentCheckFailed(Misalignment, CheckAlignMsg),
/// Writing to read-only memory. /// Writing to read-only memory.
WriteToReadOnly(AllocId), WriteToReadOnly(AllocId),
/// Trying to access the data behind a function pointer. /// Trying to access the data behind a function pointer.

View file

@ -142,11 +142,12 @@ use crate::ty::GenericArgKind;
use crate::ty::{self, Instance, Ty, TyCtxt}; use crate::ty::{self, Instance, Ty, TyCtxt};
pub use self::error::{ pub use self::error::{
struct_error, BadBytesAccess, CheckInAllocMsg, ErrorHandled, EvalToAllocationRawResult, struct_error, BadBytesAccess, CheckAlignMsg, CheckInAllocMsg, ErrorHandled,
EvalToConstValueResult, EvalToValTreeResult, ExpectedKind, InterpError, InterpErrorInfo, EvalToAllocationRawResult, EvalToConstValueResult, EvalToValTreeResult, ExpectedKind,
InterpResult, InvalidMetaKind, InvalidProgramInfo, MachineStopType, Misalignment, PointerKind, InterpError, InterpErrorInfo, InterpResult, InvalidMetaKind, InvalidProgramInfo,
ReportedErrorInfo, ResourceExhaustionInfo, ScalarSizeMismatch, UndefinedBehaviorInfo, MachineStopType, Misalignment, PointerKind, ReportedErrorInfo, ResourceExhaustionInfo,
UnsupportedOpInfo, ValidationErrorInfo, ValidationErrorKind, ScalarSizeMismatch, UndefinedBehaviorInfo, UnsupportedOpInfo, ValidationErrorInfo,
ValidationErrorKind,
}; };
pub use self::value::Scalar; pub use self::value::Scalar;

View file

@ -2,7 +2,7 @@ error[E0080]: evaluation of constant value failed
--> $DIR/const-ub-checks.rs:LL:CC --> $DIR/const-ub-checks.rs:LL:CC
| |
LL | ptr.read(); LL | ptr.read();
| ^^^^^^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required | ^^^^^^^^^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
note: erroneous constant encountered note: erroneous constant encountered
--> $DIR/const-ub-checks.rs:LL:CC --> $DIR/const-ub-checks.rs:LL:CC

View file

@ -0,0 +1,8 @@
#![feature(pointer_byte_offsets)]
fn main() {
let v: Vec<u16> = vec![1, 2];
// This read is also misaligned. We make sure that the OOB message has priority.
let x = unsafe { *v.as_ptr().wrapping_byte_add(5) }; //~ ERROR: out-of-bounds
panic!("this should never print: {}", x);
}

View file

@ -1,18 +1,18 @@
error: Undefined Behavior: memory access failed: ALLOC has size 2, so pointer to 1 byte starting at offset 5 is out-of-bounds error: Undefined Behavior: memory access failed: ALLOC has size 4, so pointer to 2 bytes starting at offset 5 is out-of-bounds
--> $DIR/out_of_bounds_read1.rs:LL:CC --> $DIR/out_of_bounds_read.rs:LL:CC
| |
LL | let x = unsafe { *v.as_ptr().wrapping_offset(5) }; LL | let x = unsafe { *v.as_ptr().wrapping_byte_add(5) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: ALLOC has size 2, so pointer to 1 byte starting at offset 5 is out-of-bounds | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: ALLOC has size 4, so pointer to 2 bytes starting at offset 5 is out-of-bounds
| |
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
help: ALLOC was allocated here: help: ALLOC was allocated here:
--> $DIR/out_of_bounds_read1.rs:LL:CC --> $DIR/out_of_bounds_read.rs:LL:CC
| |
LL | let v: Vec<u8> = vec![1, 2]; LL | let v: Vec<u16> = vec![1, 2];
| ^^^^^^^^^^ | ^^^^^^^^^^
= note: BACKTRACE (of the first span): = note: BACKTRACE (of the first span):
= note: inside `main` at $DIR/out_of_bounds_read1.rs:LL:CC = note: inside `main` at $DIR/out_of_bounds_read.rs:LL:CC
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

View file

@ -1,5 +0,0 @@
fn main() {
let v: Vec<u8> = vec![1, 2];
let x = unsafe { *v.as_ptr().wrapping_offset(5) }; //~ ERROR: out-of-bounds
panic!("this should never print: {}", x);
}

View file

@ -1,5 +0,0 @@
fn main() {
let v: Vec<u8> = vec![1, 2];
let x = unsafe { *v.as_ptr().wrapping_offset(5) }; //~ ERROR: out-of-bounds
panic!("this should never print: {}", x);
}

View file

@ -0,0 +1,7 @@
#![feature(pointer_byte_offsets)]
fn main() {
let mut v: Vec<u16> = vec![1, 2];
// This read is also misaligned. We make sure that the OOB message has priority.
unsafe { *v.as_mut_ptr().wrapping_byte_add(5) = 0 }; //~ ERROR: out-of-bounds
}

View file

@ -1,18 +1,18 @@
error: Undefined Behavior: memory access failed: ALLOC has size 2, so pointer to 1 byte starting at offset 5 is out-of-bounds error: Undefined Behavior: memory access failed: ALLOC has size 4, so pointer to 2 bytes starting at offset 5 is out-of-bounds
--> $DIR/out_of_bounds_read2.rs:LL:CC --> $DIR/out_of_bounds_write.rs:LL:CC
| |
LL | let x = unsafe { *v.as_ptr().wrapping_offset(5) }; LL | unsafe { *v.as_mut_ptr().wrapping_byte_add(5) = 0 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: ALLOC has size 2, so pointer to 1 byte starting at offset 5 is out-of-bounds | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: ALLOC has size 4, so pointer to 2 bytes starting at offset 5 is out-of-bounds
| |
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
help: ALLOC was allocated here: help: ALLOC was allocated here:
--> $DIR/out_of_bounds_read2.rs:LL:CC --> $DIR/out_of_bounds_write.rs:LL:CC
| |
LL | let v: Vec<u8> = vec![1, 2]; LL | let mut v: Vec<u16> = vec![1, 2];
| ^^^^^^^^^^ | ^^^^^^^^^^
= note: BACKTRACE (of the first span): = note: BACKTRACE (of the first span):
= note: inside `main` at $DIR/out_of_bounds_read2.rs:LL:CC = note: inside `main` at $DIR/out_of_bounds_write.rs:LL:CC
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

View file

@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/alignment.rs:LL:CC --> $DIR/alignment.rs:LL:CC
| |
LL | *(x_ptr as *mut u32) = 42; *(x_ptr.add(1) as *mut u32) = 42; LL | *(x_ptr as *mut u32) = 42; *(x_ptr.add(1) as *mut u32) = 42;
| ^ accessing memory with alignment ALIGN, but alignment ALIGN is required | ^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
| |
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

View file

@ -8,7 +8,7 @@ pub struct S {
} }
unsafe fn foo(x: *const S) -> u8 { unsafe fn foo(x: *const S) -> u8 {
unsafe { (*x).x } //~ERROR: accessing memory with alignment 1, but alignment 4 is required unsafe { (*x).x } //~ERROR: based on pointer with alignment 1, but alignment 4 is required
} }
fn main() { fn main() {

View file

@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/field_requires_parent_struct_alignment.rs:LL:CC --> $DIR/field_requires_parent_struct_alignment.rs:LL:CC
| |
LL | unsafe { (*x).x } LL | unsafe { (*x).x }
| ^^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required | ^^^^^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
| |
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

View file

@ -15,7 +15,7 @@ pub struct Packed {
} }
unsafe fn foo(x: *const Aligned) -> u8 { unsafe fn foo(x: *const Aligned) -> u8 {
unsafe { (*x).packed.x } //~ERROR: accessing memory with alignment 1, but alignment 16 is required unsafe { (*x).packed.x } //~ERROR: based on pointer with alignment 1, but alignment 16 is required
} }
fn main() { fn main() {

View file

@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/field_requires_parent_struct_alignment2.rs:LL:CC --> $DIR/field_requires_parent_struct_alignment2.rs:LL:CC
| |
LL | unsafe { (*x).packed.x } LL | unsafe { (*x).packed.x }
| ^^^^^^^^^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required | ^^^^^^^^^^^^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
| |
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

View file

@ -12,6 +12,6 @@ fn main() {
// Manually make sure the pointer is properly aligned. // Manually make sure the pointer is properly aligned.
let base_addr_aligned = if base_addr % 2 == 0 { base_addr } else { base_addr + 1 }; let base_addr_aligned = if base_addr % 2 == 0 { base_addr } else { base_addr + 1 };
let u16_ptr = base_addr_aligned as *mut u16; let u16_ptr = base_addr_aligned as *mut u16;
unsafe { *u16_ptr = 2 }; //~ERROR: memory with alignment 1, but alignment 2 is required unsafe { *u16_ptr = 2 }; //~ERROR: with alignment 1, but alignment 2 is required
println!("{:?}", x); println!("{:?}", x);
} }

View file

@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/intptrcast_alignment_check.rs:LL:CC --> $DIR/intptrcast_alignment_check.rs:LL:CC
| |
LL | unsafe { *u16_ptr = 2 }; LL | unsafe { *u16_ptr = 2 };
| ^^^^^^^^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required | ^^^^^^^^^^^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
| |
= help: this usually indicates that your program performed an invalid operation and caused Undefined Behavior = help: this usually indicates that your program performed an invalid operation and caused Undefined Behavior
= help: but due to `-Zmiri-symbolic-alignment-check`, alignment errors can also be false positives = help: but due to `-Zmiri-symbolic-alignment-check`, alignment errors can also be false positives

View file

@ -7,6 +7,6 @@ fn main() {
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error. let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
let x = &x[0] as *const _ as *const u32; let x = &x[0] as *const _ as *const u32;
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned. // This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
let _x = unsafe { *x }; //~ERROR: memory with alignment 2, but alignment 4 is required let _x = unsafe { *x }; //~ERROR: with alignment 2, but alignment 4 is required
} }
} }

View file

@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/unaligned_ptr1.rs:LL:CC --> $DIR/unaligned_ptr1.rs:LL:CC
| |
LL | let _x = unsafe { *x }; LL | let _x = unsafe { *x };
| ^^ accessing memory with alignment ALIGN, but alignment ALIGN is required | ^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
| |
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

View file

@ -8,5 +8,5 @@ fn main() {
let x = (x.as_ptr() as *const u8).wrapping_offset(3) as *const u32; let x = (x.as_ptr() as *const u8).wrapping_offset(3) as *const u32;
// This must fail because alignment is violated: the offset is not sufficiently aligned. // This must fail because alignment is violated: the offset is not sufficiently aligned.
// Also make the offset not a power of 2, that used to ICE. // Also make the offset not a power of 2, that used to ICE.
let _x = unsafe { *x }; //~ERROR: memory with alignment 1, but alignment 4 is required let _x = unsafe { *x }; //~ERROR: with alignment 1, but alignment 4 is required
} }

View file

@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/unaligned_ptr2.rs:LL:CC --> $DIR/unaligned_ptr2.rs:LL:CC
| |
LL | let _x = unsafe { *x }; LL | let _x = unsafe { *x };
| ^^ accessing memory with alignment ALIGN, but alignment ALIGN is required | ^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
| |
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

View file

@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/unaligned_ptr3.rs:LL:CC --> $DIR/unaligned_ptr3.rs:LL:CC
| |
LL | let _x = unsafe { *x }; LL | let _x = unsafe { *x };
| ^^ accessing memory with alignment ALIGN, but alignment ALIGN is required | ^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
| |
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

View file

@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/unaligned_ptr4.rs:LL:CC --> $DIR/unaligned_ptr4.rs:LL:CC
| |
LL | let _val = unsafe { *ptr }; LL | let _val = unsafe { *ptr };
| ^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required | ^^^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
| |
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

View file

@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/unaligned_ptr_zst.rs:LL:CC --> $DIR/unaligned_ptr_zst.rs:LL:CC
| |
LL | let _x = unsafe { *x }; LL | let _x = unsafe { *x };
| ^^ accessing memory with alignment ALIGN, but alignment ALIGN is required | ^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
| |
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

View file

@ -7,14 +7,14 @@ const MISALIGNED_LOAD: () = unsafe {
let mem = [0u32; 8]; let mem = [0u32; 8];
let ptr = mem.as_ptr().byte_add(1); let ptr = mem.as_ptr().byte_add(1);
let _val = *ptr; //~ERROR: evaluation of constant value failed let _val = *ptr; //~ERROR: evaluation of constant value failed
//~^NOTE: accessing memory with alignment 1, but alignment 4 is required //~^NOTE: based on pointer with alignment 1, but alignment 4 is required
}; };
const MISALIGNED_STORE: () = unsafe { const MISALIGNED_STORE: () = unsafe {
let mut mem = [0u32; 8]; let mut mem = [0u32; 8];
let ptr = mem.as_mut_ptr().byte_add(1); let ptr = mem.as_mut_ptr().byte_add(1);
*ptr = 0; //~ERROR: evaluation of constant value failed *ptr = 0; //~ERROR: evaluation of constant value failed
//~^NOTE: accessing memory with alignment 1, but alignment 4 is required //~^NOTE: based on pointer with alignment 1, but alignment 4 is required
}; };
const MISALIGNED_COPY: () = unsafe { const MISALIGNED_COPY: () = unsafe {
@ -34,7 +34,7 @@ const MISALIGNED_FIELD: () = unsafe {
let ptr = mem.as_ptr().cast::<Aligned>(); let ptr = mem.as_ptr().cast::<Aligned>();
// Accessing an f32 field but we still require the alignment of the pointer type. // Accessing an f32 field but we still require the alignment of the pointer type.
let _val = (*ptr).0; //~ERROR: evaluation of constant value failed let _val = (*ptr).0; //~ERROR: evaluation of constant value failed
//~^NOTE: accessing memory with alignment 4, but alignment 16 is required //~^NOTE: based on pointer with alignment 4, but alignment 16 is required
}; };
const OOB: () = unsafe { const OOB: () = unsafe {

View file

@ -2,13 +2,13 @@ error[E0080]: evaluation of constant value failed
--> $DIR/raw-pointer-ub.rs:9:16 --> $DIR/raw-pointer-ub.rs:9:16
| |
LL | let _val = *ptr; LL | let _val = *ptr;
| ^^^^ accessing memory with alignment 1, but alignment 4 is required | ^^^^ accessing memory based on pointer with alignment 1, but alignment 4 is required
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/raw-pointer-ub.rs:16:5 --> $DIR/raw-pointer-ub.rs:16:5
| |
LL | *ptr = 0; LL | *ptr = 0;
| ^^^^^^^^ accessing memory with alignment 1, but alignment 4 is required | ^^^^^^^^ accessing memory based on pointer with alignment 1, but alignment 4 is required
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL --> $SRC_DIR/core/src/intrinsics.rs:LL:COL
@ -29,7 +29,7 @@ error[E0080]: evaluation of constant value failed
--> $DIR/raw-pointer-ub.rs:36:16 --> $DIR/raw-pointer-ub.rs:36:16
| |
LL | let _val = (*ptr).0; LL | let _val = (*ptr).0;
| ^^^^^^^^ accessing memory with alignment 4, but alignment 16 is required | ^^^^^^^^ accessing memory based on pointer with alignment 4, but alignment 16 is required
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/raw-pointer-ub.rs:43:16 --> $DIR/raw-pointer-ub.rs:43:16

View file

@ -151,7 +151,7 @@ LL | const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) };
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
| |
= note: accessing memory with alignment 1, but alignment 4 is required = note: accessing memory based on pointer with alignment 1, but alignment 4 is required
| |
note: inside `std::ptr::read::<u32>` note: inside `std::ptr::read::<u32>`
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL