Skip to content

[clang-repl] fix segfault in CleanUpPTU() #75629

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

p4vook
Copy link

@p4vook p4vook commented Dec 15, 2023

Check if the last translation unit or its first declaration are actually empty and do not nead cleanup.

Previously this caused segmentation fault on empty PTUs.

Add a regression test.

Fixes: #72980

Check if the last translation unit or its first declaration
are actually empty and do not nead cleanup.

Previously this caused segmentation fault on empty PTUs.

Add a regression test.

Fixes: llvm#72980
Signed-off-by: Pavel Kalugin <[email protected]>
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 Dec 15, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 15, 2023

@llvm/pr-subscribers-clang

Author: Pavel Kalugin (p4vook)

Changes

Check if the last translation unit or its first declaration are actually empty and do not nead cleanup.

Previously this caused segmentation fault on empty PTUs.

Add a regression test.

Fixes: #72980


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

2 Files Affected:

  • (modified) clang/lib/Interpreter/IncrementalParser.cpp (+8)
  • (added) clang/test/Interpreter/anonymous-scope-fail.cpp (+10)
diff --git a/clang/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp
index 370bcbfee8b014..f894af881134bb 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -373,7 +373,15 @@ std::unique_ptr<llvm::Module> IncrementalParser::GenModule() {
 
 void IncrementalParser::CleanUpPTU(PartialTranslationUnit &PTU) {
   TranslationUnitDecl *MostRecentTU = PTU.TUPart;
+  if (!MostRecentTU) {
+    return;
+  }
+
   TranslationUnitDecl *FirstTU = MostRecentTU->getFirstDecl();
+  if (!FirstTU) {
+    return;
+  }
+
   if (StoredDeclsMap *Map = FirstTU->getPrimaryContext()->getLookupPtr()) {
     for (auto I = Map->begin(); I != Map->end(); ++I) {
       StoredDeclsList &List = I->second;
diff --git a/clang/test/Interpreter/anonymous-scope-fail.cpp b/clang/test/Interpreter/anonymous-scope-fail.cpp
new file mode 100644
index 00000000000000..c32b42d2859d97
--- /dev/null
+++ b/clang/test/Interpreter/anonymous-scope-fail.cpp
@@ -0,0 +1,10 @@
+// RUN: clang-repl "int x = 10;" "{ int t; a::b(t); }" "int y = 10;"
+// REQUIRES: host-supports-jit
+// UNSUPPORTED: system-aix
+// RUN: cat %s | not clang-repl | FileCheck %s
+{ int t; a::b(t); }
+extern "C" int printf(const char *, ...);
+int i = 42;
+auto r1 = printf("i = %d\n", i);
+// CHECK: i = 42
+%quit

@@ -0,0 +1,10 @@
// RUN: clang-repl "int x = 10;" "{ int t; a::b(t); }" "int y = 10;"
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 this in the code-undo.cpp test?

Copy link
Author

@p4vook p4vook Dec 15, 2023

Choose a reason for hiding this comment

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

I don't quite understand, the code-undo.cpp doesn't seem to have anything that produces an error, but this test does.
If we define a::b(t), clang-repl stops segfaulting here.
Is it okay to put tests with errors into code-undo.cpp?

Copy link
Contributor

Choose a reason for hiding this comment

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

Then we can put it in a new file called error-recovery.cpp.

Copy link
Contributor

Choose a reason for hiding this comment

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

@p4vook ping.

Copy link
Author

Choose a reason for hiding this comment

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

Yep, we can, I'll try to do it this evening. I couldn't figure out why this test failed though, I probably need to fix it somehow.

@p4vook p4vook marked this pull request as draft January 27, 2024 10:59
@p4vook
Copy link
Author

p4vook commented Jan 27, 2024

I converted the PR to draft, as this seems to be a part of a bigger problem with inserting statements into wrong nodes of the syntax tree (this seems to only show up when namespaces are present). I believe it shouldn't be fixed just by a simple check for nullptr.

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.

clang-repl: SIGSEGV in libclang
3 participants