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