move calling miri_promise_symbolic_alignment to a shared helper

This commit is contained in:
Ralf Jung
2023-11-20 08:22:56 +01:00
parent bebba4f6e0
commit 2a3fcc0a57
4 changed files with 38 additions and 65 deletions
+25
View File
@@ -2859,3 +2859,28 @@ pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -
write_bytes(dst, val, count)
}
}
/// Inform Miri that a given pointer definitely has a certain alignment.
#[cfg(miri)]
pub(crate) const fn miri_promise_symbolic_alignment(ptr: *const (), align: usize) {
extern "Rust" {
/// Miri-provided extern function to promise that a given pointer is properly aligned for
/// "symbolic" alignment checks. Will fail if the pointer is not actually aligned or `align` is
/// not a power of two. Has no effect when alignment checks are concrete (which is the default).
fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
}
fn runtime(ptr: *const (), align: usize) {
// SAFETY: this call is always safe.
unsafe {
miri_promise_symbolic_alignment(ptr, align);
}
}
const fn compiletime(_ptr: *const (), _align: usize) {}
// SAFETY: the extra behavior at runtime is for UB checks only.
unsafe {
const_eval_select((ptr, align), compiletime, runtime);
}
}
+1 -21
View File
@@ -1374,27 +1374,7 @@ pub const fn align_offset(self, align: usize) -> usize
// Inform Miri that we want to consider the resulting pointer to be suitably aligned.
#[cfg(miri)]
if ret != usize::MAX {
fn runtime(ptr: *const (), align: usize) {
extern "Rust" {
pub fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
}
// SAFETY: this call is always safe.
unsafe {
miri_promise_symbolic_alignment(ptr, align);
}
}
const fn compiletime(_ptr: *const (), _align: usize) {}
// SAFETY: the extra behavior at runtime is for UB checks only.
unsafe {
intrinsics::const_eval_select(
(self.wrapping_add(ret).cast(), align),
compiletime,
runtime,
);
}
intrinsics::miri_promise_symbolic_alignment(self.wrapping_add(ret).cast(), align);
}
ret
+4 -21
View File
@@ -1641,27 +1641,10 @@ pub const fn align_offset(self, align: usize) -> usize
// Inform Miri that we want to consider the resulting pointer to be suitably aligned.
#[cfg(miri)]
if ret != usize::MAX {
fn runtime(ptr: *const (), align: usize) {
extern "Rust" {
pub fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
}
// SAFETY: this call is always safe.
unsafe {
miri_promise_symbolic_alignment(ptr, align);
}
}
const fn compiletime(_ptr: *const (), _align: usize) {}
// SAFETY: the extra behavior at runtime is for UB checks only.
unsafe {
intrinsics::const_eval_select(
(self.wrapping_add(ret).cast_const().cast(), align),
compiletime,
runtime,
);
}
intrinsics::miri_promise_symbolic_alignment(
self.wrapping_add(ret).cast_const().cast(),
align,
);
}
ret
+8 -23
View File
@@ -3870,16 +3870,10 @@ pub unsafe fn align_to<U>(&self) -> (&[T], &[U], &[T]) {
let (us_len, ts_len) = rest.align_to_offsets::<U>();
// Inform Miri that we want to consider the "middle" pointer to be suitably aligned.
#[cfg(miri)]
{
extern "Rust" {
pub fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
}
// SAFETY: this call is always safe.
unsafe {
miri_promise_symbolic_alignment(rest.as_ptr().cast(), mem::align_of::<U>());
}
}
crate::intrinsics::miri_promise_symbolic_alignment(
rest.as_ptr().cast(),
mem::align_of::<U>(),
);
// SAFETY: now `rest` is definitely aligned, so `from_raw_parts` below is okay,
// since the caller guarantees that we can transmute `T` to `U` safely.
unsafe {
@@ -3952,19 +3946,10 @@ pub unsafe fn align_to_mut<U>(&mut self) -> (&mut [T], &mut [U], &mut [T]) {
let mut_ptr = rest.as_mut_ptr();
// Inform Miri that we want to consider the "middle" pointer to be suitably aligned.
#[cfg(miri)]
{
extern "Rust" {
pub fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
}
// SAFETY: this call is always safe.
unsafe {
miri_promise_symbolic_alignment(
mut_ptr.cast() as *const (),
mem::align_of::<U>(),
);
}
}
crate::intrinsics::miri_promise_symbolic_alignment(
mut_ptr.cast() as *const (),
mem::align_of::<U>(),
);
// We can't use `rest` again after this, that would invalidate its alias `mut_ptr`!
// SAFETY: see comments for `align_to`.
unsafe {