Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 15.5k
Array and Vec's Clone specialization is maybe unsound with conditionally Copy types. #132442
Copy link
Copy link
Closed
Labels
A-specializationArea: Trait impl specializationArea: Trait impl specializationC-bugCategory: This is a bug.Category: This is a bug.E-needs-investigationCall for participation: This issues needs some investigation to determine current statusCall for participation: This issues needs some investigation to determine current statusI-lang-radarItems that are on lang's radar and will need eventual work or consideration.Items that are on lang's radar and will need eventual work or consideration.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-mediumMedium priorityMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Description
Metadata
Metadata
Assignees
Labels
A-specializationArea: Trait impl specializationArea: Trait impl specializationC-bugCategory: This is a bug.Category: This is a bug.E-needs-investigationCall for participation: This issues needs some investigation to determine current statusCall for participation: This issues needs some investigation to determine current statusI-lang-radarItems that are on lang's radar and will need eventual work or consideration.Items that are on lang's radar and will need eventual work or consideration.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-mediumMedium priorityMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Currently, the
Cloneimpl for[T; N]uses specialization. IfTimplementsCopy, then cloning a[T; N]will do a memcpy (ignoringT'sCloneimpl). IfTdoesn't implementCopy, then it will iterate and callT'sCloneimpl.However, specialization doesn't look at lifetimes. Therefore, even if
TimplementsCopyfor only some lifetimes, cloning a[T; N]will do a memcopy. This is incorrect in the case whereTactually doesn't implementCopy. For example:In the above code,
Weirdonly implementsCopywhen its lifetime is'static. Therefore, I think that cloning a[Weird<'a>; 1]should call theclone()method. However, running the above code in either stable (1.82.0) or nightly (1.84.0-nightly (2024-10-30 759e07f063fb8e6306ff)) rust doesn't print anything. I believe that this is incorrect.I am unsure whether or not this can cause UB in purely safe code, (hence the "maybe" in the issue title). But maybe someone else could figure out how to do that?
However, I have written this code, which uses unsafe code to implement a
WeirdCowtype whose public API I believe is sound on its own, but can be used in combination with array's Clone specialization to cause use-after-free. Either thisWeirdCowtype is unsound, or the array's Clone implementation is unsound, and I can't figure out which.@rustbot label +I-unsound