Ted Kremenek | d55522f | 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 | a6d04d5 | 2011-02-15 07:42:33 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Chandler Carruth | 3a02247 | 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 | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 507ff53 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Argyrios Kyrtzidis | dff865d | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 26984fb | 2010-04-09 20:26:58 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Optional.h" |
Benjamin Kramer | 3307c508 | 2012-02-04 12:31:12 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Benjamin Kramer | c048322 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringSwitch.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | d55522f | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | |
| 29 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 30 | using namespace ento; |
Ted Kremenek | d55522f | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 31 | |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 32 | enum class OpenVariant { |
| 33 | /// The standard open() call: |
| 34 | /// int open(const char *path, int oflag, ...); |
| 35 | Open, |
| 36 | |
| 37 | /// The variant taking a directory file descriptor and a relative path: |
| 38 | /// int openat(int fd, const char *path, int oflag, ...); |
| 39 | OpenAt |
| 40 | }; |
| 41 | |
Ted Kremenek | d55522f | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 42 | namespace { |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 43 | class UnixAPIChecker : public Checker< check::PreStmt<CallExpr> > { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 44 | mutable std::unique_ptr<BugType> BT_open, BT_pthreadOnce, BT_mallocZero; |
Argyrios Kyrtzidis | dff865d | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 45 | mutable Optional<uint64_t> Val_O_CREAT; |
Ted Kremenek | 212182d | 2010-04-08 22:15:34 +0000 | [diff] [blame] | 46 | |
| 47 | public: |
Argyrios Kyrtzidis | dff865d | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 48 | void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; |
Jordy Rose | f3dd00a | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 49 | |
| 50 | void CheckOpen(CheckerContext &C, const CallExpr *CE) const; |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 51 | void CheckOpenAt(CheckerContext &C, const CallExpr *CE) const; |
| 52 | |
Jordy Rose | f3dd00a | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 53 | void CheckPthreadOnce(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 54 | void CheckCallocZero(CheckerContext &C, const CallExpr *CE) const; |
Jordy Rose | f3dd00a | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 55 | void CheckMallocZero(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 56 | void CheckReallocZero(CheckerContext &C, const CallExpr *CE) const; |
Jordan Rose | 9e068aa | 2012-10-30 01:37:16 +0000 | [diff] [blame] | 57 | void CheckReallocfZero(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 58 | void CheckAllocaZero(CheckerContext &C, const CallExpr *CE) const; |
David Majnemer | 5116993 | 2016-10-31 05:37:48 +0000 | [diff] [blame] | 59 | void CheckAllocaWithAlignZero(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 60 | void CheckVallocZero(CheckerContext &C, const CallExpr *CE) const; |
Jordy Rose | f3dd00a | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 61 | |
| 62 | typedef void (UnixAPIChecker::*SubChecker)(CheckerContext &, |
| 63 | const CallExpr *) const; |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 64 | private: |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 65 | |
| 66 | void CheckOpenVariant(CheckerContext &C, |
| 67 | const CallExpr *CE, OpenVariant Variant) const; |
| 68 | |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 69 | bool ReportZeroByteAllocation(CheckerContext &C, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 70 | ProgramStateRef falseState, |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 71 | const Expr *arg, |
| 72 | const char *fn_name) const; |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 73 | void BasicAllocationCheck(CheckerContext &C, |
| 74 | const CallExpr *CE, |
| 75 | const unsigned numArgs, |
| 76 | const unsigned sizeArg, |
| 77 | const char *fn) const; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 78 | void LazyInitialize(std::unique_ptr<BugType> &BT, const char *name) const { |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 79 | if (BT) |
| 80 | return; |
| 81 | BT.reset(new BugType(this, name, categories::UnixAPI)); |
| 82 | } |
Jordan Rose | cd4db5c | 2014-08-20 16:58:03 +0000 | [diff] [blame] | 83 | void ReportOpenBug(CheckerContext &C, |
| 84 | ProgramStateRef State, |
| 85 | const char *Msg, |
| 86 | SourceRange SR) const; |
Ted Kremenek | d55522f | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 87 | }; |
| 88 | } //end anonymous namespace |
| 89 | |
Ted Kremenek | d55522f | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 90 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d55522f | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 91 | // "open" (man 2 open) |
| 92 | //===----------------------------------------------------------------------===// |
| 93 | |
Jordan Rose | cd4db5c | 2014-08-20 16:58:03 +0000 | [diff] [blame] | 94 | void UnixAPIChecker::ReportOpenBug(CheckerContext &C, |
| 95 | ProgramStateRef State, |
| 96 | const char *Msg, |
| 97 | SourceRange SR) const { |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 98 | ExplodedNode *N = C.generateErrorNode(State); |
Jordan Rose | cd4db5c | 2014-08-20 16:58:03 +0000 | [diff] [blame] | 99 | if (!N) |
| 100 | return; |
| 101 | |
| 102 | LazyInitialize(BT_open, "Improper use of 'open'"); |
| 103 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 104 | auto Report = llvm::make_unique<BugReport>(*BT_open, Msg, N); |
Jordan Rose | cd4db5c | 2014-08-20 16:58:03 +0000 | [diff] [blame] | 105 | Report->addRange(SR); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 106 | C.emitReport(std::move(Report)); |
Jordan Rose | cd4db5c | 2014-08-20 16:58:03 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Jordy Rose | f3dd00a | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 109 | void UnixAPIChecker::CheckOpen(CheckerContext &C, const CallExpr *CE) const { |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 110 | CheckOpenVariant(C, CE, OpenVariant::Open); |
| 111 | } |
| 112 | |
| 113 | void UnixAPIChecker::CheckOpenAt(CheckerContext &C, const CallExpr *CE) const { |
| 114 | CheckOpenVariant(C, CE, OpenVariant::OpenAt); |
| 115 | } |
| 116 | |
| 117 | void UnixAPIChecker::CheckOpenVariant(CheckerContext &C, |
| 118 | const CallExpr *CE, |
| 119 | OpenVariant Variant) const { |
| 120 | // The index of the argument taking the flags open flags (O_RDONLY, |
| 121 | // O_WRONLY, O_CREAT, etc.), |
| 122 | unsigned int FlagsArgIndex; |
| 123 | const char *VariantName; |
| 124 | switch (Variant) { |
| 125 | case OpenVariant::Open: |
| 126 | FlagsArgIndex = 1; |
| 127 | VariantName = "open"; |
| 128 | break; |
| 129 | case OpenVariant::OpenAt: |
| 130 | FlagsArgIndex = 2; |
| 131 | VariantName = "openat"; |
| 132 | break; |
| 133 | }; |
| 134 | |
| 135 | // All calls should at least provide arguments up to the 'flags' parameter. |
| 136 | unsigned int MinArgCount = FlagsArgIndex + 1; |
| 137 | |
| 138 | // If the flags has O_CREAT set then open/openat() require an additional |
| 139 | // argument specifying the file mode (permission bits) for the created file. |
| 140 | unsigned int CreateModeArgIndex = FlagsArgIndex + 1; |
| 141 | |
| 142 | // The create mode argument should be the last argument. |
| 143 | unsigned int MaxArgCount = CreateModeArgIndex + 1; |
| 144 | |
Jordan Rose | cd4db5c | 2014-08-20 16:58:03 +0000 | [diff] [blame] | 145 | ProgramStateRef state = C.getState(); |
| 146 | |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 147 | if (CE->getNumArgs() < MinArgCount) { |
Jordan Rose | cd4db5c | 2014-08-20 16:58:03 +0000 | [diff] [blame] | 148 | // The frontend should issue a warning for this case, so this is a sanity |
| 149 | // check. |
| 150 | return; |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 151 | } else if (CE->getNumArgs() == MaxArgCount) { |
| 152 | const Expr *Arg = CE->getArg(CreateModeArgIndex); |
Jordan Rose | ba129af | 2014-08-20 16:58:09 +0000 | [diff] [blame] | 153 | QualType QT = Arg->getType(); |
| 154 | if (!QT->isIntegerType()) { |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 155 | SmallString<256> SBuf; |
| 156 | llvm::raw_svector_ostream OS(SBuf); |
| 157 | OS << "The " << CreateModeArgIndex + 1 |
| 158 | << llvm::getOrdinalSuffix(CreateModeArgIndex + 1) |
| 159 | << " argument to '" << VariantName << "' is not an integer"; |
| 160 | |
Jordan Rose | ba129af | 2014-08-20 16:58:09 +0000 | [diff] [blame] | 161 | ReportOpenBug(C, state, |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 162 | SBuf.c_str(), |
Jordan Rose | ba129af | 2014-08-20 16:58:09 +0000 | [diff] [blame] | 163 | Arg->getSourceRange()); |
| 164 | return; |
| 165 | } |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 166 | } else if (CE->getNumArgs() > MaxArgCount) { |
| 167 | SmallString<256> SBuf; |
| 168 | llvm::raw_svector_ostream OS(SBuf); |
| 169 | OS << "Call to '" << VariantName << "' with more than " << MaxArgCount |
| 170 | << " arguments"; |
| 171 | |
Jordan Rose | cd4db5c | 2014-08-20 16:58:03 +0000 | [diff] [blame] | 172 | ReportOpenBug(C, state, |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 173 | SBuf.c_str(), |
| 174 | CE->getArg(MaxArgCount)->getSourceRange()); |
Jordan Rose | cd4db5c | 2014-08-20 16:58:03 +0000 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
Ted Kremenek | 212182d | 2010-04-08 22:15:34 +0000 | [diff] [blame] | 178 | // The definition of O_CREAT is platform specific. We need a better way |
| 179 | // of querying this information from the checking environment. |
Jordy Rose | f3dd00a | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 180 | if (!Val_O_CREAT.hasValue()) { |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 181 | if (C.getASTContext().getTargetInfo().getTriple().getVendor() |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 182 | == llvm::Triple::Apple) |
Jordy Rose | f3dd00a | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 183 | Val_O_CREAT = 0x0200; |
Ted Kremenek | 212182d | 2010-04-08 22:15:34 +0000 | [diff] [blame] | 184 | else { |
| 185 | // FIXME: We need a more general way of getting the O_CREAT value. |
| 186 | // We could possibly grovel through the preprocessor state, but |
Argyrios Kyrtzidis | 1696f50 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 187 | // that would require passing the Preprocessor object to the ExprEngine. |
Jordan Rose | 6b33c6f | 2014-03-26 17:05:46 +0000 | [diff] [blame] | 188 | // See also: MallocChecker.cpp / M_ZERO. |
Ted Kremenek | 212182d | 2010-04-08 22:15:34 +0000 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | } |
| 192 | |
Ted Kremenek | d55522f | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 193 | // Now check if oflags has O_CREAT set. |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 194 | const Expr *oflagsEx = CE->getArg(FlagsArgIndex); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 195 | const SVal V = state->getSVal(oflagsEx, C.getLocationContext()); |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 196 | if (!V.getAs<NonLoc>()) { |
Ted Kremenek | d55522f | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 197 | // The case where 'V' can be a location can only be due to a bad header, |
| 198 | // so in this case bail out. |
| 199 | return; |
| 200 | } |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 201 | NonLoc oflags = V.castAs<NonLoc>(); |
| 202 | NonLoc ocreateFlag = C.getSValBuilder() |
| 203 | .makeIntVal(Val_O_CREAT.getValue(), oflagsEx->getType()).castAs<NonLoc>(); |
Ted Kremenek | dc89142 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 204 | SVal maskedFlagsUC = C.getSValBuilder().evalBinOpNN(state, BO_And, |
Ted Kremenek | 9d0bb1e | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 205 | oflags, ocreateFlag, |
| 206 | oflagsEx->getType()); |
Ted Kremenek | d55522f | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 207 | if (maskedFlagsUC.isUnknownOrUndef()) |
| 208 | return; |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 209 | DefinedSVal maskedFlags = maskedFlagsUC.castAs<DefinedSVal>(); |
Ted Kremenek | d55522f | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 210 | |
| 211 | // Check if maskedFlags is non-zero. |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 212 | ProgramStateRef trueState, falseState; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 213 | std::tie(trueState, falseState) = state->assume(maskedFlags); |
Ted Kremenek | d55522f | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 214 | |
| 215 | // Only emit an error if the value of 'maskedFlags' is properly |
| 216 | // constrained; |
| 217 | if (!(trueState && !falseState)) |
| 218 | return; |
| 219 | |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 220 | if (CE->getNumArgs() < MaxArgCount) { |
| 221 | SmallString<256> SBuf; |
| 222 | llvm::raw_svector_ostream OS(SBuf); |
| 223 | OS << "Call to '" << VariantName << "' requires a " |
| 224 | << CreateModeArgIndex + 1 |
| 225 | << llvm::getOrdinalSuffix(CreateModeArgIndex + 1) |
| 226 | << " argument when the 'O_CREAT' flag is set"; |
Jordan Rose | cd4db5c | 2014-08-20 16:58:03 +0000 | [diff] [blame] | 227 | ReportOpenBug(C, trueState, |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 228 | SBuf.c_str(), |
Jordan Rose | cd4db5c | 2014-08-20 16:58:03 +0000 | [diff] [blame] | 229 | oflagsEx->getSourceRange()); |
Ted Kremenek | d55522f | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ea4a5ab | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 234 | // pthread_once |
| 235 | //===----------------------------------------------------------------------===// |
| 236 | |
Jordy Rose | f3dd00a | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 237 | void UnixAPIChecker::CheckPthreadOnce(CheckerContext &C, |
| 238 | const CallExpr *CE) const { |
Ted Kremenek | ea4a5ab | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 239 | |
| 240 | // This is similar to 'CheckDispatchOnce' in the MacOSXAPIChecker. |
| 241 | // They can possibly be refactored. |
| 242 | |
Ted Kremenek | ea4a5ab | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 243 | if (CE->getNumArgs() < 1) |
| 244 | return; |
| 245 | |
| 246 | // Check if the first argument is stack allocated. If so, issue a warning |
| 247 | // because that's likely to be bad news. |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 248 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 249 | const MemRegion *R = |
| 250 | state->getSVal(CE->getArg(0), C.getLocationContext()).getAsRegion(); |
Ted Kremenek | ea4a5ab | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 251 | if (!R || !isa<StackSpaceRegion>(R->getMemorySpace())) |
| 252 | return; |
| 253 | |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 254 | ExplodedNode *N = C.generateErrorNode(state); |
Ted Kremenek | ea4a5ab | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 255 | if (!N) |
| 256 | return; |
| 257 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 258 | SmallString<256> S; |
Ted Kremenek | ea4a5ab | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 259 | llvm::raw_svector_ostream os(S); |
| 260 | os << "Call to 'pthread_once' uses"; |
| 261 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) |
| 262 | os << " the local variable '" << VR->getDecl()->getName() << '\''; |
| 263 | else |
| 264 | os << " stack allocated memory"; |
| 265 | os << " for the \"control\" value. Using such transient memory for " |
| 266 | "the control value is potentially dangerous."; |
| 267 | if (isa<VarRegion>(R) && isa<StackLocalsSpaceRegion>(R->getMemorySpace())) |
| 268 | os << " Perhaps you intended to declare the variable as 'static'?"; |
| 269 | |
Jordy Rose | f3dd00a | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 270 | LazyInitialize(BT_pthreadOnce, "Improper use of 'pthread_once'"); |
| 271 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 272 | auto report = llvm::make_unique<BugReport>(*BT_pthreadOnce, os.str(), N); |
Ted Kremenek | ea4a5ab | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 273 | report->addRange(CE->getArg(0)->getSourceRange()); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 274 | C.emitReport(std::move(report)); |
Ted Kremenek | ea4a5ab | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | //===----------------------------------------------------------------------===// |
Jordan Rose | 9e068aa | 2012-10-30 01:37:16 +0000 | [diff] [blame] | 278 | // "calloc", "malloc", "realloc", "reallocf", "alloca" and "valloc" |
| 279 | // with allocation size 0 |
Ted Kremenek | 0c27bcf | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 280 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 281 | // FIXME: Eventually these should be rolled into the MallocChecker, but right now |
| 282 | // they're more basic and valuable for widespread use. |
Ted Kremenek | 0c27bcf | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 283 | |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 284 | // Returns true if we try to do a zero byte allocation, false otherwise. |
| 285 | // Fills in trueState and falseState. |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 286 | static bool IsZeroByteAllocation(ProgramStateRef state, |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 287 | const SVal argVal, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 288 | ProgramStateRef *trueState, |
| 289 | ProgramStateRef *falseState) { |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 290 | std::tie(*trueState, *falseState) = |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 291 | state->assume(argVal.castAs<DefinedSVal>()); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 292 | |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 293 | return (*falseState && !*trueState); |
| 294 | } |
| 295 | |
| 296 | // Generates an error report, indicating that the function whose name is given |
| 297 | // will perform a zero byte allocation. |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 298 | // Returns false if an error occurred, true otherwise. |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 299 | bool UnixAPIChecker::ReportZeroByteAllocation(CheckerContext &C, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 300 | ProgramStateRef falseState, |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 301 | const Expr *arg, |
| 302 | const char *fn_name) const { |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 303 | ExplodedNode *N = C.generateErrorNode(falseState); |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 304 | if (!N) |
| 305 | return false; |
| 306 | |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 307 | LazyInitialize(BT_mallocZero, |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 308 | "Undefined allocation of 0 bytes (CERT MEM04-C; CWE-131)"); |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 309 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 310 | SmallString<256> S; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 311 | llvm::raw_svector_ostream os(S); |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 312 | os << "Call to '" << fn_name << "' has an allocation size of 0 bytes"; |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 313 | auto report = llvm::make_unique<BugReport>(*BT_mallocZero, os.str(), N); |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 314 | |
| 315 | report->addRange(arg->getSourceRange()); |
Jordan Rose | a0f7d35 | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 316 | bugreporter::trackNullOrUndefValue(N, arg, *report); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 317 | C.emitReport(std::move(report)); |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 318 | |
| 319 | return true; |
| 320 | } |
| 321 | |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 322 | // Does a basic check for 0-sized allocations suitable for most of the below |
| 323 | // functions (modulo "calloc") |
| 324 | void UnixAPIChecker::BasicAllocationCheck(CheckerContext &C, |
| 325 | const CallExpr *CE, |
| 326 | const unsigned numArgs, |
| 327 | const unsigned sizeArg, |
| 328 | const char *fn) const { |
| 329 | // Sanity check for the correct number of arguments |
| 330 | if (CE->getNumArgs() != numArgs) |
| 331 | return; |
| 332 | |
| 333 | // Check if the allocation size is 0. |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 334 | ProgramStateRef state = C.getState(); |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 335 | ProgramStateRef trueState = nullptr, falseState = nullptr; |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 336 | const Expr *arg = CE->getArg(sizeArg); |
| 337 | SVal argVal = state->getSVal(arg, C.getLocationContext()); |
| 338 | |
| 339 | if (argVal.isUnknownOrUndef()) |
| 340 | return; |
| 341 | |
| 342 | // Is the value perfectly constrained to zero? |
| 343 | if (IsZeroByteAllocation(state, argVal, &trueState, &falseState)) { |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 344 | (void) ReportZeroByteAllocation(C, falseState, arg, fn); |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 345 | return; |
| 346 | } |
Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 347 | // Assume the value is non-zero going forward. |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 348 | assert(trueState); |
| 349 | if (trueState != state) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 350 | C.addTransition(trueState); |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 353 | void UnixAPIChecker::CheckCallocZero(CheckerContext &C, |
| 354 | const CallExpr *CE) const { |
| 355 | unsigned int nArgs = CE->getNumArgs(); |
| 356 | if (nArgs != 2) |
| 357 | return; |
| 358 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 359 | ProgramStateRef state = C.getState(); |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 360 | ProgramStateRef trueState = nullptr, falseState = nullptr; |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 361 | |
| 362 | unsigned int i; |
| 363 | for (i = 0; i < nArgs; i++) { |
| 364 | const Expr *arg = CE->getArg(i); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 365 | SVal argVal = state->getSVal(arg, C.getLocationContext()); |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 366 | if (argVal.isUnknownOrUndef()) { |
| 367 | if (i == 0) |
| 368 | continue; |
| 369 | else |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | if (IsZeroByteAllocation(state, argVal, &trueState, &falseState)) { |
| 374 | if (ReportZeroByteAllocation(C, falseState, arg, "calloc")) |
| 375 | return; |
| 376 | else if (i == 0) |
| 377 | continue; |
| 378 | else |
| 379 | return; |
| 380 | } |
| 381 | } |
| 382 | |
Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 383 | // Assume the value is non-zero going forward. |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 384 | assert(trueState); |
| 385 | if (trueState != state) |
| 386 | C.addTransition(trueState); |
| 387 | } |
| 388 | |
Jordy Rose | f3dd00a | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 389 | void UnixAPIChecker::CheckMallocZero(CheckerContext &C, |
| 390 | const CallExpr *CE) const { |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 391 | BasicAllocationCheck(C, CE, 1, 0, "malloc"); |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void UnixAPIChecker::CheckReallocZero(CheckerContext &C, |
| 395 | const CallExpr *CE) const { |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 396 | BasicAllocationCheck(C, CE, 2, 1, "realloc"); |
Ted Kremenek | 0c27bcf | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 397 | } |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 398 | |
Jordan Rose | 9e068aa | 2012-10-30 01:37:16 +0000 | [diff] [blame] | 399 | void UnixAPIChecker::CheckReallocfZero(CheckerContext &C, |
| 400 | const CallExpr *CE) const { |
| 401 | BasicAllocationCheck(C, CE, 2, 1, "reallocf"); |
| 402 | } |
| 403 | |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 404 | void UnixAPIChecker::CheckAllocaZero(CheckerContext &C, |
| 405 | const CallExpr *CE) const { |
| 406 | BasicAllocationCheck(C, CE, 1, 0, "alloca"); |
| 407 | } |
| 408 | |
David Majnemer | 5116993 | 2016-10-31 05:37:48 +0000 | [diff] [blame] | 409 | void UnixAPIChecker::CheckAllocaWithAlignZero(CheckerContext &C, |
| 410 | const CallExpr *CE) const { |
| 411 | BasicAllocationCheck(C, CE, 2, 0, "__builtin_alloca_with_align"); |
| 412 | } |
| 413 | |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 414 | void UnixAPIChecker::CheckVallocZero(CheckerContext &C, |
| 415 | const CallExpr *CE) const { |
| 416 | BasicAllocationCheck(C, CE, 1, 0, "valloc"); |
| 417 | } |
| 418 | |
| 419 | |
Ted Kremenek | 0c27bcf | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 420 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d55522f | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 421 | // Central dispatch function. |
| 422 | //===----------------------------------------------------------------------===// |
| 423 | |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 424 | void UnixAPIChecker::checkPreStmt(const CallExpr *CE, |
| 425 | CheckerContext &C) const { |
Jordan Rose | 6cd16c5 | 2012-07-10 23:13:01 +0000 | [diff] [blame] | 426 | const FunctionDecl *FD = C.getCalleeDecl(CE); |
| 427 | if (!FD || FD->getKind() != Decl::Function) |
| 428 | return; |
| 429 | |
Devin Coughlin | aa0fd76 | 2016-12-17 01:08:17 +0000 | [diff] [blame] | 430 | // Don't treat functions in namespaces with the same name a Unix function |
| 431 | // as a call to the Unix function. |
| 432 | const DeclContext *NamespaceCtx = FD->getEnclosingNamespaceContext(); |
| 433 | if (NamespaceCtx && isa<NamespaceDecl>(NamespaceCtx)) |
| 434 | return; |
| 435 | |
Jordan Rose | 6cd16c5 | 2012-07-10 23:13:01 +0000 | [diff] [blame] | 436 | StringRef FName = C.getCalleeName(FD); |
Anna Zaks | c6aa531 | 2011-12-01 05:57:37 +0000 | [diff] [blame] | 437 | if (FName.empty()) |
Ted Kremenek | d55522f | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 438 | return; |
| 439 | |
Jordy Rose | f3dd00a | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 440 | SubChecker SC = |
Anna Zaks | c6aa531 | 2011-12-01 05:57:37 +0000 | [diff] [blame] | 441 | llvm::StringSwitch<SubChecker>(FName) |
Jordy Rose | f3dd00a | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 442 | .Case("open", &UnixAPIChecker::CheckOpen) |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 443 | .Case("openat", &UnixAPIChecker::CheckOpenAt) |
Jordy Rose | f3dd00a | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 444 | .Case("pthread_once", &UnixAPIChecker::CheckPthreadOnce) |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 445 | .Case("calloc", &UnixAPIChecker::CheckCallocZero) |
Jordy Rose | f3dd00a | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 446 | .Case("malloc", &UnixAPIChecker::CheckMallocZero) |
Ted Kremenek | 134a83a | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 447 | .Case("realloc", &UnixAPIChecker::CheckReallocZero) |
Jordan Rose | 9e068aa | 2012-10-30 01:37:16 +0000 | [diff] [blame] | 448 | .Case("reallocf", &UnixAPIChecker::CheckReallocfZero) |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 449 | .Cases("alloca", "__builtin_alloca", &UnixAPIChecker::CheckAllocaZero) |
David Majnemer | 5116993 | 2016-10-31 05:37:48 +0000 | [diff] [blame] | 450 | .Case("__builtin_alloca_with_align", |
| 451 | &UnixAPIChecker::CheckAllocaWithAlignZero) |
Ted Kremenek | 940e00f | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 452 | .Case("valloc", &UnixAPIChecker::CheckVallocZero) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 453 | .Default(nullptr); |
Ted Kremenek | d55522f | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 454 | |
Jordy Rose | f3dd00a | 2011-07-15 06:28:59 +0000 | [diff] [blame] | 455 | if (SC) |
| 456 | (this->*SC)(C, CE); |
Ted Kremenek | d55522f | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 457 | } |
Argyrios Kyrtzidis | dff865d | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 458 | |
| 459 | //===----------------------------------------------------------------------===// |
| 460 | // Registration. |
| 461 | //===----------------------------------------------------------------------===// |
| 462 | |
| 463 | void ento::registerUnixAPIChecker(CheckerManager &mgr) { |
| 464 | mgr.registerChecker<UnixAPIChecker>(); |
| 465 | } |