[llvm] [ASan] Instrument ptrtoaddr as pointer difference (PR #211988)

Alexis Engelke via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 24 22:50:36 PDT 2026


https://github.com/aengelke created https://github.com/llvm/llvm-project/pull/211988

We want to emit ptrtoaddr instead of ptrtoint for pointer differences to
not expose the provenance. Adjust ASan instrumentation to handle this
case as well.


>From 6436ba99181027dcbd8dbc3dd5aa3c9f94123a45 Mon Sep 17 00:00:00 2001
From: Alexis Engelke <engelke at in.tum.de>
Date: Sat, 25 Jul 2026 05:49:55 +0000
Subject: [PATCH] [spr] initial version

Created using spr 1.3.8-wip
---
 .../Transforms/Instrumentation/AddressSanitizer.cpp |  2 +-
 .../asan-detect-invalid-pointer-pair.ll             | 13 +++++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index 4858ac0ca1628..2fae7d8c15ff3 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -1654,7 +1654,7 @@ void AddressSanitizer::getInterestingMemoryOperands(
 }
 
 static bool isPointerOperand(Value *V) {
-  return V->getType()->isPointerTy() || isa<PtrToIntInst>(V);
+  return V->getType()->isPointerTy() || isa<PtrToIntInst, PtrToAddrInst>(V);
 }
 
 // This is a rough heuristic; it may cause both false positives and
diff --git a/llvm/test/Instrumentation/AddressSanitizer/asan-detect-invalid-pointer-pair.ll b/llvm/test/Instrumentation/AddressSanitizer/asan-detect-invalid-pointer-pair.ll
index a98df8b1d104a..daad076d0692b 100644
--- a/llvm/test/Instrumentation/AddressSanitizer/asan-detect-invalid-pointer-pair.ll
+++ b/llvm/test/Instrumentation/AddressSanitizer/asan-detect-invalid-pointer-pair.ll
@@ -28,3 +28,16 @@ define i32 @mysub(ptr %p, ptr %q) sanitize_address {
   %w = trunc i64 %z to i32
   ret i32 %w
 }
+
+define i32 @mysub_ptrtoaddr(ptr %p, ptr %q) sanitize_address {
+; ALL-LABEL: @mysub_ptrtoaddr
+; NOSUB-NOT: call void @__sanitizer_ptr_sub
+; SUB: [[P:%[0-9A-Za-z]+]] = ptrtoaddr ptr %p to i64
+; SUB: [[Q:%[0-9A-Za-z]+]] = ptrtoaddr ptr %q to i64
+  %x = ptrtoaddr ptr %p to i64
+  %y = ptrtoaddr ptr %q to i64
+  %z = sub i64 %x, %y
+; SUB: call void @__sanitizer_ptr_sub(i64 [[P]], i64 [[Q]])
+  %w = trunc i64 %z to i32
+  ret i32 %w
+}



More information about the llvm-commits mailing list