]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/Checker/UnixAPIChecker.cpp
Update clang to r100181.
[FreeBSD/FreeBSD.git] / lib / Checker / UnixAPIChecker.cpp
1 //= UnixAPIChecker.h - Checks preconditions for various Unix APIs --*- 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 UnixAPIChecker, which is an assortment of checks on calls
11 // to various, widely used UNIX/Posix functions.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "GRExprEngineInternalChecks.h"
16 #include "clang/Checker/PathSensitive/CheckerVisitor.h"
17 #include "clang/Checker/BugReporter/BugType.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include <fcntl.h>
20
21 using namespace clang;
22
23 namespace {
24 class UnixAPIChecker : public CheckerVisitor<UnixAPIChecker> {
25   enum SubChecks {
26     OpenFn = 0,
27     NumChecks
28   };
29
30   BugType *BTypes[NumChecks];
31
32 public:
33   UnixAPIChecker() { memset(BTypes, 0, sizeof(*BTypes) * NumChecks); }
34   static void *getTag() { static unsigned tag = 0; return &tag; }
35
36   void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE);
37 };
38 } //end anonymous namespace
39
40 void clang::RegisterUnixAPIChecker(GRExprEngine &Eng) {
41   Eng.registerCheck(new UnixAPIChecker());
42 }
43
44 //===----------------------------------------------------------------------===//
45 // Utility functions.
46 //===----------------------------------------------------------------------===//
47
48 static inline void LazyInitialize(BugType *&BT, const char *name) {
49   if (BT)
50     return;
51   BT = new BugType(name, "Unix API");
52 }
53
54 //===----------------------------------------------------------------------===//
55 // "open" (man 2 open)
56 //===----------------------------------------------------------------------===//
57
58 static void CheckOpen(CheckerContext &C, const CallExpr *CE, BugType *&BT) {
59   LazyInitialize(BT, "Improper use of 'open'");
60
61   // Look at the 'oflags' argument for the O_CREAT flag.
62   const GRState *state = C.getState();
63
64   if (CE->getNumArgs() < 2) {
65     // The frontend should issue a warning for this case, so this is a sanity
66     // check.
67     return;
68   }
69
70   // Now check if oflags has O_CREAT set.
71   const Expr *oflagsEx = CE->getArg(1);
72   const SVal V = state->getSVal(oflagsEx);
73   if (!isa<NonLoc>(V)) {
74     // The case where 'V' can be a location can only be due to a bad header,
75     // so in this case bail out.
76     return;
77   }
78   NonLoc oflags = cast<NonLoc>(V);
79   NonLoc ocreateFlag =
80     cast<NonLoc>(C.getValueManager().makeIntVal((uint64_t) O_CREAT,
81                                                 oflagsEx->getType()));
82   SVal maskedFlagsUC = C.getSValuator().EvalBinOpNN(state, BinaryOperator::And,
83                                                     oflags, ocreateFlag,
84                                                     oflagsEx->getType());
85   if (maskedFlagsUC.isUnknownOrUndef())
86     return;
87   DefinedSVal maskedFlags = cast<DefinedSVal>(maskedFlagsUC);
88
89   // Check if maskedFlags is non-zero.
90   const GRState *trueState, *falseState;
91   llvm::tie(trueState, falseState) = state->Assume(maskedFlags);
92
93   // Only emit an error if the value of 'maskedFlags' is properly
94   // constrained;
95   if (!(trueState && !falseState))
96     return;
97
98   if (CE->getNumArgs() < 3) {
99     ExplodedNode *N = C.GenerateSink(trueState);
100     if (!N)
101       return;
102
103     EnhancedBugReport *report =
104       new EnhancedBugReport(*BT,
105                             "Call to 'open' requires a third argument when "
106                             "the 'O_CREAT' flag is set", N);
107     report->addRange(oflagsEx->getSourceRange());
108     C.EmitReport(report);
109   }
110 }
111
112 //===----------------------------------------------------------------------===//
113 // Central dispatch function.
114 //===----------------------------------------------------------------------===//
115
116 typedef void (*SubChecker)(CheckerContext &C, const CallExpr *CE, BugType *&BT);
117 namespace {
118   class SubCheck {
119     SubChecker SC;
120     BugType **BT;
121   public:
122     SubCheck(SubChecker sc, BugType *& bt) : SC(sc), BT(&bt) {}
123     SubCheck() : SC(NULL), BT(NULL) {}
124
125     void run(CheckerContext &C, const CallExpr *CE) const {
126       if (SC)
127         SC(C, CE, *BT);
128     }
129   };
130 } // end anonymous namespace
131
132 void UnixAPIChecker::PreVisitCallExpr(CheckerContext &C, const CallExpr *CE) {
133   // Get the callee.  All the functions we care about are C functions
134   // with simple identifiers.
135   const GRState *state = C.getState();
136   const Expr *Callee = CE->getCallee();
137   const FunctionTextRegion *Fn =
138     dyn_cast_or_null<FunctionTextRegion>(state->getSVal(Callee).getAsRegion());
139
140   if (!Fn)
141     return;
142
143   const IdentifierInfo *FI = Fn->getDecl()->getIdentifier();
144   if (!FI)
145     return;
146
147   const SubCheck &SC =
148     llvm::StringSwitch<SubCheck>(FI->getName())
149       .Case("open", SubCheck(CheckOpen, BTypes[OpenFn]))
150       .Default(SubCheck());
151
152   SC.run(C, CE);
153 }