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