[llvm] [llubi] Use correct tag bitwidth to recover provenances (PR #230852)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 10 13:22:09 PDT 2026


https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/230852

Tag always uses the pointer width rather than the padded one. Previously the tag lookup always missed due to the width mismatch.

The test is generated by DeepSeek-V4.1-Flash.


>From 7116aca480a7f317eb0b44161e63862aa91a1b6d Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sun, 11 Oct 2026 04:16:13 +0800
Subject: [PATCH 1/2] [llubi] Add pre-commit tests. NFC.

---
 llvm/test/tools/llubi/non_byte_size_ptr.ll | 25 ++++++++++++++++++++++
 1 file changed, 25 insertions(+)
 create mode 100644 llvm/test/tools/llubi/non_byte_size_ptr.ll

diff --git a/llvm/test/tools/llubi/non_byte_size_ptr.ll b/llvm/test/tools/llubi/non_byte_size_ptr.ll
new file mode 100644
index 0000000000000..977fd6693b7cf
--- /dev/null
+++ b/llvm/test/tools/llubi/non_byte_size_ptr.ll
@@ -0,0 +1,25 @@
+; NOTE: Assertions have been autogenerated by utils/update_llubi_test_checks.py UTC_ARGS: --version 6
+; RUN: not llubi --verbose < %s 2>&1 | FileCheck %s
+
+target datalayout = "e-p:52:64"
+
+ at g = global i8 0
+
+; The pointer is padded with 4 bits. Make sure we use the tag with correct bitwidth to recover
+; the provenance.
+
+define void @main() {
+  %p = alloca ptr
+  store ptr @g, ptr %p
+  %q = load ptr, ptr %p
+  load i8, ptr %q
+  ret void
+}
+; CHECK: Entering function: main
+; CHECK-NEXT:   %p = alloca ptr, align 8 => ptr 0x10 [p]
+; CHECK-NEXT:   store ptr @g, ptr %p, align 8
+; CHECK-NEXT:   %q = load ptr, ptr %p, align 8 => ptr 0x8 [nullary]
+; CHECK-NEXT: Stacktrace:
+; CHECK-NEXT: #0   %1 = load i8, ptr %q, align 1 at @main <stdin>:15
+; CHECK-NEXT: Immediate UB detected: Invalid memory access via a pointer with nullary provenance.
+; CHECK-NEXT: error: Execution of function 'main' failed.

>From b27fa4c0d198d49dd4708551f58d92b7e6ed0100 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sun, 11 Oct 2026 04:17:56 +0800
Subject: [PATCH 2/2] [llubi] Use correct tag bitwidth to recover provenances

---
 llvm/test/tools/llubi/non_byte_size_ptr.ll | 11 +++++------
 llvm/tools/llubi/lib/Context.cpp           |  2 +-
 2 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/llvm/test/tools/llubi/non_byte_size_ptr.ll b/llvm/test/tools/llubi/non_byte_size_ptr.ll
index 977fd6693b7cf..1b12c813bd3eb 100644
--- a/llvm/test/tools/llubi/non_byte_size_ptr.ll
+++ b/llvm/test/tools/llubi/non_byte_size_ptr.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llubi_test_checks.py UTC_ARGS: --version 6
-; RUN: not llubi --verbose < %s 2>&1 | FileCheck %s
+; RUN: llubi --verbose < %s 2>&1 | FileCheck %s
 
 target datalayout = "e-p:52:64"
 
@@ -18,8 +18,7 @@ define void @main() {
 ; CHECK: Entering function: main
 ; CHECK-NEXT:   %p = alloca ptr, align 8 => ptr 0x10 [p]
 ; CHECK-NEXT:   store ptr @g, ptr %p, align 8
-; CHECK-NEXT:   %q = load ptr, ptr %p, align 8 => ptr 0x8 [nullary]
-; CHECK-NEXT: Stacktrace:
-; CHECK-NEXT: #0   %1 = load i8, ptr %q, align 1 at @main <stdin>:15
-; CHECK-NEXT: Immediate UB detected: Invalid memory access via a pointer with nullary provenance.
-; CHECK-NEXT: error: Execution of function 'main' failed.
+; CHECK-NEXT:   %q = load ptr, ptr %p, align 8 => ptr 0x8 [@g]
+; CHECK-NEXT:   %1 = load i8, ptr %q, align 1 => i8 0
+; CHECK-NEXT:   ret void
+; CHECK-NEXT: Exiting function: main
diff --git a/llvm/tools/llubi/lib/Context.cpp b/llvm/tools/llubi/lib/Context.cpp
index ff4d14b5a2c1b..82a08b159ce09 100644
--- a/llvm/tools/llubi/lib/Context.cpp
+++ b/llvm/tools/llubi/lib/Context.cpp
@@ -561,7 +561,7 @@ AnyValue Context::fromBytes(ConstBytesView Bytes, Type *Ty,
   assert(Ty->isPointerTy() && "Expect a pointer type");
   // Try to recover provenance from the tag.
   if (IsTagValid) {
-    APInt Tag(NumBitsToExtract, RawTagBits);
+    APInt Tag(NumBits, RawTagBits);
     if (auto Prov = TaggedProvenances.lookup(Tag))
       return Pointer(std::move(Prov), Bits);
   }



More information about the llvm-commits mailing list