Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringExtras.h"
Expand Down Expand Up @@ -436,6 +437,15 @@ static cl::opt<AsanDtorKind> ClOverrideDestructorKind(
"Use global destructors")),
cl::init(AsanDtorKind::Invalid), cl::Hidden);

static SmallSet<unsigned, 8> SrcAddrSpaces;
static cl::list<unsigned> ClAddrSpaces(
"asan-instrument-address-spaces",
cl::desc("Only instrument variables in the specified address spaces."),
cl::Hidden, cl::CommaSeparated, cl::ZeroOrMore,
cl::callback([](const unsigned &AddrSpace) {
SrcAddrSpaces.insert(AddrSpace);
}));

// Debug flags.

static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
Expand Down Expand Up @@ -1355,11 +1365,25 @@ static bool GlobalWasGeneratedByCompiler(GlobalVariable *G) {
static bool isUnsupportedAMDGPUAddrspace(Value *Addr) {
Type *PtrTy = cast<PointerType>(Addr->getType()->getScalarType());
unsigned int AddrSpace = PtrTy->getPointerAddressSpace();
// Globals in address space 1 and 4 are supported for AMDGPU.
if (AddrSpace == 3 || AddrSpace == 5)
return true;
return false;
}

static bool isSupportedAddrspace(const Triple &TargetTriple, Value *Addr) {
Type *PtrTy = cast<PointerType>(Addr->getType()->getScalarType());
unsigned int AddrSpace = PtrTy->getPointerAddressSpace();

if (!SrcAddrSpaces.empty())
return SrcAddrSpaces.count(AddrSpace);

if (TargetTriple.isAMDGPU())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: this function could be simplified to always check SrcAddrSpaces, by populating SrcAddrSpaces at the start of the pass with the AMDGPU or default address space values as appropriate.

return !isUnsupportedAMDGPUAddrspace(Addr);

return AddrSpace == 0;
}

Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
// Shadow >> scale
Shadow = IRB.CreateLShr(Shadow, Mapping.Scale);
Expand Down Expand Up @@ -1423,10 +1447,9 @@ bool AddressSanitizer::isInterestingAlloca(const AllocaInst &AI) {
}

bool AddressSanitizer::ignoreAccess(Instruction *Inst, Value *Ptr) {
// Instrument accesses from different address spaces only for AMDGPU.
Type *PtrTy = cast<PointerType>(Ptr->getType()->getScalarType());
if (PtrTy->getPointerAddressSpace() != 0 &&
!(TargetTriple.isAMDGPU() && !isUnsupportedAMDGPUAddrspace(Ptr)))
// Check whether the target supports sanitizing the address space
// of the pointer.
if (!isSupportedAddrspace(TargetTriple, Ptr))
return true;

// Ignore swifterror addresses.
Expand Down Expand Up @@ -2089,9 +2112,7 @@ bool ModuleAddressSanitizer::shouldInstrumentGlobal(GlobalVariable *G) const {
return false;
if (!Ty->isSized()) return false;
if (!G->hasInitializer()) return false;
// Globals in address space 1 and 4 are supported for AMDGPU.
if (G->getAddressSpace() &&
!(TargetTriple.isAMDGPU() && !isUnsupportedAMDGPUAddrspace(G)))
if (!isSupportedAddrspace(TargetTriple, G))
return false;
if (GlobalWasGeneratedByCompiler(G)) return false; // Our own globals.
// Two problems with thread-locals:
Expand Down
Loading