Skip to content

Commit

Permalink
generate getelementptr instead of inttoptr for ptr::invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
joboet committed Feb 17, 2024
1 parent 12b5498 commit 721f991
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
26 changes: 14 additions & 12 deletions library/core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,9 @@ pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
#[rustc_allow_const_fn_unstable(ptr_metadata)]
#[rustc_diagnostic_item = "ptr_null"]
pub const fn null<T: ?Sized + Thin>() -> *const T {
from_raw_parts(invalid(0), ())
// Use transmute instead casting so that miri knows that the pointer is invalid.
// SAFETY: every usize is a valid pointer (on all current platforms).
unsafe { mem::transmute(0usize) }
}

/// Creates a null mutable raw pointer.
Expand All @@ -563,7 +565,9 @@ pub const fn null<T: ?Sized + Thin>() -> *const T {
#[rustc_allow_const_fn_unstable(ptr_metadata)]
#[rustc_diagnostic_item = "ptr_null_mut"]
pub const fn null_mut<T: ?Sized + Thin>() -> *mut T {
from_raw_parts_mut(invalid_mut(0), ())
// Use transmute instead casting so that miri knows that the pointer is invalid.
// SAFETY: every usize is a valid pointer (on all current platforms).
unsafe { mem::transmute(0usize) }
}

/// Creates an invalid pointer with the given address.
Expand All @@ -590,11 +594,10 @@ pub const fn null_mut<T: ?Sized + Thin>() -> *mut T {
#[unstable(feature = "strict_provenance", issue = "95228")]
pub const fn invalid<T>(addr: usize) -> *const T {
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
// We use transmute rather than a cast so tools like Miri can tell that this
// is *not* the same as from_exposed_addr.
// SAFETY: every valid integer is also a valid pointer (as long as you don't dereference that
// pointer).
unsafe { mem::transmute(addr) }
// We offset the null pointer instead of using a cast so that LLVM doesn't
// use inttoptr and so tools like Miri can tell that it is *not* the same
// as from_exposed_addr.
null::<T>().wrapping_byte_add(addr)
}

/// Creates an invalid mutable pointer with the given address.
Expand All @@ -621,11 +624,10 @@ pub const fn invalid<T>(addr: usize) -> *const T {
#[unstable(feature = "strict_provenance", issue = "95228")]
pub const fn invalid_mut<T>(addr: usize) -> *mut T {
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
// We use transmute rather than a cast so tools like Miri can tell that this
// is *not* the same as from_exposed_addr.
// SAFETY: every valid integer is also a valid pointer (as long as you don't dereference that
// pointer).
unsafe { mem::transmute(addr) }
// We offset the null pointer instead of using a cast so that LLVM doesn't
// use inttoptr and so tools like Miri can tell that it is *not* the same
// as from_exposed_addr.
null_mut::<T>().wrapping_byte_add(addr)
}

/// Convert an address back to a pointer, picking up a previously 'exposed' provenance.
Expand Down
15 changes: 15 additions & 0 deletions tests/codegen/strict-provenance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// compile-flags: -O

#![crate_type = "lib"]
#![feature(strict_provenance)]

use std::ptr;

// CHECK-LABEL: @invalid
#[no_mangle]
fn invalid(addr: usize) -> *const () {
// CHECK: start
// CHECK-NEXT: %0 = getelementptr i8, ptr null, i64 %addr
// CHECK-NEXT: ret ptr %0
ptr::invalid(addr)
}

0 comments on commit 721f991

Please sign in to comment.