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