]> git.ipfire.org Git - thirdparty/gcc.git/commit
[PATCH 10/15] arm: Implement cortex-M return signing address codegen
authorAndrea Corallo <andrea.corallo@arm.com>
Thu, 20 Jan 2022 14:36:23 +0000 (15:36 +0100)
committerAndrea Corallo <andrea.corallo@arm.com>
Mon, 23 Jan 2023 10:38:35 +0000 (11:38 +0100)
commit651460b452d752058e38620bf64541822e25c69c
tree7343d35f50cbfdfef62339442c00715609932f64
parentcea85c669704332baacfe8a50f18d8498a025309
[PATCH 10/15] arm: Implement cortex-M return signing address codegen

Hi all,

this patch enables address return signature and verification based on
Armv8.1-M Pointer Authentication [1].

To sign the return address, we use the PAC R12, LR, SP instruction
upon function entry.  This is signing LR using SP and storing the
result in R12.  R12 will be pushed into the stack.

During function epilogue R12 will be popped and AUT R12, LR, SP will
be used to verify that the content of LR is still valid before return.

Here an example of PAC instrumented function prologue and epilogue:

void foo (void);

int main()
{
  foo ();
  return 0;
}

Compiled with '-march=armv8.1-m.main -mbranch-protection=pac-ret
-mthumb' translates into:

main:
pac ip, lr, sp
push {r3, r7, ip, lr}
add r7, sp, #0
bl foo
movs r3, #0
mov r0, r3
pop {r3, r7, ip, lr}
aut ip, lr, sp
bx lr

The patch also takes care of generating a PACBTI instruction in place
of the sequence BTI+PAC when Branch Target Identification is enabled
contextually.

Ex. the previous example compiled with '-march=armv8.1-m.main
-mbranch-protection=pac-ret+bti -mthumb' translates into:

main:
pacbti ip, lr, sp
push {r3, r7, ip, lr}
add r7, sp, #0
bl foo
movs r3, #0
mov r0, r3
pop {r3, r7, ip, lr}
aut ip, lr, sp
bx lr

As part of previous upstream suggestions a test for varargs has been
added and '-mtpcs-frame' is deemed being incompatible with this return
signing address feature being introduced.

[1] <https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/armv8-1-m-pointer-authentication-and-branch-target-identification-extension>

gcc/
* config/arm/arm.h (arm_arch8m_main): Declare it.
* config/arm/arm-protos.h (arm_current_function_pac_enabled_p):
Declare it.
* config/arm/arm.cc (arm_arch8m_main): Define it.
(arm_option_reconfigure_globals): Set arm_arch8m_main.
(arm_compute_frame_layout, arm_expand_prologue)
(thumb2_expand_return, arm_expand_epilogue)
(arm_conditional_register_usage): Update for pac codegen.
(arm_current_function_pac_enabled_p): New function.
(aarch_bti_enabled) New function.
(use_return_insn): Return zero when pac is enabled.
* config/arm/arm.md (pac_ip_lr_sp, pacbti_ip_lr_sp, aut_ip_lr_sp):
Add new patterns.
* config/arm/unspecs.md (UNSPEC_PAC_NOP)
(VUNSPEC_PACBTI_NOP, VUNSPEC_AUT_NOP): Add unspecs.

gcc/testsuite/

* gcc.target/arm/pac.h : New file.
* gcc.target/arm/pac-1.c : New test case.
* gcc.target/arm/pac-2.c : Likewise.
* gcc.target/arm/pac-3.c : Likewise.
* gcc.target/arm/pac-4.c : Likewise.
* gcc.target/arm/pac-5.c : Likewise.
* gcc.target/arm/pac-6.c : Likewise.
* gcc.target/arm/pac-7.c : Likewise.
* gcc.target/arm/pac-8.c : Likewise.
* gcc.target/arm/pac-9.c : Likewise.
* gcc.target/arm/pac-10.c : Likewise.
* gcc.target/arm/pac-11.c : Likewise.
17 files changed:
gcc/config/arm/arm-protos.h
gcc/config/arm/arm.cc
gcc/config/arm/arm.h
gcc/config/arm/arm.md
gcc/config/arm/unspecs.md
gcc/testsuite/gcc.target/arm/pac-1.c [new file with mode: 0644]
gcc/testsuite/gcc.target/arm/pac-10.c [new file with mode: 0644]
gcc/testsuite/gcc.target/arm/pac-11.c [new file with mode: 0644]
gcc/testsuite/gcc.target/arm/pac-2.c [new file with mode: 0644]
gcc/testsuite/gcc.target/arm/pac-3.c [new file with mode: 0644]
gcc/testsuite/gcc.target/arm/pac-4.c [new file with mode: 0644]
gcc/testsuite/gcc.target/arm/pac-5.c [new file with mode: 0644]
gcc/testsuite/gcc.target/arm/pac-6.c [new file with mode: 0644]
gcc/testsuite/gcc.target/arm/pac-7.c [new file with mode: 0644]
gcc/testsuite/gcc.target/arm/pac-8.c [new file with mode: 0644]
gcc/testsuite/gcc.target/arm/pac-9.c [new file with mode: 0644]
gcc/testsuite/gcc.target/arm/pac.h [new file with mode: 0644]