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

Enable running arrow-array and arrow-arith with miri and avoid strict provenance warning #5387

Merged
merged 4 commits into from
Feb 18, 2024
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
5 changes: 2 additions & 3 deletions .github/workflows/miri.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ cargo miri test -p arrow-buffer
cargo miri test -p arrow-data --features ffi
cargo miri test -p arrow-schema --features ffi
cargo miri test -p arrow-ord
# inline assembly not supported by Miri
# cargo miri test -p arrow-array
# cargo miri test -p arrow-arith
cargo miri test -p arrow-array
cargo miri test -p arrow-arith
4 changes: 2 additions & 2 deletions arrow-buffer/src/bigint/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn div_rem_word(hi: u64, lo: u64, divisor: u64) -> (u64, u64) {

// LLVM fails to use the div instruction as it is not able to prove
// that hi < divisor, and therefore the result will fit into 64-bits
#[cfg(target_arch = "x86_64")]
#[cfg(all(target_arch = "x86_64", not(miri)))]
unsafe {
let mut quot = lo;
let mut rem = hi;
Expand All @@ -202,7 +202,7 @@ fn div_rem_word(hi: u64, lo: u64, divisor: u64) -> (u64, u64) {
);
(quot, rem)
}
#[cfg(not(target_arch = "x86_64"))]
#[cfg(any(not(target_arch = "x86_64"), miri))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah nice 👍

Didn't realize it was this simple change to get Miri working 👀

{
let x = (u128::from(hi) << 64) + u128::from(lo);
let y = u128::from(divisor);
Expand Down
10 changes: 9 additions & 1 deletion arrow-buffer/src/buffer/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,15 @@ fn dangling_ptr() -> NonNull<u8> {
// SAFETY: ALIGNMENT is a non-zero usize which is then casted
// to a *mut T. Therefore, `ptr` is not null and the conditions for
// calling new_unchecked() are respected.
unsafe { NonNull::new_unchecked(ALIGNMENT as *mut u8) }
#[cfg(miri)]
{
// Since miri implies a nightly rust version we can use the unstable strict_provenance feature
unsafe { NonNull::new_unchecked(std::ptr::invalid_mut(ALIGNMENT)) }
}
#[cfg(not(miri))]
{
unsafe { NonNull::new_unchecked(ALIGNMENT as *mut u8) }
}
Comment on lines +483 to +491
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not as familiar with what's happening here, but I guess as long as it's gated so only affects Miri, should be alright?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invalid_mut is basically doing the same thing as the cast below, but does not trigger a warning about integer to pointer casts. But it is behind a feature flag and only available on nightly rust.

If you want to go down the rabbit hole of what is pointer provenance, this would be a good point to start: https://github.com/RalfJung/rfcs/blob/provenance/text/0000-rust-has-provenance.md

}

impl<A: ArrowNativeType> Extend<A> for MutableBuffer {
Expand Down
3 changes: 3 additions & 0 deletions arrow-buffer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

//! Low-level buffer abstractions for [Apache Arrow Rust](https://docs.rs/arrow)

// used by [`buffer::mutable::dangling_ptr`]
#![cfg_attr(miri, feature(strict_provenance))]

pub mod alloc;
pub mod buffer;
pub use buffer::*;
Expand Down
Loading