|
| 1 | +//@ build-pass |
| 2 | +//@ edition: 2024 |
| 3 | +#![feature(const_raw_ptr_comparison)] |
| 4 | +#![feature(fn_align)] |
| 5 | +// Generally: |
| 6 | +// For any `Some` return, `None` would also be valid, unless otherwise noted. |
| 7 | +// For any `None` return, only `None` is valid, unless otherwise noted. |
| 8 | + |
| 9 | +macro_rules! do_test { |
| 10 | + ($a:expr, $b:expr, $expected:pat) => { |
| 11 | + const _: () = { |
| 12 | + let a: *const _ = $a; |
| 13 | + let b: *const _ = $b; |
| 14 | + assert!(matches!(<*const u8>::guaranteed_eq(a.cast(), b.cast()), $expected)); |
| 15 | + }; |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +#[repr(align(2))] |
| 20 | +struct T(#[allow(unused)] u16); |
| 21 | + |
| 22 | +#[repr(align(2))] |
| 23 | +struct AlignedZst; |
| 24 | + |
| 25 | +static A: T = T(42); |
| 26 | +static B: T = T(42); |
| 27 | +static mut MUT_STATIC: T = T(42); |
| 28 | +static ZST: () = (); |
| 29 | +static ALIGNED_ZST: AlignedZst = AlignedZst; |
| 30 | +static LARGE_WORD_ALIGNED: [usize; 2] = [0, 1]; |
| 31 | +static mut MUT_LARGE_WORD_ALIGNED: [usize; 2] = [0, 1]; |
| 32 | + |
| 33 | +const FN_PTR: *const () = { |
| 34 | + fn foo() {} |
| 35 | + unsafe { std::mem::transmute(foo as fn()) } |
| 36 | +}; |
| 37 | + |
| 38 | +const ALIGNED_FN_PTR: *const () = { |
| 39 | + #[rustc_align(2)] |
| 40 | + fn aligned_foo() {} |
| 41 | + unsafe { std::mem::transmute(aligned_foo as fn()) } |
| 42 | +}; |
| 43 | + |
| 44 | +// Only on armv5te-* and armv4t-* |
| 45 | +#[cfg(all( |
| 46 | + target_arch = "arm", |
| 47 | + not(target_feature = "v6"), |
| 48 | +))] |
| 49 | +const ALIGNED_THUMB_FN_PTR: *const () = { |
| 50 | + #[rustc_align(2)] |
| 51 | + #[instruction_set(arm::t32)] |
| 52 | + fn aligned_thumb_foo() {} |
| 53 | + unsafe { std::mem::transmute(aligned_thumb_foo as fn()) } |
| 54 | +}; |
| 55 | + |
| 56 | +trait Trait { |
| 57 | + #[allow(unused)] |
| 58 | + fn method(&self) -> u8; |
| 59 | +} |
| 60 | +impl Trait for u32 { |
| 61 | + fn method(&self) -> u8 { 1 } |
| 62 | +} |
| 63 | +impl Trait for i32 { |
| 64 | + fn method(&self) -> u8 { 2 } |
| 65 | +} |
| 66 | + |
| 67 | +const VTABLE_PTR_1: *const () = { |
| 68 | + let [_data, vtable] = unsafe { |
| 69 | + std::mem::transmute::<&dyn Trait, [*const (); 2]>(&42_u32 as &dyn Trait) |
| 70 | + }; |
| 71 | + vtable |
| 72 | +}; |
| 73 | +const VTABLE_PTR_2: *const () = { |
| 74 | + let [_data, vtable] = unsafe { |
| 75 | + std::mem::transmute::<&dyn Trait, [*const (); 2]>(&42_i32 as &dyn Trait) |
| 76 | + }; |
| 77 | + vtable |
| 78 | +}; |
| 79 | + |
| 80 | +// Cannot be `None`: static's address, references, and `fn` pointers cannot be null, |
| 81 | +// and `is_null` is stable with strong guarantees, and `is_null` is implemented using |
| 82 | +// `guaranteed_cmp`. |
| 83 | +do_test!(&A, std::ptr::null::<()>(), Some(false)); |
| 84 | +do_test!(&ZST, std::ptr::null::<()>(), Some(false)); |
| 85 | +do_test!(&(), std::ptr::null::<()>(), Some(false)); |
| 86 | +do_test!(const { &() }, std::ptr::null::<()>(), Some(false)); |
| 87 | +do_test!(FN_PTR, std::ptr::null::<()>(), Some(false)); |
| 88 | + |
| 89 | +// Statics cannot be duplicated |
| 90 | +do_test!(&A, &A, Some(true)); |
| 91 | + |
| 92 | +// Two non-ZST statics cannot have the same address |
| 93 | +do_test!(&A, &B, Some(false)); |
| 94 | +do_test!(&A, &raw const MUT_STATIC, Some(false)); |
| 95 | + |
| 96 | +// One-past-the-end of one static can be equal to the address of another static. |
| 97 | +do_test!(&A, (&raw const B).wrapping_add(1), None); |
| 98 | + |
| 99 | +// Cannot know if ZST static is at the same address with anything non-null (if alignment allows). |
| 100 | +do_test!(&A, &ZST, None); |
| 101 | +do_test!(&A, &ALIGNED_ZST, None); |
| 102 | + |
| 103 | +// Unclear if ZST statics can be placed "in the middle of" non-ZST statics. |
| 104 | +// For now, we conservatively say they could, and return None here. |
| 105 | +do_test!(&ZST, (&raw const A).wrapping_byte_add(1), None); |
| 106 | + |
| 107 | +// As per https://doc.rust-lang.org/nightly/reference/items/static-items.html#r-items.static.storage-disjointness |
| 108 | +// immutable statics are allowed to overlap with const items and promoteds. |
| 109 | +do_test!(&A, &T(42), None); |
| 110 | +do_test!(&A, const { &T(42) }, None); |
| 111 | +do_test!(&A, { const X: T = T(42); &X }, None); |
| 112 | + |
| 113 | +// These could return Some(false), since only immutable statics can overlap with const items |
| 114 | +// and promoteds. |
| 115 | +do_test!(&raw const MUT_STATIC, &T(42), None); |
| 116 | +do_test!(&raw const MUT_STATIC, const { &T(42) }, None); |
| 117 | +do_test!(&raw const MUT_STATIC, { const X: T = T(42); &X }, None); |
| 118 | + |
| 119 | +// An odd offset from a 2-aligned allocation can never be equal to an even offset from a |
| 120 | +// 2-aligned allocation, even if the offsets are out-of-bounds. |
| 121 | +do_test!(&A, (&raw const B).wrapping_byte_add(1), Some(false)); |
| 122 | +do_test!(&A, (&raw const B).wrapping_byte_add(5), Some(false)); |
| 123 | +do_test!(&A, (&raw const ALIGNED_ZST).wrapping_byte_add(1), Some(false)); |
| 124 | +do_test!(&ALIGNED_ZST, (&raw const A).wrapping_byte_add(1), Some(false)); |
| 125 | +do_test!(&A, (&T(42) as *const T).wrapping_byte_add(1), Some(false)); |
| 126 | +do_test!(&A, (const { &T(42) } as *const T).wrapping_byte_add(1), Some(false)); |
| 127 | +do_test!(&A, ({ const X: T = T(42); &X } as *const T).wrapping_byte_add(1), Some(false)); |
| 128 | + |
| 129 | +// Pointers into the same static are equal if and only if their offset is the same, |
| 130 | +// even if either is out-of-bounds. |
| 131 | +do_test!(&A, &A, Some(true)); |
| 132 | +do_test!(&A, &A.0, Some(true)); |
| 133 | +do_test!(&A, (&raw const A).wrapping_byte_add(1), Some(false)); |
| 134 | +do_test!(&A, (&raw const A).wrapping_byte_add(2), Some(false)); |
| 135 | +do_test!(&A, (&raw const A).wrapping_byte_add(51), Some(false)); |
| 136 | +do_test!((&raw const A).wrapping_byte_add(51), (&raw const A).wrapping_byte_add(51), Some(true)); |
| 137 | + |
| 138 | +// Pointers to the same fn may be unequal, since `fn`s can be duplicated. |
| 139 | +do_test!(FN_PTR, FN_PTR, None); |
| 140 | +do_test!(ALIGNED_FN_PTR, ALIGNED_FN_PTR, None); |
| 141 | + |
| 142 | +// Pointers to different fns may be equal, since `fn`s can be deduplicated. |
| 143 | +do_test!(FN_PTR, ALIGNED_FN_PTR, None); |
| 144 | + |
| 145 | +// Pointers to the same vtable may be unequal, since vtables can be duplicated. |
| 146 | +do_test!(VTABLE_PTR_1, VTABLE_PTR_1, None); |
| 147 | + |
| 148 | +// Pointers to different vtables may be equal, since vtables can be deduplicated. |
| 149 | +do_test!(VTABLE_PTR_1, VTABLE_PTR_2, None); |
| 150 | + |
| 151 | +// Function pointers to aligned function allocations are not necessarily actually aligned, |
| 152 | +// due to platform-specific semantics. |
| 153 | +// See https://github.com/rust-lang/rust/issues/144661 |
| 154 | +// FIXME: This could return `Some` on platforms where function pointers' addresses actually |
| 155 | +// correspond to function addresses including alignment, or on ARM if t32 function pointers |
| 156 | +// have their low bit set for consteval. |
| 157 | +do_test!(ALIGNED_FN_PTR, ALIGNED_FN_PTR.wrapping_byte_offset(1), None); |
| 158 | +#[cfg(all( |
| 159 | + target_arch = "arm", |
| 160 | + not(target_feature = "v6"), |
| 161 | +))] |
| 162 | +do_test!(ALIGNED_THUMB_FN_PTR, ALIGNED_THUMB_FN_PTR.wrapping_byte_offset(1), None); |
| 163 | + |
| 164 | +// Conservatively say we don't know. |
| 165 | +do_test!(FN_PTR, VTABLE_PTR_1, None); |
| 166 | +do_test!((&raw const LARGE_WORD_ALIGNED).cast::<usize>().wrapping_add(1), VTABLE_PTR_1, None); |
| 167 | +do_test!((&raw const MUT_LARGE_WORD_ALIGNED).cast::<usize>().wrapping_add(1), VTABLE_PTR_1, None); |
| 168 | +do_test!((&raw const LARGE_WORD_ALIGNED).cast::<usize>().wrapping_add(1), FN_PTR, None); |
| 169 | +do_test!((&raw const MUT_LARGE_WORD_ALIGNED).cast::<usize>().wrapping_add(1), FN_PTR, None); |
| 170 | + |
| 171 | +fn main() {} |
0 commit comments