Lokathor / bytemuck

A crate for mucking around with piles of bytes
https://docs.rs/bytemuck
Apache License 2.0
697 stars 77 forks source link

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

Closed sebcrozet closed 1 year ago

sebcrozet commented 1 year ago

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?

Lokathor commented 1 year ago

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.

bonsairobo commented 1 year ago

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

Lokathor commented 1 year ago

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

sebcrozet commented 1 year ago

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!