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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: None (earnol)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/121586.diff


1 Files Affected:

- (modified) compiler-rt/lib/ubsan/ubsan_value.h (+5-2) 


``````````diff
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();
   }

``````````

</details>


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


More information about the llvm-commits mailing list