This change adds the support for __builtin_return_address
for ARMv8.3A Pointer Authentication.
Location of the authentication code in the pointer depends on
the system configuration, therefore a dedicated instruction is used for
effectively removing the authentication code without
authenticating the pointer.
Details
Diff Detail
Event Timeline
clang/test/CodeGen/arm64-extractreturnaddress.c | ||
---|---|---|
15 ↗ | (On Diff #246183) | CHECK-NEXT: entry: |
16 ↗ | (On Diff #246183) | CHECK-PAC: %0 = call i8* @llvm.extractreturnaddress.p0i8.p0i8(i8* inttoptr (i64 42 to i8*)) |
Needs a test in clang/test that __builtin_extract_return_address is translated to llvm.extractreturnaddress.
What if LLVM IR contains a call to llvm.extractreturnaddress, but the target is not AArch64?
llvm/include/llvm/CodeGen/ISDOpcodes.h | ||
---|---|---|
491–492 | Needs a comment about EXTRACTRETURNADDR. And also a slightly different grouping, so the non-commented/undocumented things stand out. |
llvm/include/llvm/CodeGen/ISDOpcodes.h | ||
---|---|---|
491–492 | agree, I. added documentation for all now. |
Changing the implementation from builtin_extract_return_addr to builtin_return_address.
Stripping PAC in __builtin_return_address is a better solution, because:
- we should not expose PAC bits to the user because it could be passed around and that would be an ABI break.
- builtin_extract_return_addr has a pair, the builtin_frob_return_addr which won't be supported with PAC.
I'm thinking of adding a warning if __builtin_frob_return_addr is used with PAC on.
Same change will be proposed from gcc too.
I'm afraid the patch does not work yet. For example, when the following program
void *f() { void g(); g(); return __builtin_return_address(0); }
is compiled with
./bin/clang -target aarch64-eabi -march=armv8.3-a -mbranch-protection=pac-ret -S -O2 h.c
The issue is that the definition of the instructions XPAC{D,I} is incorrect: it does not mention at all the operand to those insns.
Another problem is that the patch does not work with -O0. When compiling without optimisations, AArch64 backend used GlobalISel.
I have patches for these two issues. I'll post the one for XPAC{D,I} tomorrow and perhaps in a couple of days the GlobalISel one and we're good to go.
llvm/lib/Target/AArch64/AArch64ISelLowering.cpp | ||
---|---|---|
6495 | We shouldn't be hardcoding the X0 register here. We already have the encoded return address in ReturnAddress SDNode *St = DAG.getMachineNode(AArch64::XPACI, DL, VT, ReturnAddress); | |
6500 | Rename Reg to Chain. |
Err, they do mention the operand, but only as an input one, it should be input/output.
Sorry, I thought about committing all PAC/BTI patches together, but there's no reason, is there?
So, let's go ahead and commit the two dealing with __builtin-return_address .
Needs a comment about EXTRACTRETURNADDR. And also a slightly different grouping, so the non-commented/undocumented things stand out.