|
20 | 20 | #include "llvm/ADT/DenseMap.h" |
21 | 21 | #include "llvm/ADT/DepthFirstIterator.h" |
22 | 22 | #include "llvm/ADT/SmallPtrSet.h" |
| 23 | +#include "llvm/ADT/SmallSet.h" |
23 | 24 | #include "llvm/ADT/SmallVector.h" |
24 | 25 | #include "llvm/ADT/Statistic.h" |
25 | 26 | #include "llvm/ADT/StringExtras.h" |
@@ -441,6 +442,15 @@ static cl::opt<AsanDtorKind> ClOverrideDestructorKind( |
441 | 442 | "Use global destructors")), |
442 | 443 | cl::init(AsanDtorKind::Invalid), cl::Hidden); |
443 | 444 |
|
| 445 | +static SmallSet<unsigned, 8> SrcAddrSpaces; |
| 446 | +static cl::list<unsigned> ClAddrSpaces( |
| 447 | + "asan-instrument-address-spaces", |
| 448 | + cl::desc("Only instrument variables in the specified address spaces."), |
| 449 | + cl::Hidden, cl::CommaSeparated, cl::ZeroOrMore, |
| 450 | + cl::callback([](const unsigned &AddrSpace) { |
| 451 | + SrcAddrSpaces.insert(AddrSpace); |
| 452 | + })); |
| 453 | + |
444 | 454 | // Debug flags. |
445 | 455 |
|
446 | 456 | static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden, |
@@ -1363,11 +1373,25 @@ static bool GlobalWasGeneratedByCompiler(GlobalVariable *G) { |
1363 | 1373 | static bool isUnsupportedAMDGPUAddrspace(Value *Addr) { |
1364 | 1374 | Type *PtrTy = cast<PointerType>(Addr->getType()->getScalarType()); |
1365 | 1375 | unsigned int AddrSpace = PtrTy->getPointerAddressSpace(); |
| 1376 | + // Globals in address space 1 and 4 are supported for AMDGPU. |
1366 | 1377 | if (AddrSpace == 3 || AddrSpace == 5) |
1367 | 1378 | return true; |
1368 | 1379 | return false; |
1369 | 1380 | } |
1370 | 1381 |
|
| 1382 | +static bool isSupportedAddrspace(const Triple &TargetTriple, Value *Addr) { |
| 1383 | + Type *PtrTy = cast<PointerType>(Addr->getType()->getScalarType()); |
| 1384 | + unsigned int AddrSpace = PtrTy->getPointerAddressSpace(); |
| 1385 | + |
| 1386 | + if (!SrcAddrSpaces.empty()) |
| 1387 | + return SrcAddrSpaces.count(AddrSpace); |
| 1388 | + |
| 1389 | + if (TargetTriple.isAMDGPU()) |
| 1390 | + return !isUnsupportedAMDGPUAddrspace(Addr); |
| 1391 | + |
| 1392 | + return AddrSpace == 0; |
| 1393 | +} |
| 1394 | + |
1371 | 1395 | Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) { |
1372 | 1396 | // Shadow >> scale |
1373 | 1397 | Shadow = IRB.CreateLShr(Shadow, Mapping.Scale); |
@@ -1431,10 +1455,9 @@ bool AddressSanitizer::isInterestingAlloca(const AllocaInst &AI) { |
1431 | 1455 | } |
1432 | 1456 |
|
1433 | 1457 | bool AddressSanitizer::ignoreAccess(Instruction *Inst, Value *Ptr) { |
1434 | | - // Instrument accesses from different address spaces only for AMDGPU. |
1435 | | - Type *PtrTy = cast<PointerType>(Ptr->getType()->getScalarType()); |
1436 | | - if (PtrTy->getPointerAddressSpace() != 0 && |
1437 | | - !(TargetTriple.isAMDGPU() && !isUnsupportedAMDGPUAddrspace(Ptr))) |
| 1458 | + // Check whether the target supports sanitizing the address space |
| 1459 | + // of the pointer. |
| 1460 | + if (!isSupportedAddrspace(TargetTriple, Ptr)) |
1438 | 1461 | return true; |
1439 | 1462 |
|
1440 | 1463 | // Ignore swifterror addresses. |
@@ -2097,9 +2120,7 @@ bool ModuleAddressSanitizer::shouldInstrumentGlobal(GlobalVariable *G) const { |
2097 | 2120 | return false; |
2098 | 2121 | if (!Ty->isSized()) return false; |
2099 | 2122 | if (!G->hasInitializer()) return false; |
2100 | | - // Globals in address space 1 and 4 are supported for AMDGPU. |
2101 | | - if (G->getAddressSpace() && |
2102 | | - !(TargetTriple.isAMDGPU() && !isUnsupportedAMDGPUAddrspace(G))) |
| 2123 | + if (!isSupportedAddrspace(TargetTriple, G)) |
2103 | 2124 | return false; |
2104 | 2125 | if (GlobalWasGeneratedByCompiler(G)) return false; // Our own globals. |
2105 | 2126 | // Two problems with thread-locals: |
|
0 commit comments