SanitizerMetadata.cpp 4.2 KB

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