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