Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upactix-service Cell::get_mut() is unsound #83
Comments
This comment has been minimized.
This comment has been minimized.
This is internal code. There is no repeated call to get_mut() anywhere in code |
This comment has been minimized.
This comment has been minimized.
Please, don’t start |
This comment has been minimized.
This comment has been minimized.
These two references do not need to exist in the same function to trigger undefined behavior, they only need to exist at the same point in time. An easy way to see if this is a problem in practice is replace |
This comment has been minimized.
This comment has been minimized.
@fafhrd91, I don't think "unsound" was meant to be personal or offensive. Why close this so quickly? |
This comment has been minimized.
This comment has been minimized.
I need unit test that shows UB. Unit test must use public api. |
This comment has been minimized.
This comment has been minimized.
The unsoundness of the Running the code via
(@repnop identified that |
This comment has been minimized.
This comment has been minimized.
I think it's worth to fix, re-opening. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
should be fixed in master |
This comment has been minimized.
This comment has been minimized.
It's possible to get around the |
This comment has been minimized.
This comment has been minimized.
its also worth noting that adding a |
This comment has been minimized.
This comment has been minimized.
Undefined behavior occurs when external code attempts to obtain two mutable references to the same data. The lifetime of the data is irrelevant to this issue. If If panicking is undesirable, it is possible to return Option or Result from this function instead of panicking, see I wonder, what was the original rationale for migrating from |
This comment has been minimized.
This comment has been minimized.
@Nemo157 new playground panics with BorrowMutError, is that what should happen? |
This comment has been minimized.
This comment has been minimized.
@repnop i dont see how this could be fixed internally |
This comment has been minimized.
This comment has been minimized.
If you run the code from the playground under MIRI it fails before the
because at this point there exist two independent |
This comment has been minimized.
This comment has been minimized.
Would it be possible to simply change it to |
This comment has been minimized.
This comment has been minimized.
As a PoC this patch applied to |
This comment has been minimized.
This comment has been minimized.
this patch is boring |
This comment has been minimized.
This comment has been minimized.
So is resolving silent data corruption. |
This comment has been minimized.
This comment has been minimized.
@fafhrd91 seriously? Please just stop writing Rust. You do not respect semver, you do not respect soundness, so why are you using a language predominantly based around doing these things right? |
Shnatsel commentedJan 8, 2020
The following code is unsound:
actix-net/actix-service/src/cell.rs
Lines 34 to 36 in 7c5fa25
This uses
Rc::as_ref()
to obtain a reference to the underlying data, which does not guarantee uniqueness. It is possible to obtain several mutable references to the same memory location by calling this function repeatedly:This may lead to arbitrary memory errors triggered from safe code, the most common of which would be use-after-free. These two references do not need to exist in the same function to trigger undefined behavior, they only need to exist at the same point in time.
A proper way to implement
Cell:get_mut()
would be calling Rc::get_mut() which guarantees uniqueness instead ofRc::as_ref()
.