LLVM  15.0.0git
EHContGuardCatchret.cpp
Go to the documentation of this file.
1 //===-- EHContGuardCatchret.cpp - Catchret target symbols -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file contains a machine function pass to insert a symbol before each
11 /// valid catchret target and store this in the MachineFunction's
12 /// CatchRetTargets vector. This will be used to emit the table of valid targets
13 /// used by EHCont Guard.
14 ///
15 //===----------------------------------------------------------------------===//
16 
17 #include "llvm/ADT/Statistic.h"
21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/InitializePasses.h"
23 
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "ehcontguard-catchret"
27 
28 STATISTIC(EHContGuardCatchretTargets,
29  "Number of EHCont Guard catchret targets");
30 
31 namespace {
32 
33 /// MachineFunction pass to insert a symbol before each valid catchret target
34 /// and store these in the MachineFunction's CatchRetTargets vector.
35 class EHContGuardCatchret : public MachineFunctionPass {
36 public:
37  static char ID;
38 
39  EHContGuardCatchret() : MachineFunctionPass(ID) {
41  }
42 
43  StringRef getPassName() const override {
44  return "EH Cont Guard catchret targets";
45  }
46 
47  bool runOnMachineFunction(MachineFunction &MF) override;
48 };
49 
50 } // end anonymous namespace
51 
53 
54 INITIALIZE_PASS(EHContGuardCatchret, "EHContGuardCatchret",
55  "Insert symbols at valid catchret targets for /guard:ehcont",
56  false, false)
58  return new EHContGuardCatchret();
59 }
60 
61 bool EHContGuardCatchret::runOnMachineFunction(MachineFunction &MF) {
62 
63  // Skip modules for which the ehcontguard flag is not set.
64  if (!MF.getMMI().getModule()->getModuleFlag("ehcontguard"))
65  return false;
66 
67  // Skip functions that do not have catchret
68  if (!MF.hasEHCatchret())
69  return false;
70 
71  bool Result = false;
72 
73  for (MachineBasicBlock &MBB : MF) {
74  if (MBB.isEHCatchretTarget()) {
75  MF.addCatchretTarget(MBB.getEHCatchretSymbol());
76  EHContGuardCatchretTargets++;
77  Result = true;
78  }
79  }
80 
81  return Result;
82 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
INITIALIZE_PASS
INITIALIZE_PASS(EHContGuardCatchret, "EHContGuardCatchret", "Insert symbols at valid catchret targets for /guard:ehcont", false, false) FunctionPass *llvm
Definition: EHContGuardCatchret.cpp:54
Statistic.h
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
llvm::MachineBasicBlock::getEHCatchretSymbol
MCSymbol * getEHCatchretSymbol() const
Return the EHCatchret Symbol for this basic block.
Definition: MachineBasicBlock.cpp:88
MachineBasicBlock.h
llvm::Module::getModuleFlag
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Definition: Module.cpp:322
llvm::createEHContGuardCatchretPass
FunctionPass * createEHContGuardCatchretPass()
Creates EHContGuard catchret target identification pass.
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
llvm::MachineFunction::hasEHCatchret
bool hasEHCatchret() const
Definition: MachineFunction.h:1092
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
llvm::MachineBasicBlock::isEHCatchretTarget
bool isEHCatchretTarget() const
Returns true if this is a target block of a catchret.
Definition: MachineBasicBlock.h:569
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:94
Passes.h
llvm::MachineFunction::getMMI
MachineModuleInfo & getMMI() const
Definition: MachineFunction.h:607
MachineFunctionPass.h
MachineModuleInfo.h
llvm::MachineFunction
Definition: MachineFunction.h:257
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::initializeEHContGuardCatchretPass
void initializeEHContGuardCatchretPass(PassRegistry &)
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
InitializePasses.h
llvm::MachineModuleInfo::getModule
const Module * getModule() const
Definition: MachineModuleInfo.h:151
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38