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