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

Span's From<&[T]> and From<&[T; N]> facilitate UB #33

Open
SFBdragon opened this issue May 5, 2024 · 0 comments
Open

Span's From<&[T]> and From<&[T; N]> facilitate UB #33

SFBdragon opened this issue May 5, 2024 · 0 comments

Comments

@SFBdragon
Copy link
Owner

SFBdragon commented May 5, 2024

Dereferencing &x as *const _ as *mut _ is UB.

This means some of the API surface of Talc is genuinely problematic, particularly the From implementations on Span for &[T] and &[T; N]. Not UB in itself (and technically Talc's docs on unsafety cover this), but it makes UB needlessly easy to invoke.

Here, the .into() is doing the legwork in hiding the issue as per the above. MIRI indeed takes issue with it.

fn main() {
    let mut arena = [0u8; 10000];

    unsafe {
        let mut talc = talc::Talc::new(talc::ClaimOnOom::new(arena.as_ref().into()));
        let x = talc.malloc(std::alloc::Layout::new::<[u64; 3]>()).unwrap();
        *x.as_ptr().cast::<[u64; 3]>() = [1, 2, 3];
    }
}

These From implementations should be removed. This is a breaking change though and will necessitate a major version bump.

@SFBdragon SFBdragon changed the title Span's From<&[T]> and From<&[T; N]> encourage UB Span's From<&[T]> and From<&[T; N]> facilitate UB May 5, 2024
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

No branches or pull requests

1 participant