10b57cec5SDimitry Andric //===-- AArch64BranchTargets.cpp -- Harden code using v8.5-A BTI extension -==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This pass inserts BTI instructions at the start of every function and basic
100b57cec5SDimitry Andric // block which could be indirectly called. The hardware will (when enabled)
110b57cec5SDimitry Andric // trap when an indirect branch or call instruction targets an instruction
120b57cec5SDimitry Andric // which is not a valid BTI instruction. This is intended to guard against
130b57cec5SDimitry Andric // control-flow hijacking attacks. Note that this does not do anything for RET
140b57cec5SDimitry Andric // instructions, as they can be more precisely protected by return address
150b57cec5SDimitry Andric // signing.
160b57cec5SDimitry Andric //
170b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
180b57cec5SDimitry Andric
19e8d8bef9SDimitry Andric #include "AArch64MachineFunctionInfo.h"
200b57cec5SDimitry Andric #include "AArch64Subtarget.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineJumpTableInfo.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
250b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
260b57cec5SDimitry Andric
270b57cec5SDimitry Andric using namespace llvm;
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric #define DEBUG_TYPE "aarch64-branch-targets"
300b57cec5SDimitry Andric #define AARCH64_BRANCH_TARGETS_NAME "AArch64 Branch Targets"
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric namespace {
330b57cec5SDimitry Andric class AArch64BranchTargets : public MachineFunctionPass {
340b57cec5SDimitry Andric public:
350b57cec5SDimitry Andric static char ID;
AArch64BranchTargets()360b57cec5SDimitry Andric AArch64BranchTargets() : MachineFunctionPass(ID) {}
370b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override;
380b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
getPassName() const390b57cec5SDimitry Andric StringRef getPassName() const override { return AARCH64_BRANCH_TARGETS_NAME; }
400b57cec5SDimitry Andric
410b57cec5SDimitry Andric private:
42*bdd1243dSDimitry Andric void addBTI(MachineBasicBlock &MBB, bool CouldCall, bool CouldJump,
43*bdd1243dSDimitry Andric bool NeedsWinCFI);
440b57cec5SDimitry Andric };
450b57cec5SDimitry Andric } // end anonymous namespace
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric char AArch64BranchTargets::ID = 0;
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric INITIALIZE_PASS(AArch64BranchTargets, "aarch64-branch-targets",
500b57cec5SDimitry Andric AARCH64_BRANCH_TARGETS_NAME, false, false)
510b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const520b57cec5SDimitry Andric void AArch64BranchTargets::getAnalysisUsage(AnalysisUsage &AU) const {
530b57cec5SDimitry Andric AU.setPreservesCFG();
540b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric
createAArch64BranchTargetsPass()570b57cec5SDimitry Andric FunctionPass *llvm::createAArch64BranchTargetsPass() {
580b57cec5SDimitry Andric return new AArch64BranchTargets();
590b57cec5SDimitry Andric }
600b57cec5SDimitry Andric
runOnMachineFunction(MachineFunction & MF)610b57cec5SDimitry Andric bool AArch64BranchTargets::runOnMachineFunction(MachineFunction &MF) {
62e8d8bef9SDimitry Andric if (!MF.getInfo<AArch64FunctionInfo>()->branchTargetEnforcement())
630b57cec5SDimitry Andric return false;
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric LLVM_DEBUG(
660b57cec5SDimitry Andric dbgs() << "********** AArch64 Branch Targets **********\n"
670b57cec5SDimitry Andric << "********** Function: " << MF.getName() << '\n');
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric // LLVM does not consider basic blocks which are the targets of jump tables
700b57cec5SDimitry Andric // to be address-taken (the address can't escape anywhere else), but they are
710b57cec5SDimitry Andric // used for indirect branches, so need BTI instructions.
720b57cec5SDimitry Andric SmallPtrSet<MachineBasicBlock *, 8> JumpTableTargets;
730b57cec5SDimitry Andric if (auto *JTI = MF.getJumpTableInfo())
740b57cec5SDimitry Andric for (auto &JTE : JTI->getJumpTables())
750b57cec5SDimitry Andric for (auto *MBB : JTE.MBBs)
760b57cec5SDimitry Andric JumpTableTargets.insert(MBB);
770b57cec5SDimitry Andric
780b57cec5SDimitry Andric bool MadeChange = false;
79*bdd1243dSDimitry Andric bool HasWinCFI = MF.hasWinCFI();
800b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) {
810b57cec5SDimitry Andric bool CouldCall = false, CouldJump = false;
82fe6060f1SDimitry Andric // Even in cases where a function has internal linkage and is only called
83fe6060f1SDimitry Andric // directly in its translation unit, it can still be called indirectly if
84fe6060f1SDimitry Andric // the linker decides to add a thunk to it for whatever reason (say, for
85fe6060f1SDimitry Andric // example, if it is finally placed far from its call site and a BL is not
86fe6060f1SDimitry Andric // long-range enough). PLT entries and tail-calls use BR, but when they are
870b57cec5SDimitry Andric // are in guarded pages should all use x16 or x17 to hold the called
880b57cec5SDimitry Andric // address, so we don't need to set CouldJump here. BR instructions in
890b57cec5SDimitry Andric // non-guarded pages (which might be non-BTI-aware code) are allowed to
900b57cec5SDimitry Andric // branch to a "BTI c" using any register.
91fe6060f1SDimitry Andric if (&MBB == &*MF.begin())
920b57cec5SDimitry Andric CouldCall = true;
930b57cec5SDimitry Andric
940b57cec5SDimitry Andric // If the block itself is address-taken, it could be indirectly branched
950b57cec5SDimitry Andric // to, but not called.
960b57cec5SDimitry Andric if (MBB.hasAddressTaken() || JumpTableTargets.count(&MBB))
970b57cec5SDimitry Andric CouldJump = true;
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric if (CouldCall || CouldJump) {
100*bdd1243dSDimitry Andric addBTI(MBB, CouldCall, CouldJump, HasWinCFI);
1010b57cec5SDimitry Andric MadeChange = true;
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric return MadeChange;
1060b57cec5SDimitry Andric }
1070b57cec5SDimitry Andric
addBTI(MachineBasicBlock & MBB,bool CouldCall,bool CouldJump,bool HasWinCFI)1080b57cec5SDimitry Andric void AArch64BranchTargets::addBTI(MachineBasicBlock &MBB, bool CouldCall,
109*bdd1243dSDimitry Andric bool CouldJump, bool HasWinCFI) {
1100b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Adding BTI " << (CouldJump ? "j" : "")
1110b57cec5SDimitry Andric << (CouldCall ? "c" : "") << " to " << MBB.getName()
1120b57cec5SDimitry Andric << "\n");
1130b57cec5SDimitry Andric
1140b57cec5SDimitry Andric const AArch64InstrInfo *TII = static_cast<const AArch64InstrInfo *>(
1150b57cec5SDimitry Andric MBB.getParent()->getSubtarget().getInstrInfo());
1160b57cec5SDimitry Andric
1170b57cec5SDimitry Andric unsigned HintNum = 32;
1180b57cec5SDimitry Andric if (CouldCall)
1190b57cec5SDimitry Andric HintNum |= 2;
1200b57cec5SDimitry Andric if (CouldJump)
1210b57cec5SDimitry Andric HintNum |= 4;
1220b57cec5SDimitry Andric assert(HintNum != 32 && "No target kinds!");
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric auto MBBI = MBB.begin();
1250b57cec5SDimitry Andric
126fe6060f1SDimitry Andric // Skip the meta instructions, those will be removed anyway.
127fe6060f1SDimitry Andric for (; MBBI != MBB.end() &&
128fe6060f1SDimitry Andric (MBBI->isMetaInstruction() || MBBI->getOpcode() == AArch64::EMITBKEY);
129fe6060f1SDimitry Andric ++MBBI)
130e837bb5cSDimitry Andric ;
131e837bb5cSDimitry Andric
132e837bb5cSDimitry Andric // SCTLR_EL1.BT[01] is set to 0 by default which means
133e837bb5cSDimitry Andric // PACI[AB]SP are implicitly BTI C so no BTI C instruction is needed there.
134e837bb5cSDimitry Andric if (MBBI != MBB.end() && HintNum == 34 &&
135e837bb5cSDimitry Andric (MBBI->getOpcode() == AArch64::PACIASP ||
1360b57cec5SDimitry Andric MBBI->getOpcode() == AArch64::PACIBSP))
1370b57cec5SDimitry Andric return;
1380b57cec5SDimitry Andric
139*bdd1243dSDimitry Andric if (HasWinCFI && MBBI->getFlag(MachineInstr::FrameSetup)) {
140*bdd1243dSDimitry Andric BuildMI(MBB, MBB.begin(), MBB.findDebugLoc(MBB.begin()),
141*bdd1243dSDimitry Andric TII->get(AArch64::SEH_Nop));
142*bdd1243dSDimitry Andric }
1430b57cec5SDimitry Andric BuildMI(MBB, MBB.begin(), MBB.findDebugLoc(MBB.begin()),
1440b57cec5SDimitry Andric TII->get(AArch64::HINT))
1450b57cec5SDimitry Andric .addImm(HintNum);
1460b57cec5SDimitry Andric }
147