Skip to content

Commit 2a3fcc0

Browse files
committed
move calling miri_promise_symbolic_alignment to a shared helper
1 parent bebba4f commit 2a3fcc0

File tree

4 files changed

+38
-65
lines changed

4 files changed

+38
-65
lines changed

library/core/src/intrinsics.rs

+25
Original file line numberDiff line numberDiff line change
@@ -2859,3 +2859,28 @@ pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
28592859
write_bytes(dst, val, count)
28602860
}
28612861
}
2862+
2863+
/// Inform Miri that a given pointer definitely has a certain alignment.
2864+
#[cfg(miri)]
2865+
pub(crate) const fn miri_promise_symbolic_alignment(ptr: *const (), align: usize) {
2866+
extern "Rust" {
2867+
/// Miri-provided extern function to promise that a given pointer is properly aligned for
2868+
/// "symbolic" alignment checks. Will fail if the pointer is not actually aligned or `align` is
2869+
/// not a power of two. Has no effect when alignment checks are concrete (which is the default).
2870+
fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
2871+
}
2872+
2873+
fn runtime(ptr: *const (), align: usize) {
2874+
// SAFETY: this call is always safe.
2875+
unsafe {
2876+
miri_promise_symbolic_alignment(ptr, align);
2877+
}
2878+
}
2879+
2880+
const fn compiletime(_ptr: *const (), _align: usize) {}
2881+
2882+
// SAFETY: the extra behavior at runtime is for UB checks only.
2883+
unsafe {
2884+
const_eval_select((ptr, align), compiletime, runtime);
2885+
}
2886+
}

library/core/src/ptr/const_ptr.rs

+1-21
Original file line numberDiff line numberDiff line change
@@ -1374,27 +1374,7 @@ impl<T: ?Sized> *const T {
13741374
// Inform Miri that we want to consider the resulting pointer to be suitably aligned.
13751375
#[cfg(miri)]
13761376
if ret != usize::MAX {
1377-
fn runtime(ptr: *const (), align: usize) {
1378-
extern "Rust" {
1379-
pub fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
1380-
}
1381-
1382-
// SAFETY: this call is always safe.
1383-
unsafe {
1384-
miri_promise_symbolic_alignment(ptr, align);
1385-
}
1386-
}
1387-
1388-
const fn compiletime(_ptr: *const (), _align: usize) {}
1389-
1390-
// SAFETY: the extra behavior at runtime is for UB checks only.
1391-
unsafe {
1392-
intrinsics::const_eval_select(
1393-
(self.wrapping_add(ret).cast(), align),
1394-
compiletime,
1395-
runtime,
1396-
);
1397-
}
1377+
intrinsics::miri_promise_symbolic_alignment(self.wrapping_add(ret).cast(), align);
13981378
}
13991379

14001380
ret

library/core/src/ptr/mut_ptr.rs

+4-21
Original file line numberDiff line numberDiff line change
@@ -1641,27 +1641,10 @@ impl<T: ?Sized> *mut T {
16411641
// Inform Miri that we want to consider the resulting pointer to be suitably aligned.
16421642
#[cfg(miri)]
16431643
if ret != usize::MAX {
1644-
fn runtime(ptr: *const (), align: usize) {
1645-
extern "Rust" {
1646-
pub fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
1647-
}
1648-
1649-
// SAFETY: this call is always safe.
1650-
unsafe {
1651-
miri_promise_symbolic_alignment(ptr, align);
1652-
}
1653-
}
1654-
1655-
const fn compiletime(_ptr: *const (), _align: usize) {}
1656-
1657-
// SAFETY: the extra behavior at runtime is for UB checks only.
1658-
unsafe {
1659-
intrinsics::const_eval_select(
1660-
(self.wrapping_add(ret).cast_const().cast(), align),
1661-
compiletime,
1662-
runtime,
1663-
);
1664-
}
1644+
intrinsics::miri_promise_symbolic_alignment(
1645+
self.wrapping_add(ret).cast_const().cast(),
1646+
align,
1647+
);
16651648
}
16661649

16671650
ret

library/core/src/slice/mod.rs

+8-23
Original file line numberDiff line numberDiff line change
@@ -3870,16 +3870,10 @@ impl<T> [T] {
38703870
let (us_len, ts_len) = rest.align_to_offsets::<U>();
38713871
// Inform Miri that we want to consider the "middle" pointer to be suitably aligned.
38723872
#[cfg(miri)]
3873-
{
3874-
extern "Rust" {
3875-
pub fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
3876-
}
3877-
3878-
// SAFETY: this call is always safe.
3879-
unsafe {
3880-
miri_promise_symbolic_alignment(rest.as_ptr().cast(), mem::align_of::<U>());
3881-
}
3882-
}
3873+
crate::intrinsics::miri_promise_symbolic_alignment(
3874+
rest.as_ptr().cast(),
3875+
mem::align_of::<U>(),
3876+
);
38833877
// SAFETY: now `rest` is definitely aligned, so `from_raw_parts` below is okay,
38843878
// since the caller guarantees that we can transmute `T` to `U` safely.
38853879
unsafe {
@@ -3952,19 +3946,10 @@ impl<T> [T] {
39523946
let mut_ptr = rest.as_mut_ptr();
39533947
// Inform Miri that we want to consider the "middle" pointer to be suitably aligned.
39543948
#[cfg(miri)]
3955-
{
3956-
extern "Rust" {
3957-
pub fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
3958-
}
3959-
3960-
// SAFETY: this call is always safe.
3961-
unsafe {
3962-
miri_promise_symbolic_alignment(
3963-
mut_ptr.cast() as *const (),
3964-
mem::align_of::<U>(),
3965-
);
3966-
}
3967-
}
3949+
crate::intrinsics::miri_promise_symbolic_alignment(
3950+
mut_ptr.cast() as *const (),
3951+
mem::align_of::<U>(),
3952+
);
39683953
// We can't use `rest` again after this, that would invalidate its alias `mut_ptr`!
39693954
// SAFETY: see comments for `align_to`.
39703955
unsafe {

0 commit comments

Comments
 (0)