[llvm] Do not use R12 for indirect tail calls with PACBTI (PR #82661)

Eleanor Bonnici via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 17 08:47:00 PDT 2024


eleanor-arm wrote:

> That AArch64 change was for a bug in branch-protection-pauth-lr

Thanks for the clarification. I don't think you need to use `"sign-return-address"="all"` in the test [here](https://github.com/ostannard/llvm-project/blob/6758bbbc95400bb82772433d4897e6aa272421dc/llvm/test/CodeGen/AArch64/branch-target-enforcement-indirect-calls.ll)  then.


> I can't really help with this if you don't tell me what codegen is incorrect. Do you have an IR file for which LLVM continues to generate code which incorrectly uses r12 in the prologue, or something else?

This 
```
define dso_local void @func1(ptr noundef readonly %fptr_arg) local_unnamed_addr {
entry:
  %0 = tail call ptr asm "", "={r12},{r12},~{lr}"(ptr %fptr_arg)
  tail call void %0()
  ret void
}
!llvm.module.flags = !{!4}

!4 = !{i32 8, !"sign-return-address", i32 1}
``` 
won't generate correct code. 
```
func1:
	.fnstart
@ %bb.0:                                @ %entry
	pac	r12, lr, sp
	.save	{r7, lr}
	push	{r7, lr}
	.save	{ra_auth_code}
	str	r12, [sp, #-4]!
	mov	r12, r0
	@APP
	@NO_APP
	ldr	r12, [sp], #4
	pop.w	{r7, lr}
	aut	r12, lr, sp
	bx	r12
.Lfunc_end0:
```

I don't see the r14 spilled here. So does this mean the PAC/AUT should not have been added to this function?

This code is correct
```
define dso_local void @func2(ptr noundef readonly %fptr_arg) local_unnamed_addr #0 {
entry:
  %0 = tail call ptr asm "", "={r12},{r12},~{lr}"(ptr %fptr_arg)
  tail call void %0()
  ret void
}

!llvm.module.flags = !{!4, !5}

!4 = !{i32 8, !"sign-return-address", i32 1}
!5 = !{i32 8, !"sign-return-address-all", i32 1}
```
```
func2:
	.fnstart
@ %bb.0:                                @ %entry
	pac	r12, lr, sp
	.save	{r7, lr}
	push	{r7, lr}
	.save	{ra_auth_code}
	str	r12, [sp, #-4]!
	mov	r12, r0
	@APP
	@NO_APP
	mov	r0, r12
	ldr	r12, [sp], #4
	pop.w	{r7, lr}
	aut	r12, lr, sp
	bx	r0
.Lfunc_end0:
```

https://github.com/llvm/llvm-project/pull/82661


More information about the llvm-commits mailing list