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