LLVM  8.0.1
Macros | Functions | Variables
HWAddressSanitizer.cpp File Reference

This file is a part of HWAddressSanitizer, an address sanity checker based on tagged addressing. More...

#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Triple.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/InstVisitor.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
#include "llvm/Pass.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Instrumentation.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#include "llvm/Transforms/Utils/PromoteMemToReg.h"
#include <sstream>
Include dependency graph for HWAddressSanitizer.cpp:

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "hwasan"
 

Functions

 INITIALIZE_PASS_BEGIN (HWAddressSanitizer, "hwasan", "HWAddressSanitizer: detect memory bugs using tagged addressing.", false, false) INITIALIZE_PASS_END(HWAddressSanitizer
 
static unsigned getPointerOperandIndex (Instruction *I)
 
static size_t TypeSizeToSizeIndex (uint32_t TypeSize)
 
static uint64_t getAllocaSizeInBytes (const AllocaInst &AI)
 
static unsigned RetagMask (unsigned AllocaNo)
 

Variables

static const char *const kHwasanModuleCtorName = "hwasan.module_ctor"
 
static const char *const kHwasanInitName = "__hwasan_init"
 
static const char *const kHwasanShadowMemoryDynamicAddress
 
static const size_t kNumberOfAccessSizes = 5
 
static const size_t kDefaultShadowScale = 4
 
static const uint64_t kDynamicShadowSentinel
 
static const unsigned kPointerTagShift = 56
 
static const unsigned kShadowBaseAlignment = 32
 
static cl::opt< std::string > ClMemoryAccessCallbackPrefix ("hwasan-memory-access-callback-prefix", cl::desc("Prefix for memory access callbacks"), cl::Hidden, cl::init("__hwasan_"))
 
static cl::opt< boolClInstrumentWithCalls ("hwasan-instrument-with-calls", cl::desc("instrument reads and writes with callbacks"), cl::Hidden, cl::init(false))
 
static cl::opt< boolClInstrumentReads ("hwasan-instrument-reads", cl::desc("instrument read instructions"), cl::Hidden, cl::init(true))
 
static cl::opt< boolClInstrumentWrites ("hwasan-instrument-writes", cl::desc("instrument write instructions"), cl::Hidden, cl::init(true))
 
static cl::opt< boolClInstrumentAtomics ("hwasan-instrument-atomics", cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden, cl::init(true))
 
static cl::opt< boolClRecover ("hwasan-recover", cl::desc("Enable recovery mode (continue-after-error)."), cl::Hidden, cl::init(false))
 
static cl::opt< boolClInstrumentStack ("hwasan-instrument-stack", cl::desc("instrument stack (allocas)"), cl::Hidden, cl::init(true))
 
static cl::opt< boolClUARRetagToZero ("hwasan-uar-retag-to-zero", cl::desc("Clear alloca tags before returning from the function to allow " "non-instrumented and instrumented function calls mix. When set " "to false, allocas are retagged before returning from the " "function to detect use after return."), cl::Hidden, cl::init(true))
 
static cl::opt< boolClGenerateTagsWithCalls ("hwasan-generate-tags-with-calls", cl::desc("generate new tags with runtime library calls"), cl::Hidden, cl::init(false))
 
static cl::opt< int > ClMatchAllTag ("hwasan-match-all-tag", cl::desc("don't report bad accesses via pointers with this tag"), cl::Hidden, cl::init(-1))
 
static cl::opt< boolClEnableKhwasan ("hwasan-kernel", cl::desc("Enable KernelHWAddressSanitizer instrumentation"), cl::Hidden, cl::init(false))
 
static cl::opt< unsigned long long > ClMappingOffset ("hwasan-mapping-offset", cl::desc("HWASan shadow mapping offset [EXPERIMENTAL]"), cl::Hidden, cl::init(0))
 
static cl::opt< boolClWithIfunc ("hwasan-with-ifunc", cl::desc("Access dynamic shadow through an ifunc global on " "platforms that support this"), cl::Hidden, cl::init(false))
 
static cl::opt< boolClWithTls ("hwasan-with-tls", cl::desc("Access dynamic shadow through an thread-local pointer on " "platforms that support this"), cl::Hidden, cl::init(true))
 
static cl::opt< boolClRecordStackHistory ("hwasan-record-stack-history", cl::desc("Record stack frames with tagged allocations " "in a thread-local ring buffer"), cl::Hidden, cl::init(true))
 
static cl::opt< boolClCreateFrameDescriptions ("hwasan-create-frame-descriptions", cl::desc("create static frame descriptions"), cl::Hidden, cl::init(true))
 
static cl::opt< boolClInstrumentMemIntrinsics ("hwasan-instrument-mem-intrinsics", cl::desc("instrument memory intrinsics"), cl::Hidden, cl::init(true))
 
 hwasan
 
HWAddressSanitizer __pad0__
 
HWAddressSanitizer false
 

Detailed Description

This file is a part of HWAddressSanitizer, an address sanity checker based on tagged addressing.

Definition in file HWAddressSanitizer.cpp.

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "hwasan"

Definition at line 51 of file HWAddressSanitizer.cpp.

Function Documentation

◆ getAllocaSizeInBytes()

static uint64_t getAllocaSizeInBytes ( const AllocaInst AI)
static

◆ getPointerOperandIndex()

static unsigned getPointerOperandIndex ( Instruction I)
static

Definition at line 479 of file HWAddressSanitizer.cpp.

References llvm::report_fatal_error(), and SI.

Referenced by TypeSizeToSizeIndex().

◆ INITIALIZE_PASS_BEGIN()

INITIALIZE_PASS_BEGIN ( HWAddressSanitizer  ,
"hwasan"  ,
"HWAddressSanitizer: detect memory bugs using tagged addressing."  ,
false  ,
false   
)

◆ RetagMask()

static unsigned RetagMask ( unsigned  AllocaNo)
static

Definition at line 672 of file HWAddressSanitizer.cpp.

References llvm::PHINode::addIncoming(), llvm::appendToCompilerUsed(), assert(), ClCreateFrameDescriptions, ClEnableKhwasan, ClGenerateTagsWithCalls, ClInstrumentStack, ClInstrumentWithCalls, ClMappingOffset, ClRecordStackHistory, ClUARRetagToZero, ClWithIfunc, ClWithTls, llvm::IRBuilder< T, Inserter >::CreateAdd(), llvm::IRBuilder< T, Inserter >::CreateAnd(), llvm::IRBuilder< T, Inserter >::CreateAShr(), llvm::MDBuilder::createBranchWeights(), llvm::IRBuilder< T, Inserter >::CreateCall(), llvm::IRBuilder< T, Inserter >::CreateConstGEP1_32(), llvm::IRBuilder< T, Inserter >::CreateICmpEQ(), llvm::IRBuilder< T, Inserter >::CreateIntToPtr(), llvm::IRBuilder< T, Inserter >::CreateLoad(), llvm::IRBuilder< T, Inserter >::CreateLShr(), llvm::IRBuilder< T, Inserter >::CreateOr(), llvm::IRBuilder< T, Inserter >::CreatePHI(), llvm::IRBuilder< T, Inserter >::CreatePointerCast(), llvm::createPrivateGlobalForString(), llvm::IRBuilder< T, Inserter >::CreatePtrToInt(), llvm::IRBuilder< T, Inserter >::CreateShl(), llvm::IRBuilder< T, Inserter >::CreateStore(), llvm::IRBuilder< T, Inserter >::CreateXor(), llvm::IRBuilder< T, Inserter >::CreateZExt(), llvm::dbgs(), llvm::SmallVectorBase::empty(), F(), llvm::Intrinsic::frameaddress, llvm::ConstantInt::get(), llvm::StructType::get(), llvm::ConstantStruct::get(), llvm::AllocaInst::getAlignment(), getAllocaSizeInBytes(), llvm::AllocaInst::getAllocatedType(), llvm::Intrinsic::getDeclaration(), llvm::Function::getFnAttribute(), llvm::IRBuilderBase::GetInsertBlock(), llvm::IRBuilderBase::getInt32Ty(), llvm::Value::getName(), llvm::ilist_node_with_parent< NodeTy, ParentTy, Options >::getNextNode(), llvm::Constant::getNullValue(), llvm::GetOrCreateFunctionComdat(), llvm::Instruction::getParent(), llvm::BasicBlock::getParent(), llvm::GlobalValue::getParent(), llvm::Type::getPointerTo(), llvm::AllocaInst::getType(), llvm::Value::getType(), llvm::GlobalValue::getType(), llvm::Use::getUser(), llvm::Attribute::getValueAsString(), llvm::Function::hasFnAttribute(), llvm::Value::hasName(), llvm::cl::init(), llvm::isAllocaPromotable(), llvm::Type::isSized(), llvm::AllocaInst::isStaticAlloca(), llvm::AllocaInst::isSwiftError(), llvm::AllocaInst::isUsedWithInAlloca(), llvm::itostr(), kDefaultShadowScale, kDynamicShadowSentinel, kPointerTagShift, LLVM_DEBUG, N, Name, llvm::GlobalValue::PrivateLinkage, llvm::SmallVectorTemplateBase< T >::push_back(), runOnFunction(), llvm::Attribute::SanitizeHWAddress, llvm::Use::set(), llvm::AllocaInst::setAlignment(), llvm::IRBuilderBase::SetInsertPoint(), llvm::Value::setName(), llvm::SmallVectorBase::size(), llvm::SplitBlockAndInsertIfThen(), llvm::StringRef::str(), llvm::Tag, llvm::Intrinsic::thread_pointer, llvm::Value::use_begin(), and llvm::Value::use_end().

◆ TypeSizeToSizeIndex()

static size_t TypeSizeToSizeIndex ( uint32_t  TypeSize)
static

Variable Documentation

◆ __pad0__

HWAddressSanitizer __pad0__

Definition at line 284 of file HWAddressSanitizer.cpp.

◆ ClCreateFrameDescriptions

cl::opt<bool> ClCreateFrameDescriptions("hwasan-create-frame-descriptions", cl::desc("create static frame descriptions"), cl::Hidden, cl::init(true))
static

Referenced by RetagMask().

◆ ClEnableKhwasan

cl::opt<bool> ClEnableKhwasan("hwasan-kernel", cl::desc("Enable KernelHWAddressSanitizer instrumentation"), cl::Hidden, cl::init(false))
static

Referenced by RetagMask().

◆ ClGenerateTagsWithCalls

cl::opt<bool> ClGenerateTagsWithCalls("hwasan-generate-tags-with-calls", cl::desc("generate new tags with runtime library calls"), cl::Hidden, cl::init(false))
static

Referenced by RetagMask().

◆ ClInstrumentAtomics

cl::opt<bool> ClInstrumentAtomics("hwasan-instrument-atomics", cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden, cl::init(true))
static

◆ ClInstrumentMemIntrinsics

cl::opt<bool> ClInstrumentMemIntrinsics("hwasan-instrument-mem-intrinsics", cl::desc("instrument memory intrinsics"), cl::Hidden, cl::init(true))
static

Referenced by TypeSizeToSizeIndex().

◆ ClInstrumentReads

cl::opt<bool> ClInstrumentReads("hwasan-instrument-reads", cl::desc("instrument read instructions"), cl::Hidden, cl::init(true))
static

◆ ClInstrumentStack

cl::opt<bool> ClInstrumentStack("hwasan-instrument-stack", cl::desc("instrument stack (allocas)"), cl::Hidden, cl::init(true))
static

Referenced by RetagMask().

◆ ClInstrumentWithCalls

cl::opt<bool> ClInstrumentWithCalls("hwasan-instrument-with-calls", cl::desc("instrument reads and writes with callbacks"), cl::Hidden, cl::init(false))
static

◆ ClInstrumentWrites

cl::opt<bool> ClInstrumentWrites("hwasan-instrument-writes", cl::desc("instrument write instructions"), cl::Hidden, cl::init(true))
static

◆ ClMappingOffset

cl::opt<unsigned long long> ClMappingOffset("hwasan-mapping-offset", cl::desc("HWASan shadow mapping offset [EXPERIMENTAL]"), cl::Hidden, cl::init(0))
static

Referenced by RetagMask().

◆ ClMatchAllTag

cl::opt<int> ClMatchAllTag("hwasan-match-all-tag", cl::desc("don't report bad accesses via pointers with this tag"), cl::Hidden, cl::init(-1))
static

Referenced by TypeSizeToSizeIndex().

◆ ClMemoryAccessCallbackPrefix

cl::opt<std::string> ClMemoryAccessCallbackPrefix("hwasan-memory-access-callback-prefix", cl::desc("Prefix for memory access callbacks"), cl::Hidden, cl::init("__hwasan_"))
static

◆ ClRecordStackHistory

cl::opt<bool> ClRecordStackHistory("hwasan-record-stack-history", cl::desc("Record stack frames with tagged allocations " "in a thread-local ring buffer"), cl::Hidden, cl::init(true))
static

Referenced by RetagMask().

◆ ClRecover

cl::opt<bool> ClRecover("hwasan-recover", cl::desc("Enable recovery mode (continue-after-error)."), cl::Hidden, cl::init(false))
static

◆ ClUARRetagToZero

cl::opt<bool> ClUARRetagToZero("hwasan-uar-retag-to-zero", cl::desc("Clear alloca tags before returning from the function to allow " "non-instrumented and instrumented function calls mix. When set " "to false, allocas are retagged before returning from the " "function to detect use after return."), cl::Hidden, cl::init(true))
static

Referenced by RetagMask().

◆ ClWithIfunc

cl::opt<bool> ClWithIfunc("hwasan-with-ifunc", cl::desc("Access dynamic shadow through an ifunc global on " "platforms that support this"), cl::Hidden, cl::init(false))
static

Referenced by RetagMask().

◆ ClWithTls

cl::opt<bool> ClWithTls("hwasan-with-tls", cl::desc("Access dynamic shadow through an thread-local pointer on " "platforms that support this"), cl::Hidden, cl::init(true))
static

Referenced by RetagMask().

◆ false

HWAddressSanitizer false

Definition at line 284 of file HWAddressSanitizer.cpp.

◆ hwasan

hwasan

Definition at line 284 of file HWAddressSanitizer.cpp.

◆ kDefaultShadowScale

const size_t kDefaultShadowScale = 4
static

Definition at line 62 of file HWAddressSanitizer.cpp.

Referenced by RetagMask().

◆ kDynamicShadowSentinel

const uint64_t kDynamicShadowSentinel
static
Initial value:
=
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)

Definition at line 63 of file HWAddressSanitizer.cpp.

Referenced by llvm::createHWAddressSanitizerPass(), and RetagMask().

◆ kHwasanInitName

const char* const kHwasanInitName = "__hwasan_init"
static

Definition at line 54 of file HWAddressSanitizer.cpp.

◆ kHwasanModuleCtorName

const char* const kHwasanModuleCtorName = "hwasan.module_ctor"
static

Definition at line 53 of file HWAddressSanitizer.cpp.

◆ kHwasanShadowMemoryDynamicAddress

const char* const kHwasanShadowMemoryDynamicAddress
static
Initial value:
=
"__hwasan_shadow_memory_dynamic_address"

Definition at line 56 of file HWAddressSanitizer.cpp.

◆ kNumberOfAccessSizes

const size_t kNumberOfAccessSizes = 5
static

◆ kPointerTagShift

const unsigned kPointerTagShift = 56
static

Definition at line 65 of file HWAddressSanitizer.cpp.

Referenced by RetagMask().

◆ kShadowBaseAlignment

const unsigned kShadowBaseAlignment = 32
static

Definition at line 67 of file HWAddressSanitizer.cpp.