[compiler-rt] [llvm] [compiler-rt][asan] Add asan checks for __builtin_assume_dereferencable (PR #190871)

via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 9 16:28:20 PDT 2026


https://github.com/PiJoules updated https://github.com/llvm/llvm-project/pull/190871

>From 89b26f7ce80a985eab53239bb4669ac87f661d96 Mon Sep 17 00:00:00 2001
From: Leonard Chan <leonardchan at google.com>
Date: Sat, 4 Apr 2026 03:15:29 +0000
Subject: [PATCH 1/2] [compiler-rt][asan] Add asan checks for
 __builtin_assume_dereferencable

This checks that the range covered by this intrinsic is dereferencable.
Specifically it checks for `llvm.assume` intrinsics using the
`dereferencable` operator bundle and just asserts the shadow for this
range is zero.

The bulk of this PR was made by gemini but it was thoroughly edited and
reviewed to the best of my ability.
---
 .../lib/asan/AIX/asan.link_with_main_exec.txt |  2 +
 compiler-rt/lib/asan/asan_errors.cpp          | 54 ++++++++++
 compiler-rt/lib/asan/asan_errors.h            | 12 +++
 compiler-rt/lib/asan/asan_interface.inc       |  2 +
 compiler-rt/lib/asan/asan_report.cpp          | 12 +++
 compiler-rt/lib/asan/asan_report.h            |  2 +
 compiler-rt/lib/asan/asan_rtl.cpp             | 16 +++
 .../asan/TestCases/assume_dereferenceable.cpp | 99 +++++++++++++++++++
 .../assume_dereferenceable_fully_poisoned.cpp | 14 +++
 .../assume_dereferenceable_halt_on_error.cpp  | 22 +++++
 .../TestCases/assume_dereferenceable_pass.cpp | 35 +++++++
 .../Instrumentation/AddressSanitizer.cpp      | 42 ++++++++
 .../assume-dereferenceable.ll                 | 18 ++++
 13 files changed, 330 insertions(+)
 create mode 100644 compiler-rt/test/asan/TestCases/assume_dereferenceable.cpp
 create mode 100644 compiler-rt/test/asan/TestCases/assume_dereferenceable_fully_poisoned.cpp
 create mode 100644 compiler-rt/test/asan/TestCases/assume_dereferenceable_halt_on_error.cpp
 create mode 100644 compiler-rt/test/asan/TestCases/assume_dereferenceable_pass.cpp
 create mode 100644 llvm/test/Instrumentation/AddressSanitizer/assume-dereferenceable.ll

diff --git a/compiler-rt/lib/asan/AIX/asan.link_with_main_exec.txt b/compiler-rt/lib/asan/AIX/asan.link_with_main_exec.txt
index 5efc48c262369..d3b8e98bd020f 100644
--- a/compiler-rt/lib/asan/AIX/asan.link_with_main_exec.txt
+++ b/compiler-rt/lib/asan/AIX/asan.link_with_main_exec.txt
@@ -1,5 +1,7 @@
 #! .
 __asan_report_load_n
+__asan_report_assume_dereferenceable
+__asan_report_assume_dereferenceable_noabort
 __asan_loadN
 __asan_report_load1
 __asan_load1
diff --git a/compiler-rt/lib/asan/asan_errors.cpp b/compiler-rt/lib/asan/asan_errors.cpp
index c4100cf7244e3..731df110bc445 100644
--- a/compiler-rt/lib/asan/asan_errors.cpp
+++ b/compiler-rt/lib/asan/asan_errors.cpp
@@ -713,4 +713,58 @@ void ErrorGeneric::Print() {
   CheckPoisonRecords(addr);
 }
 
+ErrorAssumeDereferenceable::ErrorAssumeDereferenceable(
+    u32 tid, uptr pc, uptr bp, uptr sp, uptr addr, uptr dereferenceable_size)
+    : ErrorBase(tid),
+      addr_description(addr, dereferenceable_size,
+                       /*shouldLockThreadRegistry=*/false),
+      pc(pc),
+      bp(bp),
+      sp(sp),
+      dereferenceable_size(dereferenceable_size) {
+  scariness.Clear();
+  scariness.Scare(10, "assume-dereferenceable");
+}
+
+void ErrorAssumeDereferenceable::Print() {
+  Decorator d;
+  Printf("%s", d.Error());
+  uptr addr = addr_description.Address();
+  Report(
+      "ERROR: AddressSanitizer: dereferencable-assumption-violation on address "
+      "%p at "
+      "pc %p bp %p sp %p\n",
+      (void*)addr, (void*)pc, (void*)bp, (void*)sp);
+  Printf("%s", d.Default());
+
+  Printf("%sDEREFERENCABLE ASSUMPTION of size %zu at %p thread %s%s\n",
+         d.Access(), dereferenceable_size, (void*)addr,
+         AsanThreadIdAndName(tid).c_str(), d.Default());
+
+  uptr range_start = addr;
+  bool current_poisoned = AddressIsPoisoned(range_start);
+  for (uptr i = 1; i <= dereferenceable_size; ++i) {
+    bool poisoned = (i < dereferenceable_size) ? AddressIsPoisoned(addr + i)
+                                               : !current_poisoned;
+    if (poisoned != current_poisoned) {
+      Printf("%s  range [%p, %p) is %s%s\n", d.Default(), (void*)range_start,
+             (void*)(addr + i),
+             current_poisoned ? "NOT dereferenceable" : "dereferenceable",
+             d.Default());
+      if (i < dereferenceable_size) {
+        range_start = addr + i;
+        current_poisoned = poisoned;
+      }
+    }
+  }
+
+  scariness.Print();
+  GET_STACK_TRACE_FATAL(pc, bp);
+  stack.Print();
+
+  addr_description.Print("dereferencable-assumption-violation");
+  ReportErrorSummary("dereferencable-assumption-violation", &stack);
+  PrintShadowMemoryForAddress(addr);
+}
+
 }  // namespace __asan
diff --git a/compiler-rt/lib/asan/asan_errors.h b/compiler-rt/lib/asan/asan_errors.h
index 9c6843774f376..6bd7f2abaac37 100644
--- a/compiler-rt/lib/asan/asan_errors.h
+++ b/compiler-rt/lib/asan/asan_errors.h
@@ -436,6 +436,17 @@ struct ErrorGeneric : ErrorBase {
   void Print();
 };
 
+struct ErrorAssumeDereferenceable : ErrorBase {
+  AddressDescription addr_description;
+  uptr pc, bp, sp;
+  uptr dereferenceable_size;
+
+  ErrorAssumeDereferenceable() = default;  // (*)
+  ErrorAssumeDereferenceable(u32 tid, uptr pc, uptr bp, uptr sp, uptr addr,
+                             uptr dereferenceable_size);
+  void Print();
+};
+
 // clang-format off
 #define ASAN_FOR_EACH_ERROR_KIND(macro)                    \
   macro(DeadlySignal)                                      \
@@ -462,6 +473,7 @@ struct ErrorGeneric : ErrorBase {
   macro(BadParamsToCopyContiguousContainerAnnotations)     \
   macro(ODRViolation)                                      \
   macro(InvalidPointerPair)                                \
+  macro(AssumeDereferenceable)                             \
   macro(Generic)
 // clang-format on
 
diff --git a/compiler-rt/lib/asan/asan_interface.inc b/compiler-rt/lib/asan/asan_interface.inc
index b465356b1e443..3c794376fa959 100644
--- a/compiler-rt/lib/asan/asan_interface.inc
+++ b/compiler-rt/lib/asan/asan_interface.inc
@@ -93,6 +93,8 @@ INTERFACE_FUNCTION(__asan_report_load8_noabort)
 INTERFACE_FUNCTION(__asan_report_load16_noabort)
 INTERFACE_FUNCTION(__asan_report_load_n_noabort)
 INTERFACE_FUNCTION(__asan_report_present)
+INTERFACE_FUNCTION(__asan_report_assume_dereferenceable)
+INTERFACE_FUNCTION(__asan_report_assume_dereferenceable_noabort)
 INTERFACE_FUNCTION(__asan_report_store1)
 INTERFACE_FUNCTION(__asan_report_store2)
 INTERFACE_FUNCTION(__asan_report_store4)
diff --git a/compiler-rt/lib/asan/asan_report.cpp b/compiler-rt/lib/asan/asan_report.cpp
index e50faf0223212..7c9ebbd203db9 100644
--- a/compiler-rt/lib/asan/asan_report.cpp
+++ b/compiler-rt/lib/asan/asan_report.cpp
@@ -543,6 +543,18 @@ void ReportGenericError(uptr pc, uptr bp, uptr sp, uptr addr, bool is_write,
   in_report.ReportError(error);
 }
 
+void ReportAssumeDereferenceableError(uptr pc, uptr bp, uptr sp, uptr addr,
+                                      uptr dereferenceable_size, bool fatal) {
+  if (!fatal && SuppressErrorReport(pc))
+    return;
+  ENABLE_FRAME_POINTER;
+
+  ScopedInErrorReport in_report(fatal);
+  ErrorAssumeDereferenceable error(GetCurrentTidOrInvalid(), pc, bp, sp, addr,
+                                   dereferenceable_size);
+  in_report.ReportError(error);
+}
+
 }  // namespace __asan
 
 // --------------------------- Interface --------------------- {{{1
diff --git a/compiler-rt/lib/asan/asan_report.h b/compiler-rt/lib/asan/asan_report.h
index b181849b10f92..467defa4c64a3 100644
--- a/compiler-rt/lib/asan/asan_report.h
+++ b/compiler-rt/lib/asan/asan_report.h
@@ -49,6 +49,8 @@ bool ParseFrameDescription(const char *frame_descr,
 // Different kinds of error reports.
 void ReportGenericError(uptr pc, uptr bp, uptr sp, uptr addr, bool is_write,
                         uptr access_size, u32 exp, bool fatal);
+void ReportAssumeDereferenceableError(uptr pc, uptr bp, uptr sp, uptr addr,
+                                      uptr dereferenceable_size, bool fatal);
 void ReportDeadlySignal(const SignalContext &sig);
 void ReportNewDeleteTypeMismatch(uptr addr, uptr delete_size,
                                  uptr delete_alignment,
diff --git a/compiler-rt/lib/asan/asan_rtl.cpp b/compiler-rt/lib/asan/asan_rtl.cpp
index c036a13a11029..9726f8f447b59 100644
--- a/compiler-rt/lib/asan/asan_rtl.cpp
+++ b/compiler-rt/lib/asan/asan_rtl.cpp
@@ -156,6 +156,22 @@ void __asan_report_ ## type ## _n_noabort(uptr addr, uptr size) {           \
 ASAN_REPORT_ERROR_N(load, false)
 ASAN_REPORT_ERROR_N(store, true)
 
+extern "C" NOINLINE INTERFACE_ATTRIBUTE void
+__asan_report_assume_dereferenceable(uptr addr, uptr size) {
+  if (__asan_region_is_poisoned(addr, size)) {
+    GET_CALLER_PC_BP_SP;
+    ReportAssumeDereferenceableError(pc, bp, sp, addr, size, true);
+  }
+}
+
+extern "C" NOINLINE INTERFACE_ATTRIBUTE void
+__asan_report_assume_dereferenceable_noabort(uptr addr, uptr size) {
+  if (__asan_region_is_poisoned(addr, size)) {
+    GET_CALLER_PC_BP_SP;
+    ReportAssumeDereferenceableError(pc, bp, sp, addr, size, false);
+  }
+}
+
 #define ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, exp_arg, fatal) \
   uptr sp = MEM_TO_SHADOW(addr);                                               \
   uptr s = size <= ASAN_SHADOW_GRANULARITY ? *reinterpret_cast<u8 *>(sp)       \
diff --git a/compiler-rt/test/asan/TestCases/assume_dereferenceable.cpp b/compiler-rt/test/asan/TestCases/assume_dereferenceable.cpp
new file mode 100644
index 0000000000000..e685deed1409e
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/assume_dereferenceable.cpp
@@ -0,0 +1,99 @@
+// RUN: %clangxx_asan -O0 -mllvm -asan-instrument-assume-dereferenceable=1 -fsanitize-recover=address %s -o %t && %env_asan_opts=halt_on_error=0 %run %t 2>&1 | FileCheck %s
+
+#include <stdio.h>
+#include <stdlib.h>
+
+void test_malloc_fully_oob() {
+  char *p = (char *)malloc(10);
+  fprintf(stderr, "test_malloc_fully_oob\n");
+  // CHECK: test_malloc_fully_oob
+  // CHECK: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR1:0x[0-9a-fA-F]+]]
+  // CHECK: DEREFERENCABLE ASSUMPTION of size 20 at [[PTR1]] thread T0
+  // CHECK-NEXT: range [[[PTR1]], 0x{{.*}}) is dereferenceable
+  // CHECK-NEXT: range [0x{{.*}}, 0x{{.*}}) is NOT dereferenceable
+  __builtin_assume_dereferenceable(p, 20);
+  free(p);
+}
+
+void test_malloc_partial_right() {
+  char *p = (char *)malloc(10);
+  fprintf(stderr, "test_malloc_partial_right\n");
+  // CHECK: test_malloc_partial_right
+  // CHECK: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR2:0x[0-9a-fA-F]+]]
+  // CHECK: DEREFERENCABLE ASSUMPTION of size 10 at [[PTR2]] thread T0
+  // CHECK-NEXT: range [[[PTR2]], 0x{{.*}}) is dereferenceable
+  // CHECK-NEXT: range [0x{{.*}}, 0x{{.*}}) is NOT dereferenceable
+  __builtin_assume_dereferenceable(p + 5, 10);
+  free(p);
+}
+
+void test_malloc_partial_left() {
+  char *p = (char *)malloc(10);
+  fprintf(stderr, "test_malloc_partial_left\n");
+  // CHECK: test_malloc_partial_left
+  // CHECK: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR3:0x[0-9a-fA-F]+]]
+  // CHECK: DEREFERENCABLE ASSUMPTION of size 10 at [[PTR3]] thread T0
+  // CHECK-NEXT: range [[[PTR3]], 0x{{.*}}) is NOT dereferenceable
+  // CHECK-NEXT: range [0x{{.*}}, 0x{{.*}}) is dereferenceable
+  __builtin_assume_dereferenceable(p - 5, 10);
+  free(p);
+}
+
+void test_stack_fully_oob(int i) {
+  char p[10];
+  fprintf(stderr, "test_stack_fully_oob\n");
+  // CHECK: test_stack_fully_oob
+  // CHECK: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR4:0x[0-9a-fA-F]+]]
+  // CHECK: DEREFERENCABLE ASSUMPTION of size 100 at [[PTR4]] thread T0
+  __builtin_assume_dereferenceable(p, 100);
+
+  // This is here just to force ASan to emit instrumentation for poisoning
+  // redzones around the stack. By default, ASan will not instrument stack
+  // allocations that it deems "uninteresting".
+  p[i] = 0;
+}
+
+void test_stack_partial_right() {
+  char p[10];
+  fprintf(stderr, "test_stack_partial_right\n");
+  // CHECK: test_stack_partial_right
+  // CHECK: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR5:0x[0-9a-fA-F]+]]
+  // CHECK: DEREFERENCABLE ASSUMPTION of size 10 at [[PTR5]] thread T0
+  // CHECK-NEXT: range [[[PTR5]], 0x{{.*}}) is dereferenceable
+  // CHECK-NEXT: range [0x{{.*}}, 0x{{.*}}) is NOT dereferenceable
+  __builtin_assume_dereferenceable(p + 5, 10);
+}
+
+void test_stack_partial_left() {
+  char p[10];
+  fprintf(stderr, "test_stack_partial_left\n");
+  // CHECK: test_stack_partial_left
+  // CHECK: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR6:0x[0-9a-fA-F]+]]
+  // CHECK: DEREFERENCABLE ASSUMPTION of size 10 at [[PTR6]] thread T0
+  // CHECK-NEXT: range [[[PTR6]], 0x{{.*}}) is NOT dereferenceable
+  // CHECK-NEXT: range [0x{{.*}}, 0x{{.*}}) is dereferenceable
+  __builtin_assume_dereferenceable(p - 5, 10);
+}
+
+void test_malloc_completely_poisoned() {
+  char *p = (char *)malloc(10);
+  free(p);
+  fprintf(stderr, "test_malloc_completely_poisoned\n");
+  // CHECK: test_malloc_completely_poisoned
+  // CHECK: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR7:0x[0-9a-fA-F]+]]
+  // CHECK: DEREFERENCABLE ASSUMPTION of size 10 at [[PTR7]] thread T0
+  // CHECK-NEXT: range [[[PTR7]], 0x{{.*}}) is NOT dereferenceable
+  // CHECK-NOT: is dereferenceable
+  __builtin_assume_dereferenceable(p, 10);
+}
+
+int main() {
+  test_malloc_fully_oob();
+  test_malloc_partial_right();
+  test_malloc_partial_left();
+  test_stack_fully_oob(0);
+  test_stack_partial_right();
+  test_stack_partial_left();
+  test_malloc_completely_poisoned();
+  return 0;
+}
diff --git a/compiler-rt/test/asan/TestCases/assume_dereferenceable_fully_poisoned.cpp b/compiler-rt/test/asan/TestCases/assume_dereferenceable_fully_poisoned.cpp
new file mode 100644
index 0000000000000..50ceb8c597c40
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/assume_dereferenceable_fully_poisoned.cpp
@@ -0,0 +1,14 @@
+// RUN: %clangxx_asan -O0 -mllvm -asan-instrument-assume-dereferenceable=1 %s -o %t && not %run %t 2>&1 | FileCheck %s
+
+#include <stdlib.h>
+
+int main() {
+  char *p = (char *)malloc(10);
+  free(p);
+  // CHECK: AddressSanitizer: dereferencable-assumption-violation
+  // CHECK: ASSUMPTION of size 10
+  // CHECK-NEXT: range [0x{{.*}}, 0x{{.*}}) is NOT dereferenceable
+  // CHECK-NOT: is dereferenceable
+  __builtin_assume_dereferenceable(p, 10);
+  return 0;
+}
diff --git a/compiler-rt/test/asan/TestCases/assume_dereferenceable_halt_on_error.cpp b/compiler-rt/test/asan/TestCases/assume_dereferenceable_halt_on_error.cpp
new file mode 100644
index 0000000000000..a91a83687389d
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/assume_dereferenceable_halt_on_error.cpp
@@ -0,0 +1,22 @@
+// RUN: %clangxx_asan -O0 -mllvm -asan-instrument-assume-dereferenceable=1 -fsanitize-recover=address %s -o %t
+// RUN: %env_asan_opts=halt_on_error=1 not %run %t 2>&1 | FileCheck %s
+// RUN: %env_asan_opts=halt_on_error=0 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-RECOVER
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int main() {
+  char *p = (char *)malloc(10);
+
+  // CHECK: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR:0x[0-9a-fA-F]+]]
+
+  // CHECK-RECOVER: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR:0x[0-9a-fA-F]+]]
+  __builtin_assume_dereferenceable(p, 20);
+  free(p);
+
+  fprintf(stderr, "EXECUTED AFTER ERROR\n");
+  // CHECK-NOT: EXECUTED AFTER ERROR
+  // CHECK-RECOVER: EXECUTED AFTER ERROR
+
+  return 0;
+}
diff --git a/compiler-rt/test/asan/TestCases/assume_dereferenceable_pass.cpp b/compiler-rt/test/asan/TestCases/assume_dereferenceable_pass.cpp
new file mode 100644
index 0000000000000..9f5a65888df3b
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/assume_dereferenceable_pass.cpp
@@ -0,0 +1,35 @@
+// RUN: %clangxx_asan -O0 -mllvm -asan-instrument-assume-dereferenceable=1 %s -o %t && %run %t
+
+#include <stdlib.h>
+
+void test_pass_1() {
+  char *p = (char *)malloc(20);
+  __builtin_assume_dereferenceable(p, 10);
+  __builtin_assume_dereferenceable(p, 20);
+  free(p);
+}
+
+void test_pass_2() {
+  char *p = (char *)malloc(10);
+  __builtin_assume_dereferenceable(p, 0);
+  free(p);
+}
+
+void test_stack_pass_1() {
+  char p[20];
+  __builtin_assume_dereferenceable(p, 10);
+  __builtin_assume_dereferenceable(p, 20);
+}
+
+void test_stack_pass_2() {
+  char p[10];
+  __builtin_assume_dereferenceable(p, 0);
+}
+
+int main() {
+  test_pass_1();
+  test_pass_2();
+  test_stack_pass_1();
+  test_stack_pass_2();
+  return 0;
+}
diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index 8b5969ffb3ca0..3b547728b1641 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -299,6 +299,11 @@ static cl::opt<bool> ClRedzoneByvalArgs("asan-redzone-byval-args",
                                                  "required)"), cl::Hidden,
                                         cl::init(true));
 
+static cl::opt<bool> ClInstrumentAssumeDereferenceable(
+    "asan-instrument-assume-dereferenceable",
+    cl::desc("instrument llvm.assume(dereferenceable)"), cl::Hidden,
+    cl::init(true));
+
 static cl::opt<bool> ClUseAfterScope("asan-use-after-scope",
                                      cl::desc("Check stack-use-after-scope"),
                                      cl::Hidden, cl::init(false));
@@ -924,6 +929,7 @@ struct AddressSanitizer {
   // These arrays is indexed by AccessIsWrite and Experiment.
   FunctionCallee AsanErrorCallbackSized[2][2];
   FunctionCallee AsanMemoryAccessCallbackSized[2][2];
+  FunctionCallee AsanAssumeDereferenceableCallback;
 
   FunctionCallee AsanMemmove, AsanMemcpy, AsanMemset;
   Value *LocalDynamicShadow = nullptr;
@@ -2881,6 +2887,12 @@ bool ModuleAddressSanitizer::instrumentModule() {
 
 void AddressSanitizer::initializeCallbacks(const TargetLibraryInfo *TLI) {
   IRBuilder<> IRB(*C);
+
+  const std::string EndingStr = Recover ? "_noabort" : "";
+  AsanAssumeDereferenceableCallback = M.getOrInsertFunction(
+      "__asan_report_assume_dereferenceable" + EndingStr,
+      FunctionType::get(IRB.getVoidTy(), {IntptrTy, IntptrTy}, false));
+
   // Create __asan_report* callbacks.
   // IsWrite, TypeSize and Exp are encoded in the function name.
   for (int Exp = 0; Exp < 2; Exp++) {
@@ -3099,6 +3111,7 @@ bool AddressSanitizer::instrumentFunction(Function &F,
   SmallVector<Instruction *, 8> NoReturnCalls;
   SmallVector<BasicBlock *, 16> AllBlocks;
   SmallVector<Instruction *, 16> PointerComparisonsOrSubtracts;
+  SmallVector<AssumeInst *, 8> DerefAssumptions;
 
   // Fill the set of memory operations to instrument.
   for (auto &BB : F) {
@@ -3113,6 +3126,7 @@ bool AddressSanitizer::instrumentFunction(Function &F,
       SmallVector<InterestingMemoryOperand, 1> InterestingOperands;
       getInterestingMemoryOperands(&Inst, InterestingOperands, TTI);
 
+      AssumeInst *AI;
       if (!InterestingOperands.empty()) {
         for (auto &Operand : InterestingOperands) {
           if (ClOpt && ClOptSameTemp) {
@@ -3140,6 +3154,12 @@ bool AddressSanitizer::instrumentFunction(Function &F,
         // ok, take it.
         IntrinToInstrument.push_back(MI);
         NumInsnsPerBB++;
+      } else if (ClInstrumentAssumeDereferenceable &&
+                 (AI = dyn_cast<AssumeInst>(&Inst))) {
+        if (AI->getOperandBundle("dereferenceable")) {
+          DerefAssumptions.push_back(AI);
+          NumInsnsPerBB++;
+        }
       } else {
         if (auto *CB = dyn_cast<CallBase>(&Inst)) {
           // A call inside BB.
@@ -3189,6 +3209,28 @@ bool AddressSanitizer::instrumentFunction(Function &F,
     FunctionModified = true;
   }
 
+  for (auto *AI : DerefAssumptions) {
+    if (auto Bundle = AI->getOperandBundle("dereferenceable")) {
+      Value *Ptr = Bundle->Inputs[0];
+      Value *Size = Bundle->Inputs[1];
+
+      InstrumentationIRBuilder IRB(AI);
+      Value *AddrLong = IRB.CreatePointerCast(Ptr, IntptrTy);
+      Value *SizeExt = IRB.CreateZExtOrTrunc(Size, IntptrTy);
+
+      // Skip if size is exactly 0.
+      if (ConstantInt *CI = dyn_cast<ConstantInt>(SizeExt)) {
+        if (CI->isZero())
+          continue;
+      }
+
+      RTCI.createRuntimeCall(IRB, AsanAssumeDereferenceableCallback,
+                             {AddrLong, SizeExt});
+
+      FunctionModified = true;
+    }
+  }
+
   if (ChangedStack || !NoReturnCalls.empty())
     FunctionModified = true;
 
diff --git a/llvm/test/Instrumentation/AddressSanitizer/assume-dereferenceable.ll b/llvm/test/Instrumentation/AddressSanitizer/assume-dereferenceable.ll
new file mode 100644
index 0000000000000..cb0d69da60639
--- /dev/null
+++ b/llvm/test/Instrumentation/AddressSanitizer/assume-dereferenceable.ll
@@ -0,0 +1,18 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=asan -asan-instrument-assume-dereferenceable=1 -S | FileCheck %s
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @test(ptr %p, i64 %size) sanitize_address {
+; CHECK-LABEL: @test(
+; CHECK-NEXT:    [[TMP17:%.*]] = ptrtoint ptr [[TMP4:%.*]] to i64
+; CHECK-NEXT:    call void @__asan_report_assume_dereferenceable(i64 [[TMP17]], i64 [[SIZE:%.*]])
+; CHECK-NEXT:    call void @llvm.assume(i1 true) [ "dereferenceable"(ptr [[TMP4]], i64 [[SIZE]]) ]
+; CHECK-NEXT:    ret void
+;
+  call void @llvm.assume(i1 true) [ "dereferenceable"(ptr %p, i64 %size) ]
+  ret void
+}
+
+declare void @llvm.assume(i1)

>From 29536951647d9e7ebd99e9ed9cc2efb28fdd1d31 Mon Sep 17 00:00:00 2001
From: Leonard Chan <leonardchan at google.com>
Date: Thu, 9 Apr 2026 22:11:34 +0000
Subject: [PATCH 2/2] Respond to comments

---
 compiler-rt/lib/asan/asan_errors.cpp          | 92 ++++++-------------
 compiler-rt/lib/asan/asan_errors.h            | 20 +---
 compiler-rt/lib/asan/asan_report.cpp          | 13 ++-
 .../asan/TestCases/assume_dereferenceable.cpp | 40 +++-----
 .../assume_dereferenceable_fully_poisoned.cpp |  6 +-
 .../assume_dereferenceable_halt_on_error.cpp  |  4 +-
 6 files changed, 62 insertions(+), 113 deletions(-)

diff --git a/compiler-rt/lib/asan/asan_errors.cpp b/compiler-rt/lib/asan/asan_errors.cpp
index 731df110bc445..5a05f5a1f75d4 100644
--- a/compiler-rt/lib/asan/asan_errors.cpp
+++ b/compiler-rt/lib/asan/asan_errors.cpp
@@ -454,14 +454,14 @@ static bool AdjacentShadowValuesAreFullyPoisoned(u8 *s) {
 }
 
 ErrorGeneric::ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr,
-                           bool is_write_, uptr access_size_)
+                           AccessType access_type_, uptr access_size_)
     : ErrorBase(tid),
       addr_description(addr, access_size_, /*shouldLockThreadRegistry=*/false),
       pc(pc_),
       bp(bp_),
       sp(sp_),
       access_size(access_size_),
-      is_write(is_write_),
+      access_type(access_type_),
       shadow_val(0) {
   scariness.Clear();
   if (access_size) {
@@ -472,7 +472,13 @@ ErrorGeneric::ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr,
     } else if (access_size >= 10) {
       scariness.Scare(15, "multi-byte");
     }
-    is_write ? scariness.Scare(20, "write") : scariness.Scare(1, "read");
+    if (access_type == AccessType::Write) {
+      scariness.Scare(20, "write");
+    } else if (access_type == AccessType::Read) {
+      scariness.Scare(1, "read");
+    } else if (access_type == AccessType::Assumption) {
+      scariness.Scare(1, "assumption");
+    }
 
     // Determine the error type.
     bug_descr = "unknown-crash";
@@ -498,7 +504,8 @@ ErrorGeneric::ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr,
         case kAsanHeapFreeMagic:
           bug_descr = "heap-use-after-free";
           bug_type_score = 20;
-          if (!is_write) read_after_free_bonus = 18;
+          if (access_type == AccessType::Read)
+            read_after_free_bonus = 18;
           break;
         case kAsanStackLeftRedzoneMagic:
           bug_descr = "stack-buffer-underflow";
@@ -518,7 +525,8 @@ ErrorGeneric::ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr,
         case kAsanStackAfterReturnMagic:
           bug_descr = "stack-use-after-return";
           bug_type_score = 30;
-          if (!is_write) read_after_free_bonus = 18;
+          if (access_type == AccessType::Read)
+            read_after_free_bonus = 18;
           break;
         case kAsanUserPoisonedMemoryMagic:
           bug_descr = "use-after-poison";
@@ -693,9 +701,23 @@ void ErrorGeneric::Print() {
          bug_descr, (void *)addr, (void *)pc, (void *)bp, (void *)sp);
   Printf("%s", d.Default());
 
-  Printf("%s%s of size %zu at %p thread %s%s\n", d.Access(),
-         access_size ? (is_write ? "WRITE" : "READ") : "ACCESS", access_size,
-         (void *)addr, AsanThreadIdAndName(tid).c_str(), d.Default());
+  const char* access_type = "ACCESS";
+  if (access_size) {
+    switch (this->access_type) {
+      case AccessType::Assumption:
+        access_type = "DEREFERENCEABLE ASSUMPTION";
+        break;
+      case AccessType::Read:
+        access_type = "READ";
+        break;
+      case AccessType::Write:
+        access_type = "WRITE";
+        break;
+    }
+  }
+  Printf("%s%s of size %zu at %p thread %s%s\n", d.Access(), access_type,
+         access_size, (void*)addr, AsanThreadIdAndName(tid).c_str(),
+         d.Default());
 
   scariness.Print();
   GET_STACK_TRACE_FATAL(pc, bp);
@@ -713,58 +735,4 @@ void ErrorGeneric::Print() {
   CheckPoisonRecords(addr);
 }
 
-ErrorAssumeDereferenceable::ErrorAssumeDereferenceable(
-    u32 tid, uptr pc, uptr bp, uptr sp, uptr addr, uptr dereferenceable_size)
-    : ErrorBase(tid),
-      addr_description(addr, dereferenceable_size,
-                       /*shouldLockThreadRegistry=*/false),
-      pc(pc),
-      bp(bp),
-      sp(sp),
-      dereferenceable_size(dereferenceable_size) {
-  scariness.Clear();
-  scariness.Scare(10, "assume-dereferenceable");
-}
-
-void ErrorAssumeDereferenceable::Print() {
-  Decorator d;
-  Printf("%s", d.Error());
-  uptr addr = addr_description.Address();
-  Report(
-      "ERROR: AddressSanitizer: dereferencable-assumption-violation on address "
-      "%p at "
-      "pc %p bp %p sp %p\n",
-      (void*)addr, (void*)pc, (void*)bp, (void*)sp);
-  Printf("%s", d.Default());
-
-  Printf("%sDEREFERENCABLE ASSUMPTION of size %zu at %p thread %s%s\n",
-         d.Access(), dereferenceable_size, (void*)addr,
-         AsanThreadIdAndName(tid).c_str(), d.Default());
-
-  uptr range_start = addr;
-  bool current_poisoned = AddressIsPoisoned(range_start);
-  for (uptr i = 1; i <= dereferenceable_size; ++i) {
-    bool poisoned = (i < dereferenceable_size) ? AddressIsPoisoned(addr + i)
-                                               : !current_poisoned;
-    if (poisoned != current_poisoned) {
-      Printf("%s  range [%p, %p) is %s%s\n", d.Default(), (void*)range_start,
-             (void*)(addr + i),
-             current_poisoned ? "NOT dereferenceable" : "dereferenceable",
-             d.Default());
-      if (i < dereferenceable_size) {
-        range_start = addr + i;
-        current_poisoned = poisoned;
-      }
-    }
-  }
-
-  scariness.Print();
-  GET_STACK_TRACE_FATAL(pc, bp);
-  stack.Print();
-
-  addr_description.Print("dereferencable-assumption-violation");
-  ReportErrorSummary("dereferencable-assumption-violation", &stack);
-  PrintShadowMemoryForAddress(addr);
-}
-
 }  // namespace __asan
diff --git a/compiler-rt/lib/asan/asan_errors.h b/compiler-rt/lib/asan/asan_errors.h
index 6bd7f2abaac37..b4bfbbb6c51bc 100644
--- a/compiler-rt/lib/asan/asan_errors.h
+++ b/compiler-rt/lib/asan/asan_errors.h
@@ -423,27 +423,18 @@ struct ErrorInvalidPointerPair : ErrorBase {
 };
 
 struct ErrorGeneric : ErrorBase {
+  enum class AccessType { Read, Write, Assumption };
+
   AddressDescription addr_description;
   uptr pc, bp, sp;
   uptr access_size;
   const char *bug_descr;
-  bool is_write;
+  AccessType access_type;
   u8 shadow_val;
 
   ErrorGeneric() = default;  // (*)
-  ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr, bool is_write_,
-               uptr access_size_);
-  void Print();
-};
-
-struct ErrorAssumeDereferenceable : ErrorBase {
-  AddressDescription addr_description;
-  uptr pc, bp, sp;
-  uptr dereferenceable_size;
-
-  ErrorAssumeDereferenceable() = default;  // (*)
-  ErrorAssumeDereferenceable(u32 tid, uptr pc, uptr bp, uptr sp, uptr addr,
-                             uptr dereferenceable_size);
+  ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr,
+               AccessType access_type_, uptr access_size_);
   void Print();
 };
 
@@ -473,7 +464,6 @@ struct ErrorAssumeDereferenceable : ErrorBase {
   macro(BadParamsToCopyContiguousContainerAnnotations)     \
   macro(ODRViolation)                                      \
   macro(InvalidPointerPair)                                \
-  macro(AssumeDereferenceable)                             \
   macro(Generic)
 // clang-format on
 
diff --git a/compiler-rt/lib/asan/asan_report.cpp b/compiler-rt/lib/asan/asan_report.cpp
index 7c9ebbd203db9..c5a27eea7dd8d 100644
--- a/compiler-rt/lib/asan/asan_report.cpp
+++ b/compiler-rt/lib/asan/asan_report.cpp
@@ -538,7 +538,9 @@ void ReportGenericError(uptr pc, uptr bp, uptr sp, uptr addr, bool is_write,
   (void)exp;
 
   ScopedInErrorReport in_report(fatal);
-  ErrorGeneric error(GetCurrentTidOrInvalid(), pc, bp, sp, addr, is_write,
+  ErrorGeneric error(GetCurrentTidOrInvalid(), pc, bp, sp, addr,
+                     is_write ? ErrorGeneric::AccessType::Write
+                              : ErrorGeneric::AccessType::Read,
                      access_size);
   in_report.ReportError(error);
 }
@@ -550,8 +552,10 @@ void ReportAssumeDereferenceableError(uptr pc, uptr bp, uptr sp, uptr addr,
   ENABLE_FRAME_POINTER;
 
   ScopedInErrorReport in_report(fatal);
-  ErrorAssumeDereferenceable error(GetCurrentTidOrInvalid(), pc, bp, sp, addr,
-                                   dereferenceable_size);
+  ErrorGeneric error(GetCurrentTidOrInvalid(), pc, bp, sp, addr,
+                     ErrorGeneric::AccessType::Assumption,
+                     dereferenceable_size);
+  error.bug_descr = "dereferenceable-assumption-violation";
   in_report.ReportError(error);
 }
 
@@ -612,7 +616,8 @@ uptr __asan_get_report_address() {
 
 int __asan_get_report_access_type() {
   if (ScopedInErrorReport::CurrentError().kind == kErrorKindGeneric)
-    return ScopedInErrorReport::CurrentError().Generic.is_write;
+    return ScopedInErrorReport::CurrentError().Generic.access_type ==
+           ErrorGeneric::AccessType::Write;
   return 0;
 }
 
diff --git a/compiler-rt/test/asan/TestCases/assume_dereferenceable.cpp b/compiler-rt/test/asan/TestCases/assume_dereferenceable.cpp
index e685deed1409e..f4d493b61276f 100644
--- a/compiler-rt/test/asan/TestCases/assume_dereferenceable.cpp
+++ b/compiler-rt/test/asan/TestCases/assume_dereferenceable.cpp
@@ -7,10 +7,8 @@ void test_malloc_fully_oob() {
   char *p = (char *)malloc(10);
   fprintf(stderr, "test_malloc_fully_oob\n");
   // CHECK: test_malloc_fully_oob
-  // CHECK: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR1:0x[0-9a-fA-F]+]]
-  // CHECK: DEREFERENCABLE ASSUMPTION of size 20 at [[PTR1]] thread T0
-  // CHECK-NEXT: range [[[PTR1]], 0x{{.*}}) is dereferenceable
-  // CHECK-NEXT: range [0x{{.*}}, 0x{{.*}}) is NOT dereferenceable
+  // CHECK: ERROR: AddressSanitizer: dereferenceable-assumption-violation on address [[PTR1:0x[0-9a-fA-F]+]]
+  // CHECK: DEREFERENCEABLE ASSUMPTION of size 20 at [[PTR1]] thread T0
   __builtin_assume_dereferenceable(p, 20);
   free(p);
 }
@@ -19,10 +17,8 @@ void test_malloc_partial_right() {
   char *p = (char *)malloc(10);
   fprintf(stderr, "test_malloc_partial_right\n");
   // CHECK: test_malloc_partial_right
-  // CHECK: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR2:0x[0-9a-fA-F]+]]
-  // CHECK: DEREFERENCABLE ASSUMPTION of size 10 at [[PTR2]] thread T0
-  // CHECK-NEXT: range [[[PTR2]], 0x{{.*}}) is dereferenceable
-  // CHECK-NEXT: range [0x{{.*}}, 0x{{.*}}) is NOT dereferenceable
+  // CHECK: ERROR: AddressSanitizer: dereferenceable-assumption-violation on address [[PTR2:0x[0-9a-fA-F]+]]
+  // CHECK: DEREFERENCEABLE ASSUMPTION of size 10 at [[PTR2]] thread T0
   __builtin_assume_dereferenceable(p + 5, 10);
   free(p);
 }
@@ -31,10 +27,8 @@ void test_malloc_partial_left() {
   char *p = (char *)malloc(10);
   fprintf(stderr, "test_malloc_partial_left\n");
   // CHECK: test_malloc_partial_left
-  // CHECK: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR3:0x[0-9a-fA-F]+]]
-  // CHECK: DEREFERENCABLE ASSUMPTION of size 10 at [[PTR3]] thread T0
-  // CHECK-NEXT: range [[[PTR3]], 0x{{.*}}) is NOT dereferenceable
-  // CHECK-NEXT: range [0x{{.*}}, 0x{{.*}}) is dereferenceable
+  // CHECK: ERROR: AddressSanitizer: dereferenceable-assumption-violation on address [[PTR3:0x[0-9a-fA-F]+]]
+  // CHECK: DEREFERENCEABLE ASSUMPTION of size 10 at [[PTR3]] thread T0
   __builtin_assume_dereferenceable(p - 5, 10);
   free(p);
 }
@@ -43,8 +37,8 @@ void test_stack_fully_oob(int i) {
   char p[10];
   fprintf(stderr, "test_stack_fully_oob\n");
   // CHECK: test_stack_fully_oob
-  // CHECK: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR4:0x[0-9a-fA-F]+]]
-  // CHECK: DEREFERENCABLE ASSUMPTION of size 100 at [[PTR4]] thread T0
+  // CHECK: ERROR: AddressSanitizer: dereferenceable-assumption-violation on address [[PTR4:0x[0-9a-fA-F]+]]
+  // CHECK: DEREFERENCEABLE ASSUMPTION of size 100 at [[PTR4]] thread T0
   __builtin_assume_dereferenceable(p, 100);
 
   // This is here just to force ASan to emit instrumentation for poisoning
@@ -57,10 +51,8 @@ void test_stack_partial_right() {
   char p[10];
   fprintf(stderr, "test_stack_partial_right\n");
   // CHECK: test_stack_partial_right
-  // CHECK: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR5:0x[0-9a-fA-F]+]]
-  // CHECK: DEREFERENCABLE ASSUMPTION of size 10 at [[PTR5]] thread T0
-  // CHECK-NEXT: range [[[PTR5]], 0x{{.*}}) is dereferenceable
-  // CHECK-NEXT: range [0x{{.*}}, 0x{{.*}}) is NOT dereferenceable
+  // CHECK: ERROR: AddressSanitizer: dereferenceable-assumption-violation on address [[PTR5:0x[0-9a-fA-F]+]]
+  // CHECK: DEREFERENCEABLE ASSUMPTION of size 10 at [[PTR5]] thread T0
   __builtin_assume_dereferenceable(p + 5, 10);
 }
 
@@ -68,10 +60,8 @@ void test_stack_partial_left() {
   char p[10];
   fprintf(stderr, "test_stack_partial_left\n");
   // CHECK: test_stack_partial_left
-  // CHECK: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR6:0x[0-9a-fA-F]+]]
-  // CHECK: DEREFERENCABLE ASSUMPTION of size 10 at [[PTR6]] thread T0
-  // CHECK-NEXT: range [[[PTR6]], 0x{{.*}}) is NOT dereferenceable
-  // CHECK-NEXT: range [0x{{.*}}, 0x{{.*}}) is dereferenceable
+  // CHECK: ERROR: AddressSanitizer: dereferenceable-assumption-violation on address [[PTR6:0x[0-9a-fA-F]+]]
+  // CHECK: DEREFERENCEABLE ASSUMPTION of size 10 at [[PTR6]] thread T0
   __builtin_assume_dereferenceable(p - 5, 10);
 }
 
@@ -80,10 +70,8 @@ void test_malloc_completely_poisoned() {
   free(p);
   fprintf(stderr, "test_malloc_completely_poisoned\n");
   // CHECK: test_malloc_completely_poisoned
-  // CHECK: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR7:0x[0-9a-fA-F]+]]
-  // CHECK: DEREFERENCABLE ASSUMPTION of size 10 at [[PTR7]] thread T0
-  // CHECK-NEXT: range [[[PTR7]], 0x{{.*}}) is NOT dereferenceable
-  // CHECK-NOT: is dereferenceable
+  // CHECK: ERROR: AddressSanitizer: dereferenceable-assumption-violation on address [[PTR7:0x[0-9a-fA-F]+]]
+  // CHECK: DEREFERENCEABLE ASSUMPTION of size 10 at [[PTR7]] thread T0
   __builtin_assume_dereferenceable(p, 10);
 }
 
diff --git a/compiler-rt/test/asan/TestCases/assume_dereferenceable_fully_poisoned.cpp b/compiler-rt/test/asan/TestCases/assume_dereferenceable_fully_poisoned.cpp
index 50ceb8c597c40..bea3e980b3816 100644
--- a/compiler-rt/test/asan/TestCases/assume_dereferenceable_fully_poisoned.cpp
+++ b/compiler-rt/test/asan/TestCases/assume_dereferenceable_fully_poisoned.cpp
@@ -5,10 +5,8 @@
 int main() {
   char *p = (char *)malloc(10);
   free(p);
-  // CHECK: AddressSanitizer: dereferencable-assumption-violation
-  // CHECK: ASSUMPTION of size 10
-  // CHECK-NEXT: range [0x{{.*}}, 0x{{.*}}) is NOT dereferenceable
-  // CHECK-NOT: is dereferenceable
+  // CHECK: AddressSanitizer: dereferenceable-assumption-violation
+  // CHECK: DEREFERENCEABLE ASSUMPTION of size 10
   __builtin_assume_dereferenceable(p, 10);
   return 0;
 }
diff --git a/compiler-rt/test/asan/TestCases/assume_dereferenceable_halt_on_error.cpp b/compiler-rt/test/asan/TestCases/assume_dereferenceable_halt_on_error.cpp
index a91a83687389d..f382a2955308e 100644
--- a/compiler-rt/test/asan/TestCases/assume_dereferenceable_halt_on_error.cpp
+++ b/compiler-rt/test/asan/TestCases/assume_dereferenceable_halt_on_error.cpp
@@ -8,9 +8,9 @@
 int main() {
   char *p = (char *)malloc(10);
 
-  // CHECK: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR:0x[0-9a-fA-F]+]]
+  // CHECK: ERROR: AddressSanitizer: dereferenceable-assumption-violation on address [[PTR:0x[0-9a-fA-F]+]]
 
-  // CHECK-RECOVER: ERROR: AddressSanitizer: dereferencable-assumption-violation on address [[PTR:0x[0-9a-fA-F]+]]
+  // CHECK-RECOVER: ERROR: AddressSanitizer: dereferenceable-assumption-violation on address [[PTR:0x[0-9a-fA-F]+]]
   __builtin_assume_dereferenceable(p, 20);
   free(p);
 



More information about the llvm-commits mailing list