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 | > { |
| 33 | mutable llvm::OwningPtr<BugType> BT_Null, BT_Bounds, BT_BoundsWrite, |
| 34 | BT_Overlap, BT_NotCString; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 35 | public: |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 36 | static void *getTag() { static int tag; return &tag; } |
| 37 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 38 | bool evalCall(const CallExpr *CE, CheckerContext &C) const; |
| 39 | void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const; |
| 40 | void checkLiveSymbols(const GRState *state, SymbolReaper &SR) const; |
| 41 | void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const; |
| 42 | bool wantsRegionChangeUpdate(const GRState *state) const; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 43 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 44 | const GRState *checkRegionChanges(const GRState *state, |
| 45 | const MemRegion * const *Begin, |
| 46 | const MemRegion * const *End) const; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 47 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 48 | typedef void (CStringChecker::*FnCheck)(CheckerContext &, |
| 49 | const CallExpr *) const; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 50 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 51 | void evalMemcpy(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 52 | void evalMempcpy(CheckerContext &C, const CallExpr *CE) const; |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 53 | void evalMemmove(CheckerContext &C, const CallExpr *CE) const; |
| 54 | void evalBcopy(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 55 | void evalCopyCommon(CheckerContext &C, const CallExpr *CE, |
| 56 | const GRState *state, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 57 | const Expr *Size, const Expr *Source, const Expr *Dest, |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 58 | bool Restricted = false, |
| 59 | bool IsMempcpy = false) const; |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 60 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 61 | void evalMemcmp(CheckerContext &C, const CallExpr *CE) const; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 62 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 63 | void evalstrLength(CheckerContext &C, const CallExpr *CE) const; |
| 64 | void evalstrnLength(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 65 | void evalstrLengthCommon(CheckerContext &C, const CallExpr *CE, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 66 | bool IsStrnlen = false) const; |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 67 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 68 | void evalStrcpy(CheckerContext &C, const CallExpr *CE) const; |
| 69 | void evalStrncpy(CheckerContext &C, const CallExpr *CE) const; |
| 70 | void evalStpcpy(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 71 | void evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, bool returnEnd, |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 72 | bool isBounded, bool isAppending) const; |
| 73 | |
| 74 | void evalStrcat(CheckerContext &C, const CallExpr *CE) const; |
| 75 | void evalStrncat(CheckerContext &C, const CallExpr *CE) const; |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 76 | |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 77 | void evalStrcmp(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 78 | void evalStrncmp(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 79 | void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 80 | void evalStrcmpCommon(CheckerContext &C, const CallExpr *CE, |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 81 | bool isBounded = false, bool ignoreCase = false) const; |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 82 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 83 | // Utility methods |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 84 | std::pair<const GRState*, const GRState*> |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 85 | static assumeZero(CheckerContext &C, |
| 86 | const GRState *state, SVal V, QualType Ty); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 87 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 88 | static const GRState *setCStringLength(const GRState *state, |
| 89 | const MemRegion *MR, SVal strLength); |
| 90 | static SVal getCStringLengthForRegion(CheckerContext &C, |
| 91 | const GRState *&state, |
| 92 | const Expr *Ex, const MemRegion *MR); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 93 | SVal getCStringLength(CheckerContext &C, const GRState *&state, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 94 | const Expr *Ex, SVal Buf) const; |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 95 | |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 96 | const StringLiteral *getCStringLiteral(CheckerContext &C, |
| 97 | const GRState *&state, |
| 98 | const Expr *expr, |
| 99 | SVal val) const; |
| 100 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 101 | static const GRState *InvalidateBuffer(CheckerContext &C, |
| 102 | const GRState *state, |
| 103 | const Expr *Ex, SVal V); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 104 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 105 | static bool SummarizeRegion(llvm::raw_ostream& os, ASTContext& Ctx, |
| 106 | const MemRegion *MR); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 107 | |
| 108 | // Re-usable checks |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 109 | const GRState *checkNonNull(CheckerContext &C, const GRState *state, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 110 | const Expr *S, SVal l) const; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 111 | const GRState *CheckLocation(CheckerContext &C, const GRState *state, |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 112 | const Expr *S, SVal l, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 113 | bool IsDestination = false) const; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 114 | const GRState *CheckBufferAccess(CheckerContext &C, const GRState *state, |
| 115 | const Expr *Size, |
| 116 | const Expr *FirstBuf, |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 117 | const Expr *SecondBuf = NULL, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 118 | bool FirstIsDestination = false) const; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 119 | const GRState *CheckOverlap(CheckerContext &C, const GRState *state, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 120 | const Expr *Size, const Expr *First, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 121 | const Expr *Second) const; |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 122 | void emitOverlapBug(CheckerContext &C, const GRState *state, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 123 | const Stmt *First, const Stmt *Second) const; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 124 | }; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 125 | |
| 126 | class CStringLength { |
| 127 | public: |
| 128 | typedef llvm::ImmutableMap<const MemRegion *, SVal> EntryMap; |
| 129 | }; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 130 | } //end anonymous namespace |
| 131 | |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 132 | namespace clang { |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 133 | namespace ento { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 134 | template <> |
| 135 | struct GRStateTrait<CStringLength> |
| 136 | : public GRStatePartialTrait<CStringLength::EntryMap> { |
| 137 | static void *GDMIndex() { return CStringChecker::getTag(); } |
| 138 | }; |
| 139 | } |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 140 | } |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 141 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 142 | //===----------------------------------------------------------------------===// |
| 143 | // Individual checks and utility methods. |
| 144 | //===----------------------------------------------------------------------===// |
| 145 | |
| 146 | std::pair<const GRState*, const GRState*> |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 147 | CStringChecker::assumeZero(CheckerContext &C, const GRState *state, SVal V, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 148 | QualType Ty) { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 149 | DefinedSVal *val = dyn_cast<DefinedSVal>(&V); |
| 150 | if (!val) |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 151 | return std::pair<const GRState*, const GRState *>(state, state); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 153 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 154 | DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty); |
| 155 | return state->assume(svalBuilder.evalEQ(state, *val, zero)); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 156 | } |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 157 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 158 | const GRState *CStringChecker::checkNonNull(CheckerContext &C, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 159 | const GRState *state, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 160 | const Expr *S, SVal l) const { |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 161 | // If a previous check has failed, propagate the failure. |
| 162 | if (!state) |
| 163 | return NULL; |
| 164 | |
| 165 | const GRState *stateNull, *stateNonNull; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 166 | llvm::tie(stateNull, stateNonNull) = assumeZero(C, state, l, S->getType()); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 167 | |
| 168 | if (stateNull && !stateNonNull) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 169 | ExplodedNode *N = C.generateSink(stateNull); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 170 | if (!N) |
| 171 | return NULL; |
| 172 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 173 | if (!BT_Null) |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 174 | BT_Null.reset(new BuiltinBug("API", |
| 175 | "Null pointer argument in call to byte string function")); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 176 | |
| 177 | // Generate a report for this bug. |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 178 | BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Null.get()); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 179 | EnhancedBugReport *report = new EnhancedBugReport(*BT, |
| 180 | BT->getDescription(), N); |
| 181 | |
| 182 | report->addRange(S->getSourceRange()); |
| 183 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, S); |
| 184 | C.EmitReport(report); |
| 185 | return NULL; |
| 186 | } |
| 187 | |
| 188 | // From here on, assume that the value is non-null. |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 189 | assert(stateNonNull); |
| 190 | return stateNonNull; |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 193 | // FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor? |
| 194 | const GRState *CStringChecker::CheckLocation(CheckerContext &C, |
| 195 | const GRState *state, |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 196 | const Expr *S, SVal l, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 197 | bool IsDestination) const { |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 198 | // If a previous check has failed, propagate the failure. |
| 199 | if (!state) |
| 200 | return NULL; |
| 201 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 202 | // Check for out of bound array element access. |
| 203 | const MemRegion *R = l.getAsRegion(); |
| 204 | if (!R) |
| 205 | return state; |
| 206 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 207 | const ElementRegion *ER = dyn_cast<ElementRegion>(R); |
| 208 | if (!ER) |
| 209 | return state; |
| 210 | |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 211 | assert(ER->getValueType() == C.getASTContext().CharTy && |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 212 | "CheckLocation should only be called with char* ElementRegions"); |
| 213 | |
| 214 | // Get the size of the array. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 215 | const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion()); |
| 216 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 217 | SVal Extent = svalBuilder.convertToArrayIndex(superReg->getExtent(svalBuilder)); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 218 | DefinedOrUnknownSVal Size = cast<DefinedOrUnknownSVal>(Extent); |
| 219 | |
| 220 | // Get the index of the accessed element. |
Gabor Greif | 89b0658 | 2010-09-09 10:51:37 +0000 | [diff] [blame] | 221 | DefinedOrUnknownSVal Idx = cast<DefinedOrUnknownSVal>(ER->getIndex()); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 222 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 223 | const GRState *StInBound = state->assumeInBound(Idx, Size, true); |
| 224 | const GRState *StOutBound = state->assumeInBound(Idx, Size, false); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 225 | if (StOutBound && !StInBound) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 226 | ExplodedNode *N = C.generateSink(StOutBound); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 227 | if (!N) |
| 228 | return NULL; |
| 229 | |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 230 | BuiltinBug *BT; |
| 231 | if (IsDestination) { |
| 232 | if (!BT_BoundsWrite) { |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 233 | BT_BoundsWrite.reset(new BuiltinBug("Out-of-bound array access", |
| 234 | "Byte string function overflows destination buffer")); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 235 | } |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 236 | BT = static_cast<BuiltinBug*>(BT_BoundsWrite.get()); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 237 | } else { |
| 238 | if (!BT_Bounds) { |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 239 | BT_Bounds.reset(new BuiltinBug("Out-of-bound array access", |
| 240 | "Byte string function accesses out-of-bound array element")); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 241 | } |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 242 | BT = static_cast<BuiltinBug*>(BT_Bounds.get()); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 243 | } |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 244 | |
| 245 | // FIXME: It would be nice to eventually make this diagnostic more clear, |
| 246 | // e.g., by referencing the original declaration or by saying *why* this |
| 247 | // reference is outside the range. |
| 248 | |
| 249 | // Generate a report for this bug. |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 250 | RangedBugReport *report = new RangedBugReport(*BT, BT->getDescription(), N); |
| 251 | |
| 252 | report->addRange(S->getSourceRange()); |
| 253 | C.EmitReport(report); |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | // Array bound check succeeded. From this point forward the array bound |
| 258 | // should always succeed. |
| 259 | return StInBound; |
| 260 | } |
| 261 | |
| 262 | const GRState *CStringChecker::CheckBufferAccess(CheckerContext &C, |
| 263 | const GRState *state, |
| 264 | const Expr *Size, |
| 265 | const Expr *FirstBuf, |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 266 | const Expr *SecondBuf, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 267 | bool FirstIsDestination) const { |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 268 | // If a previous check has failed, propagate the failure. |
| 269 | if (!state) |
| 270 | return NULL; |
| 271 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 272 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 273 | ASTContext &Ctx = C.getASTContext(); |
| 274 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 275 | QualType sizeTy = Size->getType(); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 276 | QualType PtrTy = Ctx.getPointerType(Ctx.CharTy); |
| 277 | |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 278 | // Check that the first buffer is non-null. |
| 279 | SVal BufVal = state->getSVal(FirstBuf); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 280 | state = checkNonNull(C, state, FirstBuf, BufVal); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 281 | if (!state) |
| 282 | return NULL; |
| 283 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 284 | // Get the access length and make sure it is known. |
| 285 | SVal LengthVal = state->getSVal(Size); |
| 286 | NonLoc *Length = dyn_cast<NonLoc>(&LengthVal); |
| 287 | if (!Length) |
| 288 | return state; |
| 289 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 290 | // Compute the offset of the last element to be accessed: size-1. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 291 | NonLoc One = cast<NonLoc>(svalBuilder.makeIntVal(1, sizeTy)); |
| 292 | NonLoc LastOffset = cast<NonLoc>(svalBuilder.evalBinOpNN(state, BO_Sub, |
| 293 | *Length, One, sizeTy)); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 294 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 295 | // Check that the first buffer is sufficiently long. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 296 | SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType()); |
Jordy Rose | b6a4026 | 2010-08-05 23:11:30 +0000 | [diff] [blame] | 297 | if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 298 | SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, |
| 299 | LastOffset, PtrTy); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 300 | state = CheckLocation(C, state, FirstBuf, BufEnd, FirstIsDestination); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 301 | |
Jordy Rose | b6a4026 | 2010-08-05 23:11:30 +0000 | [diff] [blame] | 302 | // If the buffer isn't large enough, abort. |
| 303 | if (!state) |
| 304 | return NULL; |
| 305 | } |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 306 | |
| 307 | // If there's a second buffer, check it as well. |
| 308 | if (SecondBuf) { |
| 309 | BufVal = state->getSVal(SecondBuf); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 310 | state = checkNonNull(C, state, SecondBuf, BufVal); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 311 | if (!state) |
| 312 | return NULL; |
| 313 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 314 | BufStart = svalBuilder.evalCast(BufVal, PtrTy, SecondBuf->getType()); |
Jordy Rose | b6a4026 | 2010-08-05 23:11:30 +0000 | [diff] [blame] | 315 | if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 316 | SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, |
| 317 | LastOffset, PtrTy); |
Jordy Rose | b6a4026 | 2010-08-05 23:11:30 +0000 | [diff] [blame] | 318 | state = CheckLocation(C, state, SecondBuf, BufEnd); |
| 319 | } |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | // Large enough or not, return this state! |
| 323 | return state; |
| 324 | } |
| 325 | |
| 326 | const GRState *CStringChecker::CheckOverlap(CheckerContext &C, |
| 327 | const GRState *state, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 328 | const Expr *Size, |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 329 | const Expr *First, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 330 | const Expr *Second) const { |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 331 | // Do a simple check for overlap: if the two arguments are from the same |
| 332 | // buffer, see if the end of the first is greater than the start of the second |
| 333 | // or vice versa. |
| 334 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 335 | // If a previous check has failed, propagate the failure. |
| 336 | if (!state) |
| 337 | return NULL; |
| 338 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 339 | const GRState *stateTrue, *stateFalse; |
| 340 | |
| 341 | // Get the buffer values and make sure they're known locations. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 342 | SVal firstVal = state->getSVal(First); |
| 343 | SVal secondVal = state->getSVal(Second); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 344 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 345 | Loc *firstLoc = dyn_cast<Loc>(&firstVal); |
| 346 | if (!firstLoc) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 347 | return state; |
| 348 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 349 | Loc *secondLoc = dyn_cast<Loc>(&secondVal); |
| 350 | if (!secondLoc) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 351 | return state; |
| 352 | |
| 353 | // Are the two values the same? |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 354 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 355 | llvm::tie(stateTrue, stateFalse) = |
| 356 | state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc)); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 357 | |
| 358 | if (stateTrue && !stateFalse) { |
| 359 | // 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] | 360 | emitOverlapBug(C, stateTrue, First, Second); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 361 | return NULL; |
| 362 | } |
| 363 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 364 | // assume the two expressions are not equal. |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 365 | assert(stateFalse); |
| 366 | state = stateFalse; |
| 367 | |
| 368 | // Which value comes first? |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 369 | ASTContext &Ctx = svalBuilder.getContext(); |
| 370 | QualType cmpTy = Ctx.IntTy; |
| 371 | SVal reverse = svalBuilder.evalBinOpLL(state, BO_GT, |
| 372 | *firstLoc, *secondLoc, cmpTy); |
| 373 | DefinedOrUnknownSVal *reverseTest = dyn_cast<DefinedOrUnknownSVal>(&reverse); |
| 374 | if (!reverseTest) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 375 | return state; |
| 376 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 377 | llvm::tie(stateTrue, stateFalse) = state->assume(*reverseTest); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 378 | if (stateTrue) { |
| 379 | if (stateFalse) { |
| 380 | // If we don't know which one comes first, we can't perform this test. |
| 381 | return state; |
| 382 | } else { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 383 | // Switch the values so that firstVal is before secondVal. |
| 384 | Loc *tmpLoc = firstLoc; |
| 385 | firstLoc = secondLoc; |
| 386 | secondLoc = tmpLoc; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 387 | |
| 388 | // Switch the Exprs as well, so that they still correspond. |
| 389 | const Expr *tmpExpr = First; |
| 390 | First = Second; |
| 391 | Second = tmpExpr; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | // Get the length, and make sure it too is known. |
| 396 | SVal LengthVal = state->getSVal(Size); |
| 397 | NonLoc *Length = dyn_cast<NonLoc>(&LengthVal); |
| 398 | if (!Length) |
| 399 | return state; |
| 400 | |
| 401 | // Convert the first buffer's start address to char*. |
| 402 | // Bail out if the cast fails. |
| 403 | QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 404 | SVal FirstStart = svalBuilder.evalCast(*firstLoc, CharPtrTy, First->getType()); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 405 | Loc *FirstStartLoc = dyn_cast<Loc>(&FirstStart); |
| 406 | if (!FirstStartLoc) |
| 407 | return state; |
| 408 | |
| 409 | // Compute the end of the first buffer. Bail out if THAT fails. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 410 | SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add, |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 411 | *FirstStartLoc, *Length, CharPtrTy); |
| 412 | Loc *FirstEndLoc = dyn_cast<Loc>(&FirstEnd); |
| 413 | if (!FirstEndLoc) |
| 414 | return state; |
| 415 | |
| 416 | // 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] | 417 | SVal Overlap = svalBuilder.evalBinOpLL(state, BO_GT, |
| 418 | *FirstEndLoc, *secondLoc, cmpTy); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 419 | DefinedOrUnknownSVal *OverlapTest = dyn_cast<DefinedOrUnknownSVal>(&Overlap); |
| 420 | if (!OverlapTest) |
| 421 | return state; |
| 422 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 423 | llvm::tie(stateTrue, stateFalse) = state->assume(*OverlapTest); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 424 | |
| 425 | if (stateTrue && !stateFalse) { |
| 426 | // Overlap! |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 427 | emitOverlapBug(C, stateTrue, First, Second); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 428 | return NULL; |
| 429 | } |
| 430 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 431 | // assume the two expressions don't overlap. |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 432 | assert(stateFalse); |
| 433 | return stateFalse; |
| 434 | } |
| 435 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 436 | void CStringChecker::emitOverlapBug(CheckerContext &C, const GRState *state, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 437 | const Stmt *First, const Stmt *Second) const { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 438 | ExplodedNode *N = C.generateSink(state); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 439 | if (!N) |
| 440 | return; |
| 441 | |
| 442 | if (!BT_Overlap) |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 443 | BT_Overlap.reset(new BugType("Unix API", "Improper arguments")); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 444 | |
| 445 | // Generate a report for this bug. |
| 446 | RangedBugReport *report = |
| 447 | new RangedBugReport(*BT_Overlap, |
| 448 | "Arguments must not be overlapping buffers", N); |
| 449 | report->addRange(First->getSourceRange()); |
| 450 | report->addRange(Second->getSourceRange()); |
| 451 | |
| 452 | C.EmitReport(report); |
| 453 | } |
| 454 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 455 | const GRState *CStringChecker::setCStringLength(const GRState *state, |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 456 | const MemRegion *MR, |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 457 | SVal strLength) { |
| 458 | assert(!strLength.isUndef() && "Attempt to set an undefined string length"); |
| 459 | if (strLength.isUnknown()) |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 460 | return state; |
| 461 | |
| 462 | MR = MR->StripCasts(); |
| 463 | |
| 464 | switch (MR->getKind()) { |
| 465 | case MemRegion::StringRegionKind: |
| 466 | // FIXME: This can happen if we strcpy() into a string region. This is |
| 467 | // undefined [C99 6.4.5p6], but we should still warn about it. |
| 468 | return state; |
| 469 | |
| 470 | case MemRegion::SymbolicRegionKind: |
| 471 | case MemRegion::AllocaRegionKind: |
| 472 | case MemRegion::VarRegionKind: |
| 473 | case MemRegion::FieldRegionKind: |
| 474 | case MemRegion::ObjCIvarRegionKind: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 475 | return state->set<CStringLength>(MR, strLength); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 476 | |
| 477 | case MemRegion::ElementRegionKind: |
| 478 | // FIXME: Handle element regions by upper-bounding the parent region's |
| 479 | // string length. |
| 480 | return state; |
| 481 | |
| 482 | default: |
| 483 | // Other regions (mostly non-data) can't have a reliable C string length. |
| 484 | // For now, just ignore the change. |
| 485 | // FIXME: These are rare but not impossible. We should output some kind of |
| 486 | // warning for things like strcpy((char[]){'a', 0}, "b"); |
| 487 | return state; |
| 488 | } |
| 489 | } |
| 490 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 491 | SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C, |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 492 | const GRState *&state, |
| 493 | const Expr *Ex, |
| 494 | const MemRegion *MR) { |
| 495 | // If there's a recorded length, go ahead and return it. |
| 496 | const SVal *Recorded = state->get<CStringLength>(MR); |
| 497 | if (Recorded) |
| 498 | return *Recorded; |
| 499 | |
| 500 | // Otherwise, get a new symbol and update the state. |
| 501 | unsigned Count = C.getNodeBuilder().getCurrentBlockCount(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 502 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 503 | QualType sizeTy = svalBuilder.getContext().getSizeType(); |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 504 | SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(), |
| 505 | MR, Ex, sizeTy, Count); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 506 | state = state->set<CStringLength>(MR, strLength); |
| 507 | return strLength; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 510 | SVal CStringChecker::getCStringLength(CheckerContext &C, const GRState *&state, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 511 | const Expr *Ex, SVal Buf) const { |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 512 | const MemRegion *MR = Buf.getAsRegion(); |
| 513 | if (!MR) { |
| 514 | // If we can't get a region, see if it's something we /know/ isn't a |
| 515 | // C string. In the context of locations, the only time we can issue such |
| 516 | // a warning is for labels. |
| 517 | if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&Buf)) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 518 | if (ExplodedNode *N = C.generateNode(state)) { |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 519 | if (!BT_NotCString) |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 520 | BT_NotCString.reset(new BuiltinBug("API", |
| 521 | "Argument is not a null-terminated string.")); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 522 | |
| 523 | llvm::SmallString<120> buf; |
| 524 | llvm::raw_svector_ostream os(buf); |
| 525 | os << "Argument to byte string function is the address of the label '" |
Chris Lattner | 6810630 | 2011-02-17 05:38:27 +0000 | [diff] [blame] | 526 | << Label->getLabel()->getName() |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 527 | << "', which is not a null-terminated string"; |
| 528 | |
| 529 | // Generate a report for this bug. |
| 530 | EnhancedBugReport *report = new EnhancedBugReport(*BT_NotCString, |
| 531 | os.str(), N); |
| 532 | |
| 533 | report->addRange(Ex->getSourceRange()); |
| 534 | C.EmitReport(report); |
| 535 | } |
| 536 | |
| 537 | return UndefinedVal(); |
| 538 | } |
| 539 | |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 540 | // If it's not a region and not a label, give up. |
| 541 | return UnknownVal(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 544 | // If we have a region, strip casts from it and see if we can figure out |
| 545 | // its length. For anything we can't figure out, just return UnknownVal. |
| 546 | MR = MR->StripCasts(); |
| 547 | |
| 548 | switch (MR->getKind()) { |
| 549 | case MemRegion::StringRegionKind: { |
| 550 | // Modifying the contents of string regions is undefined [C99 6.4.5p6], |
| 551 | // 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] | 552 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 553 | QualType sizeTy = svalBuilder.getContext().getSizeType(); |
| 554 | const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral(); |
| 555 | return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 556 | } |
| 557 | case MemRegion::SymbolicRegionKind: |
| 558 | case MemRegion::AllocaRegionKind: |
| 559 | case MemRegion::VarRegionKind: |
| 560 | case MemRegion::FieldRegionKind: |
| 561 | case MemRegion::ObjCIvarRegionKind: |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 562 | return getCStringLengthForRegion(C, state, Ex, MR); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 563 | case MemRegion::CompoundLiteralRegionKind: |
| 564 | // FIXME: Can we track this? Is it necessary? |
| 565 | return UnknownVal(); |
| 566 | case MemRegion::ElementRegionKind: |
| 567 | // FIXME: How can we handle this? It's not good enough to subtract the |
| 568 | // offset from the base string length; consider "123\x00567" and &a[5]. |
| 569 | return UnknownVal(); |
| 570 | default: |
| 571 | // Other regions (mostly non-data) can't have a reliable C string length. |
| 572 | // In this case, an error is emitted and UndefinedVal is returned. |
| 573 | // The caller should always be prepared to handle this case. |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 574 | if (ExplodedNode *N = C.generateNode(state)) { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 575 | if (!BT_NotCString) |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 576 | BT_NotCString.reset(new BuiltinBug("API", |
| 577 | "Argument is not a null-terminated string.")); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 578 | |
| 579 | llvm::SmallString<120> buf; |
| 580 | llvm::raw_svector_ostream os(buf); |
| 581 | |
| 582 | os << "Argument to byte string function is "; |
| 583 | |
| 584 | if (SummarizeRegion(os, C.getASTContext(), MR)) |
| 585 | os << ", which is not a null-terminated string"; |
| 586 | else |
| 587 | os << "not a null-terminated string"; |
| 588 | |
| 589 | // Generate a report for this bug. |
| 590 | EnhancedBugReport *report = new EnhancedBugReport(*BT_NotCString, |
| 591 | os.str(), N); |
| 592 | |
| 593 | report->addRange(Ex->getSourceRange()); |
| 594 | C.EmitReport(report); |
| 595 | } |
| 596 | |
| 597 | return UndefinedVal(); |
| 598 | } |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 601 | const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C, |
| 602 | const GRState *&state, const Expr *expr, SVal val) const { |
| 603 | |
| 604 | // Get the memory region pointed to by the val. |
| 605 | const MemRegion *bufRegion = val.getAsRegion(); |
| 606 | if (!bufRegion) |
| 607 | return NULL; |
| 608 | |
| 609 | // Strip casts off the memory region. |
| 610 | bufRegion = bufRegion->StripCasts(); |
| 611 | |
| 612 | // Cast the memory region to a string region. |
| 613 | const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion); |
| 614 | if (!strRegion) |
| 615 | return NULL; |
| 616 | |
| 617 | // Return the actual string in the string region. |
| 618 | return strRegion->getStringLiteral(); |
| 619 | } |
| 620 | |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 621 | const GRState *CStringChecker::InvalidateBuffer(CheckerContext &C, |
| 622 | const GRState *state, |
| 623 | const Expr *E, SVal V) { |
| 624 | Loc *L = dyn_cast<Loc>(&V); |
| 625 | if (!L) |
| 626 | return state; |
| 627 | |
| 628 | // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes |
| 629 | // some assumptions about the value that CFRefCount can't. Even so, it should |
| 630 | // probably be refactored. |
| 631 | if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(L)) { |
| 632 | const MemRegion *R = MR->getRegion()->StripCasts(); |
| 633 | |
| 634 | // Are we dealing with an ElementRegion? If so, we should be invalidating |
| 635 | // the super-region. |
| 636 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
| 637 | R = ER->getSuperRegion(); |
| 638 | // FIXME: What about layers of ElementRegions? |
| 639 | } |
| 640 | |
| 641 | // Invalidate this region. |
| 642 | unsigned Count = C.getNodeBuilder().getCurrentBlockCount(); |
Ted Kremenek | 2534528 | 2011-02-11 19:48:15 +0000 | [diff] [blame] | 643 | return state->invalidateRegion(R, E, Count, NULL); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | // If we have a non-region value by chance, just remove the binding. |
| 647 | // FIXME: is this necessary or correct? This handles the non-Region |
| 648 | // cases. Is it ever valid to store to these? |
| 649 | return state->unbindLoc(*L); |
| 650 | } |
| 651 | |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 652 | bool CStringChecker::SummarizeRegion(llvm::raw_ostream& os, ASTContext& Ctx, |
| 653 | const MemRegion *MR) { |
| 654 | const TypedRegion *TR = dyn_cast<TypedRegion>(MR); |
| 655 | if (!TR) |
| 656 | return false; |
| 657 | |
| 658 | switch (TR->getKind()) { |
| 659 | case MemRegion::FunctionTextRegionKind: { |
| 660 | const FunctionDecl *FD = cast<FunctionTextRegion>(TR)->getDecl(); |
| 661 | if (FD) |
| 662 | os << "the address of the function '" << FD << "'"; |
| 663 | else |
| 664 | os << "the address of a function"; |
| 665 | return true; |
| 666 | } |
| 667 | case MemRegion::BlockTextRegionKind: |
| 668 | os << "block text"; |
| 669 | return true; |
| 670 | case MemRegion::BlockDataRegionKind: |
| 671 | os << "a block"; |
| 672 | return true; |
| 673 | case MemRegion::CXXThisRegionKind: |
Zhongxing Xu | 02fe28c | 2010-11-26 08:52:48 +0000 | [diff] [blame] | 674 | case MemRegion::CXXTempObjectRegionKind: |
| 675 | os << "a C++ temp object of type " << TR->getValueType().getAsString(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 676 | return true; |
| 677 | case MemRegion::VarRegionKind: |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 678 | os << "a variable of type" << TR->getValueType().getAsString(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 679 | return true; |
| 680 | case MemRegion::FieldRegionKind: |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 681 | os << "a field of type " << TR->getValueType().getAsString(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 682 | return true; |
| 683 | case MemRegion::ObjCIvarRegionKind: |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 684 | os << "an instance variable of type " << TR->getValueType().getAsString(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 685 | return true; |
| 686 | default: |
| 687 | return false; |
| 688 | } |
| 689 | } |
| 690 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 691 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 692 | // evaluation of individual function calls. |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 693 | //===----------------------------------------------------------------------===// |
| 694 | |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 695 | void CStringChecker::evalCopyCommon(CheckerContext &C, |
| 696 | const CallExpr *CE, |
| 697 | const GRState *state, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 698 | const Expr *Size, const Expr *Dest, |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 699 | const Expr *Source, bool Restricted, |
| 700 | bool IsMempcpy) const { |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 701 | // See if the size argument is zero. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 702 | SVal sizeVal = state->getSVal(Size); |
| 703 | QualType sizeTy = Size->getType(); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 704 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 705 | const GRState *stateZeroSize, *stateNonZeroSize; |
| 706 | llvm::tie(stateZeroSize, stateNonZeroSize) = assumeZero(C, state, sizeVal, sizeTy); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 707 | |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 708 | // Get the value of the Dest. |
| 709 | SVal destVal = state->getSVal(Dest); |
| 710 | |
| 711 | // If the size is zero, there won't be any actual memory access, so |
| 712 | // just bind the return value to the destination buffer and return. |
| 713 | if (stateZeroSize) { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 714 | C.addTransition(stateZeroSize); |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 715 | if (IsMempcpy) |
| 716 | state->BindExpr(CE, destVal); |
| 717 | else |
| 718 | state->BindExpr(CE, sizeVal); |
| 719 | return; |
| 720 | } |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 721 | |
| 722 | // 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] | 723 | if (stateNonZeroSize) { |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 724 | |
| 725 | // Ensure the destination is not null. If it is NULL there will be a |
| 726 | // NULL pointer dereference. |
| 727 | state = checkNonNull(C, state, Dest, destVal); |
| 728 | if (!state) |
| 729 | return; |
| 730 | |
| 731 | // Get the value of the Src. |
| 732 | SVal srcVal = state->getSVal(Source); |
| 733 | |
| 734 | // Ensure the source is not null. If it is NULL there will be a |
| 735 | // NULL pointer dereference. |
| 736 | state = checkNonNull(C, state, Source, srcVal); |
| 737 | if (!state) |
| 738 | return; |
| 739 | |
| 740 | // Ensure the buffers do not overlap. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 741 | state = stateNonZeroSize; |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 742 | state = CheckBufferAccess(C, state, Size, Dest, Source, |
| 743 | /* FirstIsDst = */ true); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 744 | if (Restricted) |
| 745 | state = CheckOverlap(C, state, Size, Dest, Source); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 746 | |
| 747 | if (state) { |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 748 | |
| 749 | // If this is mempcpy, get the byte after the last byte copied and |
| 750 | // bind the expr. |
| 751 | if (IsMempcpy) { |
| 752 | loc::MemRegionVal *destRegVal = dyn_cast<loc::MemRegionVal>(&destVal); |
| 753 | |
| 754 | // Get the length to copy. |
| 755 | SVal lenVal = state->getSVal(Size); |
| 756 | NonLoc *lenValNonLoc = dyn_cast<NonLoc>(&lenVal); |
| 757 | |
| 758 | // Get the byte after the last byte copied. |
| 759 | SVal lastElement = C.getSValBuilder().evalBinOpLN(state, BO_Add, |
| 760 | *destRegVal, |
| 761 | *lenValNonLoc, |
| 762 | Dest->getType()); |
| 763 | |
| 764 | // The byte after the last byte copied is the return value. |
| 765 | state = state->BindExpr(CE, lastElement); |
| 766 | } |
| 767 | |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 768 | // Invalidate the destination. |
| 769 | // FIXME: Even if we can't perfectly model the copy, we should see if we |
| 770 | // can use LazyCompoundVals to copy the source values into the destination. |
| 771 | // This would probably remove any existing bindings past the end of the |
| 772 | // copied region, but that's still an improvement over blank invalidation. |
| 773 | state = InvalidateBuffer(C, state, Dest, state->getSVal(Dest)); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 774 | C.addTransition(state); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 775 | } |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | |
| 779 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 780 | void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) const { |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 781 | // void *memcpy(void *restrict dst, const void *restrict src, size_t n); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 782 | // The return value is the address of the destination buffer. |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 783 | const Expr *Dest = CE->getArg(0); |
| 784 | const GRState *state = C.getState(); |
| 785 | state = state->BindExpr(CE, state->getSVal(Dest)); |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 786 | evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true); |
| 787 | } |
| 788 | |
| 789 | void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const { |
| 790 | // void *mempcpy(void *restrict dst, const void *restrict src, size_t n); |
| 791 | // The return value is a pointer to the byte following the last written byte. |
| 792 | const Expr *Dest = CE->getArg(0); |
| 793 | const GRState *state = C.getState(); |
| 794 | |
| 795 | 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] | 796 | } |
| 797 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 798 | void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const { |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 799 | // void *memmove(void *dst, const void *src, size_t n); |
| 800 | // The return value is the address of the destination buffer. |
| 801 | const Expr *Dest = CE->getArg(0); |
| 802 | const GRState *state = C.getState(); |
| 803 | state = state->BindExpr(CE, state->getSVal(Dest)); |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 804 | evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1)); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 807 | void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const { |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 808 | // void bcopy(const void *src, void *dst, size_t n); |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 809 | evalCopyCommon(C, CE, C.getState(), |
| 810 | CE->getArg(2), CE->getArg(1), CE->getArg(0)); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 813 | void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const { |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 814 | // int memcmp(const void *s1, const void *s2, size_t n); |
| 815 | const Expr *Left = CE->getArg(0); |
| 816 | const Expr *Right = CE->getArg(1); |
| 817 | const Expr *Size = CE->getArg(2); |
| 818 | |
| 819 | const GRState *state = C.getState(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 820 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 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 | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 825 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 826 | const GRState *stateZeroSize, *stateNonZeroSize; |
| 827 | llvm::tie(stateZeroSize, stateNonZeroSize) = |
| 828 | assumeZero(C, state, sizeVal, sizeTy); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 829 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 830 | // If the size can be zero, the result will be 0 in that case, and we don't |
| 831 | // have to check either of the buffers. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 832 | if (stateZeroSize) { |
| 833 | state = stateZeroSize; |
| 834 | state = state->BindExpr(CE, svalBuilder.makeZeroVal(CE->getType())); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 835 | C.addTransition(state); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 838 | // 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] | 839 | if (stateNonZeroSize) { |
| 840 | state = stateNonZeroSize; |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 841 | // If we know the two buffers are the same, we know the result is 0. |
| 842 | // First, get the two buffers' addresses. Another checker will have already |
| 843 | // made sure they're not undefined. |
| 844 | DefinedOrUnknownSVal LV = cast<DefinedOrUnknownSVal>(state->getSVal(Left)); |
| 845 | DefinedOrUnknownSVal RV = cast<DefinedOrUnknownSVal>(state->getSVal(Right)); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 846 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 847 | // See if they are the same. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 848 | DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 849 | const GRState *StSameBuf, *StNotSameBuf; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 850 | llvm::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 851 | |
| 852 | // If the two arguments might be the same buffer, we know the result is zero, |
| 853 | // and we only need to check one size. |
| 854 | if (StSameBuf) { |
| 855 | state = StSameBuf; |
| 856 | state = CheckBufferAccess(C, state, Size, Left); |
| 857 | if (state) { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 858 | state = StSameBuf->BindExpr(CE, svalBuilder.makeZeroVal(CE->getType())); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 859 | C.addTransition(state); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | // If the two arguments might be different buffers, we have to check the |
| 864 | // size of both of them. |
| 865 | if (StNotSameBuf) { |
| 866 | state = StNotSameBuf; |
| 867 | state = CheckBufferAccess(C, state, Size, Left, Right); |
| 868 | if (state) { |
| 869 | // The return value is the comparison result, which we don't know. |
| 870 | unsigned Count = C.getNodeBuilder().getCurrentBlockCount(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 871 | SVal CmpV = svalBuilder.getConjuredSymbolVal(NULL, CE, Count); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 872 | state = state->BindExpr(CE, CmpV); |
| 873 | C.addTransition(state); |
| 874 | } |
| 875 | } |
| 876 | } |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 879 | void CStringChecker::evalstrLength(CheckerContext &C, |
| 880 | const CallExpr *CE) const { |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 881 | // size_t strlen(const char *s); |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 882 | evalstrLengthCommon(C, CE, /* IsStrnlen = */ false); |
| 883 | } |
| 884 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 885 | void CStringChecker::evalstrnLength(CheckerContext &C, |
| 886 | const CallExpr *CE) const { |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 887 | // size_t strnlen(const char *s, size_t maxlen); |
| 888 | evalstrLengthCommon(C, CE, /* IsStrnlen = */ true); |
| 889 | } |
| 890 | |
| 891 | void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 892 | bool IsStrnlen) const { |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 893 | const GRState *state = C.getState(); |
| 894 | const Expr *Arg = CE->getArg(0); |
| 895 | SVal ArgVal = state->getSVal(Arg); |
| 896 | |
| 897 | // Check that the argument is non-null. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 898 | state = checkNonNull(C, state, Arg, ArgVal); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 899 | |
| 900 | if (state) { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 901 | SVal strLength = getCStringLength(C, state, Arg, ArgVal); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 902 | |
| 903 | // If the argument isn't a valid C string, there's no valid state to |
| 904 | // transition to. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 905 | if (strLength.isUndef()) |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 906 | return; |
| 907 | |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 908 | // If the check is for strnlen() then bind the return value to no more than |
| 909 | // the maxlen value. |
| 910 | if (IsStrnlen) { |
| 911 | const Expr *maxlenExpr = CE->getArg(1); |
| 912 | SVal maxlenVal = state->getSVal(maxlenExpr); |
| 913 | |
| 914 | NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength); |
| 915 | NonLoc *maxlenValNL = dyn_cast<NonLoc>(&maxlenVal); |
| 916 | |
| 917 | QualType cmpTy = C.getSValBuilder().getContext().IntTy; |
| 918 | const GRState *stateTrue, *stateFalse; |
| 919 | |
| 920 | // Check if the strLength is greater than or equal to the maxlen |
| 921 | llvm::tie(stateTrue, stateFalse) = |
| 922 | state->assume(cast<DefinedOrUnknownSVal> |
| 923 | (C.getSValBuilder().evalBinOpNN(state, BO_GE, |
| 924 | *strLengthNL, *maxlenValNL, |
| 925 | cmpTy))); |
| 926 | |
| 927 | // If the strLength is greater than or equal to the maxlen, set strLength |
| 928 | // to maxlen |
| 929 | if (stateTrue && !stateFalse) { |
| 930 | strLength = maxlenVal; |
| 931 | } |
| 932 | } |
| 933 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 934 | // If getCStringLength couldn't figure out the length, conjure a return |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 935 | // value, so it can be used in constraints, at least. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 936 | if (strLength.isUnknown()) { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 937 | unsigned Count = C.getNodeBuilder().getCurrentBlockCount(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 938 | strLength = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 939 | } |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 940 | |
| 941 | // Bind the return value. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 942 | state = state->BindExpr(CE, strLength); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 943 | C.addTransition(state); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 947 | void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const { |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 948 | // char *strcpy(char *restrict dst, const char *restrict src); |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 949 | evalStrcpyCommon(C, CE, |
| 950 | /* returnEnd = */ false, |
| 951 | /* isBounded = */ false, |
| 952 | /* isAppending = */ false); |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 955 | void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const { |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 956 | // char *strcpy(char *restrict dst, const char *restrict src); |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 957 | evalStrcpyCommon(C, CE, |
| 958 | /* returnEnd = */ false, |
| 959 | /* isBounded = */ true, |
| 960 | /* isAppending = */ false); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 963 | void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const { |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 964 | // char *stpcpy(char *restrict dst, const char *restrict src); |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 965 | evalStrcpyCommon(C, CE, |
| 966 | /* returnEnd = */ true, |
| 967 | /* isBounded = */ false, |
| 968 | /* isAppending = */ false); |
| 969 | } |
| 970 | |
| 971 | void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const { |
| 972 | //char *strcat(char *restrict s1, const char *restrict s2); |
| 973 | evalStrcpyCommon(C, CE, |
| 974 | /* returnEnd = */ false, |
| 975 | /* isBounded = */ false, |
| 976 | /* isAppending = */ true); |
| 977 | } |
| 978 | |
| 979 | void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const { |
| 980 | //char *strncat(char *restrict s1, const char *restrict s2, size_t n); |
| 981 | evalStrcpyCommon(C, CE, |
| 982 | /* returnEnd = */ false, |
| 983 | /* isBounded = */ true, |
| 984 | /* isAppending = */ true); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 987 | void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 988 | bool returnEnd, bool isBounded, |
| 989 | bool isAppending) const { |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 990 | const GRState *state = C.getState(); |
| 991 | |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 992 | // Check that the destination is non-null. |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 993 | const Expr *Dst = CE->getArg(0); |
| 994 | SVal DstVal = state->getSVal(Dst); |
| 995 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 996 | state = checkNonNull(C, state, Dst, DstVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 997 | if (!state) |
| 998 | return; |
| 999 | |
| 1000 | // Check that the source is non-null. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1001 | const Expr *srcExpr = CE->getArg(1); |
| 1002 | SVal srcVal = state->getSVal(srcExpr); |
| 1003 | state = checkNonNull(C, state, srcExpr, srcVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1004 | if (!state) |
| 1005 | return; |
| 1006 | |
| 1007 | // Get the string length of the source. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1008 | SVal strLength = getCStringLength(C, state, srcExpr, srcVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1009 | |
| 1010 | // If the source isn't a valid C string, give up. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1011 | if (strLength.isUndef()) |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1012 | return; |
| 1013 | |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1014 | // If the function is strncpy, strncat, etc... it is bounded. |
| 1015 | if (isBounded) { |
| 1016 | // Get the max number of characters to copy. |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1017 | const Expr *lenExpr = CE->getArg(2); |
| 1018 | SVal lenVal = state->getSVal(lenExpr); |
| 1019 | |
Lenny Maiorani | 508c627 | 2011-04-28 18:59:43 +0000 | [diff] [blame^] | 1020 | // Cast the length to a NonLoc SVal. If it is not a NonLoc then give up. |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1021 | NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength); |
Lenny Maiorani | 508c627 | 2011-04-28 18:59:43 +0000 | [diff] [blame^] | 1022 | if (!strLengthNL) |
| 1023 | return; |
| 1024 | |
| 1025 | // Cast the max length to a NonLoc SVal. If it is not a NonLoc then give up. |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1026 | NonLoc *lenValNL = dyn_cast<NonLoc>(&lenVal); |
Lenny Maiorani | 508c627 | 2011-04-28 18:59:43 +0000 | [diff] [blame^] | 1027 | if (!lenValNL) |
| 1028 | return; |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1029 | |
| 1030 | QualType cmpTy = C.getSValBuilder().getContext().IntTy; |
| 1031 | const GRState *stateTrue, *stateFalse; |
| 1032 | |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1033 | // Check if the max number to copy is less than the length of the src. |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1034 | llvm::tie(stateTrue, stateFalse) = |
| 1035 | state->assume(cast<DefinedOrUnknownSVal> |
| 1036 | (C.getSValBuilder().evalBinOpNN(state, BO_GT, |
| 1037 | *strLengthNL, *lenValNL, |
| 1038 | cmpTy))); |
| 1039 | |
| 1040 | if (stateTrue) { |
| 1041 | // Max number to copy is less than the length of the src, so the actual |
| 1042 | // strLength copied is the max number arg. |
| 1043 | strLength = lenVal; |
| 1044 | } |
| 1045 | } |
| 1046 | |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1047 | // If this is an appending function (strcat, strncat...) then set the |
| 1048 | // string length to strlen(src) + strlen(dst) since the buffer will |
| 1049 | // ultimately contain both. |
| 1050 | if (isAppending) { |
| 1051 | // Get the string length of the destination, or give up. |
| 1052 | SVal dstStrLength = getCStringLength(C, state, Dst, DstVal); |
| 1053 | if (dstStrLength.isUndef()) |
| 1054 | return; |
| 1055 | |
| 1056 | NonLoc *srcStrLengthNL = dyn_cast<NonLoc>(&strLength); |
| 1057 | NonLoc *dstStrLengthNL = dyn_cast<NonLoc>(&dstStrLength); |
| 1058 | |
| 1059 | // If src or dst cast to NonLoc is NULL, give up. |
| 1060 | if ((!srcStrLengthNL) || (!dstStrLengthNL)) |
| 1061 | return; |
| 1062 | |
| 1063 | QualType addTy = C.getSValBuilder().getContext().getSizeType(); |
| 1064 | |
| 1065 | strLength = C.getSValBuilder().evalBinOpNN(state, BO_Add, |
| 1066 | *srcStrLengthNL, *dstStrLengthNL, |
| 1067 | addTy); |
| 1068 | } |
| 1069 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1070 | SVal Result = (returnEnd ? UnknownVal() : DstVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1071 | |
| 1072 | // If the destination is a MemRegion, try to check for a buffer overflow and |
| 1073 | // record the new string length. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1074 | if (loc::MemRegionVal *dstRegVal = dyn_cast<loc::MemRegionVal>(&DstVal)) { |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1075 | // If the length is known, we can check for an overflow. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1076 | if (NonLoc *knownStrLength = dyn_cast<NonLoc>(&strLength)) { |
| 1077 | SVal lastElement = |
| 1078 | C.getSValBuilder().evalBinOpLN(state, BO_Add, *dstRegVal, |
| 1079 | *knownStrLength, Dst->getType()); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1080 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1081 | state = CheckLocation(C, state, Dst, lastElement, /* IsDst = */ true); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1082 | if (!state) |
| 1083 | return; |
| 1084 | |
| 1085 | // 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] | 1086 | if (returnEnd) |
| 1087 | Result = lastElement; |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | // Invalidate the destination. This must happen before we set the C string |
| 1091 | // length because invalidation will clear the length. |
| 1092 | // FIXME: Even if we can't perfectly model the copy, we should see if we |
| 1093 | // can use LazyCompoundVals to copy the source values into the destination. |
| 1094 | // This would probably remove any existing bindings past the end of the |
| 1095 | // string, but that's still an improvement over blank invalidation. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1096 | state = InvalidateBuffer(C, state, Dst, *dstRegVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1097 | |
| 1098 | // Set the C string length of the destination. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1099 | state = setCStringLength(state, dstRegVal->getRegion(), strLength); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | // If this is a stpcpy-style copy, but we were unable to check for a buffer |
| 1103 | // overflow, we still need a result. Conjure a return value. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1104 | if (returnEnd && Result.isUnknown()) { |
| 1105 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1106 | unsigned Count = C.getNodeBuilder().getCurrentBlockCount(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1107 | strLength = svalBuilder.getConjuredSymbolVal(NULL, CE, Count); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | // Set the return value. |
| 1111 | state = state->BindExpr(CE, Result); |
| 1112 | C.addTransition(state); |
| 1113 | } |
| 1114 | |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1115 | void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const { |
| 1116 | //int strcmp(const char *restrict s1, const char *restrict s2); |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1117 | evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ false); |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1118 | } |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1119 | |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1120 | void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const { |
| 1121 | //int strncmp(const char *restrict s1, const char *restrict s2, size_t n); |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1122 | evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ false); |
| 1123 | } |
| 1124 | |
| 1125 | void CStringChecker::evalStrcasecmp(CheckerContext &C, |
| 1126 | const CallExpr *CE) const { |
| 1127 | //int strcasecmp(const char *restrict s1, const char *restrict s2); |
| 1128 | evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ true); |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE, |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1132 | bool isBounded, bool ignoreCase) const { |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1133 | const GRState *state = C.getState(); |
| 1134 | |
| 1135 | // Check that the first string is non-null |
| 1136 | const Expr *s1 = CE->getArg(0); |
| 1137 | SVal s1Val = state->getSVal(s1); |
| 1138 | state = checkNonNull(C, state, s1, s1Val); |
| 1139 | if (!state) |
| 1140 | return; |
| 1141 | |
| 1142 | // Check that the second string is non-null. |
| 1143 | const Expr *s2 = CE->getArg(1); |
| 1144 | SVal s2Val = state->getSVal(s2); |
| 1145 | state = checkNonNull(C, state, s2, s2Val); |
| 1146 | if (!state) |
| 1147 | return; |
| 1148 | |
| 1149 | // Get the string length of the first string or give up. |
| 1150 | SVal s1Length = getCStringLength(C, state, s1, s1Val); |
| 1151 | if (s1Length.isUndef()) |
| 1152 | return; |
| 1153 | |
| 1154 | // Get the string length of the second string or give up. |
| 1155 | SVal s2Length = getCStringLength(C, state, s2, s2Val); |
| 1156 | if (s2Length.isUndef()) |
| 1157 | return; |
| 1158 | |
| 1159 | // Get the string literal of the first string. |
| 1160 | const StringLiteral *s1StrLiteral = getCStringLiteral(C, state, s1, s1Val); |
| 1161 | if (!s1StrLiteral) |
| 1162 | return; |
| 1163 | llvm::StringRef s1StrRef = s1StrLiteral->getString(); |
| 1164 | |
| 1165 | // Get the string literal of the second string. |
| 1166 | const StringLiteral *s2StrLiteral = getCStringLiteral(C, state, s2, s2Val); |
| 1167 | if (!s2StrLiteral) |
| 1168 | return; |
| 1169 | llvm::StringRef s2StrRef = s2StrLiteral->getString(); |
| 1170 | |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1171 | int result; |
| 1172 | if (isBounded) { |
| 1173 | // Get the max number of characters to compare. |
| 1174 | const Expr *lenExpr = CE->getArg(2); |
| 1175 | SVal lenVal = state->getSVal(lenExpr); |
| 1176 | |
| 1177 | // Dynamically cast the length to a ConcreteInt. If it is not a ConcreteInt |
| 1178 | // then give up, otherwise get the value and use it as the bounds. |
| 1179 | nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&lenVal); |
| 1180 | if (!CI) |
| 1181 | return; |
| 1182 | llvm::APSInt lenInt(CI->getValue()); |
| 1183 | |
| 1184 | // Compare using the bounds provided like strncmp() does. |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1185 | if (ignoreCase) { |
| 1186 | // TODO Implement compare_lower(RHS, n) in LLVM StringRef. |
| 1187 | // result = s1StrRef.compare_lower(s2StrRef, |
| 1188 | // (size_t)lenInt.getLimitedValue()); |
| 1189 | |
| 1190 | // For now, give up. |
| 1191 | return; |
| 1192 | } else { |
| 1193 | result = s1StrRef.compare(s2StrRef, (size_t)lenInt.getLimitedValue()); |
| 1194 | } |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1195 | } else { |
| 1196 | // Compare string 1 to string 2 the same way strcmp() does. |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1197 | if (ignoreCase) { |
| 1198 | result = s1StrRef.compare_lower(s2StrRef); |
| 1199 | } else { |
| 1200 | result = s1StrRef.compare(s2StrRef); |
| 1201 | } |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1202 | } |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1203 | |
| 1204 | // Build the SVal of the comparison to bind the return value. |
| 1205 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 1206 | QualType intTy = svalBuilder.getContext().IntTy; |
| 1207 | SVal resultVal = svalBuilder.makeIntVal(result, intTy); |
| 1208 | |
| 1209 | // Bind the return value of the expression. |
| 1210 | // Set the return value. |
| 1211 | state = state->BindExpr(CE, resultVal); |
| 1212 | C.addTransition(state); |
| 1213 | } |
| 1214 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1215 | //===----------------------------------------------------------------------===// |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1216 | // The driver method, and other Checker callbacks. |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1217 | //===----------------------------------------------------------------------===// |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1218 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1219 | bool CStringChecker::evalCall(const CallExpr *CE, CheckerContext &C) const { |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1220 | // Get the callee. All the functions we care about are C functions |
| 1221 | // with simple identifiers. |
| 1222 | const GRState *state = C.getState(); |
| 1223 | const Expr *Callee = CE->getCallee(); |
| 1224 | const FunctionDecl *FD = state->getSVal(Callee).getAsFunctionDecl(); |
| 1225 | |
| 1226 | if (!FD) |
| 1227 | return false; |
| 1228 | |
| 1229 | // 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] | 1230 | IdentifierInfo *II = FD->getIdentifier(); |
| 1231 | if (!II) // if no identifier, not a simple C function |
| 1232 | return false; |
| 1233 | llvm::StringRef Name = II->getName(); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1234 | if (Name.startswith("__builtin_")) |
| 1235 | Name = Name.substr(10); |
| 1236 | |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1237 | FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name) |
| 1238 | .Cases("memcpy", "__memcpy_chk", &CStringChecker::evalMemcpy) |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 1239 | .Case("mempcpy", &CStringChecker::evalMempcpy) |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1240 | .Cases("memcmp", "bcmp", &CStringChecker::evalMemcmp) |
| 1241 | .Cases("memmove", "__memmove_chk", &CStringChecker::evalMemmove) |
| 1242 | .Cases("strcpy", "__strcpy_chk", &CStringChecker::evalStrcpy) |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1243 | .Cases("strncpy", "__strncpy_chk", &CStringChecker::evalStrncpy) |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1244 | .Cases("stpcpy", "__stpcpy_chk", &CStringChecker::evalStpcpy) |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1245 | .Cases("strcat", "__strcat_chk", &CStringChecker::evalStrcat) |
| 1246 | .Cases("strncat", "__strncat_chk", &CStringChecker::evalStrncat) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1247 | .Case("strlen", &CStringChecker::evalstrLength) |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1248 | .Case("strnlen", &CStringChecker::evalstrnLength) |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1249 | .Case("strcmp", &CStringChecker::evalStrcmp) |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1250 | .Case("strncmp", &CStringChecker::evalStrncmp) |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1251 | .Case("strcasecmp", &CStringChecker::evalStrcasecmp) |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1252 | .Case("bcopy", &CStringChecker::evalBcopy) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1253 | .Default(NULL); |
| 1254 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1255 | // 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] | 1256 | if (!evalFunction) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1257 | return false; |
| 1258 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1259 | // Check and evaluate the call. |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1260 | (this->*evalFunction)(C, CE); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1261 | return true; |
| 1262 | } |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1263 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1264 | void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1265 | // Record string length for char a[] = "abc"; |
| 1266 | const GRState *state = C.getState(); |
| 1267 | |
| 1268 | for (DeclStmt::const_decl_iterator I = DS->decl_begin(), E = DS->decl_end(); |
| 1269 | I != E; ++I) { |
| 1270 | const VarDecl *D = dyn_cast<VarDecl>(*I); |
| 1271 | if (!D) |
| 1272 | continue; |
| 1273 | |
| 1274 | // FIXME: Handle array fields of structs. |
| 1275 | if (!D->getType()->isArrayType()) |
| 1276 | continue; |
| 1277 | |
| 1278 | const Expr *Init = D->getInit(); |
| 1279 | if (!Init) |
| 1280 | continue; |
| 1281 | if (!isa<StringLiteral>(Init)) |
| 1282 | continue; |
| 1283 | |
| 1284 | Loc VarLoc = state->getLValue(D, C.getPredecessor()->getLocationContext()); |
| 1285 | const MemRegion *MR = VarLoc.getAsRegion(); |
| 1286 | if (!MR) |
| 1287 | continue; |
| 1288 | |
| 1289 | SVal StrVal = state->getSVal(Init); |
| 1290 | assert(StrVal.isValid() && "Initializer string is unknown or undefined"); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1291 | DefinedOrUnknownSVal strLength |
| 1292 | = cast<DefinedOrUnknownSVal>(getCStringLength(C, state, Init, StrVal)); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1293 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1294 | state = state->set<CStringLength>(MR, strLength); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | C.addTransition(state); |
| 1298 | } |
| 1299 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1300 | bool CStringChecker::wantsRegionChangeUpdate(const GRState *state) const { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1301 | CStringLength::EntryMap Entries = state->get<CStringLength>(); |
| 1302 | return !Entries.isEmpty(); |
| 1303 | } |
| 1304 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1305 | const GRState * |
| 1306 | CStringChecker::checkRegionChanges(const GRState *state, |
| 1307 | const MemRegion * const *Begin, |
| 1308 | const MemRegion * const *End) const { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1309 | CStringLength::EntryMap Entries = state->get<CStringLength>(); |
| 1310 | if (Entries.isEmpty()) |
| 1311 | return state; |
| 1312 | |
| 1313 | llvm::SmallPtrSet<const MemRegion *, 8> Invalidated; |
| 1314 | llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions; |
| 1315 | |
| 1316 | // First build sets for the changed regions and their super-regions. |
| 1317 | for ( ; Begin != End; ++Begin) { |
| 1318 | const MemRegion *MR = *Begin; |
| 1319 | Invalidated.insert(MR); |
| 1320 | |
| 1321 | SuperRegions.insert(MR); |
| 1322 | while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) { |
| 1323 | MR = SR->getSuperRegion(); |
| 1324 | SuperRegions.insert(MR); |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>(); |
| 1329 | |
| 1330 | // Then loop over the entries in the current state. |
| 1331 | for (CStringLength::EntryMap::iterator I = Entries.begin(), |
| 1332 | E = Entries.end(); I != E; ++I) { |
| 1333 | const MemRegion *MR = I.getKey(); |
| 1334 | |
| 1335 | // Is this entry for a super-region of a changed region? |
| 1336 | if (SuperRegions.count(MR)) { |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 1337 | Entries = F.remove(Entries, MR); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1338 | continue; |
| 1339 | } |
| 1340 | |
| 1341 | // Is this entry for a sub-region of a changed region? |
| 1342 | const MemRegion *Super = MR; |
| 1343 | while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) { |
| 1344 | Super = SR->getSuperRegion(); |
| 1345 | if (Invalidated.count(Super)) { |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 1346 | Entries = F.remove(Entries, MR); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1347 | break; |
| 1348 | } |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | return state->set<CStringLength>(Entries); |
| 1353 | } |
| 1354 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1355 | void CStringChecker::checkLiveSymbols(const GRState *state, |
| 1356 | SymbolReaper &SR) const { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1357 | // Mark all symbols in our string length map as valid. |
| 1358 | CStringLength::EntryMap Entries = state->get<CStringLength>(); |
| 1359 | |
| 1360 | for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end(); |
| 1361 | I != E; ++I) { |
| 1362 | SVal Len = I.getData(); |
| 1363 | if (SymbolRef Sym = Len.getAsSymbol()) |
| 1364 | SR.markInUse(Sym); |
| 1365 | } |
| 1366 | } |
| 1367 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1368 | void CStringChecker::checkDeadSymbols(SymbolReaper &SR, |
| 1369 | CheckerContext &C) const { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1370 | if (!SR.hasDeadSymbols()) |
| 1371 | return; |
| 1372 | |
| 1373 | const GRState *state = C.getState(); |
| 1374 | CStringLength::EntryMap Entries = state->get<CStringLength>(); |
| 1375 | if (Entries.isEmpty()) |
| 1376 | return; |
| 1377 | |
| 1378 | CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>(); |
| 1379 | for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end(); |
| 1380 | I != E; ++I) { |
| 1381 | SVal Len = I.getData(); |
| 1382 | if (SymbolRef Sym = Len.getAsSymbol()) { |
| 1383 | if (SR.isDead(Sym)) |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 1384 | Entries = F.remove(Entries, I.getKey()); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | state = state->set<CStringLength>(Entries); |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 1389 | C.generateNode(state); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1390 | } |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1391 | |
| 1392 | void ento::registerCStringChecker(CheckerManager &mgr) { |
| 1393 | mgr.registerChecker<CStringChecker>(); |
| 1394 | } |