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