Skip to content

Commit c9dcfde

Browse files
committed
[llvm][AddressSanitizer] option for applying AddressSanitizer to specific address spaces
1 parent 4ed494e commit c9dcfde

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/ADT/DenseMap.h"
2121
#include "llvm/ADT/DepthFirstIterator.h"
2222
#include "llvm/ADT/SmallPtrSet.h"
23+
#include "llvm/ADT/SmallSet.h"
2324
#include "llvm/ADT/SmallVector.h"
2425
#include "llvm/ADT/Statistic.h"
2526
#include "llvm/ADT/StringExtras.h"
@@ -436,6 +437,15 @@ static cl::opt<AsanDtorKind> ClOverrideDestructorKind(
436437
"Use global destructors")),
437438
cl::init(AsanDtorKind::Invalid), cl::Hidden);
438439

440+
static SmallSet<unsigned, 8> SrcAddrSpaces;
441+
static cl::list<unsigned> ClAddrSpaces(
442+
"asan-instrument-address-spaces",
443+
cl::desc("Only instrument variables in the specified address spaces."),
444+
cl::Hidden, cl::CommaSeparated, cl::ZeroOrMore,
445+
cl::callback([](const unsigned &AddrSpace) {
446+
SrcAddrSpaces.insert(AddrSpace);
447+
}));
448+
439449
// Debug flags.
440450

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

1374+
static bool isSupportedAddrspace(const Triple &TargetTriple, Value *Addr) {
1375+
Type *PtrTy = cast<PointerType>(Addr->getType()->getScalarType());
1376+
unsigned int AddrSpace = PtrTy->getPointerAddressSpace();
1377+
1378+
if (!SrcAddrSpaces.empty())
1379+
return SrcAddrSpaces.count(AddrSpace);
1380+
1381+
if (TargetTriple.isAMDGPU())
1382+
return !isUnsupportedAMDGPUAddrspace(Addr);
1383+
1384+
return AddrSpace == 0;
1385+
}
1386+
13631387
Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
13641388
// Shadow >> scale
13651389
Shadow = IRB.CreateLShr(Shadow, Mapping.Scale);
@@ -1423,10 +1447,9 @@ bool AddressSanitizer::isInterestingAlloca(const AllocaInst &AI) {
14231447
}
14241448

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

14321455
// Ignore swifterror addresses.
@@ -2089,9 +2112,7 @@ bool ModuleAddressSanitizer::shouldInstrumentGlobal(GlobalVariable *G) const {
20892112
return false;
20902113
if (!Ty->isSized()) return false;
20912114
if (!G->hasInitializer()) return false;
2092-
// Globals in address space 1 and 4 are supported for AMDGPU.
2093-
if (G->getAddressSpace() &&
2094-
!(TargetTriple.isAMDGPU() && !isUnsupportedAMDGPUAddrspace(G)))
2115+
if (!isSupportedAddrspace(TargetTriple, G))
20952116
return false;
20962117
if (GlobalWasGeneratedByCompiler(G)) return false; // Our own globals.
20972118
// Two problems with thread-locals:

0 commit comments

Comments
 (0)