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