1 //= UnixAPIChecker.h - Checks preconditions for various Unix APIs --*- C++ -*-//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This defines UnixAPIChecker, which is an assortment of checks on calls
11 // to various, widely used UNIX/Posix functions.
13 //===----------------------------------------------------------------------===//
15 #include "GRExprEngineInternalChecks.h"
16 #include "clang/Checker/PathSensitive/CheckerVisitor.h"
17 #include "clang/Checker/BugReporter/BugType.h"
18 #include "llvm/ADT/StringSwitch.h"
21 using namespace clang;
24 class UnixAPIChecker : public CheckerVisitor<UnixAPIChecker> {
30 BugType *BTypes[NumChecks];
33 UnixAPIChecker() { memset(BTypes, 0, sizeof(*BTypes) * NumChecks); }
34 static void *getTag() { static unsigned tag = 0; return &tag; }
36 void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE);
38 } //end anonymous namespace
40 void clang::RegisterUnixAPIChecker(GRExprEngine &Eng) {
41 Eng.registerCheck(new UnixAPIChecker());
44 //===----------------------------------------------------------------------===//
46 //===----------------------------------------------------------------------===//
48 static inline void LazyInitialize(BugType *&BT, const char *name) {
51 BT = new BugType(name, "Unix API");
54 //===----------------------------------------------------------------------===//
55 // "open" (man 2 open)
56 //===----------------------------------------------------------------------===//
58 static void CheckOpen(CheckerContext &C, const CallExpr *CE, BugType *&BT) {
59 LazyInitialize(BT, "Improper use of 'open'");
61 // Look at the 'oflags' argument for the O_CREAT flag.
62 const GRState *state = C.getState();
64 if (CE->getNumArgs() < 2) {
65 // The frontend should issue a warning for this case, so this is a sanity
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.
78 NonLoc oflags = cast<NonLoc>(V);
80 cast<NonLoc>(C.getValueManager().makeIntVal((uint64_t) O_CREAT,
81 oflagsEx->getType()));
82 SVal maskedFlagsUC = C.getSValuator().EvalBinOpNN(state, BinaryOperator::And,
85 if (maskedFlagsUC.isUnknownOrUndef())
87 DefinedSVal maskedFlags = cast<DefinedSVal>(maskedFlagsUC);
89 // Check if maskedFlags is non-zero.
90 const GRState *trueState, *falseState;
91 llvm::tie(trueState, falseState) = state->Assume(maskedFlags);
93 // Only emit an error if the value of 'maskedFlags' is properly
95 if (!(trueState && !falseState))
98 if (CE->getNumArgs() < 3) {
99 ExplodedNode *N = C.GenerateSink(trueState);
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);
112 //===----------------------------------------------------------------------===//
113 // Central dispatch function.
114 //===----------------------------------------------------------------------===//
116 typedef void (*SubChecker)(CheckerContext &C, const CallExpr *CE, BugType *&BT);
122 SubCheck(SubChecker sc, BugType *& bt) : SC(sc), BT(&bt) {}
123 SubCheck() : SC(NULL), BT(NULL) {}
125 void run(CheckerContext &C, const CallExpr *CE) const {
130 } // end anonymous namespace
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());
143 const IdentifierInfo *FI = Fn->getDecl()->getIdentifier();
148 llvm::StringSwitch<SubCheck>(FI->getName())
149 .Case("open", SubCheck(CheckOpen, BTypes[OpenFn]))
150 .Default(SubCheck());