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