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