1*e038c9c4Sjoerg //===--- SanitizerMetadata.cpp - Ignored entities for sanitizers ----------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // Class which emits metadata consumed by sanitizer instrumentation passes.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg #include "SanitizerMetadata.h"
137330f729Sjoerg #include "CodeGenModule.h"
14*e038c9c4Sjoerg #include "clang/AST/Attr.h"
157330f729Sjoerg #include "clang/AST/Type.h"
16*e038c9c4Sjoerg #include "clang/Basic/SourceManager.h"
177330f729Sjoerg #include "llvm/ADT/StringRef.h"
187330f729Sjoerg #include "llvm/IR/Constants.h"
197330f729Sjoerg
207330f729Sjoerg using namespace clang;
217330f729Sjoerg using namespace CodeGen;
227330f729Sjoerg
SanitizerMetadata(CodeGenModule & CGM)237330f729Sjoerg SanitizerMetadata::SanitizerMetadata(CodeGenModule &CGM) : CGM(CGM) {}
247330f729Sjoerg
isAsanHwasanOrMemTag(const SanitizerSet & SS)257330f729Sjoerg static bool isAsanHwasanOrMemTag(const SanitizerSet& SS) {
267330f729Sjoerg return SS.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress |
277330f729Sjoerg SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress |
287330f729Sjoerg SanitizerKind::MemTag);
297330f729Sjoerg }
307330f729Sjoerg
reportGlobalToASan(llvm::GlobalVariable * GV,SourceLocation Loc,StringRef Name,QualType Ty,bool IsDynInit,bool IsExcluded)317330f729Sjoerg void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
327330f729Sjoerg SourceLocation Loc, StringRef Name,
337330f729Sjoerg QualType Ty, bool IsDynInit,
34*e038c9c4Sjoerg bool IsExcluded) {
357330f729Sjoerg if (!isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
367330f729Sjoerg return;
37*e038c9c4Sjoerg IsDynInit &= !CGM.isInNoSanitizeList(GV, Loc, Ty, "init");
38*e038c9c4Sjoerg IsExcluded |= CGM.isInNoSanitizeList(GV, Loc, Ty);
397330f729Sjoerg
407330f729Sjoerg llvm::Metadata *LocDescr = nullptr;
417330f729Sjoerg llvm::Metadata *GlobalName = nullptr;
427330f729Sjoerg llvm::LLVMContext &VMContext = CGM.getLLVMContext();
43*e038c9c4Sjoerg if (!IsExcluded) {
44*e038c9c4Sjoerg // Don't generate source location and global name if it is on
45*e038c9c4Sjoerg // the NoSanitizeList - it won't be instrumented anyway.
467330f729Sjoerg LocDescr = getLocationMetadata(Loc);
477330f729Sjoerg if (!Name.empty())
487330f729Sjoerg GlobalName = llvm::MDString::get(VMContext, Name);
497330f729Sjoerg }
507330f729Sjoerg
517330f729Sjoerg llvm::Metadata *GlobalMetadata[] = {
527330f729Sjoerg llvm::ConstantAsMetadata::get(GV), LocDescr, GlobalName,
537330f729Sjoerg llvm::ConstantAsMetadata::get(
547330f729Sjoerg llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsDynInit)),
557330f729Sjoerg llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
56*e038c9c4Sjoerg llvm::Type::getInt1Ty(VMContext), IsExcluded))};
577330f729Sjoerg
587330f729Sjoerg llvm::MDNode *ThisGlobal = llvm::MDNode::get(VMContext, GlobalMetadata);
597330f729Sjoerg llvm::NamedMDNode *AsanGlobals =
607330f729Sjoerg CGM.getModule().getOrInsertNamedMetadata("llvm.asan.globals");
617330f729Sjoerg AsanGlobals->addOperand(ThisGlobal);
627330f729Sjoerg }
637330f729Sjoerg
reportGlobalToASan(llvm::GlobalVariable * GV,const VarDecl & D,bool IsDynInit)647330f729Sjoerg void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
657330f729Sjoerg const VarDecl &D, bool IsDynInit) {
667330f729Sjoerg if (!isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
677330f729Sjoerg return;
687330f729Sjoerg std::string QualName;
697330f729Sjoerg llvm::raw_string_ostream OS(QualName);
707330f729Sjoerg D.printQualifiedName(OS);
717330f729Sjoerg
72*e038c9c4Sjoerg bool IsExcluded = false;
737330f729Sjoerg for (auto Attr : D.specific_attrs<NoSanitizeAttr>())
747330f729Sjoerg if (Attr->getMask() & SanitizerKind::Address)
75*e038c9c4Sjoerg IsExcluded = true;
767330f729Sjoerg reportGlobalToASan(GV, D.getLocation(), OS.str(), D.getType(), IsDynInit,
77*e038c9c4Sjoerg IsExcluded);
787330f729Sjoerg }
797330f729Sjoerg
disableSanitizerForGlobal(llvm::GlobalVariable * GV)807330f729Sjoerg void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
817330f729Sjoerg // For now, just make sure the global is not modified by the ASan
827330f729Sjoerg // instrumentation.
837330f729Sjoerg if (isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
847330f729Sjoerg reportGlobalToASan(GV, SourceLocation(), "", QualType(), false, true);
857330f729Sjoerg }
867330f729Sjoerg
disableSanitizerForInstruction(llvm::Instruction * I)877330f729Sjoerg void SanitizerMetadata::disableSanitizerForInstruction(llvm::Instruction *I) {
887330f729Sjoerg I->setMetadata(CGM.getModule().getMDKindID("nosanitize"),
897330f729Sjoerg llvm::MDNode::get(CGM.getLLVMContext(), None));
907330f729Sjoerg }
917330f729Sjoerg
getLocationMetadata(SourceLocation Loc)927330f729Sjoerg llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation Loc) {
937330f729Sjoerg PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
947330f729Sjoerg if (!PLoc.isValid())
957330f729Sjoerg return nullptr;
967330f729Sjoerg llvm::LLVMContext &VMContext = CGM.getLLVMContext();
977330f729Sjoerg llvm::Metadata *LocMetadata[] = {
987330f729Sjoerg llvm::MDString::get(VMContext, PLoc.getFilename()),
997330f729Sjoerg llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1007330f729Sjoerg llvm::Type::getInt32Ty(VMContext), PLoc.getLine())),
1017330f729Sjoerg llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1027330f729Sjoerg llvm::Type::getInt32Ty(VMContext), PLoc.getColumn())),
1037330f729Sjoerg };
1047330f729Sjoerg return llvm::MDNode::get(VMContext, LocMetadata);
1057330f729Sjoerg }
106