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