Skip to content

Commit 4dc462b

Browse files
committed
[AArch64] Emit CFI instruction for updating x18 when using ShadowCallStack with exception unwinding
PR45875 notes an instance where exception handling crashes on aarch64-fuchsia where SCS is enabled by default. The underlying issue seems to be that within libunwind, various _Unwind_* functions, the x18 register is not updated if a function is marked with nounwind. This removes the check for nounwind and emits the CFI instruction that updates x18. Differential Revision: https://reviews.llvm.org/D79822
1 parent e3129fb commit 4dc462b

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

llvm/lib/Target/AArch64/AArch64FrameLowering.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,22 +2494,20 @@ bool AArch64FrameLowering::spillCalleeSavedRegisters(
24942494
BuildMI(MBB, MI, DL, TII.get(AArch64::SEH_Nop))
24952495
.setMIFlag(MachineInstr::FrameSetup);
24962496

2497-
if (!MF.getFunction().hasFnAttribute(Attribute::NoUnwind)) {
2498-
// Emit a CFI instruction that causes 8 to be subtracted from the value of
2499-
// x18 when unwinding past this frame.
2500-
static const char CFIInst[] = {
2501-
dwarf::DW_CFA_val_expression,
2502-
18, // register
2503-
2, // length
2504-
static_cast<char>(unsigned(dwarf::DW_OP_breg18)),
2505-
static_cast<char>(-8) & 0x7f, // addend (sleb128)
2506-
};
2507-
unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(
2508-
nullptr, StringRef(CFIInst, sizeof(CFIInst))));
2509-
BuildMI(MBB, MI, DL, TII.get(AArch64::CFI_INSTRUCTION))
2510-
.addCFIIndex(CFIIndex)
2511-
.setMIFlag(MachineInstr::FrameSetup);
2512-
}
2497+
// Emit a CFI instruction that causes 8 to be subtracted from the value of
2498+
// x18 when unwinding past this frame.
2499+
static const char CFIInst[] = {
2500+
dwarf::DW_CFA_val_expression,
2501+
18, // register
2502+
2, // length
2503+
static_cast<char>(unsigned(dwarf::DW_OP_breg18)),
2504+
static_cast<char>(-8) & 0x7f, // addend (sleb128)
2505+
};
2506+
unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(
2507+
nullptr, StringRef(CFIInst, sizeof(CFIInst))));
2508+
BuildMI(MBB, MI, DL, TII.get(AArch64::CFI_INSTRUCTION))
2509+
.addCFIIndex(CFIIndex)
2510+
.setMIFlag(MachineInstr::FrameSetup);
25132511

25142512
// This instruction also makes x18 live-in to the entry block.
25152513
MBB.addLiveIn(AArch64::X18);

llvm/test/CodeGen/AArch64/shadow-call-stack.ll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,10 @@ define i32 @f5() shadowcallstack nounwind {
5858
ret i32 %res
5959
}
6060

61+
define i32 @f6() shadowcallstack nounwind uwtable {
62+
; CHECK: f6:
63+
; CHECK: .cfi_escape 0x16, 0x12, 0x02, 0x82, 0x78
64+
%res = call i32 @bar()
65+
%res1 = add i32 %res, 1
66+
ret i32 %res
67+
}

0 commit comments

Comments
 (0)