-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Document how Rust array/slice types are translated for extern "C" functions. #30382
Comments
Note that this may require an RFC actually specifying this and related information. |
Also, more generally, it should be documented that a reference will be translated to a pointer, so that |
Pointers to slices (aka Pointers to fixed size arrays (aka |
So, following up: what needs to be done here, exactly, and where should it go? FFI should say that fat pointers aren't usable? |
I am going to give this one a close. I'm not going to do a whole ton of work on the existing FFI docs, focusing on the new one instead, and talking about fat vs thin pointers wrt FFI is a big thing. |
I don't understand why we're so eager to close bugs without fixing them. It isn't a matter of "existing FFI docs" vs "new one" but the fact that it is unspecified what happens. Also, it isn't just a documentation bug because the compiler should be aligned with what ever is decided. In particular, is what the Rust compiler currently does right or wrong? |
Casting a fat pointer to a thin pointer returns the fat pointer's base. If the pointer was unsized from a thin pointer, that is the original thin pointer. If the pointer was created by You are only supposed to use FFI-safe types for FFI. Fat pointers are not FFI-safe types, therefore their representation should not matter. Thin pointers are stored in the obvious way, but I think that is documented. Slices/arrays are stored in memory in the obvious way (Rust requires that
|
|
There's a lint for this, at least when declaring extern functions -- example on playpen
|
The representation of types is documented at https://doc.rust-lang.org/nightly/nomicon/data.html. In general, the rule is that types are FFI-safe ~ have a defined representation only if we say that they are that. Reading it again, it seems that we never say that primitives and thin pointers are FFI-safe - cc #31227 ! The currently most significant reason that fat pointers are not FFI safe is because we sometimes pass fat pointers as 2 arguments to a function instead of as a single 2-word argument. extern {
fn memcpy(dst: *mut u8, src: &[u8]); // THIS DOES NOT HAVE TO WORK!
} |
I don't think the array part of this issue was answered. |
I checked that and it works, i was wondering is there anything for the reverse - for e.g.: this. The other function
Of-course looking back it was a stupid mistake - but i had subconciously assumed array would decay into a ptr like in C when callback was invoked. All my tests passed as they were written in rust. Only when interfacing with C and running into occassional seg-faults that i realised the mistake. If there was a warning or some language feature to mark that this function is not only for C ABI compatibility but also strictly for a call from C (like what Same with |
Was this ever addressed? I've been searching for some documentation on whether |
@jbowens, maybe we can discuss it on internals.. or go straight to a PR |
Has this been resolved since 5 years ago?
but not about the fixed-size array, e.g. |
Currently I write:
I would like to write:
The C declaration is:
which is equivalent to:
The second form of the Rust ffi declaration is safer because it makes it clear that the function doesn't accept NULL pointers and it also makes it clear how long the input arrays are expected to be. However, the Rust reference doesn't document how it translates a reference to a slice or array to a C type, so using the second form is undefined behavior. I would like the Rust Reference to be amended to make it clear that it will call translate references to arrays/slices by passing the result of
as_ptr
/as_mut_ptr
to the C function.The text was updated successfully, but these errors were encountered: