[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:23:15 PST 2025


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

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.

>From 4063ff6597f4893b06991696292e708d8ad21f4f 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 | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

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



More information about the llvm-commits mailing list