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

misaligned pointer dereference #53

Closed
oherrala opened this issue Jun 2, 2023 · 3 comments · Fixed by #54
Closed

misaligned pointer dereference #53

oherrala opened this issue Jun 2, 2023 · 3 comments · Fixed by #54

Comments

@oherrala
Copy link

oherrala commented Jun 2, 2023

I'm seeing this in our GitHub Actions failing. Tests worked with Rust 1.69.0 using stable-i686-pc-windows-msvc toolchain, but seems to fail with Rust 1.70.0 with same toolchain.

thread '<redacted>' panicked at 'panic in a function that cannot unwind', library\core\src\panicking.rs:126:5
thread '<redacted>' panicked at 'misaligned pointer dereference: address must be a multiple of 0x8 but is 0x189f604', C:\Users\runneradmin\.cargo\registry\src\index.crates.io-1cd66030c949c28d\ipconfig-0.3.1\src\adapter.rs:293:23

Affected function:

ipconfig/src/adapter.rs

Lines 289 to 301 in f6d918f

unsafe fn get_gateways(mut gateway_ptr: PIP_ADAPTER_GATEWAY_ADDRESS_LH) -> Result<Vec<IpAddr>> {
let mut gateways = vec![];
while !gateway_ptr.is_null() {
let gateway = &*gateway_ptr;
let ipaddr = socket_address_to_ipaddr(&gateway.Address);
gateways.push(ipaddr);
gateway_ptr = gateway.Next;
}
Ok(gateways)
}

@oherrala
Copy link
Author

oherrala commented Jun 2, 2023

Rust 1.70.0 changelog has this line which might be relevant:

@saethlin
Copy link

saethlin commented Jun 2, 2023

Ouch, yeah we don't test Windows targets in crater and I don't test them personally.

It looks to me like Windows is returning a pointer that is not sufficiently aligned. The standard library has used addr_of! to navigate such issues before, perhaps that would be viable fix here? https://github.com/rust-lang/rust/blob/f91b634643de14ab5156b0c084d80bd6845fb0ae/library/std/src/sys/windows/fs.rs#L766-L778

        let (name, is_directory, next_entry) = unsafe {
            let info = buffer.as_ptr().cast::<c::FILE_ID_BOTH_DIR_INFO>();
            // While this is guaranteed to be aligned in documentation for
            // https://docs.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-file_id_both_dir_info
            // it does not seem that reality is so kind, and assuming this
            // caused crashes in some cases (https://github.com/rust-lang/rust/issues/104530)
            // presumably, this can be blamed on buggy filesystem drivers, but who knows.
            let next_entry = ptr::addr_of!((*info).NextEntryOffset).read_unaligned() as usize;
            let length = ptr::addr_of!((*info).FileNameLength).read_unaligned() as usize;
            let attrs = ptr::addr_of!((*info).FileAttributes).read_unaligned();
            let name = from_maybe_unaligned(
                ptr::addr_of!((*info).FileName).cast::<u16>(),
                length / size_of::<u16>(),
            );

@liranringel
Copy link
Owner

Thanks for the report!
I fixed it, and I will publish a new version soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants