blob: cec286d2f3e97d2a75baa7a1b1f69a5b036232f9 [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
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +000015#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000018#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000020#include "clang/Basic/TargetInfo.h"
Ted Kremenek66d51422010-04-09 20:26:58 +000021#include "llvm/ADT/Optional.h"
Benjamin Kramer5e2d2c22010-03-27 21:19:47 +000022#include "llvm/ADT/StringSwitch.h"
Ted Kremenek381d1bf2010-02-25 00:20:35 +000023#include <fcntl.h>
24
25using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000026using namespace ento;
Ted Kremenek66d51422010-04-09 20:26:58 +000027using llvm::Optional;
Ted Kremenek381d1bf2010-02-25 00:20:35 +000028
29namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000030class UnixAPIChecker : public Checker< check::PreStmt<CallExpr> > {
Jordy Roseaf5b0432011-07-15 06:28:59 +000031 mutable llvm::OwningPtr<BugType> BT_open, BT_pthreadOnce, BT_mallocZero;
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000032 mutable Optional<uint64_t> Val_O_CREAT;
Ted Kremenekbace4ba2010-04-08 22:15:34 +000033
34public:
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000035 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
Jordy Roseaf5b0432011-07-15 06:28:59 +000036
37 void CheckOpen(CheckerContext &C, const CallExpr *CE) const;
38 void CheckPthreadOnce(CheckerContext &C, const CallExpr *CE) const;
39 void CheckMallocZero(CheckerContext &C, const CallExpr *CE) const;
40
41 typedef void (UnixAPIChecker::*SubChecker)(CheckerContext &,
42 const CallExpr *) const;
Ted Kremenek381d1bf2010-02-25 00:20:35 +000043};
44} //end anonymous namespace
45
Ted Kremenek381d1bf2010-02-25 00:20:35 +000046//===----------------------------------------------------------------------===//
47// Utility functions.
48//===----------------------------------------------------------------------===//
49
Jordy Roseaf5b0432011-07-15 06:28:59 +000050static inline void LazyInitialize(llvm::OwningPtr<BugType> &BT,
51 const char *name) {
Ted Kremenek381d1bf2010-02-25 00:20:35 +000052 if (BT)
53 return;
Jordy Roseaf5b0432011-07-15 06:28:59 +000054 BT.reset(new BugType(name, "Unix API"));
Ted Kremenek381d1bf2010-02-25 00:20:35 +000055}
56
57//===----------------------------------------------------------------------===//
58// "open" (man 2 open)
59//===----------------------------------------------------------------------===//
60
Jordy Roseaf5b0432011-07-15 06:28:59 +000061void UnixAPIChecker::CheckOpen(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenekbace4ba2010-04-08 22:15:34 +000062 // The definition of O_CREAT is platform specific. We need a better way
63 // of querying this information from the checking environment.
Jordy Roseaf5b0432011-07-15 06:28:59 +000064 if (!Val_O_CREAT.hasValue()) {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +000065 if (C.getASTContext().getTargetInfo().getTriple().getVendor()
66 == llvm::Triple::Apple)
Jordy Roseaf5b0432011-07-15 06:28:59 +000067 Val_O_CREAT = 0x0200;
Ted Kremenekbace4ba2010-04-08 22:15:34 +000068 else {
69 // FIXME: We need a more general way of getting the O_CREAT value.
70 // We could possibly grovel through the preprocessor state, but
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000071 // that would require passing the Preprocessor object to the ExprEngine.
Ted Kremenekbace4ba2010-04-08 22:15:34 +000072 return;
73 }
74 }
75
Ted Kremenek381d1bf2010-02-25 00:20:35 +000076 // Look at the 'oflags' argument for the O_CREAT flag.
Ted Kremenek18c66fd2011-08-15 22:09:50 +000077 const ProgramState *state = C.getState();
Ted Kremenek381d1bf2010-02-25 00:20:35 +000078
79 if (CE->getNumArgs() < 2) {
80 // The frontend should issue a warning for this case, so this is a sanity
81 // check.
82 return;
83 }
84
85 // Now check if oflags has O_CREAT set.
86 const Expr *oflagsEx = CE->getArg(1);
87 const SVal V = state->getSVal(oflagsEx);
88 if (!isa<NonLoc>(V)) {
89 // The case where 'V' can be a location can only be due to a bad header,
90 // so in this case bail out.
91 return;
92 }
93 NonLoc oflags = cast<NonLoc>(V);
94 NonLoc ocreateFlag =
Jordy Roseaf5b0432011-07-15 06:28:59 +000095 cast<NonLoc>(C.getSValBuilder().makeIntVal(Val_O_CREAT.getValue(),
Ted Kremenek381d1bf2010-02-25 00:20:35 +000096 oflagsEx->getType()));
Ted Kremenek9c149532010-12-01 21:57:22 +000097 SVal maskedFlagsUC = C.getSValBuilder().evalBinOpNN(state, BO_And,
Ted Kremenek846eabd2010-12-01 21:28:31 +000098 oflags, ocreateFlag,
99 oflagsEx->getType());
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000100 if (maskedFlagsUC.isUnknownOrUndef())
101 return;
102 DefinedSVal maskedFlags = cast<DefinedSVal>(maskedFlagsUC);
103
104 // Check if maskedFlags is non-zero.
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000105 const ProgramState *trueState, *falseState;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000106 llvm::tie(trueState, falseState) = state->assume(maskedFlags);
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000107
108 // Only emit an error if the value of 'maskedFlags' is properly
109 // constrained;
110 if (!(trueState && !falseState))
111 return;
112
113 if (CE->getNumArgs() < 3) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000114 ExplodedNode *N = C.generateSink(trueState);
Ted Kremenekc757d792010-02-25 05:44:05 +0000115 if (!N)
116 return;
117
Jordy Roseaf5b0432011-07-15 06:28:59 +0000118 LazyInitialize(BT_open, "Improper use of 'open'");
119
Anna Zakse172e8b2011-08-17 23:00:25 +0000120 BugReport *report =
121 new BugReport(*BT_open,
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000122 "Call to 'open' requires a third argument when "
123 "the 'O_CREAT' flag is set", N);
124 report->addRange(oflagsEx->getSourceRange());
125 C.EmitReport(report);
126 }
127}
128
129//===----------------------------------------------------------------------===//
Ted Kremenek99d98382010-04-08 19:53:31 +0000130// pthread_once
131//===----------------------------------------------------------------------===//
132
Jordy Roseaf5b0432011-07-15 06:28:59 +0000133void UnixAPIChecker::CheckPthreadOnce(CheckerContext &C,
134 const CallExpr *CE) const {
Ted Kremenek99d98382010-04-08 19:53:31 +0000135
136 // This is similar to 'CheckDispatchOnce' in the MacOSXAPIChecker.
137 // They can possibly be refactored.
138
Ted Kremenek99d98382010-04-08 19:53:31 +0000139 if (CE->getNumArgs() < 1)
140 return;
141
142 // Check if the first argument is stack allocated. If so, issue a warning
143 // because that's likely to be bad news.
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000144 const ProgramState *state = C.getState();
Ted Kremenek99d98382010-04-08 19:53:31 +0000145 const MemRegion *R = state->getSVal(CE->getArg(0)).getAsRegion();
146 if (!R || !isa<StackSpaceRegion>(R->getMemorySpace()))
147 return;
148
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000149 ExplodedNode *N = C.generateSink(state);
Ted Kremenek99d98382010-04-08 19:53:31 +0000150 if (!N)
151 return;
152
153 llvm::SmallString<256> S;
154 llvm::raw_svector_ostream os(S);
155 os << "Call to 'pthread_once' uses";
156 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
157 os << " the local variable '" << VR->getDecl()->getName() << '\'';
158 else
159 os << " stack allocated memory";
160 os << " for the \"control\" value. Using such transient memory for "
161 "the control value is potentially dangerous.";
162 if (isa<VarRegion>(R) && isa<StackLocalsSpaceRegion>(R->getMemorySpace()))
163 os << " Perhaps you intended to declare the variable as 'static'?";
164
Jordy Roseaf5b0432011-07-15 06:28:59 +0000165 LazyInitialize(BT_pthreadOnce, "Improper use of 'pthread_once'");
166
Anna Zakse172e8b2011-08-17 23:00:25 +0000167 BugReport *report = new BugReport(*BT_pthreadOnce, os.str(), N);
Ted Kremenek99d98382010-04-08 19:53:31 +0000168 report->addRange(CE->getArg(0)->getSourceRange());
169 C.EmitReport(report);
170}
171
172//===----------------------------------------------------------------------===//
Ted Kremenekb12fbc22010-11-16 18:47:04 +0000173// "malloc" with allocation size 0
174//===----------------------------------------------------------------------===//
175
176// FIXME: Eventually this should be rolled into the MallocChecker, but this
177// check is more basic and is valuable for widespread use.
Jordy Roseaf5b0432011-07-15 06:28:59 +0000178void UnixAPIChecker::CheckMallocZero(CheckerContext &C,
179 const CallExpr *CE) const {
Ted Kremenekb12fbc22010-11-16 18:47:04 +0000180
181 // Sanity check that malloc takes one argument.
182 if (CE->getNumArgs() != 1)
183 return;
184
185 // Check if the allocation size is 0.
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000186 const ProgramState *state = C.getState();
Ted Kremenekb12fbc22010-11-16 18:47:04 +0000187 SVal argVal = state->getSVal(CE->getArg(0));
188
189 if (argVal.isUnknownOrUndef())
190 return;
191
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000192 const ProgramState *trueState, *falseState;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000193 llvm::tie(trueState, falseState) = state->assume(cast<DefinedSVal>(argVal));
Ted Kremenekb12fbc22010-11-16 18:47:04 +0000194
195 // Is the value perfectly constrained to zero?
196 if (falseState && !trueState) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000197 ExplodedNode *N = C.generateSink(falseState);
Ted Kremenekb12fbc22010-11-16 18:47:04 +0000198 if (!N)
199 return;
200
Ted Kremenek8fec9722010-11-17 00:50:34 +0000201 // FIXME: Add reference to CERT advisory, and/or C99 standard in bug
202 // output.
203
Jordy Roseaf5b0432011-07-15 06:28:59 +0000204 LazyInitialize(BT_mallocZero, "Undefined allocation of 0 bytes");
Ted Kremenekb12fbc22010-11-16 18:47:04 +0000205
Anna Zakse172e8b2011-08-17 23:00:25 +0000206 BugReport *report =
207 new BugReport(*BT_mallocZero, "Call to 'malloc' has an allocation"
Jordy Roseaf5b0432011-07-15 06:28:59 +0000208 " size of 0 bytes", N);
Ted Kremenekb12fbc22010-11-16 18:47:04 +0000209 report->addRange(CE->getArg(0)->getSourceRange());
Anna Zaks50bbc162011-08-19 22:33:38 +0000210 report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N,
211 CE->getArg(0)));
Ted Kremenekb12fbc22010-11-16 18:47:04 +0000212 C.EmitReport(report);
213 return;
214 }
215 // Assume the the value is non-zero going forward.
216 assert(trueState);
217 if (trueState != state) {
218 C.addTransition(trueState);
219 }
220}
221
222//===----------------------------------------------------------------------===//
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000223// Central dispatch function.
224//===----------------------------------------------------------------------===//
225
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +0000226void UnixAPIChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const {
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000227 // Get the callee. All the functions we care about are C functions
228 // with simple identifiers.
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000229 const ProgramState *state = C.getState();
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000230 const Expr *Callee = CE->getCallee();
Jordy Roseaf5b0432011-07-15 06:28:59 +0000231 const FunctionDecl *Fn = state->getSVal(Callee).getAsFunctionDecl();
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000232
233 if (!Fn)
234 return;
235
Jordy Roseaf5b0432011-07-15 06:28:59 +0000236 const IdentifierInfo *FI = Fn->getIdentifier();
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000237 if (!FI)
238 return;
239
Jordy Roseaf5b0432011-07-15 06:28:59 +0000240 SubChecker SC =
241 llvm::StringSwitch<SubChecker>(FI->getName())
242 .Case("open", &UnixAPIChecker::CheckOpen)
243 .Case("pthread_once", &UnixAPIChecker::CheckPthreadOnce)
244 .Case("malloc", &UnixAPIChecker::CheckMallocZero)
245 .Default(NULL);
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000246
Jordy Roseaf5b0432011-07-15 06:28:59 +0000247 if (SC)
248 (this->*SC)(C, CE);
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000249}
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +0000250
251//===----------------------------------------------------------------------===//
252// Registration.
253//===----------------------------------------------------------------------===//
254
255void ento::registerUnixAPIChecker(CheckerManager &mgr) {
256 mgr.registerChecker<UnixAPIChecker>();
257}