Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/CFGuardLongjmp.cpp
35234 views
//===-- CFGuardLongjmp.cpp - Longjmp symbols for CFGuard --------*- C++ -*-===//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/// \file9/// This file contains a machine function pass to insert a symbol after each10/// call to _setjmp and store this in the MachineFunction's LongjmpTargets11/// vector. This will be used to emit the table of valid longjmp targets used12/// by Control Flow Guard.13///14//===----------------------------------------------------------------------===//1516#include "llvm/ADT/Statistic.h"17#include "llvm/CodeGen/MachineBasicBlock.h"18#include "llvm/CodeGen/MachineFunctionPass.h"19#include "llvm/CodeGen/MachineInstr.h"20#include "llvm/CodeGen/MachineModuleInfo.h"21#include "llvm/CodeGen/MachineOperand.h"22#include "llvm/CodeGen/Passes.h"23#include "llvm/IR/Module.h"24#include "llvm/InitializePasses.h"2526using namespace llvm;2728#define DEBUG_TYPE "cfguard-longjmp"2930STATISTIC(CFGuardLongjmpTargets,31"Number of Control Flow Guard longjmp targets");3233namespace {3435/// MachineFunction pass to insert a symbol after each call to _setjmp and store36/// this in the MachineFunction's LongjmpTargets vector.37class CFGuardLongjmp : public MachineFunctionPass {38public:39static char ID;4041CFGuardLongjmp() : MachineFunctionPass(ID) {42initializeCFGuardLongjmpPass(*PassRegistry::getPassRegistry());43}4445StringRef getPassName() const override {46return "Control Flow Guard longjmp targets";47}4849bool runOnMachineFunction(MachineFunction &MF) override;50};5152} // end anonymous namespace5354char CFGuardLongjmp::ID = 0;5556INITIALIZE_PASS(CFGuardLongjmp, "CFGuardLongjmp",57"Insert symbols at valid longjmp targets for /guard:cf", false,58false)59FunctionPass *llvm::createCFGuardLongjmpPass() { return new CFGuardLongjmp(); }6061bool CFGuardLongjmp::runOnMachineFunction(MachineFunction &MF) {6263// Skip modules for which the cfguard flag is not set.64if (!MF.getFunction().getParent()->getModuleFlag("cfguard"))65return false;6667// Skip functions that do not have calls to _setjmp.68if (!MF.getFunction().callsFunctionThatReturnsTwice())69return false;7071SmallVector<MachineInstr *, 8> SetjmpCalls;7273// Iterate over all instructions in the function and add calls to functions74// that return twice to the list of targets.75for (MachineBasicBlock &MBB : MF) {76for (MachineInstr &MI : MBB) {7778// Skip instructions that are not calls.79if (!MI.isCall() || MI.getNumOperands() < 1)80continue;8182// Iterate over operands to find calls to global functions.83for (MachineOperand &MO : MI.operands()) {84if (!MO.isGlobal())85continue;8687auto *F = dyn_cast<Function>(MO.getGlobal());88if (!F)89continue;9091// If the instruction calls a function that returns twice, add92// it to the list of targets.93if (F->hasFnAttribute(Attribute::ReturnsTwice)) {94SetjmpCalls.push_back(&MI);95break;96}97}98}99}100101if (SetjmpCalls.empty())102return false;103104unsigned SetjmpNum = 0;105106// For each possible target, create a new symbol and insert it immediately107// after the call to setjmp. Add this symbol to the MachineFunction's list108// of longjmp targets.109for (MachineInstr *Setjmp : SetjmpCalls) {110SmallString<128> SymbolName;111raw_svector_ostream(SymbolName) << "$cfgsj_" << MF.getName() << SetjmpNum++;112MCSymbol *SjSymbol = MF.getContext().getOrCreateSymbol(SymbolName);113114Setjmp->setPostInstrSymbol(MF, SjSymbol);115MF.addLongjmpTarget(SjSymbol);116CFGuardLongjmpTargets++;117}118119return true;120}121122123