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