-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
BTreeSet causes UB by having (partially) dangling shared reference #54957
Copy link
Copy link
Closed
Labels
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-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
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-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
The following code causes UB (found by a patched miri enforcing validity invariants):
What happens is that
BTreeSet::newsets the root node to a pointer to a staticEMPTY_ROOT_NODEshared for this purpose by all B-trees that has typeLeafNode<(), ()>. That step is okay.However, in the next step,
insertcallsensure_root_is_ownedwhich indirectly turns this ptr into&LeafNode<i32, ()>. The trouble is thatLeafNode<i32, ()>is bigger thanLeafNode<(), ()>, so this reference is partially dangling: Its beginning points to allocated memory (the staticEMPTY_ROOT_NODE), but that allocated memory ends "too early". The invariant that&LeafNode<i32, ()>be dereferencable formem::size_of::<LeafNode<i32, ()>>()bytes is violated. Since we are passingdereferencable(n)to LLVM withnset to the full size of the struct, this means we have UB not just according to our own validity rules, but according to LLVM's rules.