WinCFGuard.cpp 4.7 KB

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