Anna Zaks | c800f68 | 2011-10-11 16:49:54 +0000 | [diff] [blame] | 1 | //= CStringChecker.cpp - Checks calls to C string functions --------*- C++ -*-// |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 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 CStringChecker, which is an assortment of checks on calls |
| 11 | // to functions in <string.h>. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argyrios Kyrtzidis | a0decc9 | 2011-02-15 21:25:03 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Anna Zaks | f0dfc9c | 2012-02-17 22:35:31 +0000 | [diff] [blame] | 16 | #include "InterCheckerAPI.h" |
Jordan Rose | 223f0ff | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 17 | #include "clang/Basic/CharInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 695fb50 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
Benjamin Kramer | 00bd44d | 2012-02-04 12:31:12 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringSwitch.h" |
Benjamin Kramer | a93d0f2 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 29 | using namespace ento; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 30 | |
| 31 | namespace { |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 32 | class CStringChecker : public Checker< eval::Call, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 33 | check::PreStmt<DeclStmt>, |
| 34 | check::LiveSymbols, |
| 35 | check::DeadSymbols, |
| 36 | check::RegionChanges |
| 37 | > { |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 38 | mutable OwningPtr<BugType> BT_Null, |
| 39 | BT_Bounds, |
| 40 | BT_Overlap, |
| 41 | BT_NotCString, |
| 42 | BT_AdditionOverflow; |
| 43 | |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 44 | mutable const char *CurrentFunctionDescription; |
| 45 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 46 | public: |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 47 | /// The filter is used to filter out the diagnostics which are not enabled by |
| 48 | /// the user. |
| 49 | struct CStringChecksFilter { |
| 50 | DefaultBool CheckCStringNullArg; |
| 51 | DefaultBool CheckCStringOutOfBounds; |
| 52 | DefaultBool CheckCStringBufferOverlap; |
| 53 | DefaultBool CheckCStringNotNullTerm; |
| 54 | }; |
| 55 | |
| 56 | CStringChecksFilter Filter; |
| 57 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 58 | static void *getTag() { static int tag; return &tag; } |
| 59 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 60 | bool evalCall(const CallExpr *CE, CheckerContext &C) const; |
| 61 | void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 62 | void checkLiveSymbols(ProgramStateRef state, SymbolReaper &SR) const; |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 63 | void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 64 | bool wantsRegionChangeUpdate(ProgramStateRef state) const; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 66 | ProgramStateRef |
| 67 | checkRegionChanges(ProgramStateRef state, |
Anna Zaks | bf53dfa | 2012-12-20 00:38:25 +0000 | [diff] [blame] | 68 | const InvalidatedSymbols *, |
Jordy Rose | 537716a | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 69 | ArrayRef<const MemRegion *> ExplicitRegions, |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 70 | ArrayRef<const MemRegion *> Regions, |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 71 | const CallEvent *Call) const; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 72 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 73 | typedef void (CStringChecker::*FnCheck)(CheckerContext &, |
| 74 | const CallExpr *) const; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 75 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 76 | void evalMemcpy(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 77 | void evalMempcpy(CheckerContext &C, const CallExpr *CE) const; |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 78 | void evalMemmove(CheckerContext &C, const CallExpr *CE) const; |
| 79 | void evalBcopy(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 80 | void evalCopyCommon(CheckerContext &C, const CallExpr *CE, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 81 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 82 | const Expr *Size, |
| 83 | const Expr *Source, |
| 84 | const Expr *Dest, |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 85 | bool Restricted = false, |
| 86 | bool IsMempcpy = false) const; |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 87 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 88 | void evalMemcmp(CheckerContext &C, const CallExpr *CE) const; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 89 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 90 | void evalstrLength(CheckerContext &C, const CallExpr *CE) const; |
| 91 | void evalstrnLength(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 92 | void evalstrLengthCommon(CheckerContext &C, |
| 93 | const CallExpr *CE, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 94 | bool IsStrnlen = false) const; |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 95 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 96 | void evalStrcpy(CheckerContext &C, const CallExpr *CE) const; |
| 97 | void evalStrncpy(CheckerContext &C, const CallExpr *CE) const; |
| 98 | void evalStpcpy(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 99 | void evalStrcpyCommon(CheckerContext &C, |
| 100 | const CallExpr *CE, |
| 101 | bool returnEnd, |
| 102 | bool isBounded, |
| 103 | bool isAppending) const; |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 104 | |
| 105 | void evalStrcat(CheckerContext &C, const CallExpr *CE) const; |
| 106 | void evalStrncat(CheckerContext &C, const CallExpr *CE) const; |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 107 | |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 108 | void evalStrcmp(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 109 | void evalStrncmp(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 110 | void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | 454fd2d | 2011-05-02 19:05:49 +0000 | [diff] [blame] | 111 | void evalStrncasecmp(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 112 | void evalStrcmpCommon(CheckerContext &C, |
| 113 | const CallExpr *CE, |
| 114 | bool isBounded = false, |
| 115 | bool ignoreCase = false) const; |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 116 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 117 | // Utility methods |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 118 | std::pair<ProgramStateRef , ProgramStateRef > |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 119 | static assumeZero(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 120 | ProgramStateRef state, SVal V, QualType Ty); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 121 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 122 | static ProgramStateRef setCStringLength(ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 123 | const MemRegion *MR, |
| 124 | SVal strLength); |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 125 | static SVal getCStringLengthForRegion(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 126 | ProgramStateRef &state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 127 | const Expr *Ex, |
| 128 | const MemRegion *MR, |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 129 | bool hypothetical); |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 130 | SVal getCStringLength(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 131 | ProgramStateRef &state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 132 | const Expr *Ex, |
| 133 | SVal Buf, |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 134 | bool hypothetical = false) const; |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 135 | |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 136 | const StringLiteral *getCStringLiteral(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 137 | ProgramStateRef &state, |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 138 | const Expr *expr, |
| 139 | SVal val) const; |
| 140 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 141 | static ProgramStateRef InvalidateBuffer(CheckerContext &C, |
| 142 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 143 | const Expr *Ex, SVal V); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 145 | static bool SummarizeRegion(raw_ostream &os, ASTContext &Ctx, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 146 | const MemRegion *MR); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 147 | |
| 148 | // Re-usable checks |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 149 | ProgramStateRef checkNonNull(CheckerContext &C, |
| 150 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 151 | const Expr *S, |
| 152 | SVal l) const; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 153 | ProgramStateRef CheckLocation(CheckerContext &C, |
| 154 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 155 | const Expr *S, |
| 156 | SVal l, |
| 157 | const char *message = NULL) const; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 158 | ProgramStateRef CheckBufferAccess(CheckerContext &C, |
| 159 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 160 | const Expr *Size, |
| 161 | const Expr *FirstBuf, |
| 162 | const Expr *SecondBuf, |
| 163 | const char *firstMessage = NULL, |
| 164 | const char *secondMessage = NULL, |
| 165 | bool WarnAboutSize = false) const; |
| 166 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 167 | ProgramStateRef CheckBufferAccess(CheckerContext &C, |
| 168 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 169 | const Expr *Size, |
| 170 | const Expr *Buf, |
| 171 | const char *message = NULL, |
| 172 | bool WarnAboutSize = false) const { |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 173 | // This is a convenience override. |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 174 | return CheckBufferAccess(C, state, Size, Buf, NULL, message, NULL, |
| 175 | WarnAboutSize); |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 176 | } |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 177 | ProgramStateRef CheckOverlap(CheckerContext &C, |
| 178 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 179 | const Expr *Size, |
| 180 | const Expr *First, |
| 181 | const Expr *Second) const; |
| 182 | void emitOverlapBug(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 183 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 184 | const Stmt *First, |
| 185 | const Stmt *Second) const; |
| 186 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 187 | ProgramStateRef checkAdditionOverflow(CheckerContext &C, |
| 188 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 189 | NonLoc left, |
| 190 | NonLoc right) const; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 191 | }; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 192 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 193 | } //end anonymous namespace |
| 194 | |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 195 | REGISTER_MAP_WITH_PROGRAMSTATE(CStringLength, const MemRegion *, SVal) |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 196 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 197 | //===----------------------------------------------------------------------===// |
| 198 | // Individual checks and utility methods. |
| 199 | //===----------------------------------------------------------------------===// |
| 200 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 201 | std::pair<ProgramStateRef , ProgramStateRef > |
| 202 | CStringChecker::assumeZero(CheckerContext &C, ProgramStateRef state, SVal V, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 203 | QualType Ty) { |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 204 | Optional<DefinedSVal> val = V.getAs<DefinedSVal>(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 205 | if (!val) |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 206 | return std::pair<ProgramStateRef , ProgramStateRef >(state, state); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 207 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 208 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 209 | DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty); |
| 210 | return state->assume(svalBuilder.evalEQ(state, *val, zero)); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 211 | } |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 212 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 213 | ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C, |
| 214 | ProgramStateRef state, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 215 | const Expr *S, SVal l) const { |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 216 | // If a previous check has failed, propagate the failure. |
| 217 | if (!state) |
| 218 | return NULL; |
| 219 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 220 | ProgramStateRef stateNull, stateNonNull; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 221 | llvm::tie(stateNull, stateNonNull) = assumeZero(C, state, l, S->getType()); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 222 | |
| 223 | if (stateNull && !stateNonNull) { |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 224 | if (!Filter.CheckCStringNullArg) |
| 225 | return NULL; |
| 226 | |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 227 | ExplodedNode *N = C.generateSink(stateNull); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 228 | if (!N) |
| 229 | return NULL; |
| 230 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 231 | if (!BT_Null) |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 232 | BT_Null.reset(new BuiltinBug("Unix API", |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 233 | "Null pointer argument in call to byte string function")); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 234 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 235 | SmallString<80> buf; |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 236 | llvm::raw_svector_ostream os(buf); |
| 237 | assert(CurrentFunctionDescription); |
| 238 | os << "Null pointer argument in call to " << CurrentFunctionDescription; |
| 239 | |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 240 | // Generate a report for this bug. |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 241 | BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Null.get()); |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 242 | BugReport *report = new BugReport(*BT, os.str(), N); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 243 | |
| 244 | report->addRange(S->getSourceRange()); |
Jordan Rose | a1f81bb | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 245 | bugreporter::trackNullOrUndefValue(N, S, *report); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 246 | C.emitReport(report); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 247 | return NULL; |
| 248 | } |
| 249 | |
| 250 | // From here on, assume that the value is non-null. |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 251 | assert(stateNonNull); |
| 252 | return stateNonNull; |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 255 | // FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor? |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 256 | ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C, |
| 257 | ProgramStateRef state, |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 258 | const Expr *S, SVal l, |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 259 | const char *warningMsg) const { |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 260 | // If a previous check has failed, propagate the failure. |
| 261 | if (!state) |
| 262 | return NULL; |
| 263 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 264 | // Check for out of bound array element access. |
| 265 | const MemRegion *R = l.getAsRegion(); |
| 266 | if (!R) |
| 267 | return state; |
| 268 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 269 | const ElementRegion *ER = dyn_cast<ElementRegion>(R); |
| 270 | if (!ER) |
| 271 | return state; |
| 272 | |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 273 | assert(ER->getValueType() == C.getASTContext().CharTy && |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 274 | "CheckLocation should only be called with char* ElementRegions"); |
| 275 | |
| 276 | // Get the size of the array. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 277 | const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion()); |
| 278 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | 1e02241 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 279 | SVal Extent = |
| 280 | svalBuilder.convertToArrayIndex(superReg->getExtent(svalBuilder)); |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 281 | DefinedOrUnknownSVal Size = Extent.castAs<DefinedOrUnknownSVal>(); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 282 | |
| 283 | // Get the index of the accessed element. |
David Blaikie | 7a95de6 | 2013-02-21 22:23:56 +0000 | [diff] [blame^] | 284 | DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>(); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 285 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 286 | ProgramStateRef StInBound = state->assumeInBound(Idx, Size, true); |
| 287 | ProgramStateRef StOutBound = state->assumeInBound(Idx, Size, false); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 288 | if (StOutBound && !StInBound) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 289 | ExplodedNode *N = C.generateSink(StOutBound); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 290 | if (!N) |
| 291 | return NULL; |
| 292 | |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 293 | if (!BT_Bounds) { |
| 294 | BT_Bounds.reset(new BuiltinBug("Out-of-bound array access", |
| 295 | "Byte string function accesses out-of-bound array element")); |
| 296 | } |
| 297 | BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Bounds.get()); |
| 298 | |
| 299 | // Generate a report for this bug. |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 300 | BugReport *report; |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 301 | if (warningMsg) { |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 302 | report = new BugReport(*BT, warningMsg, N); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 303 | } else { |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 304 | assert(CurrentFunctionDescription); |
| 305 | assert(CurrentFunctionDescription[0] != '\0'); |
| 306 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 307 | SmallString<80> buf; |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 308 | llvm::raw_svector_ostream os(buf); |
Jordan Rose | 223f0ff | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 309 | os << toUppercase(CurrentFunctionDescription[0]) |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 310 | << &CurrentFunctionDescription[1] |
| 311 | << " accesses out-of-bound array element"; |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 312 | report = new BugReport(*BT, os.str(), N); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 313 | } |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 314 | |
| 315 | // FIXME: It would be nice to eventually make this diagnostic more clear, |
| 316 | // e.g., by referencing the original declaration or by saying *why* this |
| 317 | // reference is outside the range. |
| 318 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 319 | report->addRange(S->getSourceRange()); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 320 | C.emitReport(report); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 321 | return NULL; |
| 322 | } |
| 323 | |
| 324 | // Array bound check succeeded. From this point forward the array bound |
| 325 | // should always succeed. |
| 326 | return StInBound; |
| 327 | } |
| 328 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 329 | ProgramStateRef CStringChecker::CheckBufferAccess(CheckerContext &C, |
| 330 | ProgramStateRef state, |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 331 | const Expr *Size, |
| 332 | const Expr *FirstBuf, |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 333 | const Expr *SecondBuf, |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 334 | const char *firstMessage, |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 335 | const char *secondMessage, |
| 336 | bool WarnAboutSize) const { |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 337 | // If a previous check has failed, propagate the failure. |
| 338 | if (!state) |
| 339 | return NULL; |
| 340 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 341 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | 1e02241 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 342 | ASTContext &Ctx = svalBuilder.getContext(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 343 | const LocationContext *LCtx = C.getLocationContext(); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 344 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 345 | QualType sizeTy = Size->getType(); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 346 | QualType PtrTy = Ctx.getPointerType(Ctx.CharTy); |
| 347 | |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 348 | // Check that the first buffer is non-null. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 349 | SVal BufVal = state->getSVal(FirstBuf, LCtx); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 350 | state = checkNonNull(C, state, FirstBuf, BufVal); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 351 | if (!state) |
| 352 | return NULL; |
| 353 | |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 354 | // If out-of-bounds checking is turned off, skip the rest. |
| 355 | if (!Filter.CheckCStringOutOfBounds) |
| 356 | return state; |
| 357 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 358 | // Get the access length and make sure it is known. |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 359 | // FIXME: This assumes the caller has already checked that the access length |
| 360 | // is positive. And that it's unsigned. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 361 | SVal LengthVal = state->getSVal(Size, LCtx); |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 362 | Optional<NonLoc> Length = LengthVal.getAs<NonLoc>(); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 363 | if (!Length) |
| 364 | return state; |
| 365 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 366 | // Compute the offset of the last element to be accessed: size-1. |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 367 | NonLoc One = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>(); |
| 368 | NonLoc LastOffset = svalBuilder |
| 369 | .evalBinOpNN(state, BO_Sub, *Length, One, sizeTy).castAs<NonLoc>(); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 370 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 371 | // Check that the first buffer is sufficiently long. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 372 | SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType()); |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 373 | if (Optional<Loc> BufLoc = BufStart.getAs<Loc>()) { |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 374 | const Expr *warningExpr = (WarnAboutSize ? Size : FirstBuf); |
| 375 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 376 | SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, |
| 377 | LastOffset, PtrTy); |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 378 | state = CheckLocation(C, state, warningExpr, BufEnd, firstMessage); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 379 | |
Jordy Rose | b6a4026 | 2010-08-05 23:11:30 +0000 | [diff] [blame] | 380 | // If the buffer isn't large enough, abort. |
| 381 | if (!state) |
| 382 | return NULL; |
| 383 | } |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 384 | |
| 385 | // If there's a second buffer, check it as well. |
| 386 | if (SecondBuf) { |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 387 | BufVal = state->getSVal(SecondBuf, LCtx); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 388 | state = checkNonNull(C, state, SecondBuf, BufVal); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 389 | if (!state) |
| 390 | return NULL; |
| 391 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 392 | BufStart = svalBuilder.evalCast(BufVal, PtrTy, SecondBuf->getType()); |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 393 | if (Optional<Loc> BufLoc = BufStart.getAs<Loc>()) { |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 394 | const Expr *warningExpr = (WarnAboutSize ? Size : SecondBuf); |
| 395 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 396 | SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, |
| 397 | LastOffset, PtrTy); |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 398 | state = CheckLocation(C, state, warningExpr, BufEnd, secondMessage); |
Jordy Rose | b6a4026 | 2010-08-05 23:11:30 +0000 | [diff] [blame] | 399 | } |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | // Large enough or not, return this state! |
| 403 | return state; |
| 404 | } |
| 405 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 406 | ProgramStateRef CStringChecker::CheckOverlap(CheckerContext &C, |
| 407 | ProgramStateRef state, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 408 | const Expr *Size, |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 409 | const Expr *First, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 410 | const Expr *Second) const { |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 411 | if (!Filter.CheckCStringBufferOverlap) |
| 412 | return state; |
| 413 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 414 | // Do a simple check for overlap: if the two arguments are from the same |
| 415 | // buffer, see if the end of the first is greater than the start of the second |
| 416 | // or vice versa. |
| 417 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 418 | // If a previous check has failed, propagate the failure. |
| 419 | if (!state) |
| 420 | return NULL; |
| 421 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 422 | ProgramStateRef stateTrue, stateFalse; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 423 | |
| 424 | // Get the buffer values and make sure they're known locations. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 425 | const LocationContext *LCtx = C.getLocationContext(); |
| 426 | SVal firstVal = state->getSVal(First, LCtx); |
| 427 | SVal secondVal = state->getSVal(Second, LCtx); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 428 | |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 429 | Optional<Loc> firstLoc = firstVal.getAs<Loc>(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 430 | if (!firstLoc) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 431 | return state; |
| 432 | |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 433 | Optional<Loc> secondLoc = secondVal.getAs<Loc>(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 434 | if (!secondLoc) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 435 | return state; |
| 436 | |
| 437 | // Are the two values the same? |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 438 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 439 | llvm::tie(stateTrue, stateFalse) = |
| 440 | state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc)); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 441 | |
| 442 | if (stateTrue && !stateFalse) { |
| 443 | // If the values are known to be equal, that's automatically an overlap. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 444 | emitOverlapBug(C, stateTrue, First, Second); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 445 | return NULL; |
| 446 | } |
| 447 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 448 | // assume the two expressions are not equal. |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 449 | assert(stateFalse); |
| 450 | state = stateFalse; |
| 451 | |
| 452 | // Which value comes first? |
Jordy Rose | ee2fde1 | 2011-06-16 05:56:50 +0000 | [diff] [blame] | 453 | QualType cmpTy = svalBuilder.getConditionType(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 454 | SVal reverse = svalBuilder.evalBinOpLL(state, BO_GT, |
| 455 | *firstLoc, *secondLoc, cmpTy); |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 456 | Optional<DefinedOrUnknownSVal> reverseTest = |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 457 | reverse.getAs<DefinedOrUnknownSVal>(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 458 | if (!reverseTest) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 459 | return state; |
| 460 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 461 | llvm::tie(stateTrue, stateFalse) = state->assume(*reverseTest); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 462 | if (stateTrue) { |
| 463 | if (stateFalse) { |
| 464 | // If we don't know which one comes first, we can't perform this test. |
| 465 | return state; |
| 466 | } else { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 467 | // Switch the values so that firstVal is before secondVal. |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 468 | std::swap(firstLoc, secondLoc); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 469 | |
| 470 | // Switch the Exprs as well, so that they still correspond. |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 471 | std::swap(First, Second); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
| 475 | // Get the length, and make sure it too is known. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 476 | SVal LengthVal = state->getSVal(Size, LCtx); |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 477 | Optional<NonLoc> Length = LengthVal.getAs<NonLoc>(); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 478 | if (!Length) |
| 479 | return state; |
| 480 | |
| 481 | // Convert the first buffer's start address to char*. |
| 482 | // Bail out if the cast fails. |
Jordy Rose | ee2fde1 | 2011-06-16 05:56:50 +0000 | [diff] [blame] | 483 | ASTContext &Ctx = svalBuilder.getContext(); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 484 | QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy); |
Jordy Rose | 1e02241 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 485 | SVal FirstStart = svalBuilder.evalCast(*firstLoc, CharPtrTy, |
| 486 | First->getType()); |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 487 | Optional<Loc> FirstStartLoc = FirstStart.getAs<Loc>(); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 488 | if (!FirstStartLoc) |
| 489 | return state; |
| 490 | |
| 491 | // Compute the end of the first buffer. Bail out if THAT fails. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 492 | SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add, |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 493 | *FirstStartLoc, *Length, CharPtrTy); |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 494 | Optional<Loc> FirstEndLoc = FirstEnd.getAs<Loc>(); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 495 | if (!FirstEndLoc) |
| 496 | return state; |
| 497 | |
| 498 | // Is the end of the first buffer past the start of the second buffer? |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 499 | SVal Overlap = svalBuilder.evalBinOpLL(state, BO_GT, |
| 500 | *FirstEndLoc, *secondLoc, cmpTy); |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 501 | Optional<DefinedOrUnknownSVal> OverlapTest = |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 502 | Overlap.getAs<DefinedOrUnknownSVal>(); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 503 | if (!OverlapTest) |
| 504 | return state; |
| 505 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 506 | llvm::tie(stateTrue, stateFalse) = state->assume(*OverlapTest); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 507 | |
| 508 | if (stateTrue && !stateFalse) { |
| 509 | // Overlap! |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 510 | emitOverlapBug(C, stateTrue, First, Second); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 511 | return NULL; |
| 512 | } |
| 513 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 514 | // assume the two expressions don't overlap. |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 515 | assert(stateFalse); |
| 516 | return stateFalse; |
| 517 | } |
| 518 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 519 | void CStringChecker::emitOverlapBug(CheckerContext &C, ProgramStateRef state, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 520 | const Stmt *First, const Stmt *Second) const { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 521 | ExplodedNode *N = C.generateSink(state); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 522 | if (!N) |
| 523 | return; |
| 524 | |
| 525 | if (!BT_Overlap) |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 526 | BT_Overlap.reset(new BugType("Unix API", "Improper arguments")); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 527 | |
| 528 | // Generate a report for this bug. |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 529 | BugReport *report = |
| 530 | new BugReport(*BT_Overlap, |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 531 | "Arguments must not be overlapping buffers", N); |
| 532 | report->addRange(First->getSourceRange()); |
| 533 | report->addRange(Second->getSourceRange()); |
| 534 | |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 535 | C.emitReport(report); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 538 | ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C, |
| 539 | ProgramStateRef state, |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 540 | NonLoc left, |
| 541 | NonLoc right) const { |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 542 | // If out-of-bounds checking is turned off, skip the rest. |
| 543 | if (!Filter.CheckCStringOutOfBounds) |
| 544 | return state; |
| 545 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 546 | // If a previous check has failed, propagate the failure. |
| 547 | if (!state) |
| 548 | return NULL; |
| 549 | |
| 550 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 551 | BasicValueFactory &BVF = svalBuilder.getBasicValueFactory(); |
| 552 | |
| 553 | QualType sizeTy = svalBuilder.getContext().getSizeType(); |
| 554 | const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy); |
| 555 | NonLoc maxVal = svalBuilder.makeIntVal(maxValInt); |
| 556 | |
Anna Zaks | e3d250e | 2011-12-11 18:43:40 +0000 | [diff] [blame] | 557 | SVal maxMinusRight; |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 558 | if (right.getAs<nonloc::ConcreteInt>()) { |
Anna Zaks | e3d250e | 2011-12-11 18:43:40 +0000 | [diff] [blame] | 559 | maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right, |
| 560 | sizeTy); |
| 561 | } else { |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 562 | // Try switching the operands. (The order of these two assignments is |
| 563 | // important!) |
| 564 | maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left, |
| 565 | sizeTy); |
| 566 | left = right; |
| 567 | } |
| 568 | |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 569 | if (Optional<NonLoc> maxMinusRightNL = maxMinusRight.getAs<NonLoc>()) { |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 570 | QualType cmpTy = svalBuilder.getConditionType(); |
| 571 | // If left > max - right, we have an overflow. |
| 572 | SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left, |
| 573 | *maxMinusRightNL, cmpTy); |
| 574 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 575 | ProgramStateRef stateOverflow, stateOkay; |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 576 | llvm::tie(stateOverflow, stateOkay) = |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 577 | state->assume(willOverflow.castAs<DefinedOrUnknownSVal>()); |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 578 | |
| 579 | if (stateOverflow && !stateOkay) { |
| 580 | // We have an overflow. Emit a bug report. |
| 581 | ExplodedNode *N = C.generateSink(stateOverflow); |
| 582 | if (!N) |
| 583 | return NULL; |
| 584 | |
| 585 | if (!BT_AdditionOverflow) |
| 586 | BT_AdditionOverflow.reset(new BuiltinBug("API", |
| 587 | "Sum of expressions causes overflow")); |
| 588 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 589 | // This isn't a great error message, but this should never occur in real |
| 590 | // code anyway -- you'd have to create a buffer longer than a size_t can |
| 591 | // represent, which is sort of a contradiction. |
Jordy Rose | 8cc2491 | 2011-06-20 03:51:53 +0000 | [diff] [blame] | 592 | const char *warning = |
| 593 | "This expression will create a string whose length is too big to " |
| 594 | "be represented as a size_t"; |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 595 | |
| 596 | // Generate a report for this bug. |
Jordy Rose | 8cc2491 | 2011-06-20 03:51:53 +0000 | [diff] [blame] | 597 | BugReport *report = new BugReport(*BT_AdditionOverflow, warning, N); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 598 | C.emitReport(report); |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 599 | |
| 600 | return NULL; |
| 601 | } |
| 602 | |
| 603 | // From now on, assume an overflow didn't occur. |
| 604 | assert(stateOkay); |
| 605 | state = stateOkay; |
| 606 | } |
| 607 | |
| 608 | return state; |
| 609 | } |
| 610 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 611 | ProgramStateRef CStringChecker::setCStringLength(ProgramStateRef state, |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 612 | const MemRegion *MR, |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 613 | SVal strLength) { |
| 614 | assert(!strLength.isUndef() && "Attempt to set an undefined string length"); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 615 | |
| 616 | MR = MR->StripCasts(); |
| 617 | |
| 618 | switch (MR->getKind()) { |
| 619 | case MemRegion::StringRegionKind: |
| 620 | // FIXME: This can happen if we strcpy() into a string region. This is |
| 621 | // undefined [C99 6.4.5p6], but we should still warn about it. |
| 622 | return state; |
| 623 | |
| 624 | case MemRegion::SymbolicRegionKind: |
| 625 | case MemRegion::AllocaRegionKind: |
| 626 | case MemRegion::VarRegionKind: |
| 627 | case MemRegion::FieldRegionKind: |
| 628 | case MemRegion::ObjCIvarRegionKind: |
Jordy Rose | 210c05b | 2011-06-15 05:14:03 +0000 | [diff] [blame] | 629 | // These are the types we can currently track string lengths for. |
| 630 | break; |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 631 | |
| 632 | case MemRegion::ElementRegionKind: |
| 633 | // FIXME: Handle element regions by upper-bounding the parent region's |
| 634 | // string length. |
| 635 | return state; |
| 636 | |
| 637 | default: |
| 638 | // Other regions (mostly non-data) can't have a reliable C string length. |
| 639 | // For now, just ignore the change. |
| 640 | // FIXME: These are rare but not impossible. We should output some kind of |
| 641 | // warning for things like strcpy((char[]){'a', 0}, "b"); |
| 642 | return state; |
| 643 | } |
Jordy Rose | 210c05b | 2011-06-15 05:14:03 +0000 | [diff] [blame] | 644 | |
| 645 | if (strLength.isUnknown()) |
| 646 | return state->remove<CStringLength>(MR); |
| 647 | |
| 648 | return state->set<CStringLength>(MR, strLength); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 651 | SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 652 | ProgramStateRef &state, |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 653 | const Expr *Ex, |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 654 | const MemRegion *MR, |
| 655 | bool hypothetical) { |
| 656 | if (!hypothetical) { |
| 657 | // If there's a recorded length, go ahead and return it. |
| 658 | const SVal *Recorded = state->get<CStringLength>(MR); |
| 659 | if (Recorded) |
| 660 | return *Recorded; |
| 661 | } |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 662 | |
| 663 | // Otherwise, get a new symbol and update the state. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 664 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 665 | QualType sizeTy = svalBuilder.getContext().getSizeType(); |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 666 | SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(), |
Ted Kremenek | 66c486f | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 667 | MR, Ex, sizeTy, |
| 668 | C.blockCount()); |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 669 | |
| 670 | if (!hypothetical) |
| 671 | state = state->set<CStringLength>(MR, strLength); |
| 672 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 673 | return strLength; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 676 | SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state, |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 677 | const Expr *Ex, SVal Buf, |
| 678 | bool hypothetical) const { |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 679 | const MemRegion *MR = Buf.getAsRegion(); |
| 680 | if (!MR) { |
| 681 | // If we can't get a region, see if it's something we /know/ isn't a |
| 682 | // C string. In the context of locations, the only time we can issue such |
| 683 | // a warning is for labels. |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 684 | if (Optional<loc::GotoLabel> Label = Buf.getAs<loc::GotoLabel>()) { |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 685 | if (!Filter.CheckCStringNotNullTerm) |
| 686 | return UndefinedVal(); |
| 687 | |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 688 | if (ExplodedNode *N = C.addTransition(state)) { |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 689 | if (!BT_NotCString) |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 690 | BT_NotCString.reset(new BuiltinBug("Unix API", |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 691 | "Argument is not a null-terminated string.")); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 692 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 693 | SmallString<120> buf; |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 694 | llvm::raw_svector_ostream os(buf); |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 695 | assert(CurrentFunctionDescription); |
| 696 | os << "Argument to " << CurrentFunctionDescription |
| 697 | << " is the address of the label '" << Label->getLabel()->getName() |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 698 | << "', which is not a null-terminated string"; |
| 699 | |
| 700 | // Generate a report for this bug. |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 701 | BugReport *report = new BugReport(*BT_NotCString, |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 702 | os.str(), N); |
| 703 | |
| 704 | report->addRange(Ex->getSourceRange()); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 705 | C.emitReport(report); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 706 | } |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 707 | return UndefinedVal(); |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 708 | |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 711 | // If it's not a region and not a label, give up. |
| 712 | return UnknownVal(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 715 | // If we have a region, strip casts from it and see if we can figure out |
| 716 | // its length. For anything we can't figure out, just return UnknownVal. |
| 717 | MR = MR->StripCasts(); |
| 718 | |
| 719 | switch (MR->getKind()) { |
| 720 | case MemRegion::StringRegionKind: { |
| 721 | // Modifying the contents of string regions is undefined [C99 6.4.5p6], |
| 722 | // so we can assume that the byte length is the correct C string length. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 723 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 724 | QualType sizeTy = svalBuilder.getContext().getSizeType(); |
| 725 | const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral(); |
| 726 | return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 727 | } |
| 728 | case MemRegion::SymbolicRegionKind: |
| 729 | case MemRegion::AllocaRegionKind: |
| 730 | case MemRegion::VarRegionKind: |
| 731 | case MemRegion::FieldRegionKind: |
| 732 | case MemRegion::ObjCIvarRegionKind: |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 733 | return getCStringLengthForRegion(C, state, Ex, MR, hypothetical); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 734 | case MemRegion::CompoundLiteralRegionKind: |
| 735 | // FIXME: Can we track this? Is it necessary? |
| 736 | return UnknownVal(); |
| 737 | case MemRegion::ElementRegionKind: |
| 738 | // FIXME: How can we handle this? It's not good enough to subtract the |
| 739 | // offset from the base string length; consider "123\x00567" and &a[5]. |
| 740 | return UnknownVal(); |
| 741 | default: |
| 742 | // Other regions (mostly non-data) can't have a reliable C string length. |
| 743 | // In this case, an error is emitted and UndefinedVal is returned. |
| 744 | // The caller should always be prepared to handle this case. |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 745 | if (!Filter.CheckCStringNotNullTerm) |
| 746 | return UndefinedVal(); |
| 747 | |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 748 | if (ExplodedNode *N = C.addTransition(state)) { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 749 | if (!BT_NotCString) |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 750 | BT_NotCString.reset(new BuiltinBug("Unix API", |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 751 | "Argument is not a null-terminated string.")); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 752 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 753 | SmallString<120> buf; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 754 | llvm::raw_svector_ostream os(buf); |
| 755 | |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 756 | assert(CurrentFunctionDescription); |
| 757 | os << "Argument to " << CurrentFunctionDescription << " is "; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 758 | |
| 759 | if (SummarizeRegion(os, C.getASTContext(), MR)) |
| 760 | os << ", which is not a null-terminated string"; |
| 761 | else |
| 762 | os << "not a null-terminated string"; |
| 763 | |
| 764 | // Generate a report for this bug. |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 765 | BugReport *report = new BugReport(*BT_NotCString, |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 766 | os.str(), N); |
| 767 | |
| 768 | report->addRange(Ex->getSourceRange()); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 769 | C.emitReport(report); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | return UndefinedVal(); |
| 773 | } |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 776 | const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 777 | ProgramStateRef &state, const Expr *expr, SVal val) const { |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 778 | |
| 779 | // Get the memory region pointed to by the val. |
| 780 | const MemRegion *bufRegion = val.getAsRegion(); |
| 781 | if (!bufRegion) |
| 782 | return NULL; |
| 783 | |
| 784 | // Strip casts off the memory region. |
| 785 | bufRegion = bufRegion->StripCasts(); |
| 786 | |
| 787 | // Cast the memory region to a string region. |
| 788 | const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion); |
| 789 | if (!strRegion) |
| 790 | return NULL; |
| 791 | |
| 792 | // Return the actual string in the string region. |
| 793 | return strRegion->getStringLiteral(); |
| 794 | } |
| 795 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 796 | ProgramStateRef CStringChecker::InvalidateBuffer(CheckerContext &C, |
| 797 | ProgramStateRef state, |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 798 | const Expr *E, SVal V) { |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 799 | Optional<Loc> L = V.getAs<Loc>(); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 800 | if (!L) |
| 801 | return state; |
| 802 | |
| 803 | // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes |
| 804 | // some assumptions about the value that CFRefCount can't. Even so, it should |
| 805 | // probably be refactored. |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 806 | if (Optional<loc::MemRegionVal> MR = L->getAs<loc::MemRegionVal>()) { |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 807 | const MemRegion *R = MR->getRegion()->StripCasts(); |
| 808 | |
| 809 | // Are we dealing with an ElementRegion? If so, we should be invalidating |
| 810 | // the super-region. |
| 811 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
| 812 | R = ER->getSuperRegion(); |
| 813 | // FIXME: What about layers of ElementRegions? |
| 814 | } |
| 815 | |
| 816 | // Invalidate this region. |
Ted Kremenek | 3133f79 | 2012-02-17 23:13:45 +0000 | [diff] [blame] | 817 | const LocationContext *LCtx = C.getPredecessor()->getLocationContext(); |
Anna Zaks | bf53dfa | 2012-12-20 00:38:25 +0000 | [diff] [blame] | 818 | return state->invalidateRegions(R, E, C.blockCount(), LCtx, |
Anna Zaks | 64eb070 | 2013-01-16 01:35:54 +0000 | [diff] [blame] | 819 | /*CausesPointerEscape*/ false); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | // If we have a non-region value by chance, just remove the binding. |
| 823 | // FIXME: is this necessary or correct? This handles the non-Region |
| 824 | // cases. Is it ever valid to store to these? |
Ted Kremenek | 56a46b5 | 2012-08-22 06:37:46 +0000 | [diff] [blame] | 825 | return state->killBinding(*L); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 828 | bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx, |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 829 | const MemRegion *MR) { |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 830 | const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(MR); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 831 | |
Jordy Rose | 096aef9 | 2011-08-12 21:41:07 +0000 | [diff] [blame] | 832 | switch (MR->getKind()) { |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 833 | case MemRegion::FunctionTextRegionKind: { |
Anna Zaks | 5fc1d0c | 2012-09-17 19:13:56 +0000 | [diff] [blame] | 834 | const NamedDecl *FD = cast<FunctionTextRegion>(MR)->getDecl(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 835 | if (FD) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 836 | os << "the address of the function '" << *FD << '\''; |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 837 | else |
| 838 | os << "the address of a function"; |
| 839 | return true; |
| 840 | } |
| 841 | case MemRegion::BlockTextRegionKind: |
| 842 | os << "block text"; |
| 843 | return true; |
| 844 | case MemRegion::BlockDataRegionKind: |
| 845 | os << "a block"; |
| 846 | return true; |
| 847 | case MemRegion::CXXThisRegionKind: |
Zhongxing Xu | 02fe28c | 2010-11-26 08:52:48 +0000 | [diff] [blame] | 848 | case MemRegion::CXXTempObjectRegionKind: |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 849 | os << "a C++ temp object of type " << TVR->getValueType().getAsString(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 850 | return true; |
| 851 | case MemRegion::VarRegionKind: |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 852 | os << "a variable of type" << TVR->getValueType().getAsString(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 853 | return true; |
| 854 | case MemRegion::FieldRegionKind: |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 855 | os << "a field of type " << TVR->getValueType().getAsString(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 856 | return true; |
| 857 | case MemRegion::ObjCIvarRegionKind: |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 858 | os << "an instance variable of type " << TVR->getValueType().getAsString(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 859 | return true; |
| 860 | default: |
| 861 | return false; |
| 862 | } |
| 863 | } |
| 864 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 865 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 866 | // evaluation of individual function calls. |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 867 | //===----------------------------------------------------------------------===// |
| 868 | |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 869 | void CStringChecker::evalCopyCommon(CheckerContext &C, |
| 870 | const CallExpr *CE, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 871 | ProgramStateRef state, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 872 | const Expr *Size, const Expr *Dest, |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 873 | const Expr *Source, bool Restricted, |
| 874 | bool IsMempcpy) const { |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 875 | CurrentFunctionDescription = "memory copy function"; |
| 876 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 877 | // See if the size argument is zero. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 878 | const LocationContext *LCtx = C.getLocationContext(); |
| 879 | SVal sizeVal = state->getSVal(Size, LCtx); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 880 | QualType sizeTy = Size->getType(); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 881 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 882 | ProgramStateRef stateZeroSize, stateNonZeroSize; |
Jordy Rose | 1e02241 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 883 | llvm::tie(stateZeroSize, stateNonZeroSize) = |
| 884 | assumeZero(C, state, sizeVal, sizeTy); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 885 | |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 886 | // Get the value of the Dest. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 887 | SVal destVal = state->getSVal(Dest, LCtx); |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 888 | |
| 889 | // If the size is zero, there won't be any actual memory access, so |
| 890 | // just bind the return value to the destination buffer and return. |
Anna Zaks | dd160f3 | 2012-05-03 18:21:28 +0000 | [diff] [blame] | 891 | if (stateZeroSize && !stateNonZeroSize) { |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 892 | stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, destVal); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 893 | C.addTransition(stateZeroSize); |
Anna Zaks | dd160f3 | 2012-05-03 18:21:28 +0000 | [diff] [blame] | 894 | return; |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 895 | } |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 896 | |
| 897 | // If the size can be nonzero, we have to check the other arguments. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 898 | if (stateNonZeroSize) { |
Jordy Rose | 22d2717 | 2011-06-04 00:04:22 +0000 | [diff] [blame] | 899 | state = stateNonZeroSize; |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 900 | |
| 901 | // Ensure the destination is not null. If it is NULL there will be a |
| 902 | // NULL pointer dereference. |
| 903 | state = checkNonNull(C, state, Dest, destVal); |
| 904 | if (!state) |
| 905 | return; |
| 906 | |
| 907 | // Get the value of the Src. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 908 | SVal srcVal = state->getSVal(Source, LCtx); |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 909 | |
| 910 | // Ensure the source is not null. If it is NULL there will be a |
| 911 | // NULL pointer dereference. |
| 912 | state = checkNonNull(C, state, Source, srcVal); |
| 913 | if (!state) |
| 914 | return; |
| 915 | |
Jordy Rose | 7182b96 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 916 | // Ensure the accesses are valid and that the buffers do not overlap. |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 917 | const char * const writeWarning = |
| 918 | "Memory copy function overflows destination buffer"; |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 919 | state = CheckBufferAccess(C, state, Size, Dest, Source, |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 920 | writeWarning, /* sourceWarning = */ NULL); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 921 | if (Restricted) |
| 922 | state = CheckOverlap(C, state, Size, Dest, Source); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 923 | |
Jordy Rose | 7182b96 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 924 | if (!state) |
| 925 | return; |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 926 | |
Jordy Rose | 7182b96 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 927 | // If this is mempcpy, get the byte after the last byte copied and |
| 928 | // bind the expr. |
| 929 | if (IsMempcpy) { |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 930 | loc::MemRegionVal destRegVal = destVal.castAs<loc::MemRegionVal>(); |
Jordy Rose | 7182b96 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 931 | |
| 932 | // Get the length to copy. |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 933 | if (Optional<NonLoc> lenValNonLoc = sizeVal.getAs<NonLoc>()) { |
Jordy Rose | 7182b96 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 934 | // Get the byte after the last byte copied. |
| 935 | SVal lastElement = C.getSValBuilder().evalBinOpLN(state, BO_Add, |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 936 | destRegVal, |
Jordy Rose | 7182b96 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 937 | *lenValNonLoc, |
| 938 | Dest->getType()); |
| 939 | |
| 940 | // The byte after the last byte copied is the return value. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 941 | state = state->BindExpr(CE, LCtx, lastElement); |
Jordy Rose | 3f8bb2f | 2011-06-04 01:47:27 +0000 | [diff] [blame] | 942 | } else { |
Jordy Rose | 7182b96 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 943 | // If we don't know how much we copied, we can at least |
| 944 | // conjure a return value for later. |
Ted Kremenek | 66c486f | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 945 | SVal result = C.getSValBuilder().conjureSymbolVal(0, CE, LCtx, |
| 946 | C.blockCount()); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 947 | state = state->BindExpr(CE, LCtx, result); |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Jordy Rose | 7182b96 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 950 | } else { |
| 951 | // All other copies return the destination buffer. |
| 952 | // (Well, bcopy() has a void return type, but this won't hurt.) |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 953 | state = state->BindExpr(CE, LCtx, destVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 954 | } |
Jordy Rose | 7182b96 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 955 | |
| 956 | // Invalidate the destination. |
| 957 | // FIXME: Even if we can't perfectly model the copy, we should see if we |
| 958 | // can use LazyCompoundVals to copy the source values into the destination. |
| 959 | // This would probably remove any existing bindings past the end of the |
| 960 | // copied region, but that's still an improvement over blank invalidation. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 961 | state = InvalidateBuffer(C, state, Dest, |
| 962 | state->getSVal(Dest, C.getLocationContext())); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 963 | C.addTransition(state); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | |
| 967 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 968 | void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 969 | if (CE->getNumArgs() < 3) |
| 970 | return; |
| 971 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 972 | // void *memcpy(void *restrict dst, const void *restrict src, size_t n); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 973 | // The return value is the address of the destination buffer. |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 974 | const Expr *Dest = CE->getArg(0); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 975 | ProgramStateRef state = C.getState(); |
Jordy Rose | 3f8bb2f | 2011-06-04 01:47:27 +0000 | [diff] [blame] | 976 | |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 977 | evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true); |
| 978 | } |
| 979 | |
| 980 | void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 981 | if (CE->getNumArgs() < 3) |
| 982 | return; |
| 983 | |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 984 | // void *mempcpy(void *restrict dst, const void *restrict src, size_t n); |
| 985 | // The return value is a pointer to the byte following the last written byte. |
| 986 | const Expr *Dest = CE->getArg(0); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 987 | ProgramStateRef state = C.getState(); |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 988 | |
| 989 | evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true, true); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 992 | void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 993 | if (CE->getNumArgs() < 3) |
| 994 | return; |
| 995 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 996 | // void *memmove(void *dst, const void *src, size_t n); |
| 997 | // The return value is the address of the destination buffer. |
| 998 | const Expr *Dest = CE->getArg(0); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 999 | ProgramStateRef state = C.getState(); |
Jordy Rose | 3f8bb2f | 2011-06-04 01:47:27 +0000 | [diff] [blame] | 1000 | |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 1001 | evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1)); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1004 | void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1005 | if (CE->getNumArgs() < 3) |
| 1006 | return; |
| 1007 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1008 | // void bcopy(const void *src, void *dst, size_t n); |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 1009 | evalCopyCommon(C, CE, C.getState(), |
| 1010 | CE->getArg(2), CE->getArg(1), CE->getArg(0)); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1013 | void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1014 | if (CE->getNumArgs() < 3) |
| 1015 | return; |
| 1016 | |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1017 | // int memcmp(const void *s1, const void *s2, size_t n); |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 1018 | CurrentFunctionDescription = "memory comparison function"; |
| 1019 | |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1020 | const Expr *Left = CE->getArg(0); |
| 1021 | const Expr *Right = CE->getArg(1); |
| 1022 | const Expr *Size = CE->getArg(2); |
| 1023 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1024 | ProgramStateRef state = C.getState(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1025 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1026 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1027 | // See if the size argument is zero. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1028 | const LocationContext *LCtx = C.getLocationContext(); |
| 1029 | SVal sizeVal = state->getSVal(Size, LCtx); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1030 | QualType sizeTy = Size->getType(); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1031 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1032 | ProgramStateRef stateZeroSize, stateNonZeroSize; |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1033 | llvm::tie(stateZeroSize, stateNonZeroSize) = |
| 1034 | assumeZero(C, state, sizeVal, sizeTy); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1035 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1036 | // If the size can be zero, the result will be 0 in that case, and we don't |
| 1037 | // have to check either of the buffers. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1038 | if (stateZeroSize) { |
| 1039 | state = stateZeroSize; |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1040 | state = state->BindExpr(CE, LCtx, |
| 1041 | svalBuilder.makeZeroVal(CE->getType())); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1042 | C.addTransition(state); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1045 | // If the size can be nonzero, we have to check the other arguments. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1046 | if (stateNonZeroSize) { |
| 1047 | state = stateNonZeroSize; |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1048 | // If we know the two buffers are the same, we know the result is 0. |
| 1049 | // First, get the two buffers' addresses. Another checker will have already |
| 1050 | // made sure they're not undefined. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1051 | DefinedOrUnknownSVal LV = |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1052 | state->getSVal(Left, LCtx).castAs<DefinedOrUnknownSVal>(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1053 | DefinedOrUnknownSVal RV = |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1054 | state->getSVal(Right, LCtx).castAs<DefinedOrUnknownSVal>(); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1055 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1056 | // See if they are the same. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1057 | DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1058 | ProgramStateRef StSameBuf, StNotSameBuf; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 1059 | llvm::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1060 | |
Jordy Rose | 1e02241 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 1061 | // If the two arguments might be the same buffer, we know the result is 0, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1062 | // and we only need to check one size. |
| 1063 | if (StSameBuf) { |
| 1064 | state = StSameBuf; |
| 1065 | state = CheckBufferAccess(C, state, Size, Left); |
| 1066 | if (state) { |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1067 | state = StSameBuf->BindExpr(CE, LCtx, |
| 1068 | svalBuilder.makeZeroVal(CE->getType())); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1069 | C.addTransition(state); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | // If the two arguments might be different buffers, we have to check the |
| 1074 | // size of both of them. |
| 1075 | if (StNotSameBuf) { |
| 1076 | state = StNotSameBuf; |
| 1077 | state = CheckBufferAccess(C, state, Size, Left, Right); |
| 1078 | if (state) { |
| 1079 | // The return value is the comparison result, which we don't know. |
Ted Kremenek | 66c486f | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 1080 | SVal CmpV = svalBuilder.conjureSymbolVal(0, CE, LCtx, C.blockCount()); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1081 | state = state->BindExpr(CE, LCtx, CmpV); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1082 | C.addTransition(state); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | } |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1088 | void CStringChecker::evalstrLength(CheckerContext &C, |
| 1089 | const CallExpr *CE) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1090 | if (CE->getNumArgs() < 1) |
| 1091 | return; |
| 1092 | |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1093 | // size_t strlen(const char *s); |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1094 | evalstrLengthCommon(C, CE, /* IsStrnlen = */ false); |
| 1095 | } |
| 1096 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1097 | void CStringChecker::evalstrnLength(CheckerContext &C, |
| 1098 | const CallExpr *CE) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1099 | if (CE->getNumArgs() < 2) |
| 1100 | return; |
| 1101 | |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1102 | // size_t strnlen(const char *s, size_t maxlen); |
| 1103 | evalstrLengthCommon(C, CE, /* IsStrnlen = */ true); |
| 1104 | } |
| 1105 | |
| 1106 | void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1107 | bool IsStrnlen) const { |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 1108 | CurrentFunctionDescription = "string length function"; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1109 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1110 | const LocationContext *LCtx = C.getLocationContext(); |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1111 | |
| 1112 | if (IsStrnlen) { |
| 1113 | const Expr *maxlenExpr = CE->getArg(1); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1114 | SVal maxlenVal = state->getSVal(maxlenExpr, LCtx); |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1115 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1116 | ProgramStateRef stateZeroSize, stateNonZeroSize; |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1117 | llvm::tie(stateZeroSize, stateNonZeroSize) = |
| 1118 | assumeZero(C, state, maxlenVal, maxlenExpr->getType()); |
| 1119 | |
| 1120 | // If the size can be zero, the result will be 0 in that case, and we don't |
| 1121 | // have to check the string itself. |
| 1122 | if (stateZeroSize) { |
| 1123 | SVal zero = C.getSValBuilder().makeZeroVal(CE->getType()); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1124 | stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, zero); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1125 | C.addTransition(stateZeroSize); |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | // If the size is GUARANTEED to be zero, we're done! |
| 1129 | if (!stateNonZeroSize) |
| 1130 | return; |
| 1131 | |
| 1132 | // Otherwise, record the assumption that the size is nonzero. |
| 1133 | state = stateNonZeroSize; |
| 1134 | } |
| 1135 | |
| 1136 | // Check that the string argument is non-null. |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1137 | const Expr *Arg = CE->getArg(0); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1138 | SVal ArgVal = state->getSVal(Arg, LCtx); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1139 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1140 | state = checkNonNull(C, state, Arg, ArgVal); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1141 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1142 | if (!state) |
| 1143 | return; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1144 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1145 | SVal strLength = getCStringLength(C, state, Arg, ArgVal); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1146 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1147 | // If the argument isn't a valid C string, there's no valid state to |
| 1148 | // transition to. |
| 1149 | if (strLength.isUndef()) |
| 1150 | return; |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1151 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1152 | DefinedOrUnknownSVal result = UnknownVal(); |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1153 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1154 | // If the check is for strnlen() then bind the return value to no more than |
| 1155 | // the maxlen value. |
| 1156 | if (IsStrnlen) { |
Jordy Rose | ee2fde1 | 2011-06-16 05:56:50 +0000 | [diff] [blame] | 1157 | QualType cmpTy = C.getSValBuilder().getConditionType(); |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1158 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1159 | // It's a little unfortunate to be getting this again, |
| 1160 | // but it's not that expensive... |
| 1161 | const Expr *maxlenExpr = CE->getArg(1); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1162 | SVal maxlenVal = state->getSVal(maxlenExpr, LCtx); |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1163 | |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1164 | Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>(); |
| 1165 | Optional<NonLoc> maxlenValNL = maxlenVal.getAs<NonLoc>(); |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1166 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1167 | if (strLengthNL && maxlenValNL) { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1168 | ProgramStateRef stateStringTooLong, stateStringNotTooLong; |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1169 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1170 | // Check if the strLength is greater than the maxlen. |
| 1171 | llvm::tie(stateStringTooLong, stateStringNotTooLong) = |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1172 | state->assume(C.getSValBuilder().evalBinOpNN( |
| 1173 | state, BO_GT, *strLengthNL, *maxlenValNL, cmpTy) |
| 1174 | .castAs<DefinedOrUnknownSVal>()); |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1175 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1176 | if (stateStringTooLong && !stateStringNotTooLong) { |
| 1177 | // If the string is longer than maxlen, return maxlen. |
| 1178 | result = *maxlenValNL; |
| 1179 | } else if (stateStringNotTooLong && !stateStringTooLong) { |
| 1180 | // If the string is shorter than maxlen, return its length. |
| 1181 | result = *strLengthNL; |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1182 | } |
| 1183 | } |
| 1184 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1185 | if (result.isUnknown()) { |
| 1186 | // If we don't have enough information for a comparison, there's |
| 1187 | // no guarantee the full string length will actually be returned. |
| 1188 | // All we know is the return value is the min of the string length |
| 1189 | // and the limit. This is better than nothing. |
Ted Kremenek | 66c486f | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 1190 | result = C.getSValBuilder().conjureSymbolVal(0, CE, LCtx, C.blockCount()); |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1191 | NonLoc resultNL = result.castAs<NonLoc>(); |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1192 | |
| 1193 | if (strLengthNL) { |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1194 | state = state->assume(C.getSValBuilder().evalBinOpNN( |
| 1195 | state, BO_LE, resultNL, *strLengthNL, cmpTy) |
| 1196 | .castAs<DefinedOrUnknownSVal>(), true); |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | if (maxlenValNL) { |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1200 | state = state->assume(C.getSValBuilder().evalBinOpNN( |
| 1201 | state, BO_LE, resultNL, *maxlenValNL, cmpTy) |
| 1202 | .castAs<DefinedOrUnknownSVal>(), true); |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | } else { |
| 1207 | // This is a plain strlen(), not strnlen(). |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1208 | result = strLength.castAs<DefinedOrUnknownSVal>(); |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1209 | |
| 1210 | // If we don't know the length of the string, conjure a return |
| 1211 | // value, so it can be used in constraints, at least. |
| 1212 | if (result.isUnknown()) { |
Ted Kremenek | 66c486f | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 1213 | result = C.getSValBuilder().conjureSymbolVal(0, CE, LCtx, C.blockCount()); |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1214 | } |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1215 | } |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1216 | |
| 1217 | // Bind the return value. |
| 1218 | assert(!result.isUnknown() && "Should have conjured a value by now"); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1219 | state = state->BindExpr(CE, LCtx, result); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1220 | C.addTransition(state); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1223 | void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1224 | if (CE->getNumArgs() < 2) |
| 1225 | return; |
| 1226 | |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1227 | // char *strcpy(char *restrict dst, const char *restrict src); |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1228 | evalStrcpyCommon(C, CE, |
| 1229 | /* returnEnd = */ false, |
| 1230 | /* isBounded = */ false, |
| 1231 | /* isAppending = */ false); |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1234 | void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1235 | if (CE->getNumArgs() < 3) |
| 1236 | return; |
| 1237 | |
Jordy Rose | c152586 | 2011-06-04 00:05:23 +0000 | [diff] [blame] | 1238 | // char *strncpy(char *restrict dst, const char *restrict src, size_t n); |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1239 | evalStrcpyCommon(C, CE, |
| 1240 | /* returnEnd = */ false, |
| 1241 | /* isBounded = */ true, |
| 1242 | /* isAppending = */ false); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1245 | void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1246 | if (CE->getNumArgs() < 2) |
| 1247 | return; |
| 1248 | |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1249 | // char *stpcpy(char *restrict dst, const char *restrict src); |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1250 | evalStrcpyCommon(C, CE, |
| 1251 | /* returnEnd = */ true, |
| 1252 | /* isBounded = */ false, |
| 1253 | /* isAppending = */ false); |
| 1254 | } |
| 1255 | |
| 1256 | void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1257 | if (CE->getNumArgs() < 2) |
| 1258 | return; |
| 1259 | |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1260 | //char *strcat(char *restrict s1, const char *restrict s2); |
| 1261 | evalStrcpyCommon(C, CE, |
| 1262 | /* returnEnd = */ false, |
| 1263 | /* isBounded = */ false, |
| 1264 | /* isAppending = */ true); |
| 1265 | } |
| 1266 | |
| 1267 | void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1268 | if (CE->getNumArgs() < 3) |
| 1269 | return; |
| 1270 | |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1271 | //char *strncat(char *restrict s1, const char *restrict s2, size_t n); |
| 1272 | evalStrcpyCommon(C, CE, |
| 1273 | /* returnEnd = */ false, |
| 1274 | /* isBounded = */ true, |
| 1275 | /* isAppending = */ true); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1278 | void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1279 | bool returnEnd, bool isBounded, |
| 1280 | bool isAppending) const { |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 1281 | CurrentFunctionDescription = "string copy function"; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1282 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1283 | const LocationContext *LCtx = C.getLocationContext(); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1284 | |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1285 | // Check that the destination is non-null. |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1286 | const Expr *Dst = CE->getArg(0); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1287 | SVal DstVal = state->getSVal(Dst, LCtx); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1288 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1289 | state = checkNonNull(C, state, Dst, DstVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1290 | if (!state) |
| 1291 | return; |
| 1292 | |
| 1293 | // Check that the source is non-null. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1294 | const Expr *srcExpr = CE->getArg(1); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1295 | SVal srcVal = state->getSVal(srcExpr, LCtx); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1296 | state = checkNonNull(C, state, srcExpr, srcVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1297 | if (!state) |
| 1298 | return; |
| 1299 | |
| 1300 | // Get the string length of the source. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1301 | SVal strLength = getCStringLength(C, state, srcExpr, srcVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1302 | |
| 1303 | // If the source isn't a valid C string, give up. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1304 | if (strLength.isUndef()) |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1305 | return; |
| 1306 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1307 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 1308 | QualType cmpTy = svalBuilder.getConditionType(); |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1309 | QualType sizeTy = svalBuilder.getContext().getSizeType(); |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1310 | |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1311 | // These two values allow checking two kinds of errors: |
| 1312 | // - actual overflows caused by a source that doesn't fit in the destination |
| 1313 | // - potential overflows caused by a bound that could exceed the destination |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1314 | SVal amountCopied = UnknownVal(); |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1315 | SVal maxLastElementIndex = UnknownVal(); |
| 1316 | const char *boundWarning = NULL; |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1317 | |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1318 | // If the function is strncpy, strncat, etc... it is bounded. |
| 1319 | if (isBounded) { |
| 1320 | // Get the max number of characters to copy. |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1321 | const Expr *lenExpr = CE->getArg(2); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1322 | SVal lenVal = state->getSVal(lenExpr, LCtx); |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1323 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1324 | // Protect against misdeclared strncpy(). |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1325 | lenVal = svalBuilder.evalCast(lenVal, sizeTy, lenExpr->getType()); |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1326 | |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1327 | Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>(); |
| 1328 | Optional<NonLoc> lenValNL = lenVal.getAs<NonLoc>(); |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1329 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1330 | // If we know both values, we might be able to figure out how much |
| 1331 | // we're copying. |
| 1332 | if (strLengthNL && lenValNL) { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1333 | ProgramStateRef stateSourceTooLong, stateSourceNotTooLong; |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1334 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1335 | // Check if the max number to copy is less than the length of the src. |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1336 | // If the bound is equal to the source length, strncpy won't null- |
| 1337 | // terminate the result! |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1338 | llvm::tie(stateSourceTooLong, stateSourceNotTooLong) = state->assume( |
| 1339 | svalBuilder.evalBinOpNN(state, BO_GE, *strLengthNL, *lenValNL, cmpTy) |
| 1340 | .castAs<DefinedOrUnknownSVal>()); |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1341 | |
| 1342 | if (stateSourceTooLong && !stateSourceNotTooLong) { |
| 1343 | // Max number to copy is less than the length of the src, so the actual |
| 1344 | // strLength copied is the max number arg. |
| 1345 | state = stateSourceTooLong; |
| 1346 | amountCopied = lenVal; |
| 1347 | |
| 1348 | } else if (!stateSourceTooLong && stateSourceNotTooLong) { |
| 1349 | // The source buffer entirely fits in the bound. |
| 1350 | state = stateSourceNotTooLong; |
| 1351 | amountCopied = strLength; |
| 1352 | } |
| 1353 | } |
| 1354 | |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 1355 | // We still want to know if the bound is known to be too large. |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1356 | if (lenValNL) { |
| 1357 | if (isAppending) { |
| 1358 | // For strncat, the check is strlen(dst) + lenVal < sizeof(dst) |
| 1359 | |
| 1360 | // Get the string length of the destination. If the destination is |
| 1361 | // memory that can't have a string length, we shouldn't be copying |
| 1362 | // into it anyway. |
| 1363 | SVal dstStrLength = getCStringLength(C, state, Dst, DstVal); |
| 1364 | if (dstStrLength.isUndef()) |
| 1365 | return; |
| 1366 | |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1367 | if (Optional<NonLoc> dstStrLengthNL = dstStrLength.getAs<NonLoc>()) { |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1368 | maxLastElementIndex = svalBuilder.evalBinOpNN(state, BO_Add, |
| 1369 | *lenValNL, |
| 1370 | *dstStrLengthNL, |
| 1371 | sizeTy); |
| 1372 | boundWarning = "Size argument is greater than the free space in the " |
| 1373 | "destination buffer"; |
| 1374 | } |
| 1375 | |
| 1376 | } else { |
| 1377 | // For strncpy, this is just checking that lenVal <= sizeof(dst) |
| 1378 | // (Yes, strncpy and strncat differ in how they treat termination. |
| 1379 | // strncat ALWAYS terminates, but strncpy doesn't.) |
Jordy Rose | 6e4244e | 2012-05-14 17:58:35 +0000 | [diff] [blame] | 1380 | |
| 1381 | // We need a special case for when the copy size is zero, in which |
| 1382 | // case strncpy will do no work at all. Our bounds check uses n-1 |
| 1383 | // as the last element accessed, so n == 0 is problematic. |
| 1384 | ProgramStateRef StateZeroSize, StateNonZeroSize; |
| 1385 | llvm::tie(StateZeroSize, StateNonZeroSize) = |
| 1386 | assumeZero(C, state, *lenValNL, sizeTy); |
| 1387 | |
| 1388 | // If the size is known to be zero, we're done. |
| 1389 | if (StateZeroSize && !StateNonZeroSize) { |
| 1390 | StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, DstVal); |
| 1391 | C.addTransition(StateZeroSize); |
| 1392 | return; |
| 1393 | } |
| 1394 | |
| 1395 | // Otherwise, go ahead and figure out the last element we'll touch. |
| 1396 | // We don't record the non-zero assumption here because we can't |
| 1397 | // be sure. We won't warn on a possible zero. |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1398 | NonLoc one = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>(); |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1399 | maxLastElementIndex = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL, |
| 1400 | one, sizeTy); |
| 1401 | boundWarning = "Size argument is greater than the length of the " |
| 1402 | "destination buffer"; |
| 1403 | } |
| 1404 | } |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 1405 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1406 | // If we couldn't pin down the copy length, at least bound it. |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1407 | // FIXME: We should actually run this code path for append as well, but |
| 1408 | // right now it creates problems with constraints (since we can end up |
| 1409 | // trying to pass constraints from symbol to symbol). |
| 1410 | if (amountCopied.isUnknown() && !isAppending) { |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1411 | // Try to get a "hypothetical" string length symbol, which we can later |
| 1412 | // set as a real value if that turns out to be the case. |
| 1413 | amountCopied = getCStringLength(C, state, lenExpr, srcVal, true); |
| 1414 | assert(!amountCopied.isUndef()); |
| 1415 | |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1416 | if (Optional<NonLoc> amountCopiedNL = amountCopied.getAs<NonLoc>()) { |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1417 | if (lenValNL) { |
| 1418 | // amountCopied <= lenVal |
| 1419 | SVal copiedLessThanBound = svalBuilder.evalBinOpNN(state, BO_LE, |
| 1420 | *amountCopiedNL, |
| 1421 | *lenValNL, |
| 1422 | cmpTy); |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1423 | state = state->assume( |
| 1424 | copiedLessThanBound.castAs<DefinedOrUnknownSVal>(), true); |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1425 | if (!state) |
| 1426 | return; |
| 1427 | } |
| 1428 | |
| 1429 | if (strLengthNL) { |
| 1430 | // amountCopied <= strlen(source) |
| 1431 | SVal copiedLessThanSrc = svalBuilder.evalBinOpNN(state, BO_LE, |
| 1432 | *amountCopiedNL, |
| 1433 | *strLengthNL, |
| 1434 | cmpTy); |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1435 | state = state->assume( |
| 1436 | copiedLessThanSrc.castAs<DefinedOrUnknownSVal>(), true); |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1437 | if (!state) |
| 1438 | return; |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | } else { |
| 1444 | // The function isn't bounded. The amount copied should match the length |
| 1445 | // of the source buffer. |
| 1446 | amountCopied = strLength; |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1449 | assert(state); |
| 1450 | |
| 1451 | // This represents the number of characters copied into the destination |
| 1452 | // buffer. (It may not actually be the strlen if the destination buffer |
| 1453 | // is not terminated.) |
| 1454 | SVal finalStrLength = UnknownVal(); |
| 1455 | |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1456 | // If this is an appending function (strcat, strncat...) then set the |
| 1457 | // string length to strlen(src) + strlen(dst) since the buffer will |
| 1458 | // ultimately contain both. |
| 1459 | if (isAppending) { |
Jordy Rose | 1e02241 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 1460 | // Get the string length of the destination. If the destination is memory |
| 1461 | // that can't have a string length, we shouldn't be copying into it anyway. |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1462 | SVal dstStrLength = getCStringLength(C, state, Dst, DstVal); |
| 1463 | if (dstStrLength.isUndef()) |
| 1464 | return; |
| 1465 | |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1466 | Optional<NonLoc> srcStrLengthNL = amountCopied.getAs<NonLoc>(); |
| 1467 | Optional<NonLoc> dstStrLengthNL = dstStrLength.getAs<NonLoc>(); |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1468 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1469 | // If we know both string lengths, we might know the final string length. |
| 1470 | if (srcStrLengthNL && dstStrLengthNL) { |
| 1471 | // Make sure the two lengths together don't overflow a size_t. |
| 1472 | state = checkAdditionOverflow(C, state, *srcStrLengthNL, *dstStrLengthNL); |
| 1473 | if (!state) |
| 1474 | return; |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1475 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1476 | finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *srcStrLengthNL, |
| 1477 | *dstStrLengthNL, sizeTy); |
| 1478 | } |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1479 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1480 | // If we couldn't get a single value for the final string length, |
| 1481 | // we can at least bound it by the individual lengths. |
| 1482 | if (finalStrLength.isUnknown()) { |
| 1483 | // Try to get a "hypothetical" string length symbol, which we can later |
| 1484 | // set as a real value if that turns out to be the case. |
| 1485 | finalStrLength = getCStringLength(C, state, CE, DstVal, true); |
| 1486 | assert(!finalStrLength.isUndef()); |
| 1487 | |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1488 | if (Optional<NonLoc> finalStrLengthNL = finalStrLength.getAs<NonLoc>()) { |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1489 | if (srcStrLengthNL) { |
| 1490 | // finalStrLength >= srcStrLength |
| 1491 | SVal sourceInResult = svalBuilder.evalBinOpNN(state, BO_GE, |
| 1492 | *finalStrLengthNL, |
| 1493 | *srcStrLengthNL, |
| 1494 | cmpTy); |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1495 | state = state->assume(sourceInResult.castAs<DefinedOrUnknownSVal>(), |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1496 | true); |
| 1497 | if (!state) |
| 1498 | return; |
| 1499 | } |
| 1500 | |
| 1501 | if (dstStrLengthNL) { |
| 1502 | // finalStrLength >= dstStrLength |
| 1503 | SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE, |
| 1504 | *finalStrLengthNL, |
| 1505 | *dstStrLengthNL, |
| 1506 | cmpTy); |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1507 | state = |
| 1508 | state->assume(destInResult.castAs<DefinedOrUnknownSVal>(), true); |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1509 | if (!state) |
| 1510 | return; |
| 1511 | } |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | } else { |
| 1516 | // Otherwise, this is a copy-over function (strcpy, strncpy, ...), and |
| 1517 | // the final string length will match the input string length. |
| 1518 | finalStrLength = amountCopied; |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1521 | // The final result of the function will either be a pointer past the last |
| 1522 | // copied element, or a pointer to the start of the destination buffer. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1523 | SVal Result = (returnEnd ? UnknownVal() : DstVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1524 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1525 | assert(state); |
| 1526 | |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1527 | // If the destination is a MemRegion, try to check for a buffer overflow and |
| 1528 | // record the new string length. |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1529 | if (Optional<loc::MemRegionVal> dstRegVal = |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1530 | DstVal.getAs<loc::MemRegionVal>()) { |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1531 | QualType ptrTy = Dst->getType(); |
| 1532 | |
| 1533 | // If we have an exact value on a bounded copy, use that to check for |
| 1534 | // overflows, rather than our estimate about how much is actually copied. |
| 1535 | if (boundWarning) { |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1536 | if (Optional<NonLoc> maxLastNL = maxLastElementIndex.getAs<NonLoc>()) { |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1537 | SVal maxLastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, |
| 1538 | *maxLastNL, ptrTy); |
| 1539 | state = CheckLocation(C, state, CE->getArg(2), maxLastElement, |
| 1540 | boundWarning); |
| 1541 | if (!state) |
| 1542 | return; |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | // Then, if the final length is known... |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1547 | if (Optional<NonLoc> knownStrLength = finalStrLength.getAs<NonLoc>()) { |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1548 | SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1549 | *knownStrLength, ptrTy); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1550 | |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1551 | // ...and we haven't checked the bound, we'll check the actual copy. |
| 1552 | if (!boundWarning) { |
| 1553 | const char * const warningMsg = |
| 1554 | "String copy function overflows destination buffer"; |
| 1555 | state = CheckLocation(C, state, Dst, lastElement, warningMsg); |
| 1556 | if (!state) |
| 1557 | return; |
| 1558 | } |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1559 | |
| 1560 | // If this is a stpcpy-style copy, the last element is the return value. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1561 | if (returnEnd) |
| 1562 | Result = lastElement; |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | // Invalidate the destination. This must happen before we set the C string |
| 1566 | // length because invalidation will clear the length. |
| 1567 | // FIXME: Even if we can't perfectly model the copy, we should see if we |
| 1568 | // can use LazyCompoundVals to copy the source values into the destination. |
| 1569 | // This would probably remove any existing bindings past the end of the |
| 1570 | // string, but that's still an improvement over blank invalidation. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1571 | state = InvalidateBuffer(C, state, Dst, *dstRegVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1572 | |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 1573 | // Set the C string length of the destination, if we know it. |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1574 | if (isBounded && !isAppending) { |
| 1575 | // strncpy is annoying in that it doesn't guarantee to null-terminate |
| 1576 | // the result string. If the original string didn't fit entirely inside |
| 1577 | // the bound (including the null-terminator), we don't know how long the |
| 1578 | // result is. |
| 1579 | if (amountCopied != strLength) |
| 1580 | finalStrLength = UnknownVal(); |
| 1581 | } |
| 1582 | state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1585 | assert(state); |
| 1586 | |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1587 | // If this is a stpcpy-style copy, but we were unable to check for a buffer |
| 1588 | // overflow, we still need a result. Conjure a return value. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1589 | if (returnEnd && Result.isUnknown()) { |
Ted Kremenek | 66c486f | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 1590 | Result = svalBuilder.conjureSymbolVal(0, CE, LCtx, C.blockCount()); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | // Set the return value. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1594 | state = state->BindExpr(CE, LCtx, Result); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1595 | C.addTransition(state); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1598 | void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1599 | if (CE->getNumArgs() < 2) |
| 1600 | return; |
| 1601 | |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1602 | //int strcmp(const char *s1, const char *s2); |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1603 | evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ false); |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1604 | } |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1605 | |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1606 | void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1607 | if (CE->getNumArgs() < 3) |
| 1608 | return; |
| 1609 | |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1610 | //int strncmp(const char *s1, const char *s2, size_t n); |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1611 | evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ false); |
| 1612 | } |
| 1613 | |
| 1614 | void CStringChecker::evalStrcasecmp(CheckerContext &C, |
| 1615 | const CallExpr *CE) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1616 | if (CE->getNumArgs() < 2) |
| 1617 | return; |
| 1618 | |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1619 | //int strcasecmp(const char *s1, const char *s2); |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1620 | evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ true); |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
Lenny Maiorani | 454fd2d | 2011-05-02 19:05:49 +0000 | [diff] [blame] | 1623 | void CStringChecker::evalStrncasecmp(CheckerContext &C, |
| 1624 | const CallExpr *CE) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1625 | if (CE->getNumArgs() < 3) |
| 1626 | return; |
| 1627 | |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1628 | //int strncasecmp(const char *s1, const char *s2, size_t n); |
Lenny Maiorani | 454fd2d | 2011-05-02 19:05:49 +0000 | [diff] [blame] | 1629 | evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ true); |
| 1630 | } |
| 1631 | |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1632 | void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE, |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1633 | bool isBounded, bool ignoreCase) const { |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 1634 | CurrentFunctionDescription = "string comparison function"; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1635 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1636 | const LocationContext *LCtx = C.getLocationContext(); |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1637 | |
| 1638 | // Check that the first string is non-null |
| 1639 | const Expr *s1 = CE->getArg(0); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1640 | SVal s1Val = state->getSVal(s1, LCtx); |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1641 | state = checkNonNull(C, state, s1, s1Val); |
| 1642 | if (!state) |
| 1643 | return; |
| 1644 | |
| 1645 | // Check that the second string is non-null. |
| 1646 | const Expr *s2 = CE->getArg(1); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1647 | SVal s2Val = state->getSVal(s2, LCtx); |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1648 | state = checkNonNull(C, state, s2, s2Val); |
| 1649 | if (!state) |
| 1650 | return; |
| 1651 | |
| 1652 | // Get the string length of the first string or give up. |
| 1653 | SVal s1Length = getCStringLength(C, state, s1, s1Val); |
| 1654 | if (s1Length.isUndef()) |
| 1655 | return; |
| 1656 | |
| 1657 | // Get the string length of the second string or give up. |
| 1658 | SVal s2Length = getCStringLength(C, state, s2, s2Val); |
| 1659 | if (s2Length.isUndef()) |
| 1660 | return; |
| 1661 | |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1662 | // If we know the two buffers are the same, we know the result is 0. |
| 1663 | // First, get the two buffers' addresses. Another checker will have already |
| 1664 | // made sure they're not undefined. |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1665 | DefinedOrUnknownSVal LV = s1Val.castAs<DefinedOrUnknownSVal>(); |
| 1666 | DefinedOrUnknownSVal RV = s2Val.castAs<DefinedOrUnknownSVal>(); |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1667 | |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1668 | // See if they are the same. |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1669 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1670 | DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1671 | ProgramStateRef StSameBuf, StNotSameBuf; |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1672 | llvm::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf); |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1673 | |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1674 | // If the two arguments might be the same buffer, we know the result is 0, |
| 1675 | // and we only need to check one size. |
| 1676 | if (StSameBuf) { |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1677 | StSameBuf = StSameBuf->BindExpr(CE, LCtx, |
| 1678 | svalBuilder.makeZeroVal(CE->getType())); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1679 | C.addTransition(StSameBuf); |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1680 | |
| 1681 | // If the two arguments are GUARANTEED to be the same, we're done! |
| 1682 | if (!StNotSameBuf) |
| 1683 | return; |
| 1684 | } |
| 1685 | |
| 1686 | assert(StNotSameBuf); |
| 1687 | state = StNotSameBuf; |
| 1688 | |
| 1689 | // At this point we can go about comparing the two buffers. |
| 1690 | // For now, we only do this if they're both known string literals. |
| 1691 | |
| 1692 | // Attempt to extract string literals from both expressions. |
| 1693 | const StringLiteral *s1StrLiteral = getCStringLiteral(C, state, s1, s1Val); |
| 1694 | const StringLiteral *s2StrLiteral = getCStringLiteral(C, state, s2, s2Val); |
| 1695 | bool canComputeResult = false; |
| 1696 | |
| 1697 | if (s1StrLiteral && s2StrLiteral) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1698 | StringRef s1StrRef = s1StrLiteral->getString(); |
| 1699 | StringRef s2StrRef = s2StrLiteral->getString(); |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1700 | |
| 1701 | if (isBounded) { |
| 1702 | // Get the max number of characters to compare. |
| 1703 | const Expr *lenExpr = CE->getArg(2); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1704 | SVal lenVal = state->getSVal(lenExpr, LCtx); |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1705 | |
| 1706 | // If the length is known, we can get the right substrings. |
| 1707 | if (const llvm::APSInt *len = svalBuilder.getKnownValue(state, lenVal)) { |
| 1708 | // Create substrings of each to compare the prefix. |
| 1709 | s1StrRef = s1StrRef.substr(0, (size_t)len->getZExtValue()); |
| 1710 | s2StrRef = s2StrRef.substr(0, (size_t)len->getZExtValue()); |
| 1711 | canComputeResult = true; |
| 1712 | } |
| 1713 | } else { |
| 1714 | // This is a normal, unbounded strcmp. |
| 1715 | canComputeResult = true; |
| 1716 | } |
| 1717 | |
| 1718 | if (canComputeResult) { |
| 1719 | // Real strcmp stops at null characters. |
| 1720 | size_t s1Term = s1StrRef.find('\0'); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1721 | if (s1Term != StringRef::npos) |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1722 | s1StrRef = s1StrRef.substr(0, s1Term); |
| 1723 | |
| 1724 | size_t s2Term = s2StrRef.find('\0'); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1725 | if (s2Term != StringRef::npos) |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1726 | s2StrRef = s2StrRef.substr(0, s2Term); |
| 1727 | |
| 1728 | // Use StringRef's comparison methods to compute the actual result. |
| 1729 | int result; |
| 1730 | |
| 1731 | if (ignoreCase) { |
| 1732 | // Compare string 1 to string 2 the same way strcasecmp() does. |
| 1733 | result = s1StrRef.compare_lower(s2StrRef); |
| 1734 | } else { |
| 1735 | // Compare string 1 to string 2 the same way strcmp() does. |
| 1736 | result = s1StrRef.compare(s2StrRef); |
| 1737 | } |
| 1738 | |
| 1739 | // Build the SVal of the comparison and bind the return value. |
| 1740 | SVal resultVal = svalBuilder.makeIntVal(result, CE->getType()); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1741 | state = state->BindExpr(CE, LCtx, resultVal); |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | if (!canComputeResult) { |
| 1746 | // Conjure a symbolic value. It's the best we can do. |
Ted Kremenek | 66c486f | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 1747 | SVal resultVal = svalBuilder.conjureSymbolVal(0, CE, LCtx, C.blockCount()); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1748 | state = state->BindExpr(CE, LCtx, resultVal); |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | // Record this as a possible path. |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1752 | C.addTransition(state); |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1755 | //===----------------------------------------------------------------------===// |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1756 | // The driver method, and other Checker callbacks. |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1757 | //===----------------------------------------------------------------------===// |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1758 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1759 | bool CStringChecker::evalCall(const CallExpr *CE, CheckerContext &C) const { |
Anna Zaks | 998e275 | 2012-02-17 22:35:26 +0000 | [diff] [blame] | 1760 | const FunctionDecl *FDecl = C.getCalleeDecl(CE); |
| 1761 | |
| 1762 | if (!FDecl) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1763 | return false; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1764 | |
Anna Zaks | 998e275 | 2012-02-17 22:35:26 +0000 | [diff] [blame] | 1765 | FnCheck evalFunction = 0; |
| 1766 | if (C.isCLibraryFunction(FDecl, "memcpy")) |
| 1767 | evalFunction = &CStringChecker::evalMemcpy; |
| 1768 | else if (C.isCLibraryFunction(FDecl, "mempcpy")) |
| 1769 | evalFunction = &CStringChecker::evalMempcpy; |
| 1770 | else if (C.isCLibraryFunction(FDecl, "memcmp")) |
| 1771 | evalFunction = &CStringChecker::evalMemcmp; |
| 1772 | else if (C.isCLibraryFunction(FDecl, "memmove")) |
| 1773 | evalFunction = &CStringChecker::evalMemmove; |
| 1774 | else if (C.isCLibraryFunction(FDecl, "strcpy")) |
| 1775 | evalFunction = &CStringChecker::evalStrcpy; |
| 1776 | else if (C.isCLibraryFunction(FDecl, "strncpy")) |
| 1777 | evalFunction = &CStringChecker::evalStrncpy; |
| 1778 | else if (C.isCLibraryFunction(FDecl, "stpcpy")) |
| 1779 | evalFunction = &CStringChecker::evalStpcpy; |
| 1780 | else if (C.isCLibraryFunction(FDecl, "strcat")) |
| 1781 | evalFunction = &CStringChecker::evalStrcat; |
| 1782 | else if (C.isCLibraryFunction(FDecl, "strncat")) |
| 1783 | evalFunction = &CStringChecker::evalStrncat; |
| 1784 | else if (C.isCLibraryFunction(FDecl, "strlen")) |
| 1785 | evalFunction = &CStringChecker::evalstrLength; |
| 1786 | else if (C.isCLibraryFunction(FDecl, "strnlen")) |
| 1787 | evalFunction = &CStringChecker::evalstrnLength; |
| 1788 | else if (C.isCLibraryFunction(FDecl, "strcmp")) |
| 1789 | evalFunction = &CStringChecker::evalStrcmp; |
| 1790 | else if (C.isCLibraryFunction(FDecl, "strncmp")) |
| 1791 | evalFunction = &CStringChecker::evalStrncmp; |
| 1792 | else if (C.isCLibraryFunction(FDecl, "strcasecmp")) |
| 1793 | evalFunction = &CStringChecker::evalStrcasecmp; |
| 1794 | else if (C.isCLibraryFunction(FDecl, "strncasecmp")) |
| 1795 | evalFunction = &CStringChecker::evalStrncasecmp; |
| 1796 | else if (C.isCLibraryFunction(FDecl, "bcopy")) |
| 1797 | evalFunction = &CStringChecker::evalBcopy; |
| 1798 | else if (C.isCLibraryFunction(FDecl, "bcmp")) |
| 1799 | evalFunction = &CStringChecker::evalMemcmp; |
| 1800 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1801 | // If the callee isn't a string function, let another checker handle it. |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1802 | if (!evalFunction) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1803 | return false; |
| 1804 | |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 1805 | // Make sure each function sets its own description. |
| 1806 | // (But don't bother in a release build.) |
| 1807 | assert(!(CurrentFunctionDescription = NULL)); |
| 1808 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1809 | // Check and evaluate the call. |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1810 | (this->*evalFunction)(C, CE); |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 1811 | |
| 1812 | // If the evaluate call resulted in no change, chain to the next eval call |
| 1813 | // handler. |
| 1814 | // Note, the custom CString evaluation calls assume that basic safety |
| 1815 | // properties are held. However, if the user chooses to turn off some of these |
| 1816 | // checks, we ignore the issues and leave the call evaluation to a generic |
| 1817 | // handler. |
| 1818 | if (!C.isDifferent()) |
| 1819 | return false; |
| 1820 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1821 | return true; |
| 1822 | } |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1823 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1824 | void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1825 | // Record string length for char a[] = "abc"; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1826 | ProgramStateRef state = C.getState(); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1827 | |
| 1828 | for (DeclStmt::const_decl_iterator I = DS->decl_begin(), E = DS->decl_end(); |
| 1829 | I != E; ++I) { |
| 1830 | const VarDecl *D = dyn_cast<VarDecl>(*I); |
| 1831 | if (!D) |
| 1832 | continue; |
| 1833 | |
| 1834 | // FIXME: Handle array fields of structs. |
| 1835 | if (!D->getType()->isArrayType()) |
| 1836 | continue; |
| 1837 | |
| 1838 | const Expr *Init = D->getInit(); |
| 1839 | if (!Init) |
| 1840 | continue; |
| 1841 | if (!isa<StringLiteral>(Init)) |
| 1842 | continue; |
| 1843 | |
Anna Zaks | 39ac187 | 2011-10-26 21:06:44 +0000 | [diff] [blame] | 1844 | Loc VarLoc = state->getLValue(D, C.getLocationContext()); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1845 | const MemRegion *MR = VarLoc.getAsRegion(); |
| 1846 | if (!MR) |
| 1847 | continue; |
| 1848 | |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1849 | SVal StrVal = state->getSVal(Init, C.getLocationContext()); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1850 | assert(StrVal.isValid() && "Initializer string is unknown or undefined"); |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1851 | DefinedOrUnknownSVal strLength = |
| 1852 | getCStringLength(C, state, Init, StrVal).castAs<DefinedOrUnknownSVal>(); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1853 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1854 | state = state->set<CStringLength>(MR, strLength); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1857 | C.addTransition(state); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1860 | bool CStringChecker::wantsRegionChangeUpdate(ProgramStateRef state) const { |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 1861 | CStringLengthTy Entries = state->get<CStringLength>(); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1862 | return !Entries.isEmpty(); |
| 1863 | } |
| 1864 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1865 | ProgramStateRef |
| 1866 | CStringChecker::checkRegionChanges(ProgramStateRef state, |
Anna Zaks | bf53dfa | 2012-12-20 00:38:25 +0000 | [diff] [blame] | 1867 | const InvalidatedSymbols *, |
Jordy Rose | 537716a | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 1868 | ArrayRef<const MemRegion *> ExplicitRegions, |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1869 | ArrayRef<const MemRegion *> Regions, |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1870 | const CallEvent *Call) const { |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 1871 | CStringLengthTy Entries = state->get<CStringLength>(); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1872 | if (Entries.isEmpty()) |
| 1873 | return state; |
| 1874 | |
| 1875 | llvm::SmallPtrSet<const MemRegion *, 8> Invalidated; |
| 1876 | llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions; |
| 1877 | |
| 1878 | // First build sets for the changed regions and their super-regions. |
Jordy Rose | 537716a | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 1879 | for (ArrayRef<const MemRegion *>::iterator |
| 1880 | I = Regions.begin(), E = Regions.end(); I != E; ++I) { |
| 1881 | const MemRegion *MR = *I; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1882 | Invalidated.insert(MR); |
| 1883 | |
| 1884 | SuperRegions.insert(MR); |
| 1885 | while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) { |
| 1886 | MR = SR->getSuperRegion(); |
| 1887 | SuperRegions.insert(MR); |
| 1888 | } |
| 1889 | } |
| 1890 | |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 1891 | CStringLengthTy::Factory &F = state->get_context<CStringLength>(); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1892 | |
| 1893 | // Then loop over the entries in the current state. |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 1894 | for (CStringLengthTy::iterator I = Entries.begin(), |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1895 | E = Entries.end(); I != E; ++I) { |
| 1896 | const MemRegion *MR = I.getKey(); |
| 1897 | |
| 1898 | // Is this entry for a super-region of a changed region? |
| 1899 | if (SuperRegions.count(MR)) { |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 1900 | Entries = F.remove(Entries, MR); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1901 | continue; |
| 1902 | } |
| 1903 | |
| 1904 | // Is this entry for a sub-region of a changed region? |
| 1905 | const MemRegion *Super = MR; |
| 1906 | while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) { |
| 1907 | Super = SR->getSuperRegion(); |
| 1908 | if (Invalidated.count(Super)) { |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 1909 | Entries = F.remove(Entries, MR); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1910 | break; |
| 1911 | } |
| 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | return state->set<CStringLength>(Entries); |
| 1916 | } |
| 1917 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1918 | void CStringChecker::checkLiveSymbols(ProgramStateRef state, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1919 | SymbolReaper &SR) const { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1920 | // Mark all symbols in our string length map as valid. |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 1921 | CStringLengthTy Entries = state->get<CStringLength>(); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1922 | |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 1923 | for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end(); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1924 | I != E; ++I) { |
| 1925 | SVal Len = I.getData(); |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1926 | |
Anna Zaks | 1d1d515 | 2011-12-06 23:12:33 +0000 | [diff] [blame] | 1927 | for (SymExpr::symbol_iterator si = Len.symbol_begin(), |
| 1928 | se = Len.symbol_end(); si != se; ++si) |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1929 | SR.markInUse(*si); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1930 | } |
| 1931 | } |
| 1932 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1933 | void CStringChecker::checkDeadSymbols(SymbolReaper &SR, |
| 1934 | CheckerContext &C) const { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1935 | if (!SR.hasDeadSymbols()) |
| 1936 | return; |
| 1937 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1938 | ProgramStateRef state = C.getState(); |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 1939 | CStringLengthTy Entries = state->get<CStringLength>(); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1940 | if (Entries.isEmpty()) |
| 1941 | return; |
| 1942 | |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 1943 | CStringLengthTy::Factory &F = state->get_context<CStringLength>(); |
| 1944 | for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end(); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1945 | I != E; ++I) { |
| 1946 | SVal Len = I.getData(); |
| 1947 | if (SymbolRef Sym = Len.getAsSymbol()) { |
| 1948 | if (SR.isDead(Sym)) |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 1949 | Entries = F.remove(Entries, I.getKey()); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | state = state->set<CStringLength>(Entries); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1954 | C.addTransition(state); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1955 | } |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1956 | |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 1957 | #define REGISTER_CHECKER(name) \ |
| 1958 | void ento::register##name(CheckerManager &mgr) {\ |
| 1959 | static CStringChecker *TheChecker = 0; \ |
| 1960 | if (TheChecker == 0) \ |
| 1961 | TheChecker = mgr.registerChecker<CStringChecker>(); \ |
| 1962 | TheChecker->Filter.Check##name = true; \ |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1963 | } |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 1964 | |
| 1965 | REGISTER_CHECKER(CStringNullArg) |
| 1966 | REGISTER_CHECKER(CStringOutOfBounds) |
| 1967 | REGISTER_CHECKER(CStringBufferOverlap) |
| 1968 | REGISTER_CHECKER(CStringNotNullTerm) |
Anna Zaks | f0dfc9c | 2012-02-17 22:35:31 +0000 | [diff] [blame] | 1969 | |
| 1970 | void ento::registerCStringCheckerBasic(CheckerManager &Mgr) { |
| 1971 | registerCStringNullArg(Mgr); |
| 1972 | } |