-
Notifications
You must be signed in to change notification settings - Fork 59
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
When can function pointers be transmuted? #81
Comments
It would be very valuable if this were defined,because I need to cast I may be able to get around this problem by just taking a struct that always erases the type:
|
I would also like to know this. More specifically, when can unsafe function pointers be transmuted into safe ones? That is, which (if any) of these are valid: mem::transmute::<unsafe extern "Rust" fn(A) -> B, extern "Rust" fn(A) -> B>(foo)
mem::transmute::<unsafe extern "C" fn(A) -> B, extern "C" fn(A) -> B>(foo)
mem::transmute::<unsafe extern "C" fn(A, ...) -> B, extern "C" fn(A, ...) -> B>(foo) I would expect at least the |
I would expect it to be valid, but only sound when you now that the function is always safe to call. |
Yeah, |
It would be nice if there was a less error-prone way to do this than just |
For triage: Clarifying that we are interpreting this as a question about validity of function pointers, not about ABI. |
On Zulip, @ubsan was asking about function pointer transmutes:
(I assume we have
T: Sized
here.)I would say we can modularize the problem (but this goes deep into validity invariant territory):
Scalar
ABI that may differ only in itsvalid_range
. In your example, the second condition is satisfied. If the mismatch is not allowed, we have UB.transmute
, of union-based type punning, and of type punning through changing pointer types: In your case, I would say your code is equivalent tox(mem::transmute<*const T, *const ()>(p))
.(Taken from #45 (comment) )
The text was updated successfully, but these errors were encountered: