SanitizerMetadata.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //===--- SanitizerMetadata.cpp - Ignored entities for sanitizers ----------===//
  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. // Class which emits metadata consumed by sanitizer instrumentation passes.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "SanitizerMetadata.h"
  13. #include "CodeGenModule.h"
  14. #include "clang/AST/Attr.h"
  15. #include "clang/AST/Type.h"
  16. #include "clang/Basic/SourceManager.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/IR/Constants.h"
  19. using namespace clang;
  20. using namespace CodeGen;
  21. SanitizerMetadata::SanitizerMetadata(CodeGenModule &CGM) : CGM(CGM) {}
  22. static bool isAsanHwasanOrMemTag(const SanitizerSet& SS) {
  23. return SS.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress |
  24. SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress |
  25. SanitizerKind::MemTag);
  26. }
  27. void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
  28. SourceLocation Loc, StringRef Name,
  29. QualType Ty, bool IsDynInit,
  30. bool IsExcluded) {
  31. if (!isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
  32. return;
  33. IsDynInit &= !CGM.isInNoSanitizeList(GV, Loc, Ty, "init");
  34. IsExcluded |= CGM.isInNoSanitizeList(GV, Loc, Ty);
  35. llvm::Metadata *LocDescr = nullptr;
  36. llvm::Metadata *GlobalName = nullptr;
  37. llvm::LLVMContext &VMContext = CGM.getLLVMContext();
  38. if (!IsExcluded) {
  39. // Don't generate source location and global name if it is on
  40. // the NoSanitizeList - it won't be instrumented anyway.
  41. LocDescr = getLocationMetadata(Loc);
  42. if (!Name.empty())
  43. GlobalName = llvm::MDString::get(VMContext, Name);
  44. }
  45. llvm::Metadata *GlobalMetadata[] = {
  46. llvm::ConstantAsMetadata::get(GV), LocDescr, GlobalName,
  47. llvm::ConstantAsMetadata::get(
  48. llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsDynInit)),
  49. llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
  50. llvm::Type::getInt1Ty(VMContext), IsExcluded))};
  51. llvm::MDNode *ThisGlobal = llvm::MDNode::get(VMContext, GlobalMetadata);
  52. llvm::NamedMDNode *AsanGlobals =
  53. CGM.getModule().getOrInsertNamedMetadata("llvm.asan.globals");
  54. AsanGlobals->addOperand(ThisGlobal);
  55. }
  56. void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
  57. const VarDecl &D, bool IsDynInit) {
  58. if (!isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
  59. return;
  60. std::string QualName;
  61. llvm::raw_string_ostream OS(QualName);
  62. D.printQualifiedName(OS);
  63. bool IsExcluded = false;
  64. for (auto Attr : D.specific_attrs<NoSanitizeAttr>())
  65. if (Attr->getMask() & SanitizerKind::Address)
  66. IsExcluded = true;
  67. reportGlobalToASan(GV, D.getLocation(), OS.str(), D.getType(), IsDynInit,
  68. IsExcluded);
  69. }
  70. void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
  71. // For now, just make sure the global is not modified by the ASan
  72. // instrumentation.
  73. if (isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
  74. reportGlobalToASan(GV, SourceLocation(), "", QualType(), false, true);
  75. }
  76. void SanitizerMetadata::disableSanitizerForInstruction(llvm::Instruction *I) {
  77. I->setMetadata(CGM.getModule().getMDKindID("nosanitize"),
  78. llvm::MDNode::get(CGM.getLLVMContext(), None));
  79. }
  80. llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation Loc) {
  81. PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
  82. if (!PLoc.isValid())
  83. return nullptr;
  84. llvm::LLVMContext &VMContext = CGM.getLLVMContext();
  85. llvm::Metadata *LocMetadata[] = {
  86. llvm::MDString::get(VMContext, PLoc.getFilename()),
  87. llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
  88. llvm::Type::getInt32Ty(VMContext), PLoc.getLine())),
  89. llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
  90. llvm::Type::getInt32Ty(VMContext), PLoc.getColumn())),
  91. };
  92. return llvm::MDNode::get(VMContext, LocMetadata);
  93. }