[clang] [compiler-rt] [UBSAN] add null and alignment checks for aggregates (PR #164548)

Hubert Tong via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 23 11:48:16 PDT 2025


================
@@ -2249,6 +2249,24 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, LValue Src, QualType Ty,
                                         bool isVolatile) {
   assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
 
+  // Sanitizer checks to verify source and destination pointers are
+  // non-null and properly aligned before copying.
+  // Without these checks, undefined behavior from invalid pointers goes undetected.
+  if (SanOpts.hasOneOf(SanitizerKind::Null | SanitizerKind::Alignment)) {
+    Address SrcAddr = Src.getAddress();
+    Address DestAddr = Dest.getAddress();
+
+    // Check source pointer for null and alignment violations
+    EmitTypeCheck(TCK_Load, SourceLocation(),
----------------
hubert-reinterpretcast wrote:

I think the scope should be expanded to other cases covered by `EmitCheckedLValue`.

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


More information about the llvm-commits mailing list