CFGuardLongjmp.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //===-- CFGuardLongjmp.cpp - Longjmp symbols for CFGuard --------*- 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 after each
  11. /// call to _setjmp and store this in the MachineFunction's LongjmpTargets
  12. /// vector. This will be used to emit the table of valid longjmp targets used
  13. /// by Control Flow Guard.
  14. ///
  15. //===----------------------------------------------------------------------===//
  16. #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/InitializePasses.h"
  24. using namespace llvm;
  25. #define DEBUG_TYPE "cfguard-longjmp"
  26. STATISTIC(CFGuardLongjmpTargets,
  27. "Number of Control Flow Guard longjmp targets");
  28. namespace {
  29. /// MachineFunction pass to insert a symbol after each call to _setjmp and store
  30. /// this in the MachineFunction's LongjmpTargets vector.
  31. class CFGuardLongjmp : public MachineFunctionPass {
  32. public:
  33. static char ID;
  34. CFGuardLongjmp() : MachineFunctionPass(ID) {
  35. initializeCFGuardLongjmpPass(*PassRegistry::getPassRegistry());
  36. }
  37. StringRef getPassName() const override {
  38. return "Control Flow Guard longjmp targets";
  39. }
  40. bool runOnMachineFunction(MachineFunction &MF) override;
  41. };
  42. } // end anonymous namespace
  43. char CFGuardLongjmp::ID = 0;
  44. INITIALIZE_PASS(CFGuardLongjmp, "CFGuardLongjmp",
  45. "Insert symbols at valid longjmp targets for /guard:cf", false,
  46. false)
  47. FunctionPass *llvm::createCFGuardLongjmpPass() { return new CFGuardLongjmp(); }
  48. bool CFGuardLongjmp::runOnMachineFunction(MachineFunction &MF) {
  49. // Skip modules for which the cfguard flag is not set.
  50. if (!MF.getMMI().getModule()->getModuleFlag("cfguard"))
  51. return false;
  52. // Skip functions that do not have calls to _setjmp.
  53. if (!MF.getFunction().callsFunctionThatReturnsTwice())
  54. return false;
  55. SmallVector<MachineInstr *, 8> SetjmpCalls;
  56. // Iterate over all instructions in the function and add calls to functions
  57. // that return twice to the list of targets.
  58. for (MachineBasicBlock &MBB : MF) {
  59. for (MachineInstr &MI : MBB) {
  60. // Skip instructions that are not calls.
  61. if (!MI.isCall() || MI.getNumOperands() < 1)
  62. continue;
  63. // Iterate over operands to find calls to global functions.
  64. for (MachineOperand &MO : MI.operands()) {
  65. if (!MO.isGlobal())
  66. continue;
  67. auto *F = dyn_cast<Function>(MO.getGlobal());
  68. if (!F)
  69. continue;
  70. // If the instruction calls a function that returns twice, add
  71. // it to the list of targets.
  72. if (F->hasFnAttribute(Attribute::ReturnsTwice)) {
  73. SetjmpCalls.push_back(&MI);
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. if (SetjmpCalls.empty())
  80. return false;
  81. unsigned SetjmpNum = 0;
  82. // For each possible target, create a new symbol and insert it immediately
  83. // after the call to setjmp. Add this symbol to the MachineFunction's list
  84. // of longjmp targets.
  85. for (MachineInstr *Setjmp : SetjmpCalls) {
  86. SmallString<128> SymbolName;
  87. raw_svector_ostream(SymbolName) << "$cfgsj_" << MF.getName() << SetjmpNum++;
  88. MCSymbol *SjSymbol = MF.getContext().getOrCreateSymbol(SymbolName);
  89. Setjmp->setPostInstrSymbol(MF, SjSymbol);
  90. MF.addLongjmpTarget(SjSymbol);
  91. CFGuardLongjmpTargets++;
  92. }
  93. return true;
  94. }