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][analyzer] Fix crash in loop unrolling #82089

Merged
merged 11 commits into from
Mar 14, 2024

Conversation

huang-me
Copy link
Contributor

StaticAnalyzer didn't check if the variable is declared in CompoundStmt under SwitchStmt, which make static analyzer reach root without finding the declaration.

Fixes #68819

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:static analyzer labels Feb 17, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 17, 2024

@llvm/pr-subscribers-clang

Author: None (huang-me)

Changes

StaticAnalyzer didn't check if the variable is declared in CompoundStmt under SwitchStmt, which make static analyzer reach root without finding the declaration.

Fixes #68819


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

1 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp (+11)
diff --git a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
index a80352816be613..b91dfa26774aa4 100644
--- a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
+++ b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
@@ -226,6 +226,17 @@ static bool isPossiblyEscaped(ExplodedNode *N, const DeclRefExpr *DR) {
           return false;
       }
     }
+
+    if (const SwitchStmt *SS = dyn_cast<SwitchStmt>(S)) {
+      for(const Stmt *CB: dyn_cast<CompoundStmt>(SS->getBody())->body()) {
+        for (const Decl *D : dyn_cast<DeclStmt>(CB)->decls()) {
+          // Once we reach the declaration of the VD we can return.
+          if (D->getCanonicalDecl() == VD)
+            return false;
+        }
+      }
+    }
+
     // Check the usage of the pass-by-ref function calls and adress-of operator
     // on VD and reference initialized by VD.
     ASTContext &ASTCtx =

@llvmbot
Copy link
Collaborator

llvmbot commented Feb 17, 2024

@llvm/pr-subscribers-clang-static-analyzer-1

Author: None (huang-me)

Changes

StaticAnalyzer didn't check if the variable is declared in CompoundStmt under SwitchStmt, which make static analyzer reach root without finding the declaration.

Fixes #68819


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

1 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp (+11)
diff --git a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
index a80352816be613..b91dfa26774aa4 100644
--- a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
+++ b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
@@ -226,6 +226,17 @@ static bool isPossiblyEscaped(ExplodedNode *N, const DeclRefExpr *DR) {
           return false;
       }
     }
+
+    if (const SwitchStmt *SS = dyn_cast<SwitchStmt>(S)) {
+      for(const Stmt *CB: dyn_cast<CompoundStmt>(SS->getBody())->body()) {
+        for (const Decl *D : dyn_cast<DeclStmt>(CB)->decls()) {
+          // Once we reach the declaration of the VD we can return.
+          if (D->getCanonicalDecl() == VD)
+            return false;
+        }
+      }
+    }
+
     // Check the usage of the pass-by-ref function calls and adress-of operator
     // on VD and reference initialized by VD.
     ASTContext &ASTCtx =

Copy link

github-actions bot commented Feb 17, 2024

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

clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp Outdated Show resolved Hide resolved
clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp Outdated Show resolved Hide resolved
@NagyDonat
Copy link
Contributor

If I understand it correctly, your change doesn't handle declarations that are in inner statements, e.g. the variable "x" in the following code:

switch (get_value()) {
case 42:
  do {
    int x;
    // ...
  } while (running);
//...
}

Is this compatible with the goals of your commit, or would the original crash remain in a situation like this?

@huang-me
Copy link
Contributor Author

If I understand it correctly, your change doesn't handle declarations that are in inner statements, e.g. the variable "x" in the following code:

switch (get_value()) {
case 42:
  do {
    int x;
    // ...
  } while (running);
//...
}

Is this compatible with the goals of your commit, or would the original crash remain in a situation like this?

As far as I understand it, the declaration within the CaseStmt would be found before reaching SwitchStmt so I don't think we need to consider this situation.

@NagyDonat
Copy link
Contributor

That sounds reasonable. Unfortunately I don't know much about the context of this change, so let's wait for a review from @danix800 (or somebody else who's knows enough) before merging this change.

Also, perhaps it would be a good idea to add a testcase (or a few of them) in the directory clang/test/Analysis to prevent regressions and illustrate the goals of this change. (As far as I know our test system is just a heap of random C/C++ files with a "how to compile/analyze this" header and "what warnings/errors should appear" markers in comments. I usually add testcases by following the example of older analogous cases.)

@huang-me
Copy link
Contributor Author

I've added the test case to illustrate the goal of this change.

Copy link
Contributor

@steakhal steakhal left a comment

Choose a reason for hiding this comment

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

Thanks for working on this.
I think iterating the direct child nodes of the switch is fine. I can't think of a better way.

clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp Outdated Show resolved Hide resolved
clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@steakhal steakhal left a comment

Choose a reason for hiding this comment

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

LGTM

EDIT: Sorry for my late reply.

@steakhal steakhal changed the title [clang][StaticAnalyzer] Crash on loop unrolling mode [clang][analyzer] Fix crash in loop unrolling Mar 5, 2024
@steakhal steakhal merged commit 8f68022 into llvm:main Mar 14, 2024
4 of 5 checks passed
Copy link

@huang-me 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 recieve 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!

qiaojbao pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request May 15, 2024
…308497902

Local branch amd-gfx 9633084 Merged main:55d4816393f897054a4721920502d45c645edf1d into amd-gfx:f95c9bf0b616
Remote branch main 8f68022 [clang][analyzer] Fix crash in loop unrolling (llvm#82089)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:static analyzer clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang][StaticAnalyzer] Crash on loop unrolling mode
4 participants