From 07a7fd314a118393e6ca5e4dbcc9c3d91ff96172 Mon Sep 17 00:00:00 2001 From: Balazs Benics Date: Fri, 10 Jun 2022 12:21:17 +0200 Subject: [PATCH] [analyzer] Print the offending function at EndAnalysis crash I've faced crashes in the past multiple times when some `check::EndAnalysis` callback caused some crash. It's really anoying that it doesn't tell which function triggered this callback. This patch adds the well-known trace for that situation as well. Example: 1. parser at end of file 2. While analyzing stack: #0 Calling test11 Note that this does not have tests. I've considered `unittests` for this purpose, by using the `ASSERT_DEATH()` similarly how we check double eval called functions in `ConflictingEvalCallsTest.cpp`, however, that the testsuite won't invoke the custom handlers. Only the message of the `llvm_unreachable()` will be printed. Consequently, it's not applicable for us testing this feature. I've also considered using an end-to-end LIT test for this. For that, we would need to somehow overload the `clang_analyzer_crash()` `ExprInspection` handler, to get triggered by other events than the `EvalCall`. I'm not saying that we could not come up with a generic way of causing crash in a specific checker callback, but I'm not sure if that would worth the effort. Reviewed By: martong Differential Revision: https://reviews.llvm.org/D127389 --- clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h | 5 +++++ clang/lib/StaticAnalyzer/Core/ExprEngine.cpp | 2 ++ 2 files changed, 7 insertions(+) diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h index 6be33d4..89c0a12 100644 --- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h +++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h @@ -228,6 +228,11 @@ public: const Stmt *getStmt() const; + const LocationContext *getRootLocationContext() const { + assert(G.roots_begin() != G.roots_end()); + return (*G.roots_begin())->getLocation().getLocationContext(); + } + void GenerateAutoTransition(ExplodedNode *N); void enqueueEndOfPath(ExplodedNodeSet &S); void GenerateCallExitNode(ExplodedNode *N); diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp index 55920c1..3e3feee 100644 --- a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp +++ b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp @@ -613,6 +613,8 @@ void ExprEngine::printJson(raw_ostream &Out, ProgramStateRef State, } void ExprEngine::processEndWorklist() { + // This prints the name of the top-level function if we crash. + PrettyStackTraceLocationContext CrashInfo(getRootLocationContext()); getCheckerManager().runCheckersForEndAnalysis(G, BR, *this); } -- 2.7.4