Skip to content

Conversation

@vasu-the-sharma
Copy link

@vasu-the-sharma vasu-the-sharma commented Oct 22, 2025

Description

This PR adds sanitizer checks for null pointers and misalignment during aggregate copy operations in EmitAggregateCopy.

Current State

When copying aggregates (structs), the compiler does not validate that source and destination pointers are non-null or properly aligned when sanitizers are enabled. This allows undefined behavior from invalid pointers to go undetected at runtime.

Solution

When null or alignment sanitizers are active, this change:

  • Retrieves raw pointer addresses from both source and destination LValues
  • Performs EmitTypeCheck on both pointers to verify they are non-null and meet alignment requirements
  • Records these checks in the SanitizerSet for runtime emission

This ensures memory safety violations are caught early, preventing crashes and corruption from invalid aggregate copies.

Testing

Added comprehensive test cases covering:

  • Null source pointer detection
  • Null destination pointer detection
  • Misaligned source pointer detection
  • Misaligned destination pointer detection
  • Normal copy operations (no false positives)

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Oct 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 22, 2025

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

@llvm/pr-subscribers-clang

Author: VASU SHARMA (vasu-the-sharma)

Changes

This PR adds null and alignment checks for aggregates like scalars


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

1 Files Affected:

  • (modified) clang/lib/CodeGen/CGExprAgg.cpp (+15)
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index eee397f1f3d19..de6d80a273dbd 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2249,6 +2249,21 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, LValue Src, QualType Ty,
                                         bool isVolatile) {
   assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
 
+  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(),
+                  SrcAddr.emitRawPointer(*this), Ty, SrcAddr.getAlignment(),
+                  SanitizerSet());
+
+    // Check destination pointer for null and alignment violations
+    EmitTypeCheck(TCK_Store, SourceLocation(),
+                  DestAddr.emitRawPointer(*this), Ty, DestAddr.getAlignment(),
+                  SanitizerSet());
+  }
+
   Address DestPtr = Dest.getAddress();
   Address SrcPtr = Src.getAddress();
 

@github-actions
Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions cpp -- clang/lib/CodeGen/CGExprAgg.cpp --diff_from_common_commit

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index de6d80a27..1d6e3c101 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2254,14 +2254,12 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, LValue Src, QualType Ty,
     Address DestAddr = Dest.getAddress();
 
     // Check source pointer for null and alignment violations
-    EmitTypeCheck(TCK_Load, SourceLocation(),
-                  SrcAddr.emitRawPointer(*this), Ty, SrcAddr.getAlignment(),
-                  SanitizerSet());
+    EmitTypeCheck(TCK_Load, SourceLocation(), SrcAddr.emitRawPointer(*this), Ty,
+                  SrcAddr.getAlignment(), SanitizerSet());
 
     // Check destination pointer for null and alignment violations
-    EmitTypeCheck(TCK_Store, SourceLocation(),
-                  DestAddr.emitRawPointer(*this), Ty, DestAddr.getAlignment(),
-                  SanitizerSet());
+    EmitTypeCheck(TCK_Store, SourceLocation(), DestAddr.emitRawPointer(*this),
+                  Ty, DestAddr.getAlignment(), SanitizerSet());
   }
 
   Address DestPtr = Dest.getAddress();

Copy link
Contributor

@tonykuttai tonykuttai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test support this change

bool isVolatile) {
assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");

if (SanOpts.hasOneOf(SanitizerKind::Null | SanitizerKind::Alignment)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add comment block here to explain

  • why we need this if block
  • what is it doing.?

Copy link
Collaborator

@hubert-reinterpretcast hubert-reinterpretcast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe there should be a Clang CodeGen test for this.

Address DestAddr = Dest.getAddress();

// Check source pointer for null and alignment violations
EmitTypeCheck(TCK_Load, SourceLocation(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

// 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)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend omitting this check and leaving sanitizePerformTypeCheck to do its work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category compiler-rt:sanitizer compiler-rt:ubsan Undefined behavior sanitizer compiler-rt

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants