swift icon indicating copy to clipboard operation
swift copied to clipboard

[Runtime] Add function_cast, switch from std::bit_cast.

Open mikeash opened this issue 8 months ago • 6 comments

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.

mikeash avatar Apr 04 '25 00:04 mikeash

@swift-ci please test

bnbarham avatar Apr 04 '25 06:04 bnbarham

Manually stripping and re-signing would be pretty annoying and error-prone. What if we implement function_cast in terms of bit_cast when there's no ptrauth, and implement it with reinterpret_cast only when __has_feature(ptrauth_calls)? Or to make the condition directly address the problem, only when the types are not is_trivially_copyable.

mikeash avatar Apr 04 '25 14:04 mikeash

Stripping and resigning is not allowed; pointer authentication is, in fact, actually supposed to do something.

rjmccall avatar Apr 04 '25 19:04 rjmccall

Right, we'd really want to auth and re-sign. But doing that manually is still not an attractive prospect.

mikeash avatar Apr 04 '25 19:04 mikeash

Seems that it's even simpler. The ptrauth annotation drops off the source once we're inside function_cast, so we can implement it as just an unconditional wrapper around std::bit_cast. I've updated the PR with this approach.

mikeash avatar Apr 04 '25 22:04 mikeash

@compnerd Are you good with this new approach?

mikeash avatar Apr 07 '25 21:04 mikeash

@swift-ci please test

mikeash avatar Apr 10 '25 13:04 mikeash

@swift-ci please smoke test macos platform

mikeash avatar Apr 11 '25 00:04 mikeash

@swift-ci please test

mikeash avatar Apr 11 '25 00:04 mikeash