-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Signature of enter
is not sound
#7
Comments
Thank you, I opened #9 to apply the suggested
I can't remember off the top of my head why I added the restriction. I spent a lot of time fiddling the signature of I'll keep the suggestion on my TODO as #10, to reevaluate someday where my head is fresher. |
Possibly the thought process was that AFAICT, the only thing that Also… I’m just realizing that even with Footnotes
|
Sure, that's okay ^^ |
Neat, another self-referencing structs crate I hadn’t known of before (but now I do because of your recent-ish blog post). 🎉
You might have seen me before on the issue trackers of many other self-referencing structs crates.
Anyways, here’s the issue: The signature of
enter
sayswhich features a
&'a mut Foo<'a>
-style type, an infamous anti-pattern in Rust generally. These types are almost impossible to produce in safe code since they promise thatFoo<'a>
is borrowed mutably for its entire existence, essentially. So unsafe code producing them results in unsoundness really easily, more on that below.The full signature in question is
If you want to follow the precedent of ouroboros or self_cell, e.g. documented here:1
then your correct signature must be the following instead:
also,
Output: 'borrow
is unnecessarily restrictive, as far as I can tell, so you could considerSo how is this unsound? Two ways, really. For one, producing
&'a mut Foo<'a>
at all is impossible to do soundly ifFoo
is a custom type that does implementDrop
. On the other hand, for types without drop glue, producing more than one&'a mut Foo<'a>
reference to the same target ever is also unsound. (This explains theDrop
situation, too, asDrop
receives&mut Self
.)Exploiting this is as easy as storing the
&'a mut Foo<'a>
(or some projection of it) inside ofFoo<'a>
itself:example output
miri output
As far as I can tell, this soundness issue is present in all existing versions of
nolife
.Footnotes
for comparison, yoke has a bit of a weird, different signature, that also appeared sound last time I looked at it, but I should consider opening an issue there, too, because the
ouroboros
/self_cell
one is just a lot better (less restrictive) for the user. ↩The text was updated successfully, but these errors were encountered: