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