-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Runtime] Add function_cast, switch from std::bit_cast. #80516
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
Conversation
@swift-ci please test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adds explicit UB rather than implicit UB. The std::bit_cast
is absolutely required even if it doesn’t work on arm64e. We should instead be stripping pointer signing and resigning after the copy for ptrauth enabled environments. That would allow this to work on all Von Neumann architectures.
The implicit UB that we form in the std::bit_cast
approach is for Harvard architectures or more complex memory architectures such as GPU, which may have multiple address spaces. In both those scenarios, the pointer itself is inconvertible and we would form something which is invalid.
Manually stripping and re-signing would be pretty annoying and error-prone. What if we implement |
Stripping and resigning is not allowed; pointer authentication is, in fact, actually supposed to do something. |
Right, we'd really want to auth and re-sign. But doing that manually is still not an attractive prospect. |
Seems that it's even simpler. The ptrauth annotation drops off the source once we're inside |
@compnerd Are you good with this new approach? |
include/swift/Basic/Casting.h
Outdated
/// doesn't work on ARM64e with address-discriminated signed function types. | ||
template <typename Destination, typename Source> | ||
Destination function_cast(Source source) { | ||
// Ptrauth attributes decay away here, so we can now bit_cast in peace. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Ptrauth attributes decay away here, so we can now bit_cast in peace. | |
// ptrauth attributes decay away here, so we can now bit_cast in peace. |
include/swift/Basic/Casting.h
Outdated
#ifndef SWIFT_BASIC_CASTING_H | ||
#define SWIFT_BASIC_CASTING_H | ||
|
||
#include "STLCompatibility.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we inline the std::bit_cast
implementation for compatibility with older C++ standards?
@swift-ci please test |
@swift-ci please smoke test macos platform |
Function types aren't always trivially copyable, e.g. with address-discriminated signed pointers on ARM64e. Introduce a function_cast helper and use that instead.
@swift-ci please test |
Function types aren't always trivially copyable, e.g. with address-discriminated signed pointers on ARM64e. Introduce a function_cast helper and use that instead.