-
Notifications
You must be signed in to change notification settings - Fork 40
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
How does safer_ffi
avoid aliasing rule problem?
#195
Comments
Good question! So, as is often the case with these things, there are two sides to the API of a function:
CalleeGiven
To illustrate: #[ffi_export]
fn foo(r: &u32, m: &mut u32) -> u32 {
if *r == 42 {
*m = 0;
*r
} else {
42
}
} ought to have its if *r == 42 { *m = 0; }
42
CallerThe previous point thus makes it UB for the caller to violate these invariants.
|
Thank you for the reply!
I see. Indeed, this is also a headache for me/us when developing https://github.com/fzyzcjy/flutter_rust_bridge. (P.S. We finally introduced some runtime overhead (think as Arc/Mutex/Rc/RefCell etc) to let Rust do the borrow checking at runtime for really safe interface.) |
Yeah, that's a very sensible thing to do. For instance, |
Hi thanks for the interesting package! I am curious, how does
safer_ffi
avoid aliasing rule problem? Since the function is not markedunsafe
, I guess it means we should ensure it is memory safe and no undefined behavior etc.For example, suppose we have the code in the Rustonomicon: https://doc.rust-lang.org/nomicon/aliasing.html
Will safer_ffi generate some kind of helpers to avoid aliasing?
Or, even simpler, it seems that we should avoid two mutable references to the same object:
P.S. suppose the example below:
I have not spent a lot of time checking whether aliasing rule requires
input
andoutput.x
not to be the same object. i.e. Rust compiler forbids the following, but I am not sure whether it optimizes based on such assumption.The text was updated successfully, but these errors were encountered: