Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 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 | |
Argyrios Kyrtzidis | 027a6ab | 2011-02-15 07:42:33 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 695fb50 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Ted Kremenek | 66d5142 | 2010-04-09 20:26:58 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Optional.h" |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringSwitch.h" |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 23 | #include <fcntl.h> |
| 24 | |
| 25 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 26 | using namespace ento; |
Ted Kremenek | 66d5142 | 2010-04-09 20:26:58 +0000 | [diff] [blame] | 27 | using llvm::Optional; |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 28 | |
| 29 | namespace { |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 30 | class UnixAPIChecker : public Checker< check::PreStmt<CallExpr> > { |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 31 | mutable llvm::OwningPtr<BugType> BT_open, BT_pthreadOnce, BT_mallocZero; |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 32 | mutable Optional<uint64_t> Val_O_CREAT; |
Ted Kremenek | bace4ba | 2010-04-08 22:15:34 +0000 | [diff] [blame] | 33 | |
| 34 | public: |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 35 | void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 36 | |
| 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 Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 43 | }; |
| 44 | } //end anonymous namespace |
| 45 | |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
| 47 | // Utility functions. |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 50 | static inline void LazyInitialize(llvm::OwningPtr<BugType> &BT, |
| 51 | const char *name) { |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 52 | if (BT) |
| 53 | return; |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 54 | BT.reset(new BugType(name, "Unix API")); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | // "open" (man 2 open) |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 61 | void UnixAPIChecker::CheckOpen(CheckerContext &C, const CallExpr *CE) const { |
Ted Kremenek | bace4ba | 2010-04-08 22:15:34 +0000 | [diff] [blame] | 62 | // The definition of O_CREAT is platform specific. We need a better way |
| 63 | // of querying this information from the checking environment. |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 64 | if (!Val_O_CREAT.hasValue()) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 65 | if (C.getASTContext().getTargetInfo().getTriple().getVendor() |
| 66 | == llvm::Triple::Apple) |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 67 | Val_O_CREAT = 0x0200; |
Ted Kremenek | bace4ba | 2010-04-08 22:15:34 +0000 | [diff] [blame] | 68 | 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 Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 71 | // that would require passing the Preprocessor object to the ExprEngine. |
Ted Kremenek | bace4ba | 2010-04-08 22:15:34 +0000 | [diff] [blame] | 72 | return; |
| 73 | } |
| 74 | } |
| 75 | |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 76 | // Look at the 'oflags' argument for the O_CREAT flag. |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 77 | const ProgramState *state = C.getState(); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 78 | |
| 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 Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 95 | cast<NonLoc>(C.getSValBuilder().makeIntVal(Val_O_CREAT.getValue(), |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 96 | oflagsEx->getType())); |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 97 | SVal maskedFlagsUC = C.getSValBuilder().evalBinOpNN(state, BO_And, |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 98 | oflags, ocreateFlag, |
| 99 | oflagsEx->getType()); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 100 | if (maskedFlagsUC.isUnknownOrUndef()) |
| 101 | return; |
| 102 | DefinedSVal maskedFlags = cast<DefinedSVal>(maskedFlagsUC); |
| 103 | |
| 104 | // Check if maskedFlags is non-zero. |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 105 | const ProgramState *trueState, *falseState; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 106 | llvm::tie(trueState, falseState) = state->assume(maskedFlags); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 107 | |
| 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 Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 114 | ExplodedNode *N = C.generateSink(trueState); |
Ted Kremenek | c757d79 | 2010-02-25 05:44:05 +0000 | [diff] [blame] | 115 | if (!N) |
| 116 | return; |
| 117 | |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 118 | LazyInitialize(BT_open, "Improper use of 'open'"); |
| 119 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 120 | BugReport *report = |
| 121 | new BugReport(*BT_open, |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 122 | "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 Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 130 | // pthread_once |
| 131 | //===----------------------------------------------------------------------===// |
| 132 | |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 133 | void UnixAPIChecker::CheckPthreadOnce(CheckerContext &C, |
| 134 | const CallExpr *CE) const { |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 135 | |
| 136 | // This is similar to 'CheckDispatchOnce' in the MacOSXAPIChecker. |
| 137 | // They can possibly be refactored. |
| 138 | |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 139 | 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 Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 144 | const ProgramState *state = C.getState(); |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 145 | const MemRegion *R = state->getSVal(CE->getArg(0)).getAsRegion(); |
| 146 | if (!R || !isa<StackSpaceRegion>(R->getMemorySpace())) |
| 147 | return; |
| 148 | |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 149 | ExplodedNode *N = C.generateSink(state); |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 150 | 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 Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 165 | LazyInitialize(BT_pthreadOnce, "Improper use of 'pthread_once'"); |
| 166 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 167 | BugReport *report = new BugReport(*BT_pthreadOnce, os.str(), N); |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 168 | report->addRange(CE->getArg(0)->getSourceRange()); |
| 169 | C.EmitReport(report); |
| 170 | } |
| 171 | |
| 172 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 173 | // "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 Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 178 | void UnixAPIChecker::CheckMallocZero(CheckerContext &C, |
| 179 | const CallExpr *CE) const { |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 180 | |
| 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 Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 186 | const ProgramState *state = C.getState(); |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 187 | SVal argVal = state->getSVal(CE->getArg(0)); |
| 188 | |
| 189 | if (argVal.isUnknownOrUndef()) |
| 190 | return; |
| 191 | |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 192 | const ProgramState *trueState, *falseState; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 193 | llvm::tie(trueState, falseState) = state->assume(cast<DefinedSVal>(argVal)); |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 194 | |
| 195 | // Is the value perfectly constrained to zero? |
| 196 | if (falseState && !trueState) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 197 | ExplodedNode *N = C.generateSink(falseState); |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 198 | if (!N) |
| 199 | return; |
| 200 | |
Ted Kremenek | 8fec972 | 2010-11-17 00:50:34 +0000 | [diff] [blame] | 201 | // FIXME: Add reference to CERT advisory, and/or C99 standard in bug |
| 202 | // output. |
| 203 | |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 204 | LazyInitialize(BT_mallocZero, "Undefined allocation of 0 bytes"); |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 205 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 206 | BugReport *report = |
| 207 | new BugReport(*BT_mallocZero, "Call to 'malloc' has an allocation" |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 208 | " size of 0 bytes", N); |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 209 | report->addRange(CE->getArg(0)->getSourceRange()); |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 210 | report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, |
| 211 | CE->getArg(0))); |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 212 | C.EmitReport(report); |
| 213 | return; |
| 214 | } |
| 215 | // Assume the the value is non-zero going forward. |
| 216 | assert(trueState); |
| 217 | if (trueState != state) { |
Anna Zaks | 063e088 | 2011-10-25 19:57:06 +0000 | [diff] [blame] | 218 | C.generateNode(trueState); |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 223 | // Central dispatch function. |
| 224 | //===----------------------------------------------------------------------===// |
| 225 | |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 226 | void UnixAPIChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const { |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 227 | // Get the callee. All the functions we care about are C functions |
| 228 | // with simple identifiers. |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 229 | const ProgramState *state = C.getState(); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 230 | const Expr *Callee = CE->getCallee(); |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 231 | const FunctionDecl *Fn = state->getSVal(Callee).getAsFunctionDecl(); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 232 | |
| 233 | if (!Fn) |
| 234 | return; |
| 235 | |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 236 | const IdentifierInfo *FI = Fn->getIdentifier(); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 237 | if (!FI) |
| 238 | return; |
| 239 | |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 240 | 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 Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 246 | |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 247 | if (SC) |
| 248 | (this->*SC)(C, CE); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 249 | } |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 250 | |
| 251 | //===----------------------------------------------------------------------===// |
| 252 | // Registration. |
| 253 | //===----------------------------------------------------------------------===// |
| 254 | |
| 255 | void ento::registerUnixAPIChecker(CheckerManager &mgr) { |
| 256 | mgr.registerChecker<UnixAPIChecker>(); |
| 257 | } |