-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove unsound cast in thread local resources #742
Conversation
I think the problem there is might be |
There would still be the issue of acquiring a mut pointer from a shared reference to a type that doesn't have interior mutability. While I prefer having the issues caught at compile-time with |
It's possible that we need to make above change and also need an UnsafeCell as a higher level. |
Yeah I think disallowing multiple mutable resource borrows of different types significantly decreases the utility of the api / makes writing "thread local systems" very hard. If the implementation isn't sound, lets fix that. Do we have a real example of this api producing unsafe behavior? It seems like our implementation is sound, but miri doesn't like the type of unsafe code we're writing? Is there a better way to write it? |
Even if you never produce aliasing &mut and never produce aliasing & and &mut this code is still unsound. The nomicon explicitly calls out transmuting/pointercasting/etc & to &mut as always UB |
Gotcha gotcha. This reddit comment helped my understand why this is problematic. I wish the nomicon link was a bit more like the reddit comment. It currently reads as hyperbole to me. Sounds like UnsafeCell is the way to go here if we want to continue allowing multiple mutable borrows of different resources (which i personally do). |
@EllenNyan that's basically exactly what I meant, but I think that's still UB in the small period where you call Disclaimer: I don't really know what I'm talking about, just have taken in stuff that (I think) RalfJung has written. |
I appreciate everyone's input and the motivations for the API. I'm currently unavailable to look into and test a proper fix that would also keep the current interface's ergonomics. If anyone would like to take up the torch, please do ^^ |
I can throw together a proper solution and PR it ^^ |
Noticed in #671 that it acquires an unique reference from a shared reference, which is always unsound regardless of runtime tracking. miri confirms this concern:
This PR changes
get_mut(&self)
toget_mut(&mut self)
and removes the problematic code.gilrs_event_system
needed to be tweaked slightly (making a temporary buffer for events). If desired, we can add a new resource that only this system can access by type to re-use a buffer. I have testedgamepad_input
andgamepad_input_events
examples with a xbox 360 controller and the behavior was the same before and after.thread_local_resource_mut_ref_aliasing
test is commented out for now since the issue it aimed to catch is now caught at compile time. Does this makeResourceRefMut
no longer needed? To keep the diff small at first, I have left it there for now.