[compiler-rt] [ubsan] Use internal_memcpy to copy ubsan bits size (PR #121586)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 3 09:38:27 PST 2025


https://github.com/earnol updated https://github.com/llvm/llvm-project/pull/121586

>From 7fb7b69cf2f712208824332f761ecabdef76dcfa Mon Sep 17 00:00:00 2001
From: Vladislav Aranov <vladislav.aranov at ericsson.com>
Date: Fri, 3 Jan 2025 11:56:10 -0500
Subject: [PATCH] [ubsan] Use internal_memcpy to copy ubsan bits size
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

While fetching amounts of bits used to correctly display ubsan
value reinterpret_cast was used, however as noted by Jakub
JelĂ­nek in https://github.com/llvm/llvm-project/pull/96240
discussion it might cause issues due to potentially unaligned
memory access. The patch addresses this problem.
---
 compiler-rt/lib/ubsan/ubsan_value.h | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/compiler-rt/lib/ubsan/ubsan_value.h b/compiler-rt/lib/ubsan/ubsan_value.h
index 430c9ea0dc8d15..f178c884dede4a 100644
--- a/compiler-rt/lib/ubsan/ubsan_value.h
+++ b/compiler-rt/lib/ubsan/ubsan_value.h
@@ -150,9 +150,12 @@ class TypeDescriptor {
 
   unsigned getIntegerBitCount() const {
     DCHECK(isIntegerTy());
-    if (isSignedBitIntTy())
-      return *reinterpret_cast<const u32 *>(getBitIntBitCountPointer());
-    else
+    if (isSignedBitIntTy()) {
+      u32 BitCountValue;
+      internal_memcpy(&BitCountValue, getBitIntBitCountPointer(),
+                      sizeof(BitCountValue)); 
+      return BitCountValue;
+    } else
       return getIntegerBitWidth();
   }
 



More information about the llvm-commits mailing list