-
Notifications
You must be signed in to change notification settings - Fork 795
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
Unsoundness: &T
and &mut T
to same object
#749
Comments
Now we have |
What about calling it |
I think this another case of #342 |
After my first 1 hour trying to resolve this... |
Ouch. Yes I guess |
@kngwyu if you want I'm happy to have a play with this and propose a WIP PR. (But I'll wait if you know how you want to solve it.) |
@davidhewitt trait PyTryFrom<'a>: PyTypeInfo<'a> {
fn try_from(obj: &PyAny) -> <Self as PyTypeInfo>::Ref<'a>;
} |
👍 this is what I'm thinking too. I was also thinking |
Fixed in #770 |
It is currently possible with pyo3 to get
&mut T
and&T
to the same object simultaneously.This is trivially demonstrated with the following code:
Because of the borrow rules the Rust compiler is safe to assume that
a
andb
are not borrowing the same object. So it may do any number of optimisations to theassert_eq!
line using the assumption thatb.value
is never modified.This test can be written which breaks these rules by using the same python object for both arguments:
This is clearly UB. With
cargo test
, theassert_eq!
check panics and the test fails. Withcargo test --release
, the test passes, presumably because some optimisation has been applied.A solution is to implement #342 to guard against this unsoundness.
If a user attempted to call
edit_foo(foo, foo)
from Python, the only choice I see is to raise a Python exception reading something like "TypeError: Attempted to access a Foo object mutably while also accessing it immutably
".I pushed this test on a branch; you can see the test failure here: https://github.com/davidhewitt/pyo3/runs/414604743?check_suite_focus=true
The text was updated successfully, but these errors were encountered: