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 | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 00bd44d | 2012-02-04 12:31:12 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringSwitch.h" |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
| 26 | |
| 27 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 28 | using namespace ento; |
Ted Kremenek | 66d5142 | 2010-04-09 20:26:58 +0000 | [diff] [blame] | 29 | using llvm::Optional; |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 30 | |
| 31 | namespace { |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 32 | class UnixAPIChecker : public Checker< check::PreStmt<CallExpr> > { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 33 | mutable OwningPtr<BugType> BT_open, BT_pthreadOnce, BT_mallocZero; |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 34 | mutable Optional<uint64_t> Val_O_CREAT; |
Ted Kremenek | bace4ba | 2010-04-08 22:15:34 +0000 | [diff] [blame] | 35 | |
| 36 | public: |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 37 | void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 38 | |
| 39 | void CheckOpen(CheckerContext &C, const CallExpr *CE) const; |
| 40 | void CheckPthreadOnce(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 41 | void CheckCallocZero(CheckerContext &C, const CallExpr *CE) const; |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 42 | void CheckMallocZero(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 43 | void CheckReallocZero(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | 3e97758 | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 44 | void CheckAllocaZero(CheckerContext &C, const CallExpr *CE) const; |
| 45 | void CheckVallocZero(CheckerContext &C, const CallExpr *CE) const; |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 46 | |
| 47 | typedef void (UnixAPIChecker::*SubChecker)(CheckerContext &, |
| 48 | const CallExpr *) const; |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 49 | private: |
| 50 | bool ReportZeroByteAllocation(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 51 | ProgramStateRef falseState, |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 52 | const Expr *arg, |
| 53 | const char *fn_name) const; |
Ted Kremenek | 3e97758 | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 54 | void BasicAllocationCheck(CheckerContext &C, |
| 55 | const CallExpr *CE, |
| 56 | const unsigned numArgs, |
| 57 | const unsigned sizeArg, |
| 58 | const char *fn) const; |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 59 | }; |
| 60 | } //end anonymous namespace |
| 61 | |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 62 | //===----------------------------------------------------------------------===// |
| 63 | // Utility functions. |
| 64 | //===----------------------------------------------------------------------===// |
| 65 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 66 | static inline void LazyInitialize(OwningPtr<BugType> &BT, |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 67 | const char *name) { |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 68 | if (BT) |
| 69 | return; |
Ted Kremenek | 6fd4505 | 2012-04-05 20:43:28 +0000 | [diff] [blame] | 70 | BT.reset(new BugType(name, categories::UnixAPI)); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | //===----------------------------------------------------------------------===// |
| 74 | // "open" (man 2 open) |
| 75 | //===----------------------------------------------------------------------===// |
| 76 | |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 77 | void UnixAPIChecker::CheckOpen(CheckerContext &C, const CallExpr *CE) const { |
Ted Kremenek | bace4ba | 2010-04-08 22:15:34 +0000 | [diff] [blame] | 78 | // The definition of O_CREAT is platform specific. We need a better way |
| 79 | // of querying this information from the checking environment. |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 80 | if (!Val_O_CREAT.hasValue()) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 81 | if (C.getASTContext().getTargetInfo().getTriple().getVendor() |
| 82 | == llvm::Triple::Apple) |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 83 | Val_O_CREAT = 0x0200; |
Ted Kremenek | bace4ba | 2010-04-08 22:15:34 +0000 | [diff] [blame] | 84 | else { |
| 85 | // FIXME: We need a more general way of getting the O_CREAT value. |
| 86 | // We could possibly grovel through the preprocessor state, but |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 87 | // that would require passing the Preprocessor object to the ExprEngine. |
Ted Kremenek | bace4ba | 2010-04-08 22:15:34 +0000 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | } |
| 91 | |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 92 | // Look at the 'oflags' argument for the O_CREAT flag. |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 93 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 94 | |
| 95 | if (CE->getNumArgs() < 2) { |
| 96 | // The frontend should issue a warning for this case, so this is a sanity |
| 97 | // check. |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | // Now check if oflags has O_CREAT set. |
| 102 | const Expr *oflagsEx = CE->getArg(1); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 103 | const SVal V = state->getSVal(oflagsEx, C.getLocationContext()); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 104 | if (!isa<NonLoc>(V)) { |
| 105 | // The case where 'V' can be a location can only be due to a bad header, |
| 106 | // so in this case bail out. |
| 107 | return; |
| 108 | } |
| 109 | NonLoc oflags = cast<NonLoc>(V); |
| 110 | NonLoc ocreateFlag = |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 111 | cast<NonLoc>(C.getSValBuilder().makeIntVal(Val_O_CREAT.getValue(), |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 112 | oflagsEx->getType())); |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 113 | SVal maskedFlagsUC = C.getSValBuilder().evalBinOpNN(state, BO_And, |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 114 | oflags, ocreateFlag, |
| 115 | oflagsEx->getType()); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 116 | if (maskedFlagsUC.isUnknownOrUndef()) |
| 117 | return; |
| 118 | DefinedSVal maskedFlags = cast<DefinedSVal>(maskedFlagsUC); |
| 119 | |
| 120 | // Check if maskedFlags is non-zero. |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 121 | ProgramStateRef trueState, falseState; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 122 | llvm::tie(trueState, falseState) = state->assume(maskedFlags); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 123 | |
| 124 | // Only emit an error if the value of 'maskedFlags' is properly |
| 125 | // constrained; |
| 126 | if (!(trueState && !falseState)) |
| 127 | return; |
| 128 | |
| 129 | if (CE->getNumArgs() < 3) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 130 | ExplodedNode *N = C.generateSink(trueState); |
Ted Kremenek | c757d79 | 2010-02-25 05:44:05 +0000 | [diff] [blame] | 131 | if (!N) |
| 132 | return; |
| 133 | |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 134 | LazyInitialize(BT_open, "Improper use of 'open'"); |
| 135 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 136 | BugReport *report = |
| 137 | new BugReport(*BT_open, |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 138 | "Call to 'open' requires a third argument when " |
| 139 | "the 'O_CREAT' flag is set", N); |
| 140 | report->addRange(oflagsEx->getSourceRange()); |
| 141 | C.EmitReport(report); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 146 | // pthread_once |
| 147 | //===----------------------------------------------------------------------===// |
| 148 | |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 149 | void UnixAPIChecker::CheckPthreadOnce(CheckerContext &C, |
| 150 | const CallExpr *CE) const { |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 151 | |
| 152 | // This is similar to 'CheckDispatchOnce' in the MacOSXAPIChecker. |
| 153 | // They can possibly be refactored. |
| 154 | |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 155 | if (CE->getNumArgs() < 1) |
| 156 | return; |
| 157 | |
| 158 | // Check if the first argument is stack allocated. If so, issue a warning |
| 159 | // because that's likely to be bad news. |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 160 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 161 | const MemRegion *R = |
| 162 | state->getSVal(CE->getArg(0), C.getLocationContext()).getAsRegion(); |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 163 | if (!R || !isa<StackSpaceRegion>(R->getMemorySpace())) |
| 164 | return; |
| 165 | |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 166 | ExplodedNode *N = C.generateSink(state); |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 167 | if (!N) |
| 168 | return; |
| 169 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 170 | SmallString<256> S; |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 171 | llvm::raw_svector_ostream os(S); |
| 172 | os << "Call to 'pthread_once' uses"; |
| 173 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) |
| 174 | os << " the local variable '" << VR->getDecl()->getName() << '\''; |
| 175 | else |
| 176 | os << " stack allocated memory"; |
| 177 | os << " for the \"control\" value. Using such transient memory for " |
| 178 | "the control value is potentially dangerous."; |
| 179 | if (isa<VarRegion>(R) && isa<StackLocalsSpaceRegion>(R->getMemorySpace())) |
| 180 | os << " Perhaps you intended to declare the variable as 'static'?"; |
| 181 | |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 182 | LazyInitialize(BT_pthreadOnce, "Improper use of 'pthread_once'"); |
| 183 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 184 | BugReport *report = new BugReport(*BT_pthreadOnce, os.str(), N); |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 185 | report->addRange(CE->getArg(0)->getSourceRange()); |
| 186 | C.EmitReport(report); |
| 187 | } |
| 188 | |
| 189 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3e97758 | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 190 | // "calloc", "malloc", "realloc", "alloca" and "valloc" with allocation size 0 |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 191 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3e97758 | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 192 | // FIXME: Eventually these should be rolled into the MallocChecker, but right now |
| 193 | // they're more basic and valuable for widespread use. |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 194 | |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 195 | // Returns true if we try to do a zero byte allocation, false otherwise. |
| 196 | // Fills in trueState and falseState. |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 197 | static bool IsZeroByteAllocation(ProgramStateRef state, |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 198 | const SVal argVal, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 199 | ProgramStateRef *trueState, |
| 200 | ProgramStateRef *falseState) { |
Ted Kremenek | 3e97758 | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 201 | llvm::tie(*trueState, *falseState) = |
| 202 | state->assume(cast<DefinedSVal>(argVal)); |
| 203 | |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 204 | return (*falseState && !*trueState); |
| 205 | } |
| 206 | |
| 207 | // Generates an error report, indicating that the function whose name is given |
| 208 | // will perform a zero byte allocation. |
| 209 | // Returns false if an error occured, true otherwise. |
| 210 | bool UnixAPIChecker::ReportZeroByteAllocation(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 211 | ProgramStateRef falseState, |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 212 | const Expr *arg, |
| 213 | const char *fn_name) const { |
| 214 | ExplodedNode *N = C.generateSink(falseState); |
| 215 | if (!N) |
| 216 | return false; |
| 217 | |
Ted Kremenek | 3e97758 | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 218 | LazyInitialize(BT_mallocZero, |
| 219 | "Undefined allocation of 0 bytes (CERT MEM04-C; CWE-131)"); |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 220 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 221 | SmallString<256> S; |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 222 | llvm::raw_svector_ostream os(S); |
| 223 | os << "Call to '" << fn_name << "' has an allocation size of 0 bytes"; |
| 224 | BugReport *report = new BugReport(*BT_mallocZero, os.str(), N); |
| 225 | |
| 226 | report->addRange(arg->getSourceRange()); |
Jordan Rose | a1f81bb | 2012-08-28 00:50:51 +0000 | [diff] [blame^] | 227 | bugreporter::trackNullOrUndefValue(N, arg, *report); |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 228 | C.EmitReport(report); |
| 229 | |
| 230 | return true; |
| 231 | } |
| 232 | |
Ted Kremenek | 3e97758 | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 233 | // Does a basic check for 0-sized allocations suitable for most of the below |
| 234 | // functions (modulo "calloc") |
| 235 | void UnixAPIChecker::BasicAllocationCheck(CheckerContext &C, |
| 236 | const CallExpr *CE, |
| 237 | const unsigned numArgs, |
| 238 | const unsigned sizeArg, |
| 239 | const char *fn) const { |
| 240 | // Sanity check for the correct number of arguments |
| 241 | if (CE->getNumArgs() != numArgs) |
| 242 | return; |
| 243 | |
| 244 | // Check if the allocation size is 0. |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 245 | ProgramStateRef state = C.getState(); |
| 246 | ProgramStateRef trueState = NULL, falseState = NULL; |
Ted Kremenek | 3e97758 | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 247 | const Expr *arg = CE->getArg(sizeArg); |
| 248 | SVal argVal = state->getSVal(arg, C.getLocationContext()); |
| 249 | |
| 250 | if (argVal.isUnknownOrUndef()) |
| 251 | return; |
| 252 | |
| 253 | // Is the value perfectly constrained to zero? |
| 254 | if (IsZeroByteAllocation(state, argVal, &trueState, &falseState)) { |
| 255 | (void) ReportZeroByteAllocation(C, falseState, arg, fn); |
| 256 | return; |
| 257 | } |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 258 | // Assume the value is non-zero going forward. |
Ted Kremenek | 3e97758 | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 259 | assert(trueState); |
| 260 | if (trueState != state) |
| 261 | C.addTransition(trueState); |
| 262 | } |
| 263 | |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 264 | void UnixAPIChecker::CheckCallocZero(CheckerContext &C, |
| 265 | const CallExpr *CE) const { |
| 266 | unsigned int nArgs = CE->getNumArgs(); |
| 267 | if (nArgs != 2) |
| 268 | return; |
| 269 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 270 | ProgramStateRef state = C.getState(); |
| 271 | ProgramStateRef trueState = NULL, falseState = NULL; |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 272 | |
| 273 | unsigned int i; |
| 274 | for (i = 0; i < nArgs; i++) { |
| 275 | const Expr *arg = CE->getArg(i); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 276 | SVal argVal = state->getSVal(arg, C.getLocationContext()); |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 277 | if (argVal.isUnknownOrUndef()) { |
| 278 | if (i == 0) |
| 279 | continue; |
| 280 | else |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | if (IsZeroByteAllocation(state, argVal, &trueState, &falseState)) { |
| 285 | if (ReportZeroByteAllocation(C, falseState, arg, "calloc")) |
| 286 | return; |
| 287 | else if (i == 0) |
| 288 | continue; |
| 289 | else |
| 290 | return; |
| 291 | } |
| 292 | } |
| 293 | |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 294 | // Assume the value is non-zero going forward. |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 295 | assert(trueState); |
| 296 | if (trueState != state) |
| 297 | C.addTransition(trueState); |
| 298 | } |
| 299 | |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 300 | void UnixAPIChecker::CheckMallocZero(CheckerContext &C, |
| 301 | const CallExpr *CE) const { |
Ted Kremenek | 3e97758 | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 302 | BasicAllocationCheck(C, CE, 1, 0, "malloc"); |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void UnixAPIChecker::CheckReallocZero(CheckerContext &C, |
| 306 | const CallExpr *CE) const { |
Ted Kremenek | 3e97758 | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 307 | BasicAllocationCheck(C, CE, 2, 1, "realloc"); |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 308 | } |
Ted Kremenek | 3e97758 | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 309 | |
| 310 | void UnixAPIChecker::CheckAllocaZero(CheckerContext &C, |
| 311 | const CallExpr *CE) const { |
| 312 | BasicAllocationCheck(C, CE, 1, 0, "alloca"); |
| 313 | } |
| 314 | |
| 315 | void UnixAPIChecker::CheckVallocZero(CheckerContext &C, |
| 316 | const CallExpr *CE) const { |
| 317 | BasicAllocationCheck(C, CE, 1, 0, "valloc"); |
| 318 | } |
| 319 | |
| 320 | |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 321 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 322 | // Central dispatch function. |
| 323 | //===----------------------------------------------------------------------===// |
| 324 | |
Ted Kremenek | 3e97758 | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 325 | void UnixAPIChecker::checkPreStmt(const CallExpr *CE, |
| 326 | CheckerContext &C) const { |
Jordan Rose | 5ef6e94 | 2012-07-10 23:13:01 +0000 | [diff] [blame] | 327 | const FunctionDecl *FD = C.getCalleeDecl(CE); |
| 328 | if (!FD || FD->getKind() != Decl::Function) |
| 329 | return; |
| 330 | |
| 331 | StringRef FName = C.getCalleeName(FD); |
Anna Zaks | b805c8f | 2011-12-01 05:57:37 +0000 | [diff] [blame] | 332 | if (FName.empty()) |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 333 | return; |
| 334 | |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 335 | SubChecker SC = |
Anna Zaks | b805c8f | 2011-12-01 05:57:37 +0000 | [diff] [blame] | 336 | llvm::StringSwitch<SubChecker>(FName) |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 337 | .Case("open", &UnixAPIChecker::CheckOpen) |
| 338 | .Case("pthread_once", &UnixAPIChecker::CheckPthreadOnce) |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 339 | .Case("calloc", &UnixAPIChecker::CheckCallocZero) |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 340 | .Case("malloc", &UnixAPIChecker::CheckMallocZero) |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 341 | .Case("realloc", &UnixAPIChecker::CheckReallocZero) |
Ted Kremenek | 3e97758 | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 342 | .Cases("alloca", "__builtin_alloca", &UnixAPIChecker::CheckAllocaZero) |
| 343 | .Case("valloc", &UnixAPIChecker::CheckVallocZero) |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 344 | .Default(NULL); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 345 | |
Jordy Rose | af5b043 | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 346 | if (SC) |
| 347 | (this->*SC)(C, CE); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 348 | } |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 349 | |
| 350 | //===----------------------------------------------------------------------===// |
| 351 | // Registration. |
| 352 | //===----------------------------------------------------------------------===// |
| 353 | |
| 354 | void ento::registerUnixAPIChecker(CheckerManager &mgr) { |
| 355 | mgr.registerChecker<UnixAPIChecker>(); |
| 356 | } |