Skip to content

[ItaniumMangle] Add substitutions for record types when mangling vtables #109970

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 7 commits into from
Sep 29, 2024

Conversation

tcwzxx
Copy link
Member

@tcwzxx tcwzxx commented Sep 25, 2024

Fix #108015

The mangleNameOrStandardSubstitution function does not add the RD type into the substitution, which causes the mangling of the <base type> to be incorrect.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 25, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 25, 2024

@llvm/pr-subscribers-clang

Author: tcwzxx (tcwzxx)

Changes

Fix #108015

The mangleNameOrStandardSubstitution function does not add the RD type into the substitution, which causes the mangling of the &lt;base type&gt; to be incorrect.


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

2 Files Affected:

  • (modified) clang/lib/AST/ItaniumMangle.cpp (+4-2)
  • (modified) clang/test/CodeGenCXX/mangle.cpp (+24)
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index b6e1da0c3192da..962e62ef86c0ca 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -7326,11 +7326,13 @@ void ItaniumMangleContextImpl::mangleCXXCtorVTable(const CXXRecordDecl *RD,
                                                    raw_ostream &Out) {
   // <special-name> ::= TC <type> <offset number> _ <base type>
   CXXNameMangler Mangler(*this, Out);
+  QualType RDType = getASTContext().getRecordType(RD);
+  QualType TypeType = getASTContext().getRecordType(Type);
   Mangler.getStream() << "_ZTC";
-  Mangler.mangleNameOrStandardSubstitution(RD);
+  Mangler.mangleType(RDType);
   Mangler.getStream() << Offset;
   Mangler.getStream() << '_';
-  Mangler.mangleNameOrStandardSubstitution(Type);
+  Mangler.mangleType(TypeType);
 }
 
 void ItaniumMangleContextImpl::mangleCXXRTTI(QualType Ty, raw_ostream &Out) {
diff --git a/clang/test/CodeGenCXX/mangle.cpp b/clang/test/CodeGenCXX/mangle.cpp
index d0800af55c87e8..848b026028be76 100644
--- a/clang/test/CodeGenCXX/mangle.cpp
+++ b/clang/test/CodeGenCXX/mangle.cpp
@@ -11,6 +11,8 @@ struct Y { };
 //CHECK: @pr5966_i = external global
 //CHECK: @_ZL8pr5966_j = internal global
 
+//CHECK: @_ZTCN6test624InstE0_NS_1A4ImplINS1_4WrapEEE
+
 // CHECK-LABEL: define{{.*}} zeroext i1 @_ZplRK1YRA100_P1X
 bool operator+(const Y&, X* (&xs)[100]) { return false; }
 
@@ -1214,3 +1216,25 @@ namespace test61 {
   // CHECK-LABEL: @_ZN6test611fINS_1XEEEvNT_1Y1aENS3_1bE
   template void f<X>(int, int);
 }
+
+namespace test62 {
+namespace A {
+
+class VBase {
+ public:
+  virtual ~VBase() {};
+};
+
+struct Wrap {};
+
+template <typename T>
+class Impl : public virtual VBase {
+ public:
+};
+
+}  // namespace A
+
+struct Inst : public A::Impl<A::Wrap> {};
+
+void Test() { Inst a; }
+}

@tcwzxx tcwzxx changed the title Use mangleType instead of mangleNameOrStandardSubstitution in mangleCXXCtorVTable function [ItaniumMangle] Use mangleType instead of mangleNameOrStandardSubstitution in mangleCXXCtorVTable function Sep 25, 2024
Copy link
Contributor

@mizvekov mizvekov left a comment

Choose a reason for hiding this comment

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

LGTM, Thanks!

Can you also add a release note, mentioning the potential ABI breaking change?

Please wait 24h before merging in case the other reviewers want to chime in.

@zyn0217
Copy link
Contributor

zyn0217 commented Sep 26, 2024

Do we need an ABI flag guarding against the change as well?

@zygoloid
Copy link
Collaborator

Do we need an ABI flag guarding against the change as well?

Yeah, I think this change should respect -fclang-abi-compat.

@tcwzxx
Copy link
Member Author

tcwzxx commented Sep 27, 2024

Thanks for the review. I have added the ABI version 19 and the release notes.

Copy link

github-actions bot commented Sep 27, 2024

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

Copy link
Contributor

@zyn0217 zyn0217 left a comment

Choose a reason for hiding this comment

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

Some nits, otherwise LGTM as well.

Comment on lines +4 to +5
//CHECK: @_ZTCN16MangleCtorVTable4InstE0_NS_1A4ImplINS1_4WrapEEE
//CHECK-CLANG-19: @_ZTCN16MangleCtorVTable4InstE0_NS_1A4ImplINS0_4WrapEEE
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 move these two checks directly above the corresponding codes? Thanks!

Copy link
Member Author

@tcwzxx tcwzxx Sep 27, 2024

Choose a reason for hiding this comment

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

The construction vtable is a global variable, so it cannot be moved above the corresponding code.

@@ -237,6 +237,11 @@ class LangOptionsBase {
/// in the initializers of members of local classes.
Ver18,

/// Attempt to be ABI-compatible with code generated by Clang 19.0.x.
/// This causes clang to:
/// - Incorrect Mangling of CXXCtorVTable
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// - Incorrect Mangling of CXXCtorVTable
/// - Incorrectly mangle CXX vtable substitutions in some cases...

It would be great if you could flesh out the behavior changes here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. Updated.

@mizvekov mizvekov changed the title [ItaniumMangle] Use mangleType instead of mangleNameOrStandardSubstitution in mangleCXXCtorVTable function [ItaniumMangle] Add substitutions for record types when mangling vtables Sep 29, 2024
@tcwzxx tcwzxx merged commit 7883b02 into llvm:main Sep 29, 2024
9 checks passed
@tcwzxx tcwzxx deleted the mangle branch October 8, 2024 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ABI] mangleCXXCtorVTable mangles an incorrect name
5 participants