Lokathor/bytemuck

cast_slice on a memory-mapped file fails on Windows but succeeds on Linux

sebcrozet opened this issue · 4 comments

I have this test which creates an empty temporary file, memory-maps it, and cast it to a &[f64]:

#[test]
fn empty_mmap_cast_slice() {
    let f = tempfile::tempfile().unwrap();
    let mmap = unsafe { memmap2::Mmap::map(&f) }.unwrap();
    let s: &[f64] = cast_slice(&mmap);
    assert!(s.is_empty());
}

While it works fine on Linux, it fails on Windows with the following error:

thread 'array_mmap::empty_mmap_cast_slice' panicked at 'cast_slice>TargetAlignmentGreaterAndInputNotAligned', C:\Users\devel\.cargo\registry\src\github.com-1ecc6299db9ec823\bytemuck-1.12.3\src\internal.rs:32:3

Is that the expected behavior?

Well it's expected if the buffer isn't aligned for an f64.

I've got no idea what the minimum alignment of mmap is on Windows vs Linux, but perhaps Linux guarantees a higher alignment minimum.

We could try looking at the raw pointer that we get from the mmap and see if it has the expected alignment.

yeah, and try_cast_slice is available if there's any risk of insufficient alignment.

It looks like mmap on an empty file is giving an address equal to 0x01:

Mmap { ptr: 0x1, len: 0 }

Let’s close this then, that’s not an issue in bytemuck. Thanks!