Skip to content

Conversation

tgs-sc
Copy link
Contributor

@tgs-sc tgs-sc commented Aug 18, 2025

While computing class layout, I met with infinite recursion. This happened while executing user expression in lldb as record type was incorrectly received from dwarf. This assert will replace the infinite recursion with an error and will simplify further debugging of such cases.

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:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 18, 2025

@llvm/pr-subscribers-clang

Author: None (tgs-sc)

Changes

While computing class layout, I met with infinite recursion. This happened while executing user expression in lldb as record type was incorrectly received from dwarf. This assert will replace the infinite recursion with an error and will simplify further debugging of such cases.


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

1 Files Affected:

  • (modified) clang/lib/AST/RecordLayoutBuilder.cpp (+2)
diff --git a/clang/lib/AST/RecordLayoutBuilder.cpp b/clang/lib/AST/RecordLayoutBuilder.cpp
index 6d819031cbef4..93571543f1c7d 100644
--- a/clang/lib/AST/RecordLayoutBuilder.cpp
+++ b/clang/lib/AST/RecordLayoutBuilder.cpp
@@ -187,6 +187,8 @@ void EmptySubobjectMap::ComputeEmptySubobjectSizes() {
   // Check the bases.
   for (const CXXBaseSpecifier &Base : Class->bases()) {
     const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
+    // Assert to prevent infinite recursion.
+    assert(BaseDecl != Class && "Class cannot inherit from itself.");
 
     CharUnits EmptySize;
     const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);

@tgs-sc
Copy link
Contributor Author

tgs-sc commented Aug 22, 2025

@fhahn, @AaronBallman, can you please look at his?

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

Because this can be hit by user-written expressions, I think an assert is actually the wrong fix; we should handle it gracefully. Otherwise, in asserts builds this is effectively a crash and in non-asserts builds it's still an infinite recursion.

CC @JDevlieghere @Michael137 for other opinions on the lldb side

@yronglin
Copy link
Contributor

Because this can be hit by user-written expressions

Does infinite recursion mean that we are building the ill-formed AST for the expression entered by the user?

@tgs-sc
Copy link
Contributor Author

tgs-sc commented Aug 22, 2025

Because this can be hit by user-written expressions, I think an assert is actually the wrong fix; we should handle it gracefully. Otherwise, in asserts builds this is effectively a crash and in non-asserts builds it's still an infinite recursion.

CC @JDevlieghere @Michael137 for other opinions on the lldb side

The user expression only triggered parsing the part of dwarf that was incorrectly created.

in asserts builds this is effectively a crash

Yes, that's exactly the reason why i want to add this assert here. Simply because it works better than infinite recursion.

This is an original PR, this patch was created from:
#154123

@JDevlieghere
Copy link
Member

We have an error handling & assert policy for LLDB. If this can be triggered by user input then it should not be an assert.

@Michael137
Copy link
Member

Michael137 commented Sep 8, 2025

Personally i think the assert makes sense here (if a user can't ever create an AST like that, which IIUC they can't). Although it is true that we try not to assert on user input as Jonas mentioned, this was a bug in LLDB's AST creation. Malformed DWARF can already cause all sorts of Clang assertions to trigger anyway, so rejecting adding an assert into Clang just because LLDB may create an incorrect AST out of it seems counter-productive

@tgs-sc
Copy link
Contributor Author

tgs-sc commented Sep 9, 2025

@fhahn, @AaronBallman, can you please give your opinion on this situation?

@Michael137
Copy link
Member

I think @AaronBallman is currently OOO until the end of the month. I'll go ahead and LGTM it, but please give @cor3ntin (or another maintainer) some time to interject.

…layout

While computing class layout, I met with infinite recursion. This
happened while executing user expression in lldb as record type was
incorrectly received from dwarf. This assert will replace the infinite
recursion with an error and will simplify further debugging of such cases.
@tgs-sc tgs-sc force-pushed the users/tgs-sc/clang-infinite-recursion-segfault branch from 0e76183 to 207f702 Compare September 9, 2025 12:44
@Michael137 Michael137 enabled auto-merge (squash) September 9, 2025 13:15
@Michael137 Michael137 merged commit 7d673ca into llvm:main Sep 9, 2025
9 checks passed
Copy link

github-actions bot commented Sep 9, 2025

@tgs-sc Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

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.

7 participants