WinCFGuard.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //===-- CodeGen/AsmPrinter/WinCFGuard.cpp - Control Flow Guard Impl ------===//
  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. // This file contains support for writing the metadata for Windows Control Flow
  10. // Guard, including address-taken functions and valid longjmp targets.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "WinCFGuard.h"
  14. #include "llvm/CodeGen/AsmPrinter.h"
  15. #include "llvm/CodeGen/MachineFunction.h"
  16. #include "llvm/CodeGen/MachineModuleInfo.h"
  17. #include "llvm/CodeGen/MachineOperand.h"
  18. #include "llvm/IR/Constants.h"
  19. #include "llvm/IR/Instructions.h"
  20. #include "llvm/IR/Metadata.h"
  21. #include "llvm/MC/MCAsmInfo.h"
  22. #include "llvm/MC/MCObjectFileInfo.h"
  23. #include "llvm/MC/MCStreamer.h"
  24. #include <vector>
  25. using namespace llvm;
  26. WinCFGuard::WinCFGuard(AsmPrinter *A) : Asm(A) {}
  27. WinCFGuard::~WinCFGuard() {}
  28. void WinCFGuard::endFunction(const MachineFunction *MF) {
  29. // Skip functions without any longjmp targets.
  30. if (MF->getLongjmpTargets().empty())
  31. return;
  32. // Copy the function's longjmp targets to a module-level list.
  33. llvm::append_range(LongjmpTargets, MF->getLongjmpTargets());
  34. }
  35. /// Returns true if this function's address is escaped in a way that might make
  36. /// it an indirect call target. Function::hasAddressTaken gives different
  37. /// results when a function is called directly with a function prototype
  38. /// mismatch, which requires a cast.
  39. static bool isPossibleIndirectCallTarget(const Function *F) {
  40. SmallVector<const Value *, 4> Users{F};
  41. while (!Users.empty()) {
  42. const Value *FnOrCast = Users.pop_back_val();
  43. for (const Use &U : FnOrCast->uses()) {
  44. const User *FnUser = U.getUser();
  45. if (isa<BlockAddress>(FnUser))
  46. continue;
  47. if (const auto *Call = dyn_cast<CallBase>(FnUser)) {
  48. if (!Call->isCallee(&U))
  49. return true;
  50. } else if (isa<Instruction>(FnUser)) {
  51. // Consider any other instruction to be an escape. This has some weird
  52. // consequences like no-op intrinsics being an escape or a store *to* a
  53. // function address being an escape.
  54. return true;
  55. } else if (const auto *C = dyn_cast<Constant>(FnUser)) {
  56. // If this is a constant pointer cast of the function, don't consider
  57. // this escape. Analyze the uses of the cast as well. This ensures that
  58. // direct calls with mismatched prototypes don't end up in the CFG
  59. // table. Consider other constants, such as vtable initializers, to
  60. // escape the function.
  61. if (C->stripPointerCasts() == F)
  62. Users.push_back(FnUser);
  63. else
  64. return true;
  65. }
  66. }
  67. }
  68. return false;
  69. }
  70. MCSymbol *WinCFGuard::lookupImpSymbol(const MCSymbol *Sym) {
  71. if (Sym->getName().startswith("__imp_"))
  72. return nullptr;
  73. return Asm->OutContext.lookupSymbol(Twine("__imp_") + Sym->getName());
  74. }
  75. void WinCFGuard::endModule() {
  76. const Module *M = Asm->MMI->getModule();
  77. std::vector<const MCSymbol *> GFIDsEntries;
  78. std::vector<const MCSymbol *> GIATsEntries;
  79. for (const Function &F : *M) {
  80. if (isPossibleIndirectCallTarget(&F)) {
  81. // If F is a dllimport and has an "__imp_" symbol already defined, add the
  82. // "__imp_" symbol to the .giats section.
  83. if (F.hasDLLImportStorageClass()) {
  84. if (MCSymbol *impSym = lookupImpSymbol(Asm->getSymbol(&F))) {
  85. GIATsEntries.push_back(impSym);
  86. }
  87. }
  88. // Add the function's symbol to the .gfids section.
  89. // Note: For dllimport functions, MSVC sometimes does not add this symbol
  90. // to the .gfids section, but only adds the corresponding "__imp_" symbol
  91. // to the .giats section. Here we always add the symbol to the .gfids
  92. // section, since this does not introduce security risks.
  93. GFIDsEntries.push_back(Asm->getSymbol(&F));
  94. }
  95. }
  96. if (GFIDsEntries.empty() && GIATsEntries.empty() && LongjmpTargets.empty())
  97. return;
  98. // Emit the symbol index of each GFIDs entry to form the .gfids section.
  99. auto &OS = *Asm->OutStreamer;
  100. OS.SwitchSection(Asm->OutContext.getObjectFileInfo()->getGFIDsSection());
  101. for (const MCSymbol *S : GFIDsEntries)
  102. OS.EmitCOFFSymbolIndex(S);
  103. // Emit the symbol index of each GIATs entry to form the .giats section.
  104. OS.SwitchSection(Asm->OutContext.getObjectFileInfo()->getGIATsSection());
  105. for (const MCSymbol *S : GIATsEntries) {
  106. OS.EmitCOFFSymbolIndex(S);
  107. }
  108. // Emit the symbol index of each longjmp target to form the .gljmp section.
  109. OS.SwitchSection(Asm->OutContext.getObjectFileInfo()->getGLJMPSection());
  110. for (const MCSymbol *S : LongjmpTargets) {
  111. OS.EmitCOFFSymbolIndex(S);
  112. }
  113. }