From 40b05b211684461de7dcd3cc6a0cd503e85cd814 Mon Sep 17 00:00:00 2001 From: Giacomo Stevanato Date: Thu, 19 Sep 2024 23:41:19 +0200 Subject: [PATCH] Remove int2ptr cast in `bevy_ptr::dangling_with_align` and remove `-Zmiri-permissive-provenance` in CI (#15311) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective - Remove an int2ptr cast in `bevy_ptr::dangling_with_align` - This is flagged by MIRI unless `-Zmiri-permissive-provenance` is used (like in CI) - Remove `-Zmiri-permissive-provenance` in CI ## Solution - Create the raw pointer like [`std::ptr::without_provenance`](https://doc.rust-lang.org/stable/std/ptr/fn.without_provenance_mut.html) does, i.e. by starting from a null pointer. --------- Co-authored-by: Alice Cecile Co-authored-by: François Mockers --- .github/workflows/ci.yml | 5 ++--- crates/bevy_ptr/src/lib.rs | 5 +++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 952877b9f1..55729a507f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -92,16 +92,15 @@ jobs: components: miri - name: CI job # To run the tests one item at a time for troubleshooting, use - # cargo --quiet test --lib -- --list | sed 's/: test$//' | MIRIFLAGS="-Zmiri-disable-isolation -Zmiri-permissive-provenance -Zmiri-disable-weak-memory-emulation" xargs -n1 cargo miri test -p bevy_ecs --lib -- --exact + # cargo --quiet test --lib -- --list | sed 's/: test$//' | MIRIFLAGS="-Zmiri-disable-isolation -Zmiri-disable-weak-memory-emulation" xargs -n1 cargo miri test -p bevy_ecs --lib -- --exact run: cargo miri test -p bevy_ecs env: # -Zrandomize-layout makes sure we dont rely on the layout of anything that might change RUSTFLAGS: -Zrandomize-layout # https://github.com/rust-lang/miri#miri--z-flags-and-environment-variables # -Zmiri-disable-isolation is needed because our executor uses `fastrand` which accesses system time. - # -Zmiri-permissive-provenance disables warnings against int2ptr casts (since those are used by once_cell) # -Zmiri-ignore-leaks is necessary because a bunch of tests don't join all threads before finishing. - MIRIFLAGS: -Zmiri-ignore-leaks -Zmiri-disable-isolation -Zmiri-permissive-provenance + MIRIFLAGS: -Zmiri-ignore-leaks -Zmiri-disable-isolation check-compiles: runs-on: ubuntu-latest diff --git a/crates/bevy_ptr/src/lib.rs b/crates/bevy_ptr/src/lib.rs index c9511bae16..bcfd060f01 100644 --- a/crates/bevy_ptr/src/lib.rs +++ b/crates/bevy_ptr/src/lib.rs @@ -13,7 +13,7 @@ use core::{ marker::PhantomData, mem::ManuallyDrop, num::NonZeroUsize, - ptr::NonNull, + ptr::{self, NonNull}, }; /// Used as a type argument to [`Ptr`], [`PtrMut`] and [`OwningPtr`] to specify that the pointer is aligned. @@ -539,7 +539,8 @@ pub const fn dangling_with_align(align: NonZeroUsize) -> NonNull { debug_assert!(align.is_power_of_two(), "Alignment must be power of two."); // SAFETY: The pointer will not be null, since it was created // from the address of a `NonZero`. - unsafe { NonNull::new_unchecked(align.get() as *mut u8) } + // TODO: use https://doc.rust-lang.org/std/ptr/struct.NonNull.html#method.with_addr once stabilised + unsafe { NonNull::new_unchecked(ptr::null_mut::().wrapping_add(align.get())) } } mod private {