Skip to content

Commit 67d5c14

Browse files
authored
[llvm][AddressSanitizer] option for applying AddressSanitizer to specific address spaces (#167770)
For some backends, e.g., BPF, it is desirable to only sanitize memory belonging to specific address spaces. More specifically, it is sometimes desirable to only apply address sanitization for arena memory belonging to address space 1. However, AddressSanitizer currently does not support selectively sanitizing address spaces. Add a new option to select which address spaces to apply AddressSanitizer to. No functional change for existing targets (namely AMD GPU) that hardcode which address spaces to sanitize
1 parent 38c1a58 commit 67d5c14

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"
@@ -441,6 +442,15 @@ static cl::opt<AsanDtorKind> ClOverrideDestructorKind(
441442
"Use global destructors")),
442443
cl::init(AsanDtorKind::Invalid), cl::Hidden);
443444

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+
444454
// Debug flags.
445455

446456
static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
@@ -1363,11 +1373,25 @@ static bool GlobalWasGeneratedByCompiler(GlobalVariable *G) {
13631373
static bool isUnsupportedAMDGPUAddrspace(Value *Addr) {
13641374
Type *PtrTy = cast<PointerType>(Addr->getType()->getScalarType());
13651375
unsigned int AddrSpace = PtrTy->getPointerAddressSpace();
1376+
// Globals in address space 1 and 4 are supported for AMDGPU.
13661377
if (AddrSpace == 3 || AddrSpace == 5)
13671378
return true;
13681379
return false;
13691380
}
13701381

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+
13711395
Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
13721396
// Shadow >> scale
13731397
Shadow = IRB.CreateLShr(Shadow, Mapping.Scale);
@@ -1431,10 +1455,9 @@ bool AddressSanitizer::isInterestingAlloca(const AllocaInst &AI) {
14311455
}
14321456

14331457
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))
14381461
return true;
14391462

14401463
// Ignore swifterror addresses.
@@ -2097,9 +2120,7 @@ bool ModuleAddressSanitizer::shouldInstrumentGlobal(GlobalVariable *G) const {
20972120
return false;
20982121
if (!Ty->isSized()) return false;
20992122
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))
21032124
return false;
21042125
if (GlobalWasGeneratedByCompiler(G)) return false; // Our own globals.
21052126
// Two problems with thread-locals:

0 commit comments

Comments
 (0)