Skip to content

Potential unsoundness in DrainProducer around drop_in_place on mutable reference #1029

Closed
@Manishearth

Description

@Manishearth

rayon/src/vec.rs

Lines 226 to 231 in 7069695

impl<'data, T: 'data + Send> Drop for DrainProducer<'data, T> {
fn drop(&mut self) {
// use `Drop for [T]`
unsafe { ptr::drop_in_place(self.slice) };
}
}

This code takes &mut Self, which itself contains an &mut [T], and runs drop glue for the elements in the slice.

While ptr::drop_in_place() does not explicitly say that the resultant value is invalid, it says it is "invalid to read". My understanding of the Rust unsafe model is that it is UB to have a live reference to a type that is invalid to read, in this case we are violating the validity constraints of &mut [T], which in turn violates validity of &mut Self.

The type does come from ManuallyDrop, but it's still UB to invalidate that whilst it has live references to it.

I think this should probably be a raw pointer + phantomdata?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions