Skip to content

Commit 831655a

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

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,11 @@ static cl::opt<AsanDtorKind> ClOverrideDestructorKind(
436436
"Use global destructors")),
437437
cl::init(AsanDtorKind::Invalid), cl::Hidden);
438438

439+
static cl::list<int> ClAddrSpaces(
440+
"asan-instrument-address-spaces",
441+
cl::desc("Only instrument variables in the specified address spaces."),
442+
cl::Hidden, cl::CommaSeparated, cl::ZeroOrMore);
443+
439444
// Debug flags.
440445

441446
static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
@@ -1355,11 +1360,26 @@ static bool GlobalWasGeneratedByCompiler(GlobalVariable *G) {
13551360
static bool isUnsupportedAMDGPUAddrspace(Value *Addr) {
13561361
Type *PtrTy = cast<PointerType>(Addr->getType()->getScalarType());
13571362
unsigned int AddrSpace = PtrTy->getPointerAddressSpace();
1363+
// Globals in address space 1 and 4 are supported for AMDGPU.
13581364
if (AddrSpace == 3 || AddrSpace == 5)
13591365
return true;
13601366
return false;
13611367
}
13621368

1369+
static bool isSupportedAddrspace(const Triple &TargetTriple, Value *Addr) {
1370+
Type *PtrTy = cast<PointerType>(Addr->getType()->getScalarType());
1371+
unsigned int AddrSpace = PtrTy->getPointerAddressSpace();
1372+
1373+
if (!ClAddrSpaces.empty())
1374+
return std::find(ClAddrSpaces.begin(), ClAddrSpaces.end(), AddrSpace) !=
1375+
ClAddrSpaces.end();
1376+
1377+
if (TargetTriple.isAMDGPU())
1378+
return !isUnsupportedAMDGPUAddrspace(Addr);
1379+
1380+
return AddrSpace == 0;
1381+
}
1382+
13631383
Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
13641384
// Shadow >> scale
13651385
Shadow = IRB.CreateLShr(Shadow, Mapping.Scale);
@@ -1423,10 +1443,9 @@ bool AddressSanitizer::isInterestingAlloca(const AllocaInst &AI) {
14231443
}
14241444

14251445
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)))
1446+
// Check whether the target supports sanitizing the address space
1447+
// of the pointer.
1448+
if (!isSupportedAddrspace(TargetTriple, Ptr))
14301449
return true;
14311450

14321451
// Ignore swifterror addresses.
@@ -2089,9 +2108,7 @@ bool ModuleAddressSanitizer::shouldInstrumentGlobal(GlobalVariable *G) const {
20892108
return false;
20902109
if (!Ty->isSized()) return false;
20912110
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)))
2111+
if (!isSupportedAddrspace(TargetTriple, G))
20952112
return false;
20962113
if (GlobalWasGeneratedByCompiler(G)) return false; // Our own globals.
20972114
// Two problems with thread-locals:

0 commit comments

Comments
 (0)