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