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

Fix analyzer crash on 'StructuralValue' #79764

Merged
merged 1 commit into from
Jan 30, 2024

Conversation

bolshakov-a
Copy link
Contributor

OpaqueValueExpr doesn't necessarily contain a source expression. Particularly, after #78041, it is used to carry the type and the value kind of a non-type template argument of floating-point type or referring to a subobject (those are so called StructuralValue arguments).

This fixes #79575.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Jan 28, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 28, 2024

@llvm/pr-subscribers-clang

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

Author: Andrey Ali Khan Bolshakov (bolshakov-a)

Changes

OpaqueValueExpr doesn't necessarily contain a source expression. Particularly, after #78041, it is used to carry the type and the value kind of a non-type template argument of floating-point type or referring to a subobject (those are so called StructuralValue arguments).

This fixes #79575.


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

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Core/Environment.cpp (+6-3)
  • (modified) clang/test/Analysis/templates.cpp (+13)
diff --git a/clang/lib/StaticAnalyzer/Core/Environment.cpp b/clang/lib/StaticAnalyzer/Core/Environment.cpp
index 4f989ed59bee38c..c77b28bc48fd674 100644
--- a/clang/lib/StaticAnalyzer/Core/Environment.cpp
+++ b/clang/lib/StaticAnalyzer/Core/Environment.cpp
@@ -40,8 +40,12 @@ static const Expr *ignoreTransparentExprs(const Expr *E) {
 
   switch (E->getStmtClass()) {
   case Stmt::OpaqueValueExprClass:
-    E = cast<OpaqueValueExpr>(E)->getSourceExpr();
-    break;
+    if (const clang::Expr *SE = cast<OpaqueValueExpr>(E)->getSourceExpr()) {
+      E = SE;
+      break;
+    } else {
+      return E;
+    }
   case Stmt::ExprWithCleanupsClass:
     E = cast<ExprWithCleanups>(E)->getSubExpr();
     break;
@@ -98,7 +102,6 @@ SVal Environment::getSVal(const EnvironmentEntry &Entry,
   case Stmt::CXXBindTemporaryExprClass:
   case Stmt::ExprWithCleanupsClass:
   case Stmt::GenericSelectionExprClass:
-  case Stmt::OpaqueValueExprClass:
   case Stmt::ConstantExprClass:
   case Stmt::ParenExprClass:
   case Stmt::SubstNonTypeTemplateParmExprClass:
diff --git a/clang/test/Analysis/templates.cpp b/clang/test/Analysis/templates.cpp
index 061c19fe7e04451..6da1821b70f26fa 100644
--- a/clang/test/Analysis/templates.cpp
+++ b/clang/test/Analysis/templates.cpp
@@ -68,3 +68,16 @@ namespace rdar13954714 {
   // force instantiation
   template void blockWithStatic<true>();
 }
+
+namespace structural_value_crash {
+  constexpr char abc[] = "abc";
+
+  template <const char* in>
+  void use_template_param() {
+    const char *p = in;
+  }
+
+  void force_instantiate() {
+    use_template_param<abc>();
+  }
+}

@bolshakov-a
Copy link
Contributor Author

@steakhal, @cor3ntin, @erichkeane

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

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

Thanks!

@zmodem
Copy link
Collaborator

zmodem commented Jan 29, 2024

I think a similar fix may be needed in clang::IgnoreParensSingleStep, see my comment here: #78041 (comment)

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.

Approved with nits. This works around the crash.

clang/lib/StaticAnalyzer/Core/Environment.cpp Outdated Show resolved Hide resolved
@bolshakov-a bolshakov-a force-pushed the fix_structural_value_analyzer_crash branch from 2a0a46f to 474f866 Compare January 29, 2024 13:00
@llvmbot llvmbot added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Jan 29, 2024
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 for the Static Analyzer.
Thanks for fixing this crash.

@steakhal
Copy link
Contributor

steakhal commented Jan 29, 2024

LGTM for the Static Analyzer.

Actually, the other hunk also makes sense. LGTM.
Shouldn't we add a regression test for the crash on that part too?

@zmodem
Copy link
Collaborator

zmodem commented Jan 29, 2024

Shouldn't we add a regression test for the crash on that part too?

+1 that would be good to have.

`OpaqueValueExpr` doesn't necessarily contain a source expression.
Particularly, after llvm#78041, it is used to carry the type and the value
kind of a non-type template argument of floating-point type or referring
to a subobject (those are so called `StructuralValue` arguments).

This fixes llvm#79575.
@bolshakov-a bolshakov-a force-pushed the fix_structural_value_analyzer_crash branch from 474f866 to b3debeb Compare January 29, 2024 15:49
@zmodem
Copy link
Collaborator

zmodem commented Jan 30, 2024

Thanks! It would be great to get this landed as soon as possible to unbreak trunk. (I believe we need it for the 18.x branch too?)

@steakhal
Copy link
Contributor

Thanks! It would be great to get this landed as soon as possible to unbreak trunk. (I believe we need it for the 18.x branch too?)

I'll take care of the backport, after this PR is merged by @bolshakov-a

@bolshakov-a
Copy link
Contributor Author

I'm just waiting for someone who would do it instead of me... I don't have commit access.

@steakhal steakhal merged commit ef67f63 into llvm:main Jan 30, 2024
4 checks passed
@bolshakov-a
Copy link
Contributor Author

Thanks!

@bolshakov-a bolshakov-a deleted the fix_structural_value_analyzer_crash branch January 30, 2024 12:12
llvmbot pushed a commit to llvmbot/llvm-project that referenced this pull request Jan 30, 2024
`OpaqueValueExpr` doesn't necessarily contain a source expression.
Particularly, after llvm#78041, it is used to carry the type and the value
kind of a non-type template argument of floating-point type or referring
to a subobject (those are so called `StructuralValue` arguments).

This fixes llvm#79575.

(cherry picked from commit ef67f63)
@steakhal
Copy link
Contributor

Thanks!

Thanks for the quick workaround!

FYI Backport proposed in issue #79992, that refers to PR #79997 actually doing the backport.

llvmbot pushed a commit to llvmbot/llvm-project that referenced this pull request Feb 1, 2024
`OpaqueValueExpr` doesn't necessarily contain a source expression.
Particularly, after llvm#78041, it is used to carry the type and the value
kind of a non-type template argument of floating-point type or referring
to a subobject (those are so called `StructuralValue` arguments).

This fixes llvm#79575.

(cherry picked from commit ef67f63)
tstellar pushed a commit to tstellar/llvm-project that referenced this pull request Feb 14, 2024
`OpaqueValueExpr` doesn't necessarily contain a source expression.
Particularly, after llvm#78041, it is used to carry the type and the value
kind of a non-type template argument of floating-point type or referring
to a subobject (those are so called `StructuralValue` arguments).

This fixes llvm#79575.

(cherry picked from commit ef67f63)
tstellar pushed a commit to tstellar/llvm-project that referenced this pull request Feb 14, 2024
`OpaqueValueExpr` doesn't necessarily contain a source expression.
Particularly, after llvm#78041, it is used to carry the type and the value
kind of a non-type template argument of floating-point type or referring
to a subobject (those are so called `StructuralValue` arguments).

This fixes llvm#79575.

(cherry picked from commit ef67f63)
tstellar pushed a commit to tstellar/llvm-project that referenced this pull request Feb 14, 2024
`OpaqueValueExpr` doesn't necessarily contain a source expression.
Particularly, after llvm#78041, it is used to carry the type and the value
kind of a non-type template argument of floating-point type or referring
to a subobject (those are so called `StructuralValue` arguments).

This fixes llvm#79575.

(cherry picked from commit ef67f63)
tstellar pushed a commit to tstellar/llvm-project that referenced this pull request Feb 14, 2024
`OpaqueValueExpr` doesn't necessarily contain a source expression.
Particularly, after llvm#78041, it is used to carry the type and the value
kind of a non-type template argument of floating-point type or referring
to a subobject (those are so called `StructuralValue` arguments).

This fixes llvm#79575.

(cherry picked from commit ef67f63)
@pointhex pointhex mentioned this pull request May 7, 2024
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:static analyzer clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[analyzer] Crash on using non-type template arguments
6 participants