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

Eleanor Bonnici via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 26 05:06:28 PDT 2024


eleanor-arm wrote:

> I don't think changing the `AltOrders` actually prevents the removed registers in all cases, in this case `r12` is still used:
> 
> ```c
> typedef void(func)(void);
> 
> void foo(func *fptr_arg) {
>   register func *fptr_r12 asm("r12") = fptr_arg;
> 
>   // Force the function pointer into r12, and clobber lr to force a stack frame
>   // to be created.
>   asm("" : "+r" (fptr_r12) : : "lr");
> 
>   // Indirect tail call through r12.
>   fptr_r12();
> }
> ```
> 
> ```
> $ /work/llvm/build/bin/clang --target=arm-none-eabi -march=armv8.1-m.main -c indirect-tail.c -o - -S -O1 -mbranch-protection=pac-ret
> ...
>         ldr     r12, [sp], #4
>         pop.w   {r7, lr}
>         aut     r12, lr, sp
>         bx      r12
> ...
> ```
> 
> I recently fixed a similar issue in AArch64 (#81020), would that method of creating multiple pseudo-instructions with different register classes work here too?

I've reviewed #81020 but didn't find it directly applicable to this problem. The fix code is not run when compiling any C/C++ testcases I have available with Aarch64 target. When I tried to add similar code to ARM the checks were run and the compiler crashed because `MF` is uninitialized at that point. 
It looks like to address this case when R12 is forced in assembly, more refactoring would be needed which I currently don't have time to look into. It's a very uncommon case so I don't think it should blog this fix that will fix the defect for everyone who doesn't try to specify registers explicitly in integrated assembly.    

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


More information about the llvm-commits mailing list