diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 74676ff..f1b2a9c 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -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 diff --git a/src/lib.rs b/src/lib.rs index ff4f40e..d7429c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -383,7 +383,15 @@ #[must_use] pub const fn invalid(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. @@ -408,7 +416,15 @@ pub const fn invalid(addr: usize) -> *const T { #[must_use] pub const fn invalid_mut(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.