Skip to content

[clang][analyzer] Fix a crash in alpha.unix.BlockInCriticalSection #90030

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

Merged
merged 3 commits into from
May 15, 2024

Conversation

gamesh411
Copy link
Contributor

@gamesh411 gamesh411 commented Apr 25, 2024

When analyzing C code with function pointers the checker crashes because
of how the implementation extracts IdentifierInfo. Without the fix, this
test crashes.

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

llvmbot commented Apr 25, 2024

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

@llvm/pr-subscribers-clang

Author: Endre Fülöp (gamesh411)

Changes

In CTU there is not always an AnalysisDeclContext for a given call. This
led to crashes. The AnalysisDeclContext access is now checked.


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

1 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/BlockInCriticalSectionChecker.cpp (+5-3)
diff --git a/clang/lib/StaticAnalyzer/Checkers/BlockInCriticalSectionChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/BlockInCriticalSectionChecker.cpp
index e4373915410fb2..9874a68ebe47af 100644
--- a/clang/lib/StaticAnalyzer/Checkers/BlockInCriticalSectionChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/BlockInCriticalSectionChecker.cpp
@@ -14,6 +14,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "clang/Analysis/AnalysisDeclContext.h"
 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
 #include "clang/StaticAnalyzer/Core/Checker.h"
@@ -103,9 +104,10 @@ class RAIIMutexDescriptor {
       // this function is called instead of early returning it. To avoid this, a
       // bool variable (IdentifierInfoInitialized) is used and the function will
       // be run only once.
-      Guard = &Call.getCalleeAnalysisDeclContext()->getASTContext().Idents.get(
-          GuardName);
-      IdentifierInfoInitialized = true;
+      if (AnalysisDeclContext *CalleCtx = Call.getCalleeAnalysisDeclContext()) {
+        Guard = &CalleCtx->getASTContext().Idents.get(GuardName);
+        IdentifierInfoInitialized = true;
+      }
     }
   }
 

@steakhal steakhal self-requested a review April 25, 2024 16:14
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.

In CTU there is not always an AnalysisDeclContext for a given call.

Why?

Could you demonstrate the fix in a test?

@gamesh411 gamesh411 force-pushed the block-in-critical-fix branch from d811a17 to bc759c4 Compare May 14, 2024 12:44
When analyzing C code with function pointers the checker crashes because
of how the implementation extracts IdentifierInfo. Without the fix, this
test crashes.

Add crashing test
@gamesh411 gamesh411 force-pushed the block-in-critical-fix branch from bc759c4 to af05be9 Compare May 14, 2024 12:50
@gamesh411
Copy link
Contributor Author

After reducing a crashing TU, I have found, that the issue came up without CTU analysis as well.
I have added a test case that demonstrates the crash without the fix.
I also updated the commit message to reflect the real cause.

@gamesh411 gamesh411 changed the title [clang][analyzer] Fix alpha.unix.BlockInCriticalSection for CTU [clang][analyzer] Fix alpha.unix.BlockInCriticalSection May 14, 2024
@gamesh411 gamesh411 requested a review from steakhal May 14, 2024 12:56
@steakhal steakhal changed the title [clang][analyzer] Fix alpha.unix.BlockInCriticalSection [clang][analyzer] Fix a crash in alpha.unix.BlockInCriticalSection May 14, 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.

Now the change makes sense. I disagree with the fix though, see inline.

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, thanks!

@steakhal steakhal merged commit eda098a into llvm:main May 15, 2024
3 of 4 checks passed
@gamesh411 gamesh411 deleted the block-in-critical-fix branch June 1, 2024 04:22
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.

3 participants