Skip to content

[Clang] Prevent null pointer dereferences in SVE tuple functions #94267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 1, 2024

Conversation

smanna12
Copy link
Contributor

@smanna12 smanna12 commented Jun 3, 2024

This patch

addresses a null pointer dereference issue reported by static analyzer tool in the
EmitSVETupleSetOrGet() and EmitSVETupleCreate() functions. Previously, the function
assumed that the result of dyn_cast<> to ScalableVectorType would always be non-null,
which is not guaranteed.

The fix introduces a null check after the dyn_cast<> operation. If the cast fails and
SingleVecTy is null, the function now returns nullptr to indicate an error. This prevents the
dereference of a null pointer, which could lead to undefined behavior.

Additionally, the assert message has been corrected to accurately reflect the expected
conditions.

These changes collectively enhance the robustness of the code by ensuring type safety and preventing runtime errors due to improper type casting.

This patch replaces dyn_cast<> with cast<> for obtaining ScalableVectorType pointers in the EmitSVETupleSetOrGet() and EmitSVETupleCreate() functions to ensure that the code expects valid ScalableVectorType instances and will cause an assertion if the cast is invalid, preventing possible null pointer dereferences and improving codes safety.
@smanna12 smanna12 requested a review from tahonermann June 3, 2024 18:26
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Jun 3, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 3, 2024

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: None (smanna12)

Changes

This patch replaces dyn_cast<> with cast<> for obtaining ScalableVectorType pointers in the EmitSVETupleSetOrGet() and EmitSVETupleCreate() functions to ensure that the code expects valid ScalableVectorType instances and will cause an assertion if the cast is invalid, preventing possible null pointer dereferences and improving codes safety.


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

1 Files Affected:

  • (modified) clang/lib/CodeGen/CGBuiltin.cpp (+2-2)
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 37d0c478e0330..3ba9f4693170f 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -10211,7 +10211,7 @@ Value *CodeGenFunction::EmitSVETupleSetOrGet(const SVETypeFlags &TypeFlags,
          "Expects TypleFlag isTupleSet or TypeFlags.isTupleSet()");
 
   unsigned I = cast<ConstantInt>(Ops[1])->getSExtValue();
-  auto *SingleVecTy = dyn_cast<llvm::ScalableVectorType>(
+  auto *SingleVecTy = cast<llvm::ScalableVectorType>(
                       TypeFlags.isTupleSet() ? Ops[2]->getType() : Ty);
   Value *Idx = ConstantInt::get(CGM.Int64Ty,
                                 I * SingleVecTy->getMinNumElements());
@@ -10226,7 +10226,7 @@ Value *CodeGenFunction::EmitSVETupleCreate(const SVETypeFlags &TypeFlags,
                                              ArrayRef<Value *> Ops) {
   assert(TypeFlags.isTupleCreate() && "Expects TypleFlag isTupleCreate");
 
-  auto *SrcTy = dyn_cast<llvm::ScalableVectorType>(Ops[0]->getType());
+  auto *SrcTy = cast<llvm::ScalableVectorType>(Ops[0]->getType());
   unsigned MinElts = SrcTy->getMinNumElements();
   Value *Call = llvm::PoisonValue::get(Ty);
   for (unsigned I = 0; I < Ops.size(); I++) {

@smanna12 smanna12 changed the title [Clang] Prevent null pointer dereferencein SVE tuple functions [Clang] Prevent null pointer dereference in SVE tuple functions Jun 3, 2024
@smanna12 smanna12 changed the title [Clang] Prevent null pointer dereference in SVE tuple functions [Clang] Prevent null pointer dereferences in SVE tuple functions Jun 3, 2024
Copy link

github-actions bot commented Jun 3, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@CarolineConcatto CarolineConcatto left a comment

Choose a reason for hiding this comment

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

Thank you @smanna12.

@smanna12
Copy link
Contributor Author

smanna12 commented Jul 1, 2024

Thank you @CarolineConcatto for reviews!

@smanna12 smanna12 merged commit 05d8ea7 into llvm:main Jul 1, 2024
7 checks passed
@smanna12 smanna12 deleted the FixNullIssue branch July 1, 2024 15:52
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
…m#94267)

This patch 

addresses a null pointer dereference issue reported by static analyzer
tool in the
`EmitSVETupleSetOrGet()` and `EmitSVETupleCreate()` functions.
Previously, the function
assumed that the result of `dyn_cast<>` to `ScalableVectorType` would
always be non-null,
    which is not guaranteed.

The fix introduces a null check after the `dyn_cast<>` operation. If the
cast fails and
`SingleVecTy` is null, the function now returns `nullptr` to indicate an
error. This prevents the
  dereference of a null pointer, which could lead to undefined behavior.

Additionally, the assert message has been corrected to accurately
reflect the expected
   conditions.

These changes collectively enhance the robustness of the code by
ensuring type safety and preventing runtime errors due to improper type
casting.
kbluck pushed a commit to kbluck/llvm-project that referenced this pull request Jul 6, 2024
…m#94267)

This patch 

addresses a null pointer dereference issue reported by static analyzer
tool in the
`EmitSVETupleSetOrGet()` and `EmitSVETupleCreate()` functions.
Previously, the function
assumed that the result of `dyn_cast<>` to `ScalableVectorType` would
always be non-null,
    which is not guaranteed.

The fix introduces a null check after the `dyn_cast<>` operation. If the
cast fails and
`SingleVecTy` is null, the function now returns `nullptr` to indicate an
error. This prevents the
  dereference of a null pointer, which could lead to undefined behavior.

Additionally, the assert message has been corrected to accurately
reflect the expected
   conditions.

These changes collectively enhance the robustness of the code by
ensuring type safety and preventing runtime errors due to improper type
casting.
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants