Skip to content

Commit c823ea9

Browse files
committed
move calling miri_promise_symbolic_alignment to a shared helper
1 parent db8d524 commit c823ea9

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
@@ -2849,3 +2849,28 @@ pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
28492849
write_bytes(dst, val, count)
28502850
}
28512851
}
2852+
2853+
/// Inform Miri that a given pointer definitely has a certain alignment.
2854+
#[cfg(miri)]
2855+
pub(crate) const fn miri_promise_symbolic_alignment(ptr: *const (), align: usize) {
2856+
extern "Rust" {
2857+
/// Miri-provided extern function to promise that a given pointer is properly aligned for
2858+
/// "symbolic" alignment checks. Will fail if the pointer is not actually aligned or `align` is
2859+
/// not a power of two. Has no effect when alignment checks are concrete (which is the default).
2860+
fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
2861+
}
2862+
2863+
fn runtime(ptr: *const (), align: usize) {
2864+
// SAFETY: this call is always safe.
2865+
unsafe {
2866+
miri_promise_symbolic_alignment(ptr, align);
2867+
}
2868+
}
2869+
2870+
const fn compiletime(_ptr: *const (), _align: usize) {}
2871+
2872+
// SAFETY: the extra behavior at runtime is for UB checks only.
2873+
unsafe {
2874+
const_eval_select((ptr, align), compiletime, runtime);
2875+
}
2876+
}

library/core/src/ptr/const_ptr.rs

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

13991379
ret

library/core/src/ptr/mut_ptr.rs

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

16661649
ret

library/core/src/slice/mod.rs

+8-23
Original file line numberDiff line numberDiff line change
@@ -3878,16 +3878,10 @@ impl<T> [T] {
38783878
let (us_len, ts_len) = rest.align_to_offsets::<U>();
38793879
// Inform Miri that we want to consider the "middle" pointer to be suitably aligned.
38803880
#[cfg(miri)]
3881-
{
3882-
extern "Rust" {
3883-
pub fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
3884-
}
3885-
3886-
// SAFETY: this call is always safe.
3887-
unsafe {
3888-
miri_promise_symbolic_alignment(rest.as_ptr().cast(), mem::align_of::<U>());
3889-
}
3890-
}
3881+
crate::intrinsics::miri_promise_symbolic_alignment(
3882+
rest.as_ptr().cast(),
3883+
mem::align_of::<U>(),
3884+
);
38913885
// SAFETY: now `rest` is definitely aligned, so `from_raw_parts` below is okay,
38923886
// since the caller guarantees that we can transmute `T` to `U` safely.
38933887
unsafe {
@@ -3960,19 +3954,10 @@ impl<T> [T] {
39603954
let mut_ptr = rest.as_mut_ptr();
39613955
// Inform Miri that we want to consider the "middle" pointer to be suitably aligned.
39623956
#[cfg(miri)]
3963-
{
3964-
extern "Rust" {
3965-
pub fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
3966-
}
3967-
3968-
// SAFETY: this call is always safe.
3969-
unsafe {
3970-
miri_promise_symbolic_alignment(
3971-
mut_ptr.cast() as *const (),
3972-
mem::align_of::<U>(),
3973-
);
3974-
}
3975-
}
3957+
crate::intrinsics::miri_promise_symbolic_alignment(
3958+
mut_ptr.cast() as *const (),
3959+
mem::align_of::<U>(),
3960+
);
39763961
// We can't use `rest` again after this, that would invalidate its alias `mut_ptr`!
39773962
// SAFETY: see comments for `align_to`.
39783963
unsafe {

0 commit comments

Comments
 (0)