Incorrect code generation with -arch arm64e -mbranch-protection=standard
or =pac-ret
#60239
Labels
-arch arm64e -mbranch-protection=standard
or =pac-ret
#60239
Summary
On macOS, Arm64, the combination of options
-arch arm64e -mbranch-protection=standard
generates invalid code (redundant pointer authentication) leading to application crash on return.Platform
Demonstrated on a Macbook with M1 chip, macOS 13.1, arm64e API enabled, Apple clang 14.0.0. This is the clang version coming with the latest "Command Line Tools for Xcode 14.2" from December 13, 2022.
Demonstration
This simple "hello world" code crashes with
-arch arm64e -mbranch-protection=standard
. It does not crash without-mbranch-protection
or with-mbranch-protection=bti
only.Details
The combination
-arch arm64e -mbranch-protection=standard
is fatal. The same problem is seen with-mbranch-protection=pac-ret
instead ofstandard
(the latter includes the former).-mbranch-protection=pac-ret
or=standard
authenticates the caller's return address usingpacia x30, sp
, updating x30 with a pointer authentication code.-arch arm64e
authenticates the caller's return address usingpacibsp
(same aspacib x30, sp
). This second instruction trashes the PAC in x30, recomputing a PAC with key B.autibsp
andretaa
. Theautibsp
removes the PAC from x30. Whenretaa
authenticates x30, there is no longer any PAC, the authentication fails and the program crashes.Solution: There must be only one authentication sequence. Using
-mbranch-protection=pac-ret
or=standard
shall not add PACIA instructions when-arch arm64e
is specified since pointer authentication is already used. Alternatively, an error message may report the incompatible options. But no invalid code should be generated.Generated code below:
The text was updated successfully, but these errors were encountered: