1 //=== NoReturnFunctionChecker.cpp -------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This defines NoReturnFunctionChecker, which evaluates functions that do not
11 // return to the caller.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "ExprEngineInternalChecks.h"
16 #include "clang/StaticAnalyzer/PathSensitive/CheckerVisitor.h"
17 #include "llvm/ADT/StringSwitch.h"
18 
19 using namespace clang;
20 using namespace ento;
21 
22 namespace {
23 
24 class NoReturnFunctionChecker : public CheckerVisitor<NoReturnFunctionChecker> {
25 public:
26   static void *getTag() { static int tag = 0; return &tag; }
27   void PostVisitCallExpr(CheckerContext &C, const CallExpr *CE);
28 };
29 
30 }
31 
32 void ento::RegisterNoReturnFunctionChecker(ExprEngine &Eng) {
33   Eng.registerCheck(new NoReturnFunctionChecker());
34 }
35 
36 void NoReturnFunctionChecker::PostVisitCallExpr(CheckerContext &C,
37                                                 const CallExpr *CE) {
38   const GRState *state = C.getState();
39   const Expr *Callee = CE->getCallee();
40 
41   bool BuildSinks = getFunctionExtInfo(Callee->getType()).getNoReturn();
42 
43   if (!BuildSinks) {
44     SVal L = state->getSVal(Callee);
45     const FunctionDecl *FD = L.getAsFunctionDecl();
46     if (!FD)
47       return;
48 
49     if (FD->getAttr<AnalyzerNoReturnAttr>())
50       BuildSinks = true;
51     else if (const IdentifierInfo *II = FD->getIdentifier()) {
52       // HACK: Some functions are not marked noreturn, and don't return.
53       //  Here are a few hardwired ones.  If this takes too long, we can
54       //  potentially cache these results.
55       BuildSinks
56         = llvm::StringSwitch<bool>(llvm::StringRef(II->getName()))
57             .Case("exit", true)
58             .Case("panic", true)
59             .Case("error", true)
60             .Case("Assert", true)
61             // FIXME: This is just a wrapper around throwing an exception.
62             //  Eventually inter-procedural analysis should handle this easily.
63             .Case("ziperr", true)
64             .Case("assfail", true)
65             .Case("db_error", true)
66             .Case("__assert", true)
67             .Case("__assert_rtn", true)
68             .Case("__assert_fail", true)
69             .Case("dtrace_assfail", true)
70             .Case("yy_fatal_error", true)
71             .Case("_XCAssertionFailureHandler", true)
72             .Case("_DTAssertionFailureHandler", true)
73             .Case("_TSAssertionFailureHandler", true)
74             .Default(false);
75     }
76   }
77 
78   if (BuildSinks)
79     C.generateSink(CE);
80 }
81