-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[clang]: Propagate *noreturn
attributes in CFG
#146355
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
fc3b77d
0b2e72d
3562ea0
06cbe1d
400dbd1
918475d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ | |
#include "llvm/ADT/APSInt.h" | ||
#include "llvm/ADT/ArrayRef.h" | ||
#include "llvm/ADT/DenseMap.h" | ||
#include "llvm/ADT/ScopeExit.h" | ||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/ADT/SetVector.h" | ||
#include "llvm/ADT/SmallPtrSet.h" | ||
|
@@ -2833,8 +2834,37 @@ CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) { | |
if (!FD->isVariadic()) | ||
findConstructionContextsForArguments(C); | ||
|
||
if (FD->isNoReturn() || C->isBuiltinAssumeFalse(*Context)) | ||
NoReturn = true; | ||
if (!NoReturn) | ||
NoReturn = FD->isAnalyzerNoReturn() || C->isBuiltinAssumeFalse(*Context); | ||
|
||
// Some well-known 'noreturn' functions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we consolidate this list with the one in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MikeWeller i've added comment in |
||
if (!NoReturn) | ||
NoReturn = llvm::StringSwitch<bool>(FD->getQualifiedNameAsString()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AaronBallman I wonder how you feel about having a list of well known (non standard library) functions hardcoded in the compiler? I wonder if we want to have a more principled approach here long term, e.g., having one place in the compiler that injects annotations (like noreturn), and the rest of the code paths only handling that one annotation. |
||
.Case("BloombergLP::bsls::Assert::invokeHandler", true) | ||
.Case("std::terminate", true) | ||
.Case("std::abort", true) | ||
.Case("exit", true) | ||
.Case("abort", true) | ||
.Case("panic", true) | ||
.Case("error", true) | ||
.Case("Assert", true) | ||
.Case("ziperr", true) | ||
.Case("assfail", true) | ||
.Case("db_error", true) | ||
.Case("__assert", true) | ||
.Case("__assert2", true) | ||
.Case("_wassert", true) | ||
.Case("__assert_rtn", true) | ||
.Case("__assert_fail", true) | ||
.Case("dtrace_assfail", true) | ||
.Case("yy_fatal_error", true) | ||
.Case("_XCAssertionFailureHandler", true) | ||
.Case("_DTAssertionFailureHandler", true) | ||
.Case("_TSAssertionFailureHandler", true) | ||
.Case("__builtin_trap", true) | ||
.Case("__builtin_unreachable", true) | ||
.Default(false); | ||
|
||
if (FD->hasAttr<NoThrowAttr>()) | ||
AddEHEdge = false; | ||
if (isBuiltinAssumeWithSideEffects(FD->getASTContext(), C) || | ||
|
@@ -6288,6 +6318,12 @@ void CFGBlock::printTerminatorJson(raw_ostream &Out, const LangOptions &LO, | |
// There may be many more reasons why a sink would appear during analysis | ||
// (eg. checkers may generate sinks arbitrarily), but here we only consider | ||
// sinks that would be obvious by looking at the CFG. | ||
// | ||
// This function also performs inter-procedural analysis by recursively | ||
// examining called functions to detect forwarding chains to noreturn | ||
// functions. When a function is determined to never return through this | ||
// analysis, it's automatically marked with analyzer_noreturn attribute | ||
// for caching and future reference. | ||
static bool isImmediateSinkBlock(const CFGBlock *Blk) { | ||
if (Blk->hasNoReturnElement()) | ||
return true; | ||
|
@@ -6298,10 +6334,43 @@ static bool isImmediateSinkBlock(const CFGBlock *Blk) { | |
// at least for now, but once we have better support for exceptions, | ||
// we'd need to carefully handle the case when the throw is being | ||
// immediately caught. | ||
if (llvm::any_of(*Blk, [](const CFGElement &Elm) { | ||
if (llvm::any_of(*Blk, [](const CFGElement &Elm) -> bool { | ||
if (std::optional<CFGStmt> StmtElm = Elm.getAs<CFGStmt>()) | ||
return isa<CXXThrowExpr>(StmtElm->getStmt()); | ||
return false; | ||
})) | ||
return true; | ||
|
||
auto HasNoReturnCall = [](const CallExpr *CE) { | ||
if (!CE) | ||
return false; | ||
|
||
static thread_local llvm::SmallPtrSet<const FunctionDecl *, 32> InProgress; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of static locals is a red-flag. This is not a theoretical case. It cost me 2 days. |
||
|
||
auto *FD = CE->getDirectCallee(); | ||
|
||
if (!FD || InProgress.count(FD)) | ||
return false; | ||
|
||
InProgress.insert(FD); | ||
Comment on lines
+6322
to
+6325
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would do a double lookup here with |
||
auto DoCleanup = llvm::make_scope_exit([&]() { InProgress.erase(FD); }); | ||
|
||
auto NoReturnFromCFG = [FD]() { | ||
if (!FD->getBody()) | ||
return false; | ||
|
||
auto CalleeCFG = | ||
CFG::buildCFG(FD, FD->getBody(), &FD->getASTContext(), {}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible that we end up building the CFG for the same functions over and over again? This sounds like potentially wasteful. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I understand it (please correct me if I'm wrong), if( FD->isAnalyzerNoReturn() || NoReturnFromCFG() ) {
const_cast<FunctionDecl *>(FD)->addAttr(AnalyzerNoReturnAttr::Create(
FD->getASTContext(), FD->getLocation()));
return true;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we decide to create attrs, be sure to create them as implicit, as they were not spelled in source. |
||
|
||
return CalleeCFG && CalleeCFG->getEntry().isInevitablySinking(); | ||
}; | ||
|
||
return FD->isAnalyzerNoReturn() || NoReturnFromCFG(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we have redecls, does it matter which has the attribute, or do we always bind the attribute to the canonical decl? |
||
}; | ||
|
||
if (llvm::any_of(*Blk, [&](const CFGElement &Elm) { | ||
if (std::optional<CFGStmt> StmtElm = Elm.getAs<CFGStmt>()) | ||
if (isa<CXXThrowExpr>(StmtElm->getStmt())) | ||
return true; | ||
return HasNoReturnCall(dyn_cast<CallExpr>(StmtElm->getStmt())); | ||
return false; | ||
})) | ||
return true; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,37 +50,45 @@ void NoReturnFunctionChecker::checkPostCall(const CallEvent &CE, | |
BuildSinks = getFunctionExtInfo(C->getType()).getNoReturn(); | ||
} | ||
|
||
if (!BuildSinks && CE.isGlobalCFunction()) { | ||
if (const IdentifierInfo *II = CE.getCalleeIdentifier()) { | ||
// HACK: Some functions are not marked noreturn, and don't return. | ||
// Here are a few hardwired ones. If this takes too long, we can | ||
// potentially cache these results. | ||
BuildSinks | ||
= llvm::StringSwitch<bool>(StringRef(II->getName())) | ||
.Case("exit", true) | ||
.Case("panic", true) | ||
.Case("error", true) | ||
.Case("Assert", true) | ||
// FIXME: This is just a wrapper around throwing an exception. | ||
// Eventually inter-procedural analysis should handle this easily. | ||
.Case("ziperr", true) | ||
.Case("assfail", true) | ||
.Case("db_error", true) | ||
.Case("__assert", true) | ||
.Case("__assert2", true) | ||
// For the purpose of static analysis, we do not care that | ||
// this MSVC function will return if the user decides to continue. | ||
.Case("_wassert", true) | ||
.Case("__assert_rtn", true) | ||
.Case("__assert_fail", true) | ||
.Case("dtrace_assfail", true) | ||
.Case("yy_fatal_error", true) | ||
.Case("_XCAssertionFailureHandler", true) | ||
.Case("_DTAssertionFailureHandler", true) | ||
.Case("_TSAssertionFailureHandler", true) | ||
.Default(false); | ||
} | ||
} | ||
if (!BuildSinks && CE.isGlobalCFunction()) { | ||
if (const IdentifierInfo *II = CE.getCalleeIdentifier()) { | ||
// HACK: Some functions are not marked noreturn, and don't return. | ||
// Here are a few hardwired ones. If this takes too long, we can | ||
// potentially cache these results. | ||
// | ||
// (!) In case of function list update, please also update | ||
// CFGBuilder::VisitCallExpr (CFG.cpp) | ||
BuildSinks = | ||
llvm::StringSwitch<bool>(StringRef(II->getName())) | ||
.Case("exit", true) | ||
.Case("abort", true) | ||
.Case("panic", true) | ||
.Case("error", true) | ||
.Case("Assert", true) | ||
// FIXME: This is just a wrapper around throwing an exception. | ||
// Eventually inter-procedural analysis should handle this | ||
// easily. | ||
.Case("ziperr", true) | ||
.Case("assfail", true) | ||
.Case("db_error", true) | ||
.Case("__assert", true) | ||
.Case("__assert2", true) | ||
// For the purpose of static analysis, we do not care that | ||
// this MSVC function will return if the user decides to | ||
// continue. | ||
.Case("_wassert", true) | ||
.Case("__assert_rtn", true) | ||
.Case("__assert_fail", true) | ||
.Case("dtrace_assfail", true) | ||
.Case("yy_fatal_error", true) | ||
.Case("_XCAssertionFailureHandler", true) | ||
.Case("_DTAssertionFailureHandler", true) | ||
.Case("_TSAssertionFailureHandler", true) | ||
.Case("__builtin_trap", true) | ||
.Case("__builtin_unreachable", true) | ||
.Default(false); | ||
} | ||
} | ||
Comment on lines
+53
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could the analyzer depend on the injected noreturn attributes here instead of matching again the raw identifier strings in the analyzer (again, after the CFG contruction)? |
||
|
||
if (BuildSinks) | ||
C.generateSink(C.getState(), C.getPredecessor()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
no-warning
handled by the test harness? I'm familiar/have seen// CHECK-NOT: warning
comments. Unless any extraneous warnings already fail the test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just a comment that we do not expect warning here.
check_clang_tidy.py
ignores it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway, i consider moving this kind of check to
clang\unittests\Analysis\FlowSensitive\UncheckedOptionalAccessModelTest.cpp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I was just checking/asking whether the test will fail if a warning is emitted or whether you need a
// CHECK-NOT: warning
. But it seems for most tests any warning will produce a failure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clang-tidy tests require an exact match of diagnostics, so too many or too few diagnostics will fail the test