[llvm] 67d5c14 - [llvm][AddressSanitizer] option for applying AddressSanitizer to specific address spaces (#167770)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 18 08:31:49 PST 2025
Author: Emil Tsalapatis
Date: 2025-11-18T16:31:45Z
New Revision: 67d5c14ad66f022d689cbcb0709df690938e5b6d
URL: https://github.com/llvm/llvm-project/commit/67d5c14ad66f022d689cbcb0709df690938e5b6d
DIFF: https://github.com/llvm/llvm-project/commit/67d5c14ad66f022d689cbcb0709df690938e5b6d.diff
LOG: [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
Added:
Modified:
llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index 3a14ee5addc2f..c9f249a8733ac 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
diff erent 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:
More information about the llvm-commits
mailing list