LLVM  14.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"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/InitializePasses.h"
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "ehcontguard-catchret"
29 
30 STATISTIC(EHContGuardCatchretTargets,
31  "Number of EHCont Guard catchret targets");
32 
33 namespace {
34 
35 /// MachineFunction pass to insert a symbol before each valid catchret target
36 /// and store these in the MachineFunction's CatchRetTargets vector.
37 class EHContGuardCatchret : public MachineFunctionPass {
38 public:
39  static char ID;
40 
41  EHContGuardCatchret() : MachineFunctionPass(ID) {
43  }
44 
45  StringRef getPassName() const override {
46  return "EH Cont Guard catchret targets";
47  }
48 
49  bool runOnMachineFunction(MachineFunction &MF) override;
50 };
51 
52 } // end anonymous namespace
53 
55 
56 INITIALIZE_PASS(EHContGuardCatchret, "EHContGuardCatchret",
57  "Insert symbols at valid catchret targets for /guard:ehcont",
58  false, false)
60  return new EHContGuardCatchret();
61 }
62 
63 bool EHContGuardCatchret::runOnMachineFunction(MachineFunction &MF) {
64 
65  // Skip modules for which the ehcontguard flag is not set.
66  if (!MF.getMMI().getModule()->getModuleFlag("ehcontguard"))
67  return false;
68 
69  // Skip functions that do not have catchret
70  if (!MF.hasEHCatchret())
71  return false;
72 
73  bool Result = false;
74 
75  for (MachineBasicBlock &MBB : MF) {
76  if (MBB.isEHCatchretTarget()) {
77  MF.addCatchretTarget(MBB.getEHCatchretSymbol());
78  EHContGuardCatchretTargets++;
79  Result = true;
80  }
81  }
82 
83  return Result;
84 }
MachineInstr.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
INITIALIZE_PASS
INITIALIZE_PASS(EHContGuardCatchret, "EHContGuardCatchret", "Insert symbols at valid catchret targets for /guard:ehcont", false, false) FunctionPass *llvm
Definition: EHContGuardCatchret.cpp:56
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:91
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:325
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:1053
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:546
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
Passes.h
llvm::MachineFunction::getMMI
MachineModuleInfo & getMMI() const
Definition: MachineFunction.h:585
MachineFunctionPass.h
MachineModuleInfo.h
llvm::MachineFunction
Definition: MachineFunction.h:234
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::initializeEHContGuardCatchretPass
void initializeEHContGuardCatchretPass(PassRegistry &)
MachineOperand.h
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
InitializePasses.h
llvm::MachineModuleInfo::getModule
const Module * getModule() const
Definition: MachineModuleInfo.h:174
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38