-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
as_ptr()'s use of *mut is misleading #24
Comments
The structure of
Even for C APIs that are const-correct, C's notion of constness is distinct enough from Rust's that |
I've developed a version of Miri that can execute foreign functions by interpreting LLVM bytecode, and it found a Tree Borrows violation related to mutating through Here's a minimal example that can recreate the error with an unmodified version of Miri, without the FFI. In particular, this error is triggered when mutating through
Also, I was a bit unsure about what you meant here:
Are you saying that the compiler's treatment of |
The |
Could it be because |
Yeah, I think there's a difference between |
Fixes sfackler#24
I'll note again: If you add extern types to the mix, you'll see the exact same LLVM IR as for |
See also discussion from rust-lang/unsafe-code-guidelines#236 (comment), this is still not determined |
@madsmtm I think you accidentally used the same link twice. Based on this pull request, allowing |
Apologies, I've fixed it now. |
as_ptr()
returns a*mut _
pointer. This isn't an error by itself, but under Rust's memory model, this pointer is derived from shared/immutable&self
, and therefore doesn't have permission to mutateself
, so themut
of the pointer is misleading.foreign-types/foreign-types-shared/src/lib.rs
Line 89 in 393f6ab
I assume this is for convenience, because C APIs aren't diligent about
const
vsmut
distinction. However, this is a gotcha, because C APIs that take*mut
may actually mutate the object, and that is UB from Rust's perspective.Could you add
.as_mut_ptr()
that takes&mut self
to provide a pointer safe for mutation?The text was updated successfully, but these errors were encountered: