summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmil Tsalapatis <aimilios.tsalapatis@gmail.com>2025-11-18 11:31:45 -0500
committerGitHub <noreply@github.com>2025-11-18 16:31:45 +0000
commit67d5c14ad66f022d689cbcb0709df690938e5b6d (patch)
tree822b1cc49a7bf18c9fdc5b3edebe2610ae803630
parent38c1a58605e8347afd05e31360d3bfd5c4c19ced (diff)
[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
-rw-r--r--llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp35
1 files changed, 28 insertions, 7 deletions
diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index 3a14ee5addc2..c9f249a8733a 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -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"
@@ -441,6 +442,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,
@@ -1363,11 +1373,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())
+ return !isUnsupportedAMDGPUAddrspace(Addr);
+
+ return AddrSpace == 0;
+}
+
Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
// Shadow >> scale
Shadow = IRB.CreateLShr(Shadow, Mapping.Scale);
@@ -1431,10 +1455,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.
@@ -2097,9 +2120,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: