Skip to content

[Clang] Prevent null pointer dereference in target attribute mangling #94228

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 3 commits into from
Jun 3, 2024

Conversation

smanna12
Copy link
Contributor

@smanna12 smanna12 commented Jun 3, 2024

This patch adds assertions in the getMangledNameImpl() function to ensure that the expected target attributes (TargetAttr, TargetVersionAttr, and TargetClonesAttr) are not null before they are passed to appendAttributeMangling() to prevent potential null pointer dereferences and improve the robustness of the attribute mangling process.

This assertion will trigger a runtime error with a clear message in debug build if any of the expected attributes are missing, facilitating early and easier diagnosis and debugging of such issues related to attribute mangling.

This patch adds an assert in getMangledNameImpl() to ensure that the
expected attributes (TargetAttr, TargetVersionAttr, and
TargetClonesAttr) are checked for NULL value before being used by appendAttributeMangling().
@smanna12 smanna12 requested a review from tahonermann June 3, 2024 14:23
@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 adds an assert in getMangledNameImpl() to ensure that the expected attributes (TargetAttr, TargetVersionAttr, and TargetClonesAttr) are checked for NULL value before being used by appendAttributeMangling().


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

1 Files Affected:

  • (modified) clang/lib/CodeGen/CodeGenModule.cpp (+3)
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index c2314c3a57d33..7541817e41e86 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1853,18 +1853,21 @@ static std::string getMangledNameImpl(CodeGenModule &CGM, GlobalDecl GD,
         break;
       case MultiVersionKind::Target: {
         auto *Attr = FD->getAttr<TargetAttr>();
+        assert(Attr && "Expected TargetAttr to be present for attribute mangling");
         const ABIInfo &Info = CGM.getTargetCodeGenInfo().getABIInfo();
         Info.appendAttributeMangling(Attr, Out);
         break;
       }
       case MultiVersionKind::TargetVersion: {
         auto *Attr = FD->getAttr<TargetVersionAttr>();
+        assert(Attr && "Expected TargetVersionAttr to be present for attribute mangling");
         const ABIInfo &Info = CGM.getTargetCodeGenInfo().getABIInfo();
         Info.appendAttributeMangling(Attr, Out);
         break;
       }
       case MultiVersionKind::TargetClones: {
         auto *Attr = FD->getAttr<TargetClonesAttr>();
+        assert(Attr && "Expected TargetClonesAttr to be present for attribute mangling");
         unsigned Index = GD.getMultiVersionIndex();
         const ABIInfo &Info = CGM.getTargetCodeGenInfo().getABIInfo();
         Info.appendAttributeMangling(Attr, Index, Out);

Copy link

github-actions bot commented Jun 3, 2024

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

@smanna12
Copy link
Contributor Author

smanna12 commented Jun 3, 2024

Thank you @efriedma-quic for reviews!

@smanna12 smanna12 merged commit ccaccc3 into llvm:main Jun 3, 2024
7 checks passed
@smanna12 smanna12 deleted the FixTargetAttrsNullPointer branch June 3, 2024 23:22
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