Path: blob/main/contrib/llvm-project/llvm/lib/Target/ARM/ARMBranchTargets.cpp
35294 views
//===-- ARMBranchTargets.cpp -- Harden code using v8.1-M BTI extension -----==//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This pass inserts BTI instructions at the start of every function and basic9// block which could be indirectly called. The hardware will (when enabled)10// trap when an indirect branch or call instruction targets an instruction11// which is not a valid BTI instruction. This is intended to guard against12// control-flow hijacking attacks.13//14//===----------------------------------------------------------------------===//1516#include "ARM.h"17#include "ARMInstrInfo.h"18#include "ARMMachineFunctionInfo.h"19#include "llvm/CodeGen/MachineFunctionPass.h"20#include "llvm/CodeGen/MachineInstrBuilder.h"21#include "llvm/CodeGen/MachineJumpTableInfo.h"22#include "llvm/CodeGen/MachineModuleInfo.h"23#include "llvm/Support/Debug.h"2425using namespace llvm;2627#define DEBUG_TYPE "arm-branch-targets"28#define ARM_BRANCH_TARGETS_NAME "ARM Branch Targets"2930namespace {31class ARMBranchTargets : public MachineFunctionPass {32public:33static char ID;34ARMBranchTargets() : MachineFunctionPass(ID) {}35void getAnalysisUsage(AnalysisUsage &AU) const override;36bool runOnMachineFunction(MachineFunction &MF) override;37StringRef getPassName() const override { return ARM_BRANCH_TARGETS_NAME; }3839private:40void addBTI(const ARMInstrInfo &TII, MachineBasicBlock &MBB, bool IsFirstBB);41};42} // end anonymous namespace4344char ARMBranchTargets::ID = 0;4546INITIALIZE_PASS(ARMBranchTargets, "arm-branch-targets", ARM_BRANCH_TARGETS_NAME,47false, false)4849void ARMBranchTargets::getAnalysisUsage(AnalysisUsage &AU) const {50AU.setPreservesCFG();51MachineFunctionPass::getAnalysisUsage(AU);52}5354FunctionPass *llvm::createARMBranchTargetsPass() {55return new ARMBranchTargets();56}5758bool ARMBranchTargets::runOnMachineFunction(MachineFunction &MF) {59if (!MF.getInfo<ARMFunctionInfo>()->branchTargetEnforcement())60return false;6162LLVM_DEBUG(dbgs() << "********** ARM Branch Targets **********\n"63<< "********** Function: " << MF.getName() << '\n');64const ARMInstrInfo &TII =65*static_cast<const ARMInstrInfo *>(MF.getSubtarget().getInstrInfo());6667bool MadeChange = false;68for (MachineBasicBlock &MBB : MF) {69bool IsFirstBB = &MBB == &MF.front();7071// Every function can potentially be called indirectly (even if it has72// static linkage, due to linker-generated veneers).73// If the block itself is address-taken, or is an exception landing pad, it74// could be indirectly branched to.75// Jump tables only emit indirect jumps (JUMPTABLE_ADDRS) in ARM or Thumb176// modes. These modes do not support PACBTI. As a result, BTI instructions77// are not added in the destination blocks.7879if (IsFirstBB || MBB.hasAddressTaken() || MBB.isEHPad()) {80addBTI(TII, MBB, IsFirstBB);81MadeChange = true;82}83}8485return MadeChange;86}8788/// Insert a BTI/PACBTI instruction into a given basic block \c MBB. If89/// \c IsFirstBB is true (meaning that this is the first BB in a function) try90/// to find a PAC instruction and replace it with PACBTI. Otherwise just insert91/// a BTI instruction.92/// The point of insertion is in the beginning of the BB, immediately after meta93/// instructions (such labels in exception handling landing pads).94void ARMBranchTargets::addBTI(const ARMInstrInfo &TII, MachineBasicBlock &MBB,95bool IsFirstBB) {96// Which instruction to insert: BTI or PACBTI97unsigned OpCode = ARM::t2BTI;98unsigned MIFlags = 0;99100// Skip meta instructions, including EH labels101auto MBBI = llvm::find_if_not(MBB.instrs(), [](const MachineInstr &MI) {102return MI.isMetaInstruction();103});104105// If this is the first BB in a function, check if it starts with a PAC106// instruction and in that case remove the PAC instruction.107if (IsFirstBB) {108if (MBBI != MBB.instr_end() && MBBI->getOpcode() == ARM::t2PAC) {109LLVM_DEBUG(dbgs() << "Removing a 'PAC' instr from BB '" << MBB.getName()110<< "' to replace with PACBTI\n");111OpCode = ARM::t2PACBTI;112MIFlags = MachineInstr::FrameSetup;113auto NextMBBI = std::next(MBBI);114MBBI->eraseFromParent();115MBBI = NextMBBI;116}117}118119LLVM_DEBUG(dbgs() << "Inserting a '"120<< (OpCode == ARM::t2BTI ? "BTI" : "PACBTI")121<< "' instr into BB '" << MBB.getName() << "'\n");122// Finally, insert a new instruction (either PAC or PACBTI)123BuildMI(MBB, MBBI, MBB.findDebugLoc(MBBI), TII.get(OpCode))124.setMIFlags(MIFlags);125}126127128