-
Notifications
You must be signed in to change notification settings - Fork 59
Representation of fn pointers #14
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
Comments
C++ member function pointers (which allow for dynamic dispatch) are typically, but not necessarily, larger than data pointers. If Rust wished to support calling them directly by way of an I don't know the background for the rules in C specifically that function pointers may be larger than data pointers to know if we can say that it's safe for the C ABI. However, presumably I agree that |
Pointers to member functions are separate from regular function pointers in the C++ type system and I see no reason why we should pretend they're the same in Rust, especially if it requires weakening otherwise-plausible guarantees we could give. A better motivation for making no guarantees about would be if some architectures had differently-sized address spaces for code and sizes, or if a platform ABI added extra metadata to function pointers that isn't there for other pointers, e.g., a tag to support enforcement of control flow integrity. |
They're definitely different, and they definitely do not use any of the existing ABIs. It's not the case that we support ABI polymorphism, though (except via |
Today, each function pointer refers to a specific free function that is declared with the same ABI string as the function pointer carries -- This means that, while we can't call any function with any ABI we like, it's not a total wild west either. In the past we have considered generating shims that adapt from one ABI to another (and have in fact done so in the past to codegen Rust functions with C ABI). Even exotic ABIs like A C++ pointer to member function, on the other hand is conceptually quite different from free functions and pointers to them. It's arguably even orthogonal to calling convention, since various compilers allow declaring member functions with different calling conventions (so e.g., you might have a member function that uses |
Do |
They are separate types. |
I definitely think C++ member function pointers are out of scope for this discussion. Rust's function pointers are analogous to a C function pointer (eg., I believe that we should declare — at minimum — that an This implies also that I thenk plenty of unsafe code in the wild relies on this (as @wycats and @sgrif can probably attest; they happen to be two people who I've talked to about this in the past). |
It's what bindgen generates for anything that takes a function pointer as an argument, so I think that's reasonable. :) (I can say for sure that Diesel relies on |
Why would they need a lifetime bound? IIRC they carry at most an offset into a vtable which does not depend on any object lifetimes.
How can I construct a |
You just define it: https://play.rust-lang.org/?gist=23fca1fa4d23cb71489a1733d7e6de8b&version=stable&mode=debug&edition=2015 Bindings to external symbols are always unsafe functions since you're asserting you got the signature right. |
I want to call out a comment by @rkruppe from the discussion about integer types:
I find this very well put, and it I think definitely applies here, in terms of e.g. whether we commit to a It seems like we ought to settle -- perhaps -- more generally on a policy in such cases. I feel like it's worth identifying a "default compatibility" profile that guarantees portability across all "major architectures", but perhaps identifying concerns that may apply to more esoteric architectures. |
https://github.com/rust-lang/unsafe-code-guidelines/blob/master/reference/src/layout/function-pointers.md addresses this (otherwise please re-open). |
Discussing the representation of
extern "abi" fn(..)
types:usize
?Option<extern "C" fn()>
guaranteed to be equivalent to a "C fn pointer" representation?The text was updated successfully, but these errors were encountered: