Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upInvestigate possible unsafety of `OwningHandle` #27
Comments
This comment has been minimized.
This comment has been minimized.
|
My understanding is that the issue comes from the fake lifetime parameters of the handle. OwningHandle hides the handle value itself, but depending on the implementation of the handle, that is insufficient to prevent the fake lifetime from leaking into safe code. Unfortunately, there is no way to check this in generic code. On the other hand, there is technically no unsoundness, because the only way to get a fake lifetime parameter in the first place is to perform a pointer deref in the callback passed to OwningHandle::new_with_fn, which requires unsafe code. On the other hand, that's pretty much the intended use case, so realistically, every caller will do so. I think the best option is to add more documentation explaining about how the API is dangerous and the caller is responsible for ensuring that fake lifetime parameters don't leak or aren't used unsafely. The other place to watch out is ToHandle/ToHandleMut. Currently, the only implementation of ToHandle provided is for RefCell, and it does introduce a fake lifetime. However, my understanding is that the The other issue is that ToHandle is a safe, public trait. However, in order for users to implement it, they would have to perform a pointer deref, which requires unsafe code, so there is technically no unsoundness. Still, I think it might be a good idea to make ToHandle and ToHandleMut into unsafe traits so that people realize that they have to be extremely careful when implementing these traits. |
This comment has been minimized.
This comment has been minimized.
jpernst
commented
Mar 10, 2017
•
|
As @Storyyeller points out, the unsafety here is a result of the false lifetime being exposed to the outside world. The After making that comment, I took some time to ponder this and if it was even possible to make this kind of thing safe at all. Rental was the result of that experimentation, but as can be seen from the code, it involves an absurdly arcane macro and subtle HRTB trickery to achieve it. To boil it down, the only way to make it safe is to only allow access to the borrowed value within a closure bounded by: I plan to redesign it as new enabling features land, but for now I can't envision a better way to expose an API like this. If you concede to allowing possible unsafety, though, the situation becomes much more tractable. One way would be to clearly mandate that the false lifetime must NOT appear in the type signature of the |
This comment has been minimized.
This comment has been minimized.
mitsuhiko
commented
Sep 13, 2017
|
There are some curious cases you can run into because of those faked lifetimes. In particular it permits you to crate code that looks okay but only becomes visible unsafe when you do things somewhere completely different. In my case I have something that gives access to some mmap'ed or borrowed data. The mmapp'ed version is I'm not sure what lesson is there to learn but right now I don't have much other options other than using |
Kimundi commentedMar 5, 2017
See #15 (comment)