Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use transmute for ptr::invalid #8

Merged
merged 3 commits into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ jobs:
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose

miri:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Miri
run: |
rustup toolchain install nightly --component miri
rustup override set nightly
cargo miri setup
- name: Test with Miri
run: cargo miri test
20 changes: 18 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,15 @@
#[must_use]
pub const fn invalid<T>(addr: usize) -> *const T {
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
addr as *const T
// 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).
#[cfg(miri)]
return unsafe { core::mem::transmute(addr) };
// Outside Miri we keep using casts, so that we can be a `const fn` on old Rust (pre-1.56).
#[cfg(not(miri))]
return addr as *const T;
}

/// Creates an invalid mutable pointer with the given address.
Expand All @@ -408,7 +416,15 @@ pub const fn invalid<T>(addr: usize) -> *const T {
#[must_use]
pub const fn invalid_mut<T>(addr: usize) -> *mut T {
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
addr as *mut T
// 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).
#[cfg(miri)]
return unsafe { core::mem::transmute(addr) };
// Outside Miri we keep using casts, so that we can be a `const fn` on old Rust (pre-1.56).
#[cfg(not(miri))]
return addr as *mut T;
}

/// Convert an address back to a pointer, picking up a previously 'exposed' provenance.
Expand Down