xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AArch64/AArch64BranchTargets.cpp (revision e837bb5cfb2b3a734feb1fbe20b25dbe6d550a3c)
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 
190b57cec5SDimitry Andric #include "AArch64Subtarget.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineJumpTableInfo.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
240b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric using namespace llvm;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric #define DEBUG_TYPE "aarch64-branch-targets"
290b57cec5SDimitry Andric #define AARCH64_BRANCH_TARGETS_NAME "AArch64 Branch Targets"
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric namespace {
320b57cec5SDimitry Andric class AArch64BranchTargets : public MachineFunctionPass {
330b57cec5SDimitry Andric public:
340b57cec5SDimitry Andric   static char ID;
350b57cec5SDimitry Andric   AArch64BranchTargets() : MachineFunctionPass(ID) {}
360b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override;
370b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
380b57cec5SDimitry Andric   StringRef getPassName() const override { return AARCH64_BRANCH_TARGETS_NAME; }
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric private:
410b57cec5SDimitry Andric   void addBTI(MachineBasicBlock &MBB, bool CouldCall, bool CouldJump);
420b57cec5SDimitry Andric };
430b57cec5SDimitry Andric } // end anonymous namespace
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric char AArch64BranchTargets::ID = 0;
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric INITIALIZE_PASS(AArch64BranchTargets, "aarch64-branch-targets",
480b57cec5SDimitry Andric                 AARCH64_BRANCH_TARGETS_NAME, false, false)
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric void AArch64BranchTargets::getAnalysisUsage(AnalysisUsage &AU) const {
510b57cec5SDimitry Andric   AU.setPreservesCFG();
520b57cec5SDimitry Andric   MachineFunctionPass::getAnalysisUsage(AU);
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric FunctionPass *llvm::createAArch64BranchTargetsPass() {
560b57cec5SDimitry Andric   return new AArch64BranchTargets();
570b57cec5SDimitry Andric }
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric bool AArch64BranchTargets::runOnMachineFunction(MachineFunction &MF) {
600b57cec5SDimitry Andric   const Function &F = MF.getFunction();
610b57cec5SDimitry Andric   if (!F.hasFnAttribute("branch-target-enforcement"))
620b57cec5SDimitry Andric     return false;
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric   LLVM_DEBUG(
650b57cec5SDimitry Andric       dbgs() << "********** AArch64 Branch Targets  **********\n"
660b57cec5SDimitry Andric              << "********** Function: " << MF.getName() << '\n');
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   // LLVM does not consider basic blocks which are the targets of jump tables
690b57cec5SDimitry Andric   // to be address-taken (the address can't escape anywhere else), but they are
700b57cec5SDimitry Andric   // used for indirect branches, so need BTI instructions.
710b57cec5SDimitry Andric   SmallPtrSet<MachineBasicBlock *, 8> JumpTableTargets;
720b57cec5SDimitry Andric   if (auto *JTI = MF.getJumpTableInfo())
730b57cec5SDimitry Andric     for (auto &JTE : JTI->getJumpTables())
740b57cec5SDimitry Andric       for (auto *MBB : JTE.MBBs)
750b57cec5SDimitry Andric         JumpTableTargets.insert(MBB);
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   bool MadeChange = false;
780b57cec5SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
790b57cec5SDimitry Andric     bool CouldCall = false, CouldJump = false;
800b57cec5SDimitry Andric     // If the function is address-taken or externally-visible, it could be
810b57cec5SDimitry Andric     // indirectly called. PLT entries and tail-calls use BR, but when they are
820b57cec5SDimitry Andric     // are in guarded pages should all use x16 or x17 to hold the called
830b57cec5SDimitry Andric     // address, so we don't need to set CouldJump here. BR instructions in
840b57cec5SDimitry Andric     // non-guarded pages (which might be non-BTI-aware code) are allowed to
850b57cec5SDimitry Andric     // branch to a "BTI c" using any register.
860b57cec5SDimitry Andric     if (&MBB == &*MF.begin() && (F.hasAddressTaken() || !F.hasLocalLinkage()))
870b57cec5SDimitry Andric       CouldCall = true;
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric     // If the block itself is address-taken, it could be indirectly branched
900b57cec5SDimitry Andric     // to, but not called.
910b57cec5SDimitry Andric     if (MBB.hasAddressTaken() || JumpTableTargets.count(&MBB))
920b57cec5SDimitry Andric       CouldJump = true;
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric     if (CouldCall || CouldJump) {
950b57cec5SDimitry Andric       addBTI(MBB, CouldCall, CouldJump);
960b57cec5SDimitry Andric       MadeChange = true;
970b57cec5SDimitry Andric     }
980b57cec5SDimitry Andric   }
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   return MadeChange;
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric void AArch64BranchTargets::addBTI(MachineBasicBlock &MBB, bool CouldCall,
1040b57cec5SDimitry Andric                                   bool CouldJump) {
1050b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Adding BTI " << (CouldJump ? "j" : "")
1060b57cec5SDimitry Andric                     << (CouldCall ? "c" : "") << " to " << MBB.getName()
1070b57cec5SDimitry Andric                     << "\n");
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric   const AArch64InstrInfo *TII = static_cast<const AArch64InstrInfo *>(
1100b57cec5SDimitry Andric       MBB.getParent()->getSubtarget().getInstrInfo());
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   unsigned HintNum = 32;
1130b57cec5SDimitry Andric   if (CouldCall)
1140b57cec5SDimitry Andric     HintNum |= 2;
1150b57cec5SDimitry Andric   if (CouldJump)
1160b57cec5SDimitry Andric     HintNum |= 4;
1170b57cec5SDimitry Andric   assert(HintNum != 32 && "No target kinds!");
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   auto MBBI = MBB.begin();
1200b57cec5SDimitry Andric 
121*e837bb5cSDimitry Andric   // Skip the meta instuctions, those will be removed anyway.
122*e837bb5cSDimitry Andric   for (; MBBI != MBB.end() && MBBI->isMetaInstruction(); ++MBBI)
123*e837bb5cSDimitry Andric     ;
124*e837bb5cSDimitry Andric 
125*e837bb5cSDimitry Andric   // SCTLR_EL1.BT[01] is set to 0 by default which means
126*e837bb5cSDimitry Andric   // PACI[AB]SP are implicitly BTI C so no BTI C instruction is needed there.
127*e837bb5cSDimitry Andric   if (MBBI != MBB.end() && HintNum == 34 &&
128*e837bb5cSDimitry Andric       (MBBI->getOpcode() == AArch64::PACIASP ||
1290b57cec5SDimitry Andric        MBBI->getOpcode() == AArch64::PACIBSP))
1300b57cec5SDimitry Andric     return;
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric   BuildMI(MBB, MBB.begin(), MBB.findDebugLoc(MBB.begin()),
1330b57cec5SDimitry Andric           TII->get(AArch64::HINT))
1340b57cec5SDimitry Andric       .addImm(HintNum);
1350b57cec5SDimitry Andric }
136