Skip to content
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

[Clang] Fix a crash when dumping a pack indexing type. #80439

Merged
merged 3 commits into from
Feb 2, 2024

Conversation

cor3ntin
Copy link
Contributor

@cor3ntin cor3ntin commented Feb 2, 2024

Fix a crash caused by incorrect assumptions
Reported here #72644 (comment)

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

llvmbot commented Feb 2, 2024

@llvm/pr-subscribers-clang

Author: cor3ntin (cor3ntin)

Changes

Fix a crash caused by incorrect assumptions
Reported here #72644 (comment)


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

2 Files Affected:

  • (modified) clang/lib/AST/TypePrinter.cpp (+3-3)
  • (added) clang/test/AST/ast-dump-pack-indexing-crash.cpp (+24)
diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp
index 63e56a8296db3..281f529ee1f75 100644
--- a/clang/lib/AST/TypePrinter.cpp
+++ b/clang/lib/AST/TypePrinter.cpp
@@ -1195,10 +1195,10 @@ void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) {
 
 void TypePrinter::printPackIndexingBefore(const PackIndexingType *T,
                                           raw_ostream &OS) {
-  if (T->isInstantiationDependentType())
-    OS << T->getPattern() << "...[" << T->getIndexExpr() << "]";
-  else
+  if (T->hasSelectedType())
     OS << T->getSelectedType();
+  else
+    OS << T->getPattern() << "...[" << T->getIndexExpr() << "]";
   spaceBeforePlaceHolder(OS);
 }
 
diff --git a/clang/test/AST/ast-dump-pack-indexing-crash.cpp b/clang/test/AST/ast-dump-pack-indexing-crash.cpp
new file mode 100644
index 0000000000000..1e4e38e2f7378
--- /dev/null
+++ b/clang/test/AST/ast-dump-pack-indexing-crash.cpp
@@ -0,0 +1,24 @@
+// RUN: not %clang_cc1 -std=c++2c -ast-dump %s | FileCheck  %s
+
+namespace InvalidPacksShouldNotCrash {
+
+struct NotAPack;
+template <typename T, auto V, template<typename> typename Tp>
+void not_pack() {
+    int i = 0;
+    i...[0]; // expected-error {{i does not refer to the name of a parameter pack}}
+    V...[0]; // expected-error {{V does not refer to the name of a parameter pack}}
+    NotAPack...[0] a; // expected-error{{'NotAPack' does not refer to the name of a parameter pack}}
+    T...[0] b;   // expected-error{{'T' does not refer to the name of a parameter pack}}
+    Tp...[0] c; // expected-error{{'Tp' does not refer to the name of a parameter pack}}
+}
+
+// CHECK:      -FunctionDecl {{.*}} not_pack 'void ()'
+// CHECK:           |-DeclStmt {{.*}}
+// CHECK:           | `-VarDecl {{.*}} a 'NotAPack...{{.*}}'
+// CHECK:           |-DeclStmt {{.*}}
+// CHECK:           | `-VarDecl {{.*}} 'T...{{.*}}'
+// CHECK:           `-DeclStmt {{.*}}
+// CHECK:             `-VarDecl {{.*}} c 'Tp...{{.*}}'
+
+}

Fix a crash caused by incorrect assumptions
Reported here llvm#72644 (comment)
}

// CHECK: -FunctionDecl {{.*}} not_pack 'void ()'
// CHECK: |-DeclStmt {{.*}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of testing the spaces/- and |- character, just use 'CHECK-NEXT`. Else this can get fraigle due to WS. Else the test looks fine.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So the fragility is from checking the - and the |- and the `| `` characters. I'd suggest removing those entirely.

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

LGTM other than generalizing the test a bit.

@cor3ntin cor3ntin merged commit 7a94acb into llvm:main Feb 2, 2024
3 of 4 checks passed
@bjope
Copy link
Collaborator

bjope commented Feb 4, 2024

Seems like this didn't really solve the problem reported by @bevin-hansson (at least not completely).
The new test case fails for us:

FAIL: Clang :: AST/ast-dump-pack-indexing-crash.cpp (401 of 78930)
******************** TEST 'Clang :: AST/ast-dump-pack-indexing-crash.cpp' FAILED ********************
Exit Code: 2

Command Output (stderr):
--
RUN: at line 1: not /build/bin/clang -cc1 -internal-isystem /build/lib/clang/19/include -nostdsysteminc -std=c++2c -ast-dump /llvm-project/clang/test/AST/ast-dump-pack-indexing-crash.cpp | /build/bin/FileCheck  /llvm-project/clang/test/AST/ast-dump-pack-indexing-crash.cpp
+ not /build/bin/clang -cc1 -internal-isystem /build/lib/clang/19/include -nostdsysteminc -std=c++2c -ast-dump /llvm-project/clang/test/AST/ast-dump-pack-indexing-crash.cpp
+ /build/bin/FileCheck /llvm-project/clang/test/AST/ast-dump-pack-indexing-crash.cpp
/llvm-project/clang/test/AST/ast-dump-pack-indexing-crash.cpp:9:5: error: i does not refer to the name of a parameter pack
    9 |     i...[0]; // expected-error {{i does not refer to the name of a parameter pack}}
      |     ^
/llvm-project/clang/test/AST/ast-dump-pack-indexing-crash.cpp:10:5: error: V does not refer to the name of a parameter pack
   10 |     V...[0]; // expected-error {{V does not refer to the name of a parameter pack}}
      |     ^
/llvm-project/clang/test/AST/ast-dump-pack-indexing-crash.cpp:11:5: error: 'NotAPack' does not refer to the name of a parameter pack
   11 |     NotAPack...[0] a; // expected-error{{'NotAPack' does not refer to the name of a parameter pack}}
      |     ^
Stack dump:
0.	Program arguments: /build/bin/clang -cc1 -internal-isystem /build/lib/clang/19/include -nostdsysteminc -std=c++2c -ast-dump /llvm-project/clang/test/AST/ast-dump-pack-indexing-crash.cpp
1.	/llvm-project/clang/test/AST/ast-dump-pack-indexing-crash.cpp:11:21: current parser token ';'
2.	/llvm-project/clang/test/AST/ast-dump-pack-indexing-crash.cpp:3:1: parsing namespace 'InvalidPacksShouldNotCrash'
3.	/llvm-project/clang/test/AST/ast-dump-pack-indexing-crash.cpp:7:17: parsing function body 'InvalidPacksShouldNotCrash::not_pack'
4.	/llvm-project/clang/test/AST/ast-dump-pack-indexing-crash.cpp:7:17: in compound statement ('{}')
  #0 0x0000000003b147c8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/build/bin/clang+0x3b147c8)
  #1 0x0000000003b11f1c SignalHandler(int) Signals.cpp:0:0
  #2 0x00007f49db4aa630 __restore_rt sigaction.c:0:0
  #3 0x00000000071e19f0 clang::PackIndexingType::getSelectedIndex() const (.part.0) Type.cpp:0:0
  #4 0x0000000006c9e257 clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e257)
  #5 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
  #6 0x0000000006c9e21b clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e21b)
  #7 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
  #8 0x0000000006c9e21b clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e21b)
  #9 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
 #10 0x0000000006c9e21b clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e21b)
 #11 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
 #12 0x0000000006c9e21b clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e21b)
 #13 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
 #14 0x0000000006c9e21b clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e21b)
 #15 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
 #16 0x0000000006c9e21b clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e21b)
 #17 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
 #18 0x0000000006c9e21b clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e21b)
 #19 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
 #20 0x0000000006c9e21b clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e21b)
 #21 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
 #22 0x0000000006c9e21b clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e21b)
 #23 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
 #24 0x0000000006c9e21b clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e21b)
 #25 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
 #26 0x0000000006c9e21b clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e21b)
 #27 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
 #28 0x0000000006c9e21b clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e21b)
 #29 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
 #30 0x0000000006c9e21b clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e21b)
 #31 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
 #32 0x0000000006c9e21b clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e21b)
 #33 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
 #34 0x0000000006c9e21b clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e21b)
 #35 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
 #36 0x0000000006c9e21b clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/build/bin/clang+0x6c9e21b)
 #37 0x0000000006c86ca7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/build/bin/clang+0x6c86ca7)
 ...

Well. I don't know that much about this. Just noticed that our downstream bots started to fail on this new test case. Maybe we need to look at it on Monday to give more context, I don't have more info right now at least.

@cor3ntin
Copy link
Contributor Author

cor3ntin commented Feb 4, 2024

@bjope thanks for letting me know. A reproduction would really help, as upstream bots do not exhibit this behavior at all.

@bjope
Copy link
Collaborator

bjope commented Feb 4, 2024

@cor3ntin , our downstream code is doing some extra semantic checks using code like this in Sema::CheckVariableDeclarationType:

  if (!isVM && !T->isIncompleteType() && !isDependentOrGNUAutoType(T) && !T->isPlaceholderType()) {
    uint64_t Size = Context.getTypeSizeInChars(T).getQuantity();
   ...
  }

That is hitting the infinite recursion.

So we protect the type size calculation by checking if the type is incomplete, dependent, etc.
But as Bevin pointed out here https://github.com/llvm/llvm-project/pull/72644/files#r1469490392 the "broken" PackIndexingType isn't reported as isDependent, and neither as isIncomplete.

I must say that I don't know that much about this to say what is correct. Maybe out downstream semantic checks should be protected in some more way to avoid this problem.

int i = 0;
i...[0]; // expected-error {{i does not refer to the name of a parameter pack}}
V...[0]; // expected-error {{V does not refer to the name of a parameter pack}}
NotAPack...[0] a; // expected-error{{'NotAPack' does not refer to the name of a parameter pack}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you change this to an array, such as

NotAPack...[0] a[2];

then I think you hit the same kind of infinite recursion as we see downstream even without the array nodtation.

@cor3ntin
Copy link
Contributor Author

cor3ntin commented Feb 5, 2024

@bjope I appreciate the investigation, thanks! Here is a fix #80652

agozillon pushed a commit to agozillon/llvm-project that referenced this pull request Feb 5, 2024
Fix a crash caused by incorrect assumptions
Reported here
llvm#72644 (comment)
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.

None yet

4 participants