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