Anna Zaks | cc92521 | 2011-10-11 16:49:54 +0000 | [diff] [blame] | 1 | //= CStringChecker.cpp - Checks calls to C string functions --------*- C++ -*-// |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 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" |
Anna Zaks | e56167e | 2012-02-17 22:35:31 +0000 | [diff] [blame] | 16 | #include "InterCheckerAPI.h" |
Jordan Rose | 4938f27 | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 17 | #include "clang/Basic/CharInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 507ff53 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
Benjamin Kramer | 3307c508 | 2012-02-04 12:31:12 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringSwitch.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 29 | using namespace ento; |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 30 | |
| 31 | namespace { |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 32 | class CStringChecker : public Checker< eval::Call, |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 33 | check::PreStmt<DeclStmt>, |
| 34 | check::LiveSymbols, |
| 35 | check::DeadSymbols, |
| 36 | check::RegionChanges |
| 37 | > { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 38 | mutable std::unique_ptr<BugType> BT_Null, BT_Bounds, BT_Overlap, |
| 39 | BT_NotCString, BT_AdditionOverflow; |
Anna Zaks | e0c7c27 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 40 | |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 41 | mutable const char *CurrentFunctionDescription; |
| 42 | |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 43 | public: |
Anna Zaks | e0c7c27 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 44 | /// The filter is used to filter out the diagnostics which are not enabled by |
| 45 | /// the user. |
| 46 | struct CStringChecksFilter { |
| 47 | DefaultBool CheckCStringNullArg; |
| 48 | DefaultBool CheckCStringOutOfBounds; |
| 49 | DefaultBool CheckCStringBufferOverlap; |
| 50 | DefaultBool CheckCStringNotNullTerm; |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 51 | |
| 52 | CheckName CheckNameCStringNullArg; |
| 53 | CheckName CheckNameCStringOutOfBounds; |
| 54 | CheckName CheckNameCStringBufferOverlap; |
| 55 | CheckName CheckNameCStringNotNullTerm; |
Anna Zaks | e0c7c27 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | CStringChecksFilter Filter; |
| 59 | |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 60 | static void *getTag() { static int tag; return &tag; } |
| 61 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 62 | bool evalCall(const CallExpr *CE, CheckerContext &C) const; |
| 63 | void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const; |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 64 | void checkLiveSymbols(ProgramStateRef state, SymbolReaper &SR) const; |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 65 | void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const; |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 66 | bool wantsRegionChangeUpdate(ProgramStateRef state) const; |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 68 | ProgramStateRef |
| 69 | checkRegionChanges(ProgramStateRef state, |
Anna Zaks | dc15415 | 2012-12-20 00:38:25 +0000 | [diff] [blame] | 70 | const InvalidatedSymbols *, |
Jordy Rose | 1fad663 | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 71 | ArrayRef<const MemRegion *> ExplicitRegions, |
Anna Zaks | 3d34834 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 72 | ArrayRef<const MemRegion *> Regions, |
Jordan Rose | 742920c | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 73 | const CallEvent *Call) const; |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 74 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 75 | typedef void (CStringChecker::*FnCheck)(CheckerContext &, |
| 76 | const CallExpr *) const; |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 77 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 78 | void evalMemcpy(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 79 | void evalMempcpy(CheckerContext &C, const CallExpr *CE) const; |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 80 | void evalMemmove(CheckerContext &C, const CallExpr *CE) const; |
| 81 | void evalBcopy(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 82 | void evalCopyCommon(CheckerContext &C, const CallExpr *CE, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 83 | ProgramStateRef state, |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 84 | const Expr *Size, |
| 85 | const Expr *Source, |
| 86 | const Expr *Dest, |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 87 | bool Restricted = false, |
| 88 | bool IsMempcpy = false) const; |
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 | void evalMemcmp(CheckerContext &C, const CallExpr *CE) const; |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 91 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 92 | void evalstrLength(CheckerContext &C, const CallExpr *CE) const; |
| 93 | void evalstrnLength(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 94 | void evalstrLengthCommon(CheckerContext &C, |
| 95 | const CallExpr *CE, |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 96 | bool IsStrnlen = false) const; |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 97 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 98 | void evalStrcpy(CheckerContext &C, const CallExpr *CE) const; |
| 99 | void evalStrncpy(CheckerContext &C, const CallExpr *CE) const; |
| 100 | void evalStpcpy(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 101 | void evalStrcpyCommon(CheckerContext &C, |
| 102 | const CallExpr *CE, |
| 103 | bool returnEnd, |
| 104 | bool isBounded, |
| 105 | bool isAppending) const; |
Lenny Maiorani | 467dbd5 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 106 | |
| 107 | void evalStrcat(CheckerContext &C, const CallExpr *CE) const; |
| 108 | void evalStrncat(CheckerContext &C, const CallExpr *CE) const; |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 109 | |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 110 | void evalStrcmp(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | e553e40 | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 111 | void evalStrncmp(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | 4af23c8 | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 112 | void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | 0b51027 | 2011-05-02 19:05:49 +0000 | [diff] [blame] | 113 | void evalStrncasecmp(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 114 | void evalStrcmpCommon(CheckerContext &C, |
| 115 | const CallExpr *CE, |
| 116 | bool isBounded = false, |
| 117 | bool ignoreCase = false) const; |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 118 | |
Jordan Rose | 6e3cf2b | 2013-04-22 23:18:42 +0000 | [diff] [blame] | 119 | void evalStrsep(CheckerContext &C, const CallExpr *CE) const; |
| 120 | |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 121 | // Utility methods |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 122 | std::pair<ProgramStateRef , ProgramStateRef > |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 123 | static assumeZero(CheckerContext &C, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 124 | ProgramStateRef state, SVal V, QualType Ty); |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 126 | static ProgramStateRef setCStringLength(ProgramStateRef state, |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 127 | const MemRegion *MR, |
| 128 | SVal strLength); |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 129 | static SVal getCStringLengthForRegion(CheckerContext &C, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 130 | ProgramStateRef &state, |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 131 | const Expr *Ex, |
| 132 | const MemRegion *MR, |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 133 | bool hypothetical); |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 134 | SVal getCStringLength(CheckerContext &C, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 135 | ProgramStateRef &state, |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 136 | const Expr *Ex, |
| 137 | SVal Buf, |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 138 | bool hypothetical = false) const; |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 139 | |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 140 | const StringLiteral *getCStringLiteral(CheckerContext &C, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 141 | ProgramStateRef &state, |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 142 | const Expr *expr, |
| 143 | SVal val) const; |
| 144 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 145 | static ProgramStateRef InvalidateBuffer(CheckerContext &C, |
Anton Yartsev | 968c60a | 2013-11-17 09:18:48 +0000 | [diff] [blame] | 146 | ProgramStateRef state, |
| 147 | const Expr *Ex, SVal V, |
| 148 | bool IsSourceBuffer); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 149 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 150 | static bool SummarizeRegion(raw_ostream &os, ASTContext &Ctx, |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 151 | const MemRegion *MR); |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 152 | |
| 153 | // Re-usable checks |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 154 | ProgramStateRef checkNonNull(CheckerContext &C, |
| 155 | ProgramStateRef state, |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 156 | const Expr *S, |
| 157 | SVal l) const; |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 158 | ProgramStateRef CheckLocation(CheckerContext &C, |
| 159 | ProgramStateRef state, |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 160 | const Expr *S, |
| 161 | SVal l, |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 162 | const char *message = nullptr) const; |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 163 | ProgramStateRef CheckBufferAccess(CheckerContext &C, |
| 164 | ProgramStateRef state, |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 165 | const Expr *Size, |
| 166 | const Expr *FirstBuf, |
| 167 | const Expr *SecondBuf, |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 168 | const char *firstMessage = nullptr, |
| 169 | const char *secondMessage = nullptr, |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 170 | bool WarnAboutSize = false) const; |
| 171 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 172 | ProgramStateRef CheckBufferAccess(CheckerContext &C, |
| 173 | ProgramStateRef state, |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 174 | const Expr *Size, |
| 175 | const Expr *Buf, |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 176 | const char *message = nullptr, |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 177 | bool WarnAboutSize = false) const { |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 178 | // This is a convenience override. |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 179 | return CheckBufferAccess(C, state, Size, Buf, nullptr, message, nullptr, |
Jordy Rose | 328deee | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 180 | WarnAboutSize); |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 181 | } |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 182 | ProgramStateRef CheckOverlap(CheckerContext &C, |
| 183 | ProgramStateRef state, |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 184 | const Expr *Size, |
| 185 | const Expr *First, |
| 186 | const Expr *Second) const; |
| 187 | void emitOverlapBug(CheckerContext &C, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 188 | ProgramStateRef state, |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 189 | const Stmt *First, |
| 190 | const Stmt *Second) const; |
| 191 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 192 | ProgramStateRef checkAdditionOverflow(CheckerContext &C, |
| 193 | ProgramStateRef state, |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 194 | NonLoc left, |
| 195 | NonLoc right) const; |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 196 | }; |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 197 | |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 198 | } //end anonymous namespace |
| 199 | |
Jordan Rose | 0c153cb | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 200 | REGISTER_MAP_WITH_PROGRAMSTATE(CStringLength, const MemRegion *, SVal) |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 201 | |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 202 | //===----------------------------------------------------------------------===// |
| 203 | // Individual checks and utility methods. |
| 204 | //===----------------------------------------------------------------------===// |
| 205 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 206 | std::pair<ProgramStateRef , ProgramStateRef > |
| 207 | CStringChecker::assumeZero(CheckerContext &C, ProgramStateRef state, SVal V, |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 208 | QualType Ty) { |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 209 | Optional<DefinedSVal> val = V.getAs<DefinedSVal>(); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 210 | if (!val) |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 211 | return std::pair<ProgramStateRef , ProgramStateRef >(state, state); |
Jordy Rose | 33c829a | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 212 | |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 213 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 214 | DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty); |
| 215 | return state->assume(svalBuilder.evalEQ(state, *val, zero)); |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 216 | } |
Jordy Rose | 33c829a | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 217 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 218 | ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C, |
| 219 | ProgramStateRef state, |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 220 | const Expr *S, SVal l) const { |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 221 | // If a previous check has failed, propagate the failure. |
| 222 | if (!state) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 223 | return nullptr; |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 224 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 225 | ProgramStateRef stateNull, stateNonNull; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 226 | std::tie(stateNull, stateNonNull) = assumeZero(C, state, l, S->getType()); |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 227 | |
| 228 | if (stateNull && !stateNonNull) { |
Anna Zaks | e0c7c27 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 229 | if (!Filter.CheckCStringNullArg) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 230 | return nullptr; |
Anna Zaks | e0c7c27 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 231 | |
Ted Kremenek | 750b7ac | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 232 | ExplodedNode *N = C.generateSink(stateNull); |
Jordy Rose | 33c829a | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 233 | if (!N) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 234 | return nullptr; |
Jordy Rose | 33c829a | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 235 | |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 236 | if (!BT_Null) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 237 | BT_Null.reset(new BuiltinBug( |
| 238 | Filter.CheckNameCStringNullArg, categories::UnixAPI, |
| 239 | "Null pointer argument in call to byte string function")); |
Jordy Rose | 33c829a | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 240 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 241 | SmallString<80> buf; |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 242 | llvm::raw_svector_ostream os(buf); |
| 243 | assert(CurrentFunctionDescription); |
| 244 | os << "Null pointer argument in call to " << CurrentFunctionDescription; |
| 245 | |
Jordy Rose | 33c829a | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 246 | // Generate a report for this bug. |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 247 | BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Null.get()); |
Anna Zaks | 3a6bdf8 | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 248 | BugReport *report = new BugReport(*BT, os.str(), N); |
Jordy Rose | 33c829a | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 249 | |
| 250 | report->addRange(S->getSourceRange()); |
Jordan Rose | a0f7d35 | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 251 | bugreporter::trackNullOrUndefValue(N, S, *report); |
Jordan Rose | e10d5a7 | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 252 | C.emitReport(report); |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 253 | return nullptr; |
Jordy Rose | 33c829a | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // From here on, assume that the value is non-null. |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 257 | assert(stateNonNull); |
| 258 | return stateNonNull; |
Jordy Rose | 33c829a | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 261 | // FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor? |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 262 | ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C, |
| 263 | ProgramStateRef state, |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 264 | const Expr *S, SVal l, |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 265 | const char *warningMsg) const { |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 266 | // If a previous check has failed, propagate the failure. |
| 267 | if (!state) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 268 | return nullptr; |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 269 | |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 270 | // Check for out of bound array element access. |
| 271 | const MemRegion *R = l.getAsRegion(); |
| 272 | if (!R) |
| 273 | return state; |
| 274 | |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 275 | const ElementRegion *ER = dyn_cast<ElementRegion>(R); |
| 276 | if (!ER) |
| 277 | return state; |
| 278 | |
Zhongxing Xu | 8de0a3d | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 279 | assert(ER->getValueType() == C.getASTContext().CharTy && |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 280 | "CheckLocation should only be called with char* ElementRegions"); |
| 281 | |
| 282 | // Get the size of the array. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 283 | const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion()); |
| 284 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | 455bd58 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 285 | SVal Extent = |
| 286 | svalBuilder.convertToArrayIndex(superReg->getExtent(svalBuilder)); |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 287 | DefinedOrUnknownSVal Size = Extent.castAs<DefinedOrUnknownSVal>(); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 288 | |
| 289 | // Get the index of the accessed element. |
David Blaikie | 87396b9 | 2013-02-21 22:23:56 +0000 | [diff] [blame] | 290 | DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>(); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 291 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 292 | ProgramStateRef StInBound = state->assumeInBound(Idx, Size, true); |
| 293 | ProgramStateRef StOutBound = state->assumeInBound(Idx, Size, false); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 294 | if (StOutBound && !StInBound) { |
Ted Kremenek | 750b7ac | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 295 | ExplodedNode *N = C.generateSink(StOutBound); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 296 | if (!N) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 297 | return nullptr; |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 298 | |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 299 | if (!BT_Bounds) { |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 300 | BT_Bounds.reset(new BuiltinBug( |
| 301 | Filter.CheckNameCStringOutOfBounds, "Out-of-bound array access", |
| 302 | "Byte string function accesses out-of-bound array element")); |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 303 | } |
| 304 | BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Bounds.get()); |
| 305 | |
| 306 | // Generate a report for this bug. |
Anna Zaks | 3a6bdf8 | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 307 | BugReport *report; |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 308 | if (warningMsg) { |
Anna Zaks | 3a6bdf8 | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 309 | report = new BugReport(*BT, warningMsg, N); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 310 | } else { |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 311 | assert(CurrentFunctionDescription); |
| 312 | assert(CurrentFunctionDescription[0] != '\0'); |
| 313 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 314 | SmallString<80> buf; |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 315 | llvm::raw_svector_ostream os(buf); |
Jordan Rose | 4938f27 | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 316 | os << toUppercase(CurrentFunctionDescription[0]) |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 317 | << &CurrentFunctionDescription[1] |
| 318 | << " accesses out-of-bound array element"; |
Anna Zaks | 3a6bdf8 | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 319 | report = new BugReport(*BT, os.str(), N); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 320 | } |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 321 | |
| 322 | // FIXME: It would be nice to eventually make this diagnostic more clear, |
| 323 | // e.g., by referencing the original declaration or by saying *why* this |
| 324 | // reference is outside the range. |
| 325 | |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 326 | report->addRange(S->getSourceRange()); |
Jordan Rose | e10d5a7 | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 327 | C.emitReport(report); |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 328 | return nullptr; |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // Array bound check succeeded. From this point forward the array bound |
| 332 | // should always succeed. |
| 333 | return StInBound; |
| 334 | } |
| 335 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 336 | ProgramStateRef CStringChecker::CheckBufferAccess(CheckerContext &C, |
| 337 | ProgramStateRef state, |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 338 | const Expr *Size, |
| 339 | const Expr *FirstBuf, |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 340 | const Expr *SecondBuf, |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 341 | const char *firstMessage, |
Jordy Rose | 328deee | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 342 | const char *secondMessage, |
| 343 | bool WarnAboutSize) const { |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 344 | // If a previous check has failed, propagate the failure. |
| 345 | if (!state) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 346 | return nullptr; |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 347 | |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 348 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | 455bd58 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 349 | ASTContext &Ctx = svalBuilder.getContext(); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 350 | const LocationContext *LCtx = C.getLocationContext(); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 351 | |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 352 | QualType sizeTy = Size->getType(); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 353 | QualType PtrTy = Ctx.getPointerType(Ctx.CharTy); |
| 354 | |
Jordy Rose | 33c829a | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 355 | // Check that the first buffer is non-null. |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 356 | SVal BufVal = state->getSVal(FirstBuf, LCtx); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 357 | state = checkNonNull(C, state, FirstBuf, BufVal); |
Jordy Rose | 33c829a | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 358 | if (!state) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 359 | return nullptr; |
Jordy Rose | 33c829a | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 360 | |
Anna Zaks | e0c7c27 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 361 | // If out-of-bounds checking is turned off, skip the rest. |
| 362 | if (!Filter.CheckCStringOutOfBounds) |
| 363 | return state; |
| 364 | |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 365 | // Get the access length and make sure it is known. |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 366 | // FIXME: This assumes the caller has already checked that the access length |
| 367 | // is positive. And that it's unsigned. |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 368 | SVal LengthVal = state->getSVal(Size, LCtx); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 369 | Optional<NonLoc> Length = LengthVal.getAs<NonLoc>(); |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 370 | if (!Length) |
| 371 | return state; |
| 372 | |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 373 | // Compute the offset of the last element to be accessed: size-1. |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 374 | NonLoc One = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>(); |
| 375 | NonLoc LastOffset = svalBuilder |
| 376 | .evalBinOpNN(state, BO_Sub, *Length, One, sizeTy).castAs<NonLoc>(); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 377 | |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 378 | // Check that the first buffer is sufficiently long. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 379 | SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType()); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 380 | if (Optional<Loc> BufLoc = BufStart.getAs<Loc>()) { |
Jordy Rose | 328deee | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 381 | const Expr *warningExpr = (WarnAboutSize ? Size : FirstBuf); |
| 382 | |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 383 | SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, |
| 384 | LastOffset, PtrTy); |
Jordy Rose | 328deee | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 385 | state = CheckLocation(C, state, warningExpr, BufEnd, firstMessage); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 386 | |
Jordy Rose | afdb053 | 2010-08-05 23:11:30 +0000 | [diff] [blame] | 387 | // If the buffer isn't large enough, abort. |
| 388 | if (!state) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 389 | return nullptr; |
Jordy Rose | afdb053 | 2010-08-05 23:11:30 +0000 | [diff] [blame] | 390 | } |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 391 | |
| 392 | // If there's a second buffer, check it as well. |
| 393 | if (SecondBuf) { |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 394 | BufVal = state->getSVal(SecondBuf, LCtx); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 395 | state = checkNonNull(C, state, SecondBuf, BufVal); |
Jordy Rose | 33c829a | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 396 | if (!state) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 397 | return nullptr; |
Jordy Rose | 33c829a | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 399 | BufStart = svalBuilder.evalCast(BufVal, PtrTy, SecondBuf->getType()); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 400 | if (Optional<Loc> BufLoc = BufStart.getAs<Loc>()) { |
Jordy Rose | 328deee | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 401 | const Expr *warningExpr = (WarnAboutSize ? Size : SecondBuf); |
| 402 | |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 403 | SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, |
| 404 | LastOffset, PtrTy); |
Jordy Rose | 328deee | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 405 | state = CheckLocation(C, state, warningExpr, BufEnd, secondMessage); |
Jordy Rose | afdb053 | 2010-08-05 23:11:30 +0000 | [diff] [blame] | 406 | } |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | // Large enough or not, return this state! |
| 410 | return state; |
| 411 | } |
| 412 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 413 | ProgramStateRef CStringChecker::CheckOverlap(CheckerContext &C, |
| 414 | ProgramStateRef state, |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 415 | const Expr *Size, |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 416 | const Expr *First, |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 417 | const Expr *Second) const { |
Anna Zaks | e0c7c27 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 418 | if (!Filter.CheckCStringBufferOverlap) |
| 419 | return state; |
| 420 | |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 421 | // Do a simple check for overlap: if the two arguments are from the same |
| 422 | // buffer, see if the end of the first is greater than the start of the second |
| 423 | // or vice versa. |
| 424 | |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 425 | // If a previous check has failed, propagate the failure. |
| 426 | if (!state) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 427 | return nullptr; |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 428 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 429 | ProgramStateRef stateTrue, stateFalse; |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 430 | |
| 431 | // Get the buffer values and make sure they're known locations. |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 432 | const LocationContext *LCtx = C.getLocationContext(); |
| 433 | SVal firstVal = state->getSVal(First, LCtx); |
| 434 | SVal secondVal = state->getSVal(Second, LCtx); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 435 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 436 | Optional<Loc> firstLoc = firstVal.getAs<Loc>(); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 437 | if (!firstLoc) |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 438 | return state; |
| 439 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 440 | Optional<Loc> secondLoc = secondVal.getAs<Loc>(); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 441 | if (!secondLoc) |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 442 | return state; |
| 443 | |
| 444 | // Are the two values the same? |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 445 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 446 | std::tie(stateTrue, stateFalse) = |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 447 | state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc)); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 448 | |
| 449 | if (stateTrue && !stateFalse) { |
| 450 | // 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] | 451 | emitOverlapBug(C, stateTrue, First, Second); |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 452 | return nullptr; |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Ted Kremenek | c5bea1e | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 455 | // assume the two expressions are not equal. |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 456 | assert(stateFalse); |
| 457 | state = stateFalse; |
| 458 | |
| 459 | // Which value comes first? |
Jordy Rose | 0585a61 | 2011-06-16 05:56:50 +0000 | [diff] [blame] | 460 | QualType cmpTy = svalBuilder.getConditionType(); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 461 | SVal reverse = svalBuilder.evalBinOpLL(state, BO_GT, |
| 462 | *firstLoc, *secondLoc, cmpTy); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 463 | Optional<DefinedOrUnknownSVal> reverseTest = |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 464 | reverse.getAs<DefinedOrUnknownSVal>(); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 465 | if (!reverseTest) |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 466 | return state; |
| 467 | |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 468 | std::tie(stateTrue, stateFalse) = state->assume(*reverseTest); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 469 | if (stateTrue) { |
| 470 | if (stateFalse) { |
| 471 | // If we don't know which one comes first, we can't perform this test. |
| 472 | return state; |
| 473 | } else { |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 474 | // Switch the values so that firstVal is before secondVal. |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 475 | std::swap(firstLoc, secondLoc); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 476 | |
| 477 | // Switch the Exprs as well, so that they still correspond. |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 478 | std::swap(First, Second); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | // Get the length, and make sure it too is known. |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 483 | SVal LengthVal = state->getSVal(Size, LCtx); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 484 | Optional<NonLoc> Length = LengthVal.getAs<NonLoc>(); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 485 | if (!Length) |
| 486 | return state; |
| 487 | |
| 488 | // Convert the first buffer's start address to char*. |
| 489 | // Bail out if the cast fails. |
Jordy Rose | 0585a61 | 2011-06-16 05:56:50 +0000 | [diff] [blame] | 490 | ASTContext &Ctx = svalBuilder.getContext(); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 491 | QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy); |
Jordy Rose | 455bd58 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 492 | SVal FirstStart = svalBuilder.evalCast(*firstLoc, CharPtrTy, |
| 493 | First->getType()); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 494 | Optional<Loc> FirstStartLoc = FirstStart.getAs<Loc>(); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 495 | if (!FirstStartLoc) |
| 496 | return state; |
| 497 | |
| 498 | // Compute the end of the first buffer. Bail out if THAT fails. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 499 | SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add, |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 500 | *FirstStartLoc, *Length, CharPtrTy); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 501 | Optional<Loc> FirstEndLoc = FirstEnd.getAs<Loc>(); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 502 | if (!FirstEndLoc) |
| 503 | return state; |
| 504 | |
| 505 | // 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] | 506 | SVal Overlap = svalBuilder.evalBinOpLL(state, BO_GT, |
| 507 | *FirstEndLoc, *secondLoc, cmpTy); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 508 | Optional<DefinedOrUnknownSVal> OverlapTest = |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 509 | Overlap.getAs<DefinedOrUnknownSVal>(); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 510 | if (!OverlapTest) |
| 511 | return state; |
| 512 | |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 513 | std::tie(stateTrue, stateFalse) = state->assume(*OverlapTest); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 514 | |
| 515 | if (stateTrue && !stateFalse) { |
| 516 | // Overlap! |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 517 | emitOverlapBug(C, stateTrue, First, Second); |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 518 | return nullptr; |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Ted Kremenek | c5bea1e | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 521 | // assume the two expressions don't overlap. |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 522 | assert(stateFalse); |
| 523 | return stateFalse; |
| 524 | } |
| 525 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 526 | void CStringChecker::emitOverlapBug(CheckerContext &C, ProgramStateRef state, |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 527 | const Stmt *First, const Stmt *Second) const { |
Ted Kremenek | 750b7ac | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 528 | ExplodedNode *N = C.generateSink(state); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 529 | if (!N) |
| 530 | return; |
| 531 | |
| 532 | if (!BT_Overlap) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 533 | BT_Overlap.reset(new BugType(Filter.CheckNameCStringBufferOverlap, |
| 534 | categories::UnixAPI, "Improper arguments")); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 535 | |
| 536 | // Generate a report for this bug. |
Anna Zaks | 3a6bdf8 | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 537 | BugReport *report = |
| 538 | new BugReport(*BT_Overlap, |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 539 | "Arguments must not be overlapping buffers", N); |
| 540 | report->addRange(First->getSourceRange()); |
| 541 | report->addRange(Second->getSourceRange()); |
| 542 | |
Jordan Rose | e10d5a7 | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 543 | C.emitReport(report); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 546 | ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C, |
| 547 | ProgramStateRef state, |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 548 | NonLoc left, |
| 549 | NonLoc right) const { |
Anna Zaks | e0c7c27 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 550 | // If out-of-bounds checking is turned off, skip the rest. |
| 551 | if (!Filter.CheckCStringOutOfBounds) |
| 552 | return state; |
| 553 | |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 554 | // If a previous check has failed, propagate the failure. |
| 555 | if (!state) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 556 | return nullptr; |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 557 | |
| 558 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 559 | BasicValueFactory &BVF = svalBuilder.getBasicValueFactory(); |
| 560 | |
| 561 | QualType sizeTy = svalBuilder.getContext().getSizeType(); |
| 562 | const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy); |
| 563 | NonLoc maxVal = svalBuilder.makeIntVal(maxValInt); |
| 564 | |
Anna Zaks | 7c96b7d | 2011-12-11 18:43:40 +0000 | [diff] [blame] | 565 | SVal maxMinusRight; |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 566 | if (right.getAs<nonloc::ConcreteInt>()) { |
Anna Zaks | 7c96b7d | 2011-12-11 18:43:40 +0000 | [diff] [blame] | 567 | maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right, |
| 568 | sizeTy); |
| 569 | } else { |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 570 | // Try switching the operands. (The order of these two assignments is |
| 571 | // important!) |
| 572 | maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left, |
| 573 | sizeTy); |
| 574 | left = right; |
| 575 | } |
| 576 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 577 | if (Optional<NonLoc> maxMinusRightNL = maxMinusRight.getAs<NonLoc>()) { |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 578 | QualType cmpTy = svalBuilder.getConditionType(); |
| 579 | // If left > max - right, we have an overflow. |
| 580 | SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left, |
| 581 | *maxMinusRightNL, cmpTy); |
| 582 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 583 | ProgramStateRef stateOverflow, stateOkay; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 584 | std::tie(stateOverflow, stateOkay) = |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 585 | state->assume(willOverflow.castAs<DefinedOrUnknownSVal>()); |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 586 | |
| 587 | if (stateOverflow && !stateOkay) { |
| 588 | // We have an overflow. Emit a bug report. |
| 589 | ExplodedNode *N = C.generateSink(stateOverflow); |
| 590 | if (!N) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 591 | return nullptr; |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 592 | |
| 593 | if (!BT_AdditionOverflow) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 594 | BT_AdditionOverflow.reset( |
| 595 | new BuiltinBug(Filter.CheckNameCStringOutOfBounds, "API", |
| 596 | "Sum of expressions causes overflow")); |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 597 | |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 598 | // This isn't a great error message, but this should never occur in real |
| 599 | // code anyway -- you'd have to create a buffer longer than a size_t can |
| 600 | // represent, which is sort of a contradiction. |
Jordy Rose | 789adbb | 2011-06-20 03:51:53 +0000 | [diff] [blame] | 601 | const char *warning = |
| 602 | "This expression will create a string whose length is too big to " |
| 603 | "be represented as a size_t"; |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 604 | |
| 605 | // Generate a report for this bug. |
Jordy Rose | 789adbb | 2011-06-20 03:51:53 +0000 | [diff] [blame] | 606 | BugReport *report = new BugReport(*BT_AdditionOverflow, warning, N); |
Jordan Rose | e10d5a7 | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 607 | C.emitReport(report); |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 608 | |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 609 | return nullptr; |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | // From now on, assume an overflow didn't occur. |
| 613 | assert(stateOkay); |
| 614 | state = stateOkay; |
| 615 | } |
| 616 | |
| 617 | return state; |
| 618 | } |
| 619 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 620 | ProgramStateRef CStringChecker::setCStringLength(ProgramStateRef state, |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 621 | const MemRegion *MR, |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 622 | SVal strLength) { |
| 623 | assert(!strLength.isUndef() && "Attempt to set an undefined string length"); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 624 | |
| 625 | MR = MR->StripCasts(); |
| 626 | |
| 627 | switch (MR->getKind()) { |
| 628 | case MemRegion::StringRegionKind: |
| 629 | // FIXME: This can happen if we strcpy() into a string region. This is |
| 630 | // undefined [C99 6.4.5p6], but we should still warn about it. |
| 631 | return state; |
| 632 | |
| 633 | case MemRegion::SymbolicRegionKind: |
| 634 | case MemRegion::AllocaRegionKind: |
| 635 | case MemRegion::VarRegionKind: |
| 636 | case MemRegion::FieldRegionKind: |
| 637 | case MemRegion::ObjCIvarRegionKind: |
Jordy Rose | 0e9fb28 | 2011-06-15 05:14:03 +0000 | [diff] [blame] | 638 | // These are the types we can currently track string lengths for. |
| 639 | break; |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 640 | |
| 641 | case MemRegion::ElementRegionKind: |
| 642 | // FIXME: Handle element regions by upper-bounding the parent region's |
| 643 | // string length. |
| 644 | return state; |
| 645 | |
| 646 | default: |
| 647 | // Other regions (mostly non-data) can't have a reliable C string length. |
| 648 | // For now, just ignore the change. |
| 649 | // FIXME: These are rare but not impossible. We should output some kind of |
| 650 | // warning for things like strcpy((char[]){'a', 0}, "b"); |
| 651 | return state; |
| 652 | } |
Jordy Rose | 0e9fb28 | 2011-06-15 05:14:03 +0000 | [diff] [blame] | 653 | |
| 654 | if (strLength.isUnknown()) |
| 655 | return state->remove<CStringLength>(MR); |
| 656 | |
| 657 | return state->set<CStringLength>(MR, strLength); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 660 | SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 661 | ProgramStateRef &state, |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 662 | const Expr *Ex, |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 663 | const MemRegion *MR, |
| 664 | bool hypothetical) { |
| 665 | if (!hypothetical) { |
| 666 | // If there's a recorded length, go ahead and return it. |
| 667 | const SVal *Recorded = state->get<CStringLength>(MR); |
| 668 | if (Recorded) |
| 669 | return *Recorded; |
| 670 | } |
Jordan Rose | 60619a6 | 2013-08-19 16:27:34 +0000 | [diff] [blame] | 671 | |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 672 | // Otherwise, get a new symbol and update the state. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 673 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 674 | QualType sizeTy = svalBuilder.getContext().getSizeType(); |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 675 | SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(), |
Ted Kremenek | d94854a | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 676 | MR, Ex, sizeTy, |
| 677 | C.blockCount()); |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 678 | |
Jordan Rose | 60619a6 | 2013-08-19 16:27:34 +0000 | [diff] [blame] | 679 | if (!hypothetical) { |
| 680 | if (Optional<NonLoc> strLn = strLength.getAs<NonLoc>()) { |
| 681 | // In case of unbounded calls strlen etc bound the range to SIZE_MAX/4 |
| 682 | BasicValueFactory &BVF = svalBuilder.getBasicValueFactory(); |
| 683 | const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy); |
| 684 | llvm::APSInt fourInt = APSIntType(maxValInt).getValue(4); |
| 685 | const llvm::APSInt *maxLengthInt = BVF.evalAPSInt(BO_Div, maxValInt, |
| 686 | fourInt); |
| 687 | NonLoc maxLength = svalBuilder.makeIntVal(*maxLengthInt); |
| 688 | SVal evalLength = svalBuilder.evalBinOpNN(state, BO_LE, *strLn, |
| 689 | maxLength, sizeTy); |
| 690 | state = state->assume(evalLength.castAs<DefinedOrUnknownSVal>(), true); |
| 691 | } |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 692 | state = state->set<CStringLength>(MR, strLength); |
Jordan Rose | 60619a6 | 2013-08-19 16:27:34 +0000 | [diff] [blame] | 693 | } |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 694 | |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 695 | return strLength; |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 698 | SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state, |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 699 | const Expr *Ex, SVal Buf, |
| 700 | bool hypothetical) const { |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 701 | const MemRegion *MR = Buf.getAsRegion(); |
| 702 | if (!MR) { |
| 703 | // If we can't get a region, see if it's something we /know/ isn't a |
| 704 | // C string. In the context of locations, the only time we can issue such |
| 705 | // a warning is for labels. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 706 | if (Optional<loc::GotoLabel> Label = Buf.getAs<loc::GotoLabel>()) { |
Anna Zaks | e0c7c27 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 707 | if (!Filter.CheckCStringNotNullTerm) |
| 708 | return UndefinedVal(); |
| 709 | |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 710 | if (ExplodedNode *N = C.addTransition(state)) { |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 711 | if (!BT_NotCString) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 712 | BT_NotCString.reset(new BuiltinBug( |
| 713 | Filter.CheckNameCStringNotNullTerm, categories::UnixAPI, |
| 714 | "Argument is not a null-terminated string.")); |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 715 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 716 | SmallString<120> buf; |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 717 | llvm::raw_svector_ostream os(buf); |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 718 | assert(CurrentFunctionDescription); |
| 719 | os << "Argument to " << CurrentFunctionDescription |
| 720 | << " is the address of the label '" << Label->getLabel()->getName() |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 721 | << "', which is not a null-terminated string"; |
| 722 | |
| 723 | // Generate a report for this bug. |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 724 | BugReport *report = new BugReport(*BT_NotCString, os.str(), N); |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 725 | |
| 726 | report->addRange(Ex->getSourceRange()); |
Jordan Rose | e10d5a7 | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 727 | C.emitReport(report); |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 728 | } |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 729 | return UndefinedVal(); |
Anna Zaks | e0c7c27 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 730 | |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 733 | // If it's not a region and not a label, give up. |
| 734 | return UnknownVal(); |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 737 | // If we have a region, strip casts from it and see if we can figure out |
| 738 | // its length. For anything we can't figure out, just return UnknownVal. |
| 739 | MR = MR->StripCasts(); |
| 740 | |
| 741 | switch (MR->getKind()) { |
| 742 | case MemRegion::StringRegionKind: { |
| 743 | // Modifying the contents of string regions is undefined [C99 6.4.5p6], |
| 744 | // 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] | 745 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 746 | QualType sizeTy = svalBuilder.getContext().getSizeType(); |
| 747 | const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral(); |
| 748 | return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 749 | } |
| 750 | case MemRegion::SymbolicRegionKind: |
| 751 | case MemRegion::AllocaRegionKind: |
| 752 | case MemRegion::VarRegionKind: |
| 753 | case MemRegion::FieldRegionKind: |
| 754 | case MemRegion::ObjCIvarRegionKind: |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 755 | return getCStringLengthForRegion(C, state, Ex, MR, hypothetical); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 756 | case MemRegion::CompoundLiteralRegionKind: |
| 757 | // FIXME: Can we track this? Is it necessary? |
| 758 | return UnknownVal(); |
| 759 | case MemRegion::ElementRegionKind: |
| 760 | // FIXME: How can we handle this? It's not good enough to subtract the |
| 761 | // offset from the base string length; consider "123\x00567" and &a[5]. |
| 762 | return UnknownVal(); |
| 763 | default: |
| 764 | // Other regions (mostly non-data) can't have a reliable C string length. |
| 765 | // In this case, an error is emitted and UndefinedVal is returned. |
| 766 | // The caller should always be prepared to handle this case. |
Anna Zaks | e0c7c27 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 767 | if (!Filter.CheckCStringNotNullTerm) |
| 768 | return UndefinedVal(); |
| 769 | |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 770 | if (ExplodedNode *N = C.addTransition(state)) { |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 771 | if (!BT_NotCString) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 772 | BT_NotCString.reset(new BuiltinBug( |
| 773 | Filter.CheckNameCStringNotNullTerm, categories::UnixAPI, |
| 774 | "Argument is not a null-terminated string.")); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 775 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 776 | SmallString<120> buf; |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 777 | llvm::raw_svector_ostream os(buf); |
| 778 | |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 779 | assert(CurrentFunctionDescription); |
| 780 | os << "Argument to " << CurrentFunctionDescription << " is "; |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 781 | |
| 782 | if (SummarizeRegion(os, C.getASTContext(), MR)) |
| 783 | os << ", which is not a null-terminated string"; |
| 784 | else |
| 785 | os << "not a null-terminated string"; |
| 786 | |
| 787 | // Generate a report for this bug. |
Anna Zaks | 3a6bdf8 | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 788 | BugReport *report = new BugReport(*BT_NotCString, |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 789 | os.str(), N); |
| 790 | |
| 791 | report->addRange(Ex->getSourceRange()); |
Jordan Rose | e10d5a7 | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 792 | C.emitReport(report); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | return UndefinedVal(); |
| 796 | } |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 799 | const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 800 | ProgramStateRef &state, const Expr *expr, SVal val) const { |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 801 | |
| 802 | // Get the memory region pointed to by the val. |
| 803 | const MemRegion *bufRegion = val.getAsRegion(); |
| 804 | if (!bufRegion) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 805 | return nullptr; |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 806 | |
| 807 | // Strip casts off the memory region. |
| 808 | bufRegion = bufRegion->StripCasts(); |
| 809 | |
| 810 | // Cast the memory region to a string region. |
| 811 | const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion); |
| 812 | if (!strRegion) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 813 | return nullptr; |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 814 | |
| 815 | // Return the actual string in the string region. |
| 816 | return strRegion->getStringLiteral(); |
| 817 | } |
| 818 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 819 | ProgramStateRef CStringChecker::InvalidateBuffer(CheckerContext &C, |
Anton Yartsev | 968c60a | 2013-11-17 09:18:48 +0000 | [diff] [blame] | 820 | ProgramStateRef state, |
| 821 | const Expr *E, SVal V, |
| 822 | bool IsSourceBuffer) { |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 823 | Optional<Loc> L = V.getAs<Loc>(); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 824 | if (!L) |
| 825 | return state; |
| 826 | |
| 827 | // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes |
| 828 | // some assumptions about the value that CFRefCount can't. Even so, it should |
| 829 | // probably be refactored. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 830 | if (Optional<loc::MemRegionVal> MR = L->getAs<loc::MemRegionVal>()) { |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 831 | const MemRegion *R = MR->getRegion()->StripCasts(); |
| 832 | |
| 833 | // Are we dealing with an ElementRegion? If so, we should be invalidating |
| 834 | // the super-region. |
| 835 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
| 836 | R = ER->getSuperRegion(); |
| 837 | // FIXME: What about layers of ElementRegions? |
| 838 | } |
| 839 | |
| 840 | // Invalidate this region. |
Ted Kremenek | d519cae | 2012-02-17 23:13:45 +0000 | [diff] [blame] | 841 | const LocationContext *LCtx = C.getPredecessor()->getLocationContext(); |
Anton Yartsev | 968c60a | 2013-11-17 09:18:48 +0000 | [diff] [blame] | 842 | |
| 843 | bool CausesPointerEscape = false; |
| 844 | RegionAndSymbolInvalidationTraits ITraits; |
| 845 | // Invalidate and escape only indirect regions accessible through the source |
| 846 | // buffer. |
| 847 | if (IsSourceBuffer) { |
| 848 | ITraits.setTrait(R, |
| 849 | RegionAndSymbolInvalidationTraits::TK_PreserveContents); |
| 850 | ITraits.setTrait(R, RegionAndSymbolInvalidationTraits::TK_SuppressEscape); |
| 851 | CausesPointerEscape = true; |
| 852 | } |
| 853 | |
| 854 | return state->invalidateRegions(R, E, C.blockCount(), LCtx, |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 855 | CausesPointerEscape, nullptr, nullptr, |
| 856 | &ITraits); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | // If we have a non-region value by chance, just remove the binding. |
| 860 | // FIXME: is this necessary or correct? This handles the non-Region |
| 861 | // cases. Is it ever valid to store to these? |
Ted Kremenek | 6269888 | 2012-08-22 06:37:46 +0000 | [diff] [blame] | 862 | return state->killBinding(*L); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 865 | bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx, |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 866 | const MemRegion *MR) { |
Ted Kremenek | 8df44b26 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 867 | const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(MR); |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 868 | |
Jordy Rose | add45b7 | 2011-08-12 21:41:07 +0000 | [diff] [blame] | 869 | switch (MR->getKind()) { |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 870 | case MemRegion::FunctionTextRegionKind: { |
Anna Zaks | 4278234 | 2012-09-17 19:13:56 +0000 | [diff] [blame] | 871 | const NamedDecl *FD = cast<FunctionTextRegion>(MR)->getDecl(); |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 872 | if (FD) |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 873 | os << "the address of the function '" << *FD << '\''; |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 874 | else |
| 875 | os << "the address of a function"; |
| 876 | return true; |
| 877 | } |
| 878 | case MemRegion::BlockTextRegionKind: |
| 879 | os << "block text"; |
| 880 | return true; |
| 881 | case MemRegion::BlockDataRegionKind: |
| 882 | os << "a block"; |
| 883 | return true; |
| 884 | case MemRegion::CXXThisRegionKind: |
Zhongxing Xu | 0320716 | 2010-11-26 08:52:48 +0000 | [diff] [blame] | 885 | case MemRegion::CXXTempObjectRegionKind: |
Ted Kremenek | 8df44b26 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 886 | os << "a C++ temp object of type " << TVR->getValueType().getAsString(); |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 887 | return true; |
| 888 | case MemRegion::VarRegionKind: |
Ted Kremenek | 8df44b26 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 889 | os << "a variable of type" << TVR->getValueType().getAsString(); |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 890 | return true; |
| 891 | case MemRegion::FieldRegionKind: |
Ted Kremenek | 8df44b26 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 892 | os << "a field of type " << TVR->getValueType().getAsString(); |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 893 | return true; |
| 894 | case MemRegion::ObjCIvarRegionKind: |
Ted Kremenek | 8df44b26 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 895 | os << "an instance variable of type " << TVR->getValueType().getAsString(); |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 896 | return true; |
| 897 | default: |
| 898 | return false; |
| 899 | } |
| 900 | } |
| 901 | |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 902 | //===----------------------------------------------------------------------===// |
Ted Kremenek | dc89142 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 903 | // evaluation of individual function calls. |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 904 | //===----------------------------------------------------------------------===// |
| 905 | |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 906 | void CStringChecker::evalCopyCommon(CheckerContext &C, |
| 907 | const CallExpr *CE, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 908 | ProgramStateRef state, |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 909 | const Expr *Size, const Expr *Dest, |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 910 | const Expr *Source, bool Restricted, |
| 911 | bool IsMempcpy) const { |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 912 | CurrentFunctionDescription = "memory copy function"; |
| 913 | |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 914 | // See if the size argument is zero. |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 915 | const LocationContext *LCtx = C.getLocationContext(); |
| 916 | SVal sizeVal = state->getSVal(Size, LCtx); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 917 | QualType sizeTy = Size->getType(); |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 918 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 919 | ProgramStateRef stateZeroSize, stateNonZeroSize; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 920 | std::tie(stateZeroSize, stateNonZeroSize) = |
Jordy Rose | 455bd58 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 921 | assumeZero(C, state, sizeVal, sizeTy); |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 922 | |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 923 | // Get the value of the Dest. |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 924 | SVal destVal = state->getSVal(Dest, LCtx); |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 925 | |
| 926 | // If the size is zero, there won't be any actual memory access, so |
| 927 | // just bind the return value to the destination buffer and return. |
Anna Zaks | b3b56bb | 2012-05-03 18:21:28 +0000 | [diff] [blame] | 928 | if (stateZeroSize && !stateNonZeroSize) { |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 929 | stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, destVal); |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 930 | C.addTransition(stateZeroSize); |
Anna Zaks | b3b56bb | 2012-05-03 18:21:28 +0000 | [diff] [blame] | 931 | return; |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 932 | } |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 933 | |
| 934 | // 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] | 935 | if (stateNonZeroSize) { |
Jordy Rose | 63b84be | 2011-06-04 00:04:22 +0000 | [diff] [blame] | 936 | state = stateNonZeroSize; |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 937 | |
| 938 | // Ensure the destination is not null. If it is NULL there will be a |
| 939 | // NULL pointer dereference. |
| 940 | state = checkNonNull(C, state, Dest, destVal); |
| 941 | if (!state) |
| 942 | return; |
| 943 | |
| 944 | // Get the value of the Src. |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 945 | SVal srcVal = state->getSVal(Source, LCtx); |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 946 | |
| 947 | // Ensure the source is not null. If it is NULL there will be a |
| 948 | // NULL pointer dereference. |
| 949 | state = checkNonNull(C, state, Source, srcVal); |
| 950 | if (!state) |
| 951 | return; |
| 952 | |
Jordy Rose | fb5e8c2 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 953 | // Ensure the accesses are valid and that the buffers do not overlap. |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 954 | const char * const writeWarning = |
| 955 | "Memory copy function overflows destination buffer"; |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 956 | state = CheckBufferAccess(C, state, Size, Dest, Source, |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 957 | writeWarning, /* sourceWarning = */ nullptr); |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 958 | if (Restricted) |
| 959 | state = CheckOverlap(C, state, Size, Dest, Source); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 960 | |
Jordy Rose | fb5e8c2 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 961 | if (!state) |
| 962 | return; |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 963 | |
Jordy Rose | fb5e8c2 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 964 | // If this is mempcpy, get the byte after the last byte copied and |
| 965 | // bind the expr. |
| 966 | if (IsMempcpy) { |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 967 | loc::MemRegionVal destRegVal = destVal.castAs<loc::MemRegionVal>(); |
Jordy Rose | fb5e8c2 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 968 | |
| 969 | // Get the length to copy. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 970 | if (Optional<NonLoc> lenValNonLoc = sizeVal.getAs<NonLoc>()) { |
Jordy Rose | fb5e8c2 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 971 | // Get the byte after the last byte copied. |
| 972 | SVal lastElement = C.getSValBuilder().evalBinOpLN(state, BO_Add, |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 973 | destRegVal, |
Jordy Rose | fb5e8c2 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 974 | *lenValNonLoc, |
| 975 | Dest->getType()); |
| 976 | |
| 977 | // The byte after the last byte copied is the return value. |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 978 | state = state->BindExpr(CE, LCtx, lastElement); |
Jordy Rose | 097c539 | 2011-06-04 01:47:27 +0000 | [diff] [blame] | 979 | } else { |
Jordy Rose | fb5e8c2 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 980 | // If we don't know how much we copied, we can at least |
| 981 | // conjure a return value for later. |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 982 | SVal result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx, |
Ted Kremenek | d94854a | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 983 | C.blockCount()); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 984 | state = state->BindExpr(CE, LCtx, result); |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Jordy Rose | fb5e8c2 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 987 | } else { |
| 988 | // All other copies return the destination buffer. |
| 989 | // (Well, bcopy() has a void return type, but this won't hurt.) |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 990 | state = state->BindExpr(CE, LCtx, destVal); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 991 | } |
Jordy Rose | fb5e8c2 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 992 | |
Anton Yartsev | 968c60a | 2013-11-17 09:18:48 +0000 | [diff] [blame] | 993 | // Invalidate the destination (regular invalidation without pointer-escaping |
| 994 | // the address of the top-level region). |
Jordy Rose | fb5e8c2 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 995 | // FIXME: Even if we can't perfectly model the copy, we should see if we |
| 996 | // can use LazyCompoundVals to copy the source values into the destination. |
| 997 | // This would probably remove any existing bindings past the end of the |
| 998 | // copied region, but that's still an improvement over blank invalidation. |
Anton Yartsev | 968c60a | 2013-11-17 09:18:48 +0000 | [diff] [blame] | 999 | state = InvalidateBuffer(C, state, Dest, C.getSVal(Dest), |
| 1000 | /*IsSourceBuffer*/false); |
| 1001 | |
| 1002 | // Invalidate the source (const-invalidation without const-pointer-escaping |
| 1003 | // the address of the top-level region). |
| 1004 | state = InvalidateBuffer(C, state, Source, C.getSVal(Source), |
| 1005 | /*IsSourceBuffer*/true); |
| 1006 | |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1007 | C.addTransition(state); |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1012 | void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1013 | if (CE->getNumArgs() < 3) |
| 1014 | return; |
| 1015 | |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1016 | // void *memcpy(void *restrict dst, const void *restrict src, size_t n); |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1017 | // The return value is the address of the destination buffer. |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1018 | const Expr *Dest = CE->getArg(0); |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1019 | ProgramStateRef state = C.getState(); |
Jordy Rose | 097c539 | 2011-06-04 01:47:27 +0000 | [diff] [blame] | 1020 | |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 1021 | evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true); |
| 1022 | } |
| 1023 | |
| 1024 | void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1025 | if (CE->getNumArgs() < 3) |
| 1026 | return; |
| 1027 | |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 1028 | // void *mempcpy(void *restrict dst, const void *restrict src, size_t n); |
| 1029 | // The return value is a pointer to the byte following the last written byte. |
| 1030 | const Expr *Dest = CE->getArg(0); |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1031 | ProgramStateRef state = C.getState(); |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 1032 | |
| 1033 | 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] | 1034 | } |
| 1035 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1036 | void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1037 | if (CE->getNumArgs() < 3) |
| 1038 | return; |
| 1039 | |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1040 | // void *memmove(void *dst, const void *src, size_t n); |
| 1041 | // The return value is the address of the destination buffer. |
| 1042 | const Expr *Dest = CE->getArg(0); |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1043 | ProgramStateRef state = C.getState(); |
Jordy Rose | 097c539 | 2011-06-04 01:47:27 +0000 | [diff] [blame] | 1044 | |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 1045 | evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1)); |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1048 | void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1049 | if (CE->getNumArgs() < 3) |
| 1050 | return; |
| 1051 | |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1052 | // void bcopy(const void *src, void *dst, size_t n); |
Lenny Maiorani | 79d7414 | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 1053 | evalCopyCommon(C, CE, C.getState(), |
| 1054 | CE->getArg(2), CE->getArg(1), CE->getArg(0)); |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1057 | void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1058 | if (CE->getNumArgs() < 3) |
| 1059 | return; |
| 1060 | |
Jordy Rose | 65136fb | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1061 | // int memcmp(const void *s1, const void *s2, size_t n); |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 1062 | CurrentFunctionDescription = "memory comparison function"; |
| 1063 | |
Jordy Rose | 65136fb | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1064 | const Expr *Left = CE->getArg(0); |
| 1065 | const Expr *Right = CE->getArg(1); |
| 1066 | const Expr *Size = CE->getArg(2); |
| 1067 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1068 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1069 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | 65136fb | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1070 | |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1071 | // See if the size argument is zero. |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1072 | const LocationContext *LCtx = C.getLocationContext(); |
| 1073 | SVal sizeVal = state->getSVal(Size, LCtx); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1074 | QualType sizeTy = Size->getType(); |
Jordy Rose | 65136fb | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1075 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1076 | ProgramStateRef stateZeroSize, stateNonZeroSize; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1077 | std::tie(stateZeroSize, stateNonZeroSize) = |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1078 | assumeZero(C, state, sizeVal, sizeTy); |
Jordy Rose | 65136fb | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1079 | |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1080 | // If the size can be zero, the result will be 0 in that case, and we don't |
| 1081 | // have to check either of the buffers. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1082 | if (stateZeroSize) { |
| 1083 | state = stateZeroSize; |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1084 | state = state->BindExpr(CE, LCtx, |
| 1085 | svalBuilder.makeZeroVal(CE->getType())); |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1086 | C.addTransition(state); |
Jordy Rose | 65136fb | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1089 | // 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] | 1090 | if (stateNonZeroSize) { |
| 1091 | state = stateNonZeroSize; |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1092 | // If we know the two buffers are the same, we know the result is 0. |
| 1093 | // First, get the two buffers' addresses. Another checker will have already |
| 1094 | // made sure they're not undefined. |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1095 | DefinedOrUnknownSVal LV = |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1096 | state->getSVal(Left, LCtx).castAs<DefinedOrUnknownSVal>(); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1097 | DefinedOrUnknownSVal RV = |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1098 | state->getSVal(Right, LCtx).castAs<DefinedOrUnknownSVal>(); |
Jordy Rose | 65136fb | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1099 | |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1100 | // See if they are the same. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1101 | DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV); |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1102 | ProgramStateRef StSameBuf, StNotSameBuf; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1103 | std::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf); |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1104 | |
Jordy Rose | 455bd58 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 1105 | // If the two arguments might be the same buffer, we know the result is 0, |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1106 | // and we only need to check one size. |
| 1107 | if (StSameBuf) { |
| 1108 | state = StSameBuf; |
| 1109 | state = CheckBufferAccess(C, state, Size, Left); |
| 1110 | if (state) { |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1111 | state = StSameBuf->BindExpr(CE, LCtx, |
| 1112 | svalBuilder.makeZeroVal(CE->getType())); |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1113 | C.addTransition(state); |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | // If the two arguments might be different buffers, we have to check the |
| 1118 | // size of both of them. |
| 1119 | if (StNotSameBuf) { |
| 1120 | state = StNotSameBuf; |
| 1121 | state = CheckBufferAccess(C, state, Size, Left, Right); |
| 1122 | if (state) { |
| 1123 | // The return value is the comparison result, which we don't know. |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 1124 | SVal CmpV = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx, |
| 1125 | C.blockCount()); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1126 | state = state->BindExpr(CE, LCtx, CmpV); |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1127 | C.addTransition(state); |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | } |
Jordy Rose | 65136fb | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1133 | void CStringChecker::evalstrLength(CheckerContext &C, |
| 1134 | const CallExpr *CE) const { |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1135 | if (CE->getNumArgs() < 1) |
| 1136 | return; |
| 1137 | |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1138 | // size_t strlen(const char *s); |
Ted Kremenek | 280a01f | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1139 | evalstrLengthCommon(C, CE, /* IsStrnlen = */ false); |
| 1140 | } |
| 1141 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1142 | void CStringChecker::evalstrnLength(CheckerContext &C, |
| 1143 | const CallExpr *CE) const { |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1144 | if (CE->getNumArgs() < 2) |
| 1145 | return; |
| 1146 | |
Ted Kremenek | 280a01f | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1147 | // size_t strnlen(const char *s, size_t maxlen); |
| 1148 | evalstrLengthCommon(C, CE, /* IsStrnlen = */ true); |
| 1149 | } |
| 1150 | |
| 1151 | void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE, |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1152 | bool IsStrnlen) const { |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 1153 | CurrentFunctionDescription = "string length function"; |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1154 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1155 | const LocationContext *LCtx = C.getLocationContext(); |
Jordy Rose | d359289 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1156 | |
| 1157 | if (IsStrnlen) { |
| 1158 | const Expr *maxlenExpr = CE->getArg(1); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1159 | SVal maxlenVal = state->getSVal(maxlenExpr, LCtx); |
Jordy Rose | d359289 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1160 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1161 | ProgramStateRef stateZeroSize, stateNonZeroSize; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1162 | std::tie(stateZeroSize, stateNonZeroSize) = |
Jordy Rose | d359289 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1163 | assumeZero(C, state, maxlenVal, maxlenExpr->getType()); |
| 1164 | |
| 1165 | // If the size can be zero, the result will be 0 in that case, and we don't |
| 1166 | // have to check the string itself. |
| 1167 | if (stateZeroSize) { |
| 1168 | SVal zero = C.getSValBuilder().makeZeroVal(CE->getType()); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1169 | stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, zero); |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1170 | C.addTransition(stateZeroSize); |
Jordy Rose | d359289 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | // If the size is GUARANTEED to be zero, we're done! |
| 1174 | if (!stateNonZeroSize) |
| 1175 | return; |
| 1176 | |
| 1177 | // Otherwise, record the assumption that the size is nonzero. |
| 1178 | state = stateNonZeroSize; |
| 1179 | } |
| 1180 | |
| 1181 | // Check that the string argument is non-null. |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1182 | const Expr *Arg = CE->getArg(0); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1183 | SVal ArgVal = state->getSVal(Arg, LCtx); |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1184 | |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1185 | state = checkNonNull(C, state, Arg, ArgVal); |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1186 | |
Jordy Rose | 45d8c12 | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1187 | if (!state) |
| 1188 | return; |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1189 | |
Jordy Rose | 45d8c12 | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1190 | SVal strLength = getCStringLength(C, state, Arg, ArgVal); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1191 | |
Jordy Rose | 45d8c12 | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1192 | // If the argument isn't a valid C string, there's no valid state to |
| 1193 | // transition to. |
| 1194 | if (strLength.isUndef()) |
| 1195 | return; |
Jordy Rose | d359289 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1196 | |
Jordy Rose | 45d8c12 | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1197 | DefinedOrUnknownSVal result = UnknownVal(); |
Jordy Rose | d359289 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1198 | |
Jordy Rose | 45d8c12 | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1199 | // If the check is for strnlen() then bind the return value to no more than |
| 1200 | // the maxlen value. |
| 1201 | if (IsStrnlen) { |
Jordy Rose | 0585a61 | 2011-06-16 05:56:50 +0000 | [diff] [blame] | 1202 | QualType cmpTy = C.getSValBuilder().getConditionType(); |
Jordy Rose | d359289 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1203 | |
Jordy Rose | 45d8c12 | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1204 | // It's a little unfortunate to be getting this again, |
| 1205 | // but it's not that expensive... |
| 1206 | const Expr *maxlenExpr = CE->getArg(1); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1207 | SVal maxlenVal = state->getSVal(maxlenExpr, LCtx); |
Ted Kremenek | 280a01f | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1208 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1209 | Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>(); |
| 1210 | Optional<NonLoc> maxlenValNL = maxlenVal.getAs<NonLoc>(); |
Ted Kremenek | 280a01f | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1211 | |
Jordy Rose | 45d8c12 | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1212 | if (strLengthNL && maxlenValNL) { |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1213 | ProgramStateRef stateStringTooLong, stateStringNotTooLong; |
Jordy Rose | d359289 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1214 | |
Jordy Rose | 45d8c12 | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1215 | // Check if the strLength is greater than the maxlen. |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1216 | std::tie(stateStringTooLong, stateStringNotTooLong) = state->assume( |
| 1217 | C.getSValBuilder() |
| 1218 | .evalBinOpNN(state, BO_GT, *strLengthNL, *maxlenValNL, cmpTy) |
| 1219 | .castAs<DefinedOrUnknownSVal>()); |
Jordy Rose | d359289 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1220 | |
Jordy Rose | 45d8c12 | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1221 | if (stateStringTooLong && !stateStringNotTooLong) { |
| 1222 | // If the string is longer than maxlen, return maxlen. |
| 1223 | result = *maxlenValNL; |
| 1224 | } else if (stateStringNotTooLong && !stateStringTooLong) { |
| 1225 | // If the string is shorter than maxlen, return its length. |
| 1226 | result = *strLengthNL; |
Ted Kremenek | 280a01f | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1227 | } |
| 1228 | } |
| 1229 | |
Jordy Rose | 45d8c12 | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1230 | if (result.isUnknown()) { |
| 1231 | // If we don't have enough information for a comparison, there's |
| 1232 | // no guarantee the full string length will actually be returned. |
| 1233 | // All we know is the return value is the min of the string length |
| 1234 | // and the limit. This is better than nothing. |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 1235 | result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx, |
| 1236 | C.blockCount()); |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1237 | NonLoc resultNL = result.castAs<NonLoc>(); |
Jordy Rose | 45d8c12 | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1238 | |
| 1239 | if (strLengthNL) { |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1240 | state = state->assume(C.getSValBuilder().evalBinOpNN( |
| 1241 | state, BO_LE, resultNL, *strLengthNL, cmpTy) |
| 1242 | .castAs<DefinedOrUnknownSVal>(), true); |
Jordy Rose | 45d8c12 | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | if (maxlenValNL) { |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1246 | state = state->assume(C.getSValBuilder().evalBinOpNN( |
| 1247 | state, BO_LE, resultNL, *maxlenValNL, cmpTy) |
| 1248 | .castAs<DefinedOrUnknownSVal>(), true); |
Jordy Rose | 45d8c12 | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | } else { |
| 1253 | // This is a plain strlen(), not strnlen(). |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1254 | result = strLength.castAs<DefinedOrUnknownSVal>(); |
Jordy Rose | 45d8c12 | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1255 | |
| 1256 | // If we don't know the length of the string, conjure a return |
| 1257 | // value, so it can be used in constraints, at least. |
| 1258 | if (result.isUnknown()) { |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 1259 | result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx, |
| 1260 | C.blockCount()); |
Jordy Rose | 45d8c12 | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1261 | } |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1262 | } |
Jordy Rose | 45d8c12 | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1263 | |
| 1264 | // Bind the return value. |
| 1265 | assert(!result.isUnknown() && "Should have conjured a value by now"); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1266 | state = state->BindExpr(CE, LCtx, result); |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1267 | C.addTransition(state); |
Jordy Rose | b052e8f | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1270 | void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1271 | if (CE->getNumArgs() < 2) |
| 1272 | return; |
| 1273 | |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1274 | // char *strcpy(char *restrict dst, const char *restrict src); |
Lenny Maiorani | 467dbd5 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1275 | evalStrcpyCommon(C, CE, |
| 1276 | /* returnEnd = */ false, |
| 1277 | /* isBounded = */ false, |
| 1278 | /* isAppending = */ false); |
Ted Kremenek | fb1a79a | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1281 | void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1282 | if (CE->getNumArgs() < 3) |
| 1283 | return; |
| 1284 | |
Jordy Rose | 4451cd4 | 2011-06-04 00:05:23 +0000 | [diff] [blame] | 1285 | // char *strncpy(char *restrict dst, const char *restrict src, size_t n); |
Lenny Maiorani | 467dbd5 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1286 | evalStrcpyCommon(C, CE, |
| 1287 | /* returnEnd = */ false, |
| 1288 | /* isBounded = */ true, |
| 1289 | /* isAppending = */ false); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1292 | void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1293 | if (CE->getNumArgs() < 2) |
| 1294 | return; |
| 1295 | |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1296 | // char *stpcpy(char *restrict dst, const char *restrict src); |
Lenny Maiorani | 467dbd5 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1297 | evalStrcpyCommon(C, CE, |
| 1298 | /* returnEnd = */ true, |
| 1299 | /* isBounded = */ false, |
| 1300 | /* isAppending = */ false); |
| 1301 | } |
| 1302 | |
| 1303 | void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1304 | if (CE->getNumArgs() < 2) |
| 1305 | return; |
| 1306 | |
Lenny Maiorani | 467dbd5 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1307 | //char *strcat(char *restrict s1, const char *restrict s2); |
| 1308 | evalStrcpyCommon(C, CE, |
| 1309 | /* returnEnd = */ false, |
| 1310 | /* isBounded = */ false, |
| 1311 | /* isAppending = */ true); |
| 1312 | } |
| 1313 | |
| 1314 | void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1315 | if (CE->getNumArgs() < 3) |
| 1316 | return; |
| 1317 | |
Lenny Maiorani | 467dbd5 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1318 | //char *strncat(char *restrict s1, const char *restrict s2, size_t n); |
| 1319 | evalStrcpyCommon(C, CE, |
| 1320 | /* returnEnd = */ false, |
| 1321 | /* isBounded = */ true, |
| 1322 | /* isAppending = */ true); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Ted Kremenek | dc89142 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1325 | void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, |
Lenny Maiorani | 467dbd5 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1326 | bool returnEnd, bool isBounded, |
| 1327 | bool isAppending) const { |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 1328 | CurrentFunctionDescription = "string copy function"; |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1329 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1330 | const LocationContext *LCtx = C.getLocationContext(); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1331 | |
Lenny Maiorani | 467dbd5 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1332 | // Check that the destination is non-null. |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1333 | const Expr *Dst = CE->getArg(0); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1334 | SVal DstVal = state->getSVal(Dst, LCtx); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1335 | |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1336 | state = checkNonNull(C, state, Dst, DstVal); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1337 | if (!state) |
| 1338 | return; |
| 1339 | |
| 1340 | // Check that the source is non-null. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1341 | const Expr *srcExpr = CE->getArg(1); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1342 | SVal srcVal = state->getSVal(srcExpr, LCtx); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1343 | state = checkNonNull(C, state, srcExpr, srcVal); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1344 | if (!state) |
| 1345 | return; |
| 1346 | |
| 1347 | // Get the string length of the source. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1348 | SVal strLength = getCStringLength(C, state, srcExpr, srcVal); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1349 | |
| 1350 | // If the source isn't a valid C string, give up. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1351 | if (strLength.isUndef()) |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1352 | return; |
| 1353 | |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1354 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 1355 | QualType cmpTy = svalBuilder.getConditionType(); |
Jordy Rose | b41f7c5 | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1356 | QualType sizeTy = svalBuilder.getContext().getSizeType(); |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1357 | |
Jordy Rose | b41f7c5 | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1358 | // These two values allow checking two kinds of errors: |
| 1359 | // - actual overflows caused by a source that doesn't fit in the destination |
| 1360 | // - potential overflows caused by a bound that could exceed the destination |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1361 | SVal amountCopied = UnknownVal(); |
Jordy Rose | b41f7c5 | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1362 | SVal maxLastElementIndex = UnknownVal(); |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 1363 | const char *boundWarning = nullptr; |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1364 | |
Lenny Maiorani | 467dbd5 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1365 | // If the function is strncpy, strncat, etc... it is bounded. |
| 1366 | if (isBounded) { |
| 1367 | // Get the max number of characters to copy. |
Ted Kremenek | fb1a79a | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1368 | const Expr *lenExpr = CE->getArg(2); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1369 | SVal lenVal = state->getSVal(lenExpr, LCtx); |
Ted Kremenek | fb1a79a | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1370 | |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1371 | // Protect against misdeclared strncpy(). |
Jordy Rose | b41f7c5 | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1372 | lenVal = svalBuilder.evalCast(lenVal, sizeTy, lenExpr->getType()); |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1373 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1374 | Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>(); |
| 1375 | Optional<NonLoc> lenValNL = lenVal.getAs<NonLoc>(); |
Ted Kremenek | fb1a79a | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1376 | |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1377 | // If we know both values, we might be able to figure out how much |
| 1378 | // we're copying. |
| 1379 | if (strLengthNL && lenValNL) { |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1380 | ProgramStateRef stateSourceTooLong, stateSourceNotTooLong; |
Ted Kremenek | fb1a79a | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1381 | |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1382 | // Check if the max number to copy is less than the length of the src. |
Jordy Rose | b41f7c5 | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1383 | // If the bound is equal to the source length, strncpy won't null- |
| 1384 | // terminate the result! |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1385 | std::tie(stateSourceTooLong, stateSourceNotTooLong) = state->assume( |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1386 | svalBuilder.evalBinOpNN(state, BO_GE, *strLengthNL, *lenValNL, cmpTy) |
| 1387 | .castAs<DefinedOrUnknownSVal>()); |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1388 | |
| 1389 | if (stateSourceTooLong && !stateSourceNotTooLong) { |
| 1390 | // Max number to copy is less than the length of the src, so the actual |
| 1391 | // strLength copied is the max number arg. |
| 1392 | state = stateSourceTooLong; |
| 1393 | amountCopied = lenVal; |
| 1394 | |
| 1395 | } else if (!stateSourceTooLong && stateSourceNotTooLong) { |
| 1396 | // The source buffer entirely fits in the bound. |
| 1397 | state = stateSourceNotTooLong; |
| 1398 | amountCopied = strLength; |
| 1399 | } |
| 1400 | } |
| 1401 | |
Jordy Rose | 328deee | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 1402 | // We still want to know if the bound is known to be too large. |
Jordy Rose | b41f7c5 | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1403 | if (lenValNL) { |
| 1404 | if (isAppending) { |
| 1405 | // For strncat, the check is strlen(dst) + lenVal < sizeof(dst) |
| 1406 | |
| 1407 | // Get the string length of the destination. If the destination is |
| 1408 | // memory that can't have a string length, we shouldn't be copying |
| 1409 | // into it anyway. |
| 1410 | SVal dstStrLength = getCStringLength(C, state, Dst, DstVal); |
| 1411 | if (dstStrLength.isUndef()) |
| 1412 | return; |
| 1413 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1414 | if (Optional<NonLoc> dstStrLengthNL = dstStrLength.getAs<NonLoc>()) { |
Jordy Rose | b41f7c5 | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1415 | maxLastElementIndex = svalBuilder.evalBinOpNN(state, BO_Add, |
| 1416 | *lenValNL, |
| 1417 | *dstStrLengthNL, |
| 1418 | sizeTy); |
| 1419 | boundWarning = "Size argument is greater than the free space in the " |
| 1420 | "destination buffer"; |
| 1421 | } |
| 1422 | |
| 1423 | } else { |
| 1424 | // For strncpy, this is just checking that lenVal <= sizeof(dst) |
| 1425 | // (Yes, strncpy and strncat differ in how they treat termination. |
| 1426 | // strncat ALWAYS terminates, but strncpy doesn't.) |
Jordy Rose | 459d5f6 | 2012-05-14 17:58:35 +0000 | [diff] [blame] | 1427 | |
| 1428 | // We need a special case for when the copy size is zero, in which |
| 1429 | // case strncpy will do no work at all. Our bounds check uses n-1 |
| 1430 | // as the last element accessed, so n == 0 is problematic. |
| 1431 | ProgramStateRef StateZeroSize, StateNonZeroSize; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1432 | std::tie(StateZeroSize, StateNonZeroSize) = |
Jordy Rose | 459d5f6 | 2012-05-14 17:58:35 +0000 | [diff] [blame] | 1433 | assumeZero(C, state, *lenValNL, sizeTy); |
| 1434 | |
| 1435 | // If the size is known to be zero, we're done. |
| 1436 | if (StateZeroSize && !StateNonZeroSize) { |
| 1437 | StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, DstVal); |
| 1438 | C.addTransition(StateZeroSize); |
| 1439 | return; |
| 1440 | } |
| 1441 | |
| 1442 | // Otherwise, go ahead and figure out the last element we'll touch. |
| 1443 | // We don't record the non-zero assumption here because we can't |
| 1444 | // be sure. We won't warn on a possible zero. |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1445 | NonLoc one = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>(); |
Jordy Rose | b41f7c5 | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1446 | maxLastElementIndex = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL, |
| 1447 | one, sizeTy); |
| 1448 | boundWarning = "Size argument is greater than the length of the " |
| 1449 | "destination buffer"; |
| 1450 | } |
| 1451 | } |
Jordy Rose | 328deee | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 1452 | |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1453 | // If we couldn't pin down the copy length, at least bound it. |
Jordy Rose | b41f7c5 | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1454 | // FIXME: We should actually run this code path for append as well, but |
| 1455 | // right now it creates problems with constraints (since we can end up |
| 1456 | // trying to pass constraints from symbol to symbol). |
| 1457 | if (amountCopied.isUnknown() && !isAppending) { |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1458 | // Try to get a "hypothetical" string length symbol, which we can later |
| 1459 | // set as a real value if that turns out to be the case. |
| 1460 | amountCopied = getCStringLength(C, state, lenExpr, srcVal, true); |
| 1461 | assert(!amountCopied.isUndef()); |
| 1462 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1463 | if (Optional<NonLoc> amountCopiedNL = amountCopied.getAs<NonLoc>()) { |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1464 | if (lenValNL) { |
| 1465 | // amountCopied <= lenVal |
| 1466 | SVal copiedLessThanBound = svalBuilder.evalBinOpNN(state, BO_LE, |
| 1467 | *amountCopiedNL, |
| 1468 | *lenValNL, |
| 1469 | cmpTy); |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1470 | state = state->assume( |
| 1471 | copiedLessThanBound.castAs<DefinedOrUnknownSVal>(), true); |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1472 | if (!state) |
| 1473 | return; |
| 1474 | } |
| 1475 | |
| 1476 | if (strLengthNL) { |
| 1477 | // amountCopied <= strlen(source) |
| 1478 | SVal copiedLessThanSrc = svalBuilder.evalBinOpNN(state, BO_LE, |
| 1479 | *amountCopiedNL, |
| 1480 | *strLengthNL, |
| 1481 | cmpTy); |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1482 | state = state->assume( |
| 1483 | copiedLessThanSrc.castAs<DefinedOrUnknownSVal>(), true); |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1484 | if (!state) |
| 1485 | return; |
| 1486 | } |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | } else { |
| 1491 | // The function isn't bounded. The amount copied should match the length |
| 1492 | // of the source buffer. |
| 1493 | amountCopied = strLength; |
Ted Kremenek | fb1a79a | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1496 | assert(state); |
| 1497 | |
| 1498 | // This represents the number of characters copied into the destination |
| 1499 | // buffer. (It may not actually be the strlen if the destination buffer |
| 1500 | // is not terminated.) |
| 1501 | SVal finalStrLength = UnknownVal(); |
| 1502 | |
Lenny Maiorani | 467dbd5 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1503 | // If this is an appending function (strcat, strncat...) then set the |
| 1504 | // string length to strlen(src) + strlen(dst) since the buffer will |
| 1505 | // ultimately contain both. |
| 1506 | if (isAppending) { |
Jordy Rose | 455bd58 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 1507 | // Get the string length of the destination. If the destination is memory |
| 1508 | // that can't have a string length, we shouldn't be copying into it anyway. |
Lenny Maiorani | 467dbd5 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1509 | SVal dstStrLength = getCStringLength(C, state, Dst, DstVal); |
| 1510 | if (dstStrLength.isUndef()) |
| 1511 | return; |
| 1512 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1513 | Optional<NonLoc> srcStrLengthNL = amountCopied.getAs<NonLoc>(); |
| 1514 | Optional<NonLoc> dstStrLengthNL = dstStrLength.getAs<NonLoc>(); |
Lenny Maiorani | 467dbd5 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1515 | |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1516 | // If we know both string lengths, we might know the final string length. |
| 1517 | if (srcStrLengthNL && dstStrLengthNL) { |
| 1518 | // Make sure the two lengths together don't overflow a size_t. |
| 1519 | state = checkAdditionOverflow(C, state, *srcStrLengthNL, *dstStrLengthNL); |
| 1520 | if (!state) |
| 1521 | return; |
Lenny Maiorani | 467dbd5 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1522 | |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1523 | finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *srcStrLengthNL, |
| 1524 | *dstStrLengthNL, sizeTy); |
| 1525 | } |
Lenny Maiorani | 467dbd5 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1526 | |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1527 | // If we couldn't get a single value for the final string length, |
| 1528 | // we can at least bound it by the individual lengths. |
| 1529 | if (finalStrLength.isUnknown()) { |
| 1530 | // Try to get a "hypothetical" string length symbol, which we can later |
| 1531 | // set as a real value if that turns out to be the case. |
| 1532 | finalStrLength = getCStringLength(C, state, CE, DstVal, true); |
| 1533 | assert(!finalStrLength.isUndef()); |
| 1534 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1535 | if (Optional<NonLoc> finalStrLengthNL = finalStrLength.getAs<NonLoc>()) { |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1536 | if (srcStrLengthNL) { |
| 1537 | // finalStrLength >= srcStrLength |
| 1538 | SVal sourceInResult = svalBuilder.evalBinOpNN(state, BO_GE, |
| 1539 | *finalStrLengthNL, |
| 1540 | *srcStrLengthNL, |
| 1541 | cmpTy); |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1542 | state = state->assume(sourceInResult.castAs<DefinedOrUnknownSVal>(), |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1543 | true); |
| 1544 | if (!state) |
| 1545 | return; |
| 1546 | } |
| 1547 | |
| 1548 | if (dstStrLengthNL) { |
| 1549 | // finalStrLength >= dstStrLength |
| 1550 | SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE, |
| 1551 | *finalStrLengthNL, |
| 1552 | *dstStrLengthNL, |
| 1553 | cmpTy); |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1554 | state = |
| 1555 | state->assume(destInResult.castAs<DefinedOrUnknownSVal>(), true); |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1556 | if (!state) |
| 1557 | return; |
| 1558 | } |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | } else { |
| 1563 | // Otherwise, this is a copy-over function (strcpy, strncpy, ...), and |
| 1564 | // the final string length will match the input string length. |
| 1565 | finalStrLength = amountCopied; |
Lenny Maiorani | 467dbd5 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1568 | // The final result of the function will either be a pointer past the last |
| 1569 | // copied element, or a pointer to the start of the destination buffer. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1570 | SVal Result = (returnEnd ? UnknownVal() : DstVal); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1571 | |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1572 | assert(state); |
| 1573 | |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1574 | // If the destination is a MemRegion, try to check for a buffer overflow and |
| 1575 | // record the new string length. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1576 | if (Optional<loc::MemRegionVal> dstRegVal = |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1577 | DstVal.getAs<loc::MemRegionVal>()) { |
Jordy Rose | b41f7c5 | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1578 | QualType ptrTy = Dst->getType(); |
| 1579 | |
| 1580 | // If we have an exact value on a bounded copy, use that to check for |
| 1581 | // overflows, rather than our estimate about how much is actually copied. |
| 1582 | if (boundWarning) { |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1583 | if (Optional<NonLoc> maxLastNL = maxLastElementIndex.getAs<NonLoc>()) { |
Jordy Rose | b41f7c5 | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1584 | SVal maxLastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, |
| 1585 | *maxLastNL, ptrTy); |
| 1586 | state = CheckLocation(C, state, CE->getArg(2), maxLastElement, |
| 1587 | boundWarning); |
| 1588 | if (!state) |
| 1589 | return; |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | // Then, if the final length is known... |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1594 | if (Optional<NonLoc> knownStrLength = finalStrLength.getAs<NonLoc>()) { |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1595 | SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, |
Jordy Rose | b41f7c5 | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1596 | *knownStrLength, ptrTy); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1597 | |
Jordy Rose | b41f7c5 | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1598 | // ...and we haven't checked the bound, we'll check the actual copy. |
| 1599 | if (!boundWarning) { |
| 1600 | const char * const warningMsg = |
| 1601 | "String copy function overflows destination buffer"; |
| 1602 | state = CheckLocation(C, state, Dst, lastElement, warningMsg); |
| 1603 | if (!state) |
| 1604 | return; |
| 1605 | } |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1606 | |
| 1607 | // 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] | 1608 | if (returnEnd) |
| 1609 | Result = lastElement; |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Anton Yartsev | 968c60a | 2013-11-17 09:18:48 +0000 | [diff] [blame] | 1612 | // Invalidate the destination (regular invalidation without pointer-escaping |
| 1613 | // the address of the top-level region). This must happen before we set the |
| 1614 | // C string length because invalidation will clear the length. |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1615 | // FIXME: Even if we can't perfectly model the copy, we should see if we |
| 1616 | // can use LazyCompoundVals to copy the source values into the destination. |
| 1617 | // This would probably remove any existing bindings past the end of the |
| 1618 | // string, but that's still an improvement over blank invalidation. |
Anton Yartsev | 968c60a | 2013-11-17 09:18:48 +0000 | [diff] [blame] | 1619 | state = InvalidateBuffer(C, state, Dst, *dstRegVal, |
| 1620 | /*IsSourceBuffer*/false); |
| 1621 | |
| 1622 | // Invalidate the source (const-invalidation without const-pointer-escaping |
| 1623 | // the address of the top-level region). |
| 1624 | state = InvalidateBuffer(C, state, srcExpr, srcVal, /*IsSourceBuffer*/true); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1625 | |
Jordy Rose | 328deee | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 1626 | // Set the C string length of the destination, if we know it. |
Jordy Rose | b41f7c5 | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1627 | if (isBounded && !isAppending) { |
| 1628 | // strncpy is annoying in that it doesn't guarantee to null-terminate |
| 1629 | // the result string. If the original string didn't fit entirely inside |
| 1630 | // the bound (including the null-terminator), we don't know how long the |
| 1631 | // result is. |
| 1632 | if (amountCopied != strLength) |
| 1633 | finalStrLength = UnknownVal(); |
| 1634 | } |
| 1635 | state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1638 | assert(state); |
| 1639 | |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1640 | // If this is a stpcpy-style copy, but we were unable to check for a buffer |
| 1641 | // overflow, we still need a result. Conjure a return value. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1642 | if (returnEnd && Result.isUnknown()) { |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 1643 | Result = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount()); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | // Set the return value. |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1647 | state = state->BindExpr(CE, LCtx, Result); |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1648 | C.addTransition(state); |
Jordy Rose | 722f558 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1651 | void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1652 | if (CE->getNumArgs() < 2) |
| 1653 | return; |
| 1654 | |
Jordy Rose | c026370 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1655 | //int strcmp(const char *s1, const char *s2); |
Lenny Maiorani | 4af23c8 | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1656 | evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ false); |
Lenny Maiorani | e553e40 | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1657 | } |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1658 | |
Lenny Maiorani | e553e40 | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1659 | void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const { |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1660 | if (CE->getNumArgs() < 3) |
| 1661 | return; |
| 1662 | |
Jordy Rose | c026370 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1663 | //int strncmp(const char *s1, const char *s2, size_t n); |
Lenny Maiorani | 4af23c8 | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1664 | evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ false); |
| 1665 | } |
| 1666 | |
| 1667 | void CStringChecker::evalStrcasecmp(CheckerContext &C, |
| 1668 | const CallExpr *CE) const { |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1669 | if (CE->getNumArgs() < 2) |
| 1670 | return; |
| 1671 | |
Jordy Rose | c026370 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1672 | //int strcasecmp(const char *s1, const char *s2); |
Lenny Maiorani | 4af23c8 | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1673 | evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ true); |
Lenny Maiorani | e553e40 | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Lenny Maiorani | 0b51027 | 2011-05-02 19:05:49 +0000 | [diff] [blame] | 1676 | void CStringChecker::evalStrncasecmp(CheckerContext &C, |
| 1677 | const CallExpr *CE) const { |
Anna Zaks | b508d29 | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1678 | if (CE->getNumArgs() < 3) |
| 1679 | return; |
| 1680 | |
Jordy Rose | c026370 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1681 | //int strncasecmp(const char *s1, const char *s2, size_t n); |
Lenny Maiorani | 0b51027 | 2011-05-02 19:05:49 +0000 | [diff] [blame] | 1682 | evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ true); |
| 1683 | } |
| 1684 | |
Lenny Maiorani | e553e40 | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1685 | void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE, |
Lenny Maiorani | 4af23c8 | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1686 | bool isBounded, bool ignoreCase) const { |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 1687 | CurrentFunctionDescription = "string comparison function"; |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1688 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1689 | const LocationContext *LCtx = C.getLocationContext(); |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1690 | |
| 1691 | // Check that the first string is non-null |
| 1692 | const Expr *s1 = CE->getArg(0); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1693 | SVal s1Val = state->getSVal(s1, LCtx); |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1694 | state = checkNonNull(C, state, s1, s1Val); |
| 1695 | if (!state) |
| 1696 | return; |
| 1697 | |
| 1698 | // Check that the second string is non-null. |
| 1699 | const Expr *s2 = CE->getArg(1); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1700 | SVal s2Val = state->getSVal(s2, LCtx); |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1701 | state = checkNonNull(C, state, s2, s2Val); |
| 1702 | if (!state) |
| 1703 | return; |
| 1704 | |
| 1705 | // Get the string length of the first string or give up. |
| 1706 | SVal s1Length = getCStringLength(C, state, s1, s1Val); |
| 1707 | if (s1Length.isUndef()) |
| 1708 | return; |
| 1709 | |
| 1710 | // Get the string length of the second string or give up. |
| 1711 | SVal s2Length = getCStringLength(C, state, s2, s2Val); |
| 1712 | if (s2Length.isUndef()) |
| 1713 | return; |
| 1714 | |
Jordy Rose | c026370 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1715 | // If we know the two buffers are the same, we know the result is 0. |
| 1716 | // First, get the two buffers' addresses. Another checker will have already |
| 1717 | // made sure they're not undefined. |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1718 | DefinedOrUnknownSVal LV = s1Val.castAs<DefinedOrUnknownSVal>(); |
| 1719 | DefinedOrUnknownSVal RV = s2Val.castAs<DefinedOrUnknownSVal>(); |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1720 | |
Jordy Rose | c026370 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1721 | // See if they are the same. |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1722 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | c026370 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1723 | DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV); |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1724 | ProgramStateRef StSameBuf, StNotSameBuf; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1725 | std::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf); |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1726 | |
Jordy Rose | c026370 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1727 | // If the two arguments might be the same buffer, we know the result is 0, |
| 1728 | // and we only need to check one size. |
| 1729 | if (StSameBuf) { |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1730 | StSameBuf = StSameBuf->BindExpr(CE, LCtx, |
| 1731 | svalBuilder.makeZeroVal(CE->getType())); |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1732 | C.addTransition(StSameBuf); |
Jordy Rose | c026370 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1733 | |
| 1734 | // If the two arguments are GUARANTEED to be the same, we're done! |
| 1735 | if (!StNotSameBuf) |
| 1736 | return; |
| 1737 | } |
| 1738 | |
| 1739 | assert(StNotSameBuf); |
| 1740 | state = StNotSameBuf; |
| 1741 | |
| 1742 | // At this point we can go about comparing the two buffers. |
| 1743 | // For now, we only do this if they're both known string literals. |
| 1744 | |
| 1745 | // Attempt to extract string literals from both expressions. |
| 1746 | const StringLiteral *s1StrLiteral = getCStringLiteral(C, state, s1, s1Val); |
| 1747 | const StringLiteral *s2StrLiteral = getCStringLiteral(C, state, s2, s2Val); |
| 1748 | bool canComputeResult = false; |
| 1749 | |
| 1750 | if (s1StrLiteral && s2StrLiteral) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1751 | StringRef s1StrRef = s1StrLiteral->getString(); |
| 1752 | StringRef s2StrRef = s2StrLiteral->getString(); |
Jordy Rose | c026370 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1753 | |
| 1754 | if (isBounded) { |
| 1755 | // Get the max number of characters to compare. |
| 1756 | const Expr *lenExpr = CE->getArg(2); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1757 | SVal lenVal = state->getSVal(lenExpr, LCtx); |
Jordy Rose | c026370 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1758 | |
| 1759 | // If the length is known, we can get the right substrings. |
| 1760 | if (const llvm::APSInt *len = svalBuilder.getKnownValue(state, lenVal)) { |
| 1761 | // Create substrings of each to compare the prefix. |
| 1762 | s1StrRef = s1StrRef.substr(0, (size_t)len->getZExtValue()); |
| 1763 | s2StrRef = s2StrRef.substr(0, (size_t)len->getZExtValue()); |
| 1764 | canComputeResult = true; |
| 1765 | } |
| 1766 | } else { |
| 1767 | // This is a normal, unbounded strcmp. |
| 1768 | canComputeResult = true; |
| 1769 | } |
| 1770 | |
| 1771 | if (canComputeResult) { |
| 1772 | // Real strcmp stops at null characters. |
| 1773 | size_t s1Term = s1StrRef.find('\0'); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1774 | if (s1Term != StringRef::npos) |
Jordy Rose | c026370 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1775 | s1StrRef = s1StrRef.substr(0, s1Term); |
| 1776 | |
| 1777 | size_t s2Term = s2StrRef.find('\0'); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1778 | if (s2Term != StringRef::npos) |
Jordy Rose | c026370 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1779 | s2StrRef = s2StrRef.substr(0, s2Term); |
| 1780 | |
| 1781 | // Use StringRef's comparison methods to compute the actual result. |
| 1782 | int result; |
| 1783 | |
| 1784 | if (ignoreCase) { |
| 1785 | // Compare string 1 to string 2 the same way strcasecmp() does. |
| 1786 | result = s1StrRef.compare_lower(s2StrRef); |
| 1787 | } else { |
| 1788 | // Compare string 1 to string 2 the same way strcmp() does. |
| 1789 | result = s1StrRef.compare(s2StrRef); |
| 1790 | } |
| 1791 | |
| 1792 | // Build the SVal of the comparison and bind the return value. |
| 1793 | SVal resultVal = svalBuilder.makeIntVal(result, CE->getType()); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1794 | state = state->BindExpr(CE, LCtx, resultVal); |
Jordy Rose | c026370 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | if (!canComputeResult) { |
| 1799 | // Conjure a symbolic value. It's the best we can do. |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 1800 | SVal resultVal = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx, |
| 1801 | C.blockCount()); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1802 | state = state->BindExpr(CE, LCtx, resultVal); |
Jordy Rose | c026370 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | // Record this as a possible path. |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1806 | C.addTransition(state); |
Lenny Maiorani | f3539ad | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Jordan Rose | 6e3cf2b | 2013-04-22 23:18:42 +0000 | [diff] [blame] | 1809 | void CStringChecker::evalStrsep(CheckerContext &C, const CallExpr *CE) const { |
| 1810 | //char *strsep(char **stringp, const char *delim); |
| 1811 | if (CE->getNumArgs() < 2) |
| 1812 | return; |
| 1813 | |
| 1814 | // Sanity: does the search string parameter match the return type? |
| 1815 | const Expr *SearchStrPtr = CE->getArg(0); |
| 1816 | QualType CharPtrTy = SearchStrPtr->getType()->getPointeeType(); |
| 1817 | if (CharPtrTy.isNull() || |
| 1818 | CE->getType().getUnqualifiedType() != CharPtrTy.getUnqualifiedType()) |
| 1819 | return; |
| 1820 | |
| 1821 | CurrentFunctionDescription = "strsep()"; |
| 1822 | ProgramStateRef State = C.getState(); |
| 1823 | const LocationContext *LCtx = C.getLocationContext(); |
| 1824 | |
| 1825 | // Check that the search string pointer is non-null (though it may point to |
| 1826 | // a null string). |
| 1827 | SVal SearchStrVal = State->getSVal(SearchStrPtr, LCtx); |
| 1828 | State = checkNonNull(C, State, SearchStrPtr, SearchStrVal); |
| 1829 | if (!State) |
| 1830 | return; |
| 1831 | |
| 1832 | // Check that the delimiter string is non-null. |
| 1833 | const Expr *DelimStr = CE->getArg(1); |
| 1834 | SVal DelimStrVal = State->getSVal(DelimStr, LCtx); |
| 1835 | State = checkNonNull(C, State, DelimStr, DelimStrVal); |
| 1836 | if (!State) |
| 1837 | return; |
| 1838 | |
| 1839 | SValBuilder &SVB = C.getSValBuilder(); |
| 1840 | SVal Result; |
| 1841 | if (Optional<Loc> SearchStrLoc = SearchStrVal.getAs<Loc>()) { |
| 1842 | // Get the current value of the search string pointer, as a char*. |
| 1843 | Result = State->getSVal(*SearchStrLoc, CharPtrTy); |
| 1844 | |
| 1845 | // Invalidate the search string, representing the change of one delimiter |
| 1846 | // character to NUL. |
Anton Yartsev | 968c60a | 2013-11-17 09:18:48 +0000 | [diff] [blame] | 1847 | State = InvalidateBuffer(C, State, SearchStrPtr, Result, |
| 1848 | /*IsSourceBuffer*/false); |
Jordan Rose | 6e3cf2b | 2013-04-22 23:18:42 +0000 | [diff] [blame] | 1849 | |
| 1850 | // Overwrite the search string pointer. The new value is either an address |
| 1851 | // further along in the same string, or NULL if there are no more tokens. |
| 1852 | State = State->bindLoc(*SearchStrLoc, |
| 1853 | SVB.conjureSymbolVal(getTag(), CE, LCtx, CharPtrTy, |
| 1854 | C.blockCount())); |
| 1855 | } else { |
| 1856 | assert(SearchStrVal.isUnknown()); |
| 1857 | // Conjure a symbolic value. It's the best we can do. |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 1858 | Result = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount()); |
Jordan Rose | 6e3cf2b | 2013-04-22 23:18:42 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | // Set the return value, and finish. |
| 1862 | State = State->BindExpr(CE, LCtx, Result); |
| 1863 | C.addTransition(State); |
| 1864 | } |
| 1865 | |
| 1866 | |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1867 | //===----------------------------------------------------------------------===// |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1868 | // The driver method, and other Checker callbacks. |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1869 | //===----------------------------------------------------------------------===// |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1870 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1871 | bool CStringChecker::evalCall(const CallExpr *CE, CheckerContext &C) const { |
Anna Zaks | 6348a81 | 2012-02-17 22:35:26 +0000 | [diff] [blame] | 1872 | const FunctionDecl *FDecl = C.getCalleeDecl(CE); |
| 1873 | |
| 1874 | if (!FDecl) |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1875 | return false; |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1876 | |
Jordan Rose | 6e3cf2b | 2013-04-22 23:18:42 +0000 | [diff] [blame] | 1877 | // FIXME: Poorly-factored string switches are slow. |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 1878 | FnCheck evalFunction = nullptr; |
Anna Zaks | 6348a81 | 2012-02-17 22:35:26 +0000 | [diff] [blame] | 1879 | if (C.isCLibraryFunction(FDecl, "memcpy")) |
| 1880 | evalFunction = &CStringChecker::evalMemcpy; |
| 1881 | else if (C.isCLibraryFunction(FDecl, "mempcpy")) |
| 1882 | evalFunction = &CStringChecker::evalMempcpy; |
| 1883 | else if (C.isCLibraryFunction(FDecl, "memcmp")) |
| 1884 | evalFunction = &CStringChecker::evalMemcmp; |
| 1885 | else if (C.isCLibraryFunction(FDecl, "memmove")) |
| 1886 | evalFunction = &CStringChecker::evalMemmove; |
| 1887 | else if (C.isCLibraryFunction(FDecl, "strcpy")) |
| 1888 | evalFunction = &CStringChecker::evalStrcpy; |
| 1889 | else if (C.isCLibraryFunction(FDecl, "strncpy")) |
| 1890 | evalFunction = &CStringChecker::evalStrncpy; |
| 1891 | else if (C.isCLibraryFunction(FDecl, "stpcpy")) |
| 1892 | evalFunction = &CStringChecker::evalStpcpy; |
| 1893 | else if (C.isCLibraryFunction(FDecl, "strcat")) |
| 1894 | evalFunction = &CStringChecker::evalStrcat; |
| 1895 | else if (C.isCLibraryFunction(FDecl, "strncat")) |
| 1896 | evalFunction = &CStringChecker::evalStrncat; |
| 1897 | else if (C.isCLibraryFunction(FDecl, "strlen")) |
| 1898 | evalFunction = &CStringChecker::evalstrLength; |
| 1899 | else if (C.isCLibraryFunction(FDecl, "strnlen")) |
| 1900 | evalFunction = &CStringChecker::evalstrnLength; |
| 1901 | else if (C.isCLibraryFunction(FDecl, "strcmp")) |
| 1902 | evalFunction = &CStringChecker::evalStrcmp; |
| 1903 | else if (C.isCLibraryFunction(FDecl, "strncmp")) |
| 1904 | evalFunction = &CStringChecker::evalStrncmp; |
| 1905 | else if (C.isCLibraryFunction(FDecl, "strcasecmp")) |
| 1906 | evalFunction = &CStringChecker::evalStrcasecmp; |
| 1907 | else if (C.isCLibraryFunction(FDecl, "strncasecmp")) |
| 1908 | evalFunction = &CStringChecker::evalStrncasecmp; |
Jordan Rose | 6e3cf2b | 2013-04-22 23:18:42 +0000 | [diff] [blame] | 1909 | else if (C.isCLibraryFunction(FDecl, "strsep")) |
| 1910 | evalFunction = &CStringChecker::evalStrsep; |
Anna Zaks | 6348a81 | 2012-02-17 22:35:26 +0000 | [diff] [blame] | 1911 | else if (C.isCLibraryFunction(FDecl, "bcopy")) |
| 1912 | evalFunction = &CStringChecker::evalBcopy; |
| 1913 | else if (C.isCLibraryFunction(FDecl, "bcmp")) |
| 1914 | evalFunction = &CStringChecker::evalMemcmp; |
| 1915 | |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1916 | // 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] | 1917 | if (!evalFunction) |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1918 | return false; |
| 1919 | |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 1920 | // Make sure each function sets its own description. |
| 1921 | // (But don't bother in a release build.) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame^] | 1922 | assert(!(CurrentFunctionDescription = nullptr)); |
Jordy Rose | dceb0cf | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 1923 | |
Jordy Rose | d5d2e50 | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1924 | // Check and evaluate the call. |
Ted Kremenek | dc89142 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1925 | (this->*evalFunction)(C, CE); |
Anna Zaks | e0c7c27 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 1926 | |
| 1927 | // If the evaluate call resulted in no change, chain to the next eval call |
| 1928 | // handler. |
| 1929 | // Note, the custom CString evaluation calls assume that basic safety |
| 1930 | // properties are held. However, if the user chooses to turn off some of these |
| 1931 | // checks, we ignore the issues and leave the call evaluation to a generic |
| 1932 | // handler. |
| 1933 | if (!C.isDifferent()) |
| 1934 | return false; |
| 1935 | |
Jordy Rose | 134a236 | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1936 | return true; |
| 1937 | } |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1938 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1939 | void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const { |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1940 | // Record string length for char a[] = "abc"; |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1941 | ProgramStateRef state = C.getState(); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1942 | |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 1943 | for (const auto *I : DS->decls()) { |
| 1944 | const VarDecl *D = dyn_cast<VarDecl>(I); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1945 | if (!D) |
| 1946 | continue; |
| 1947 | |
| 1948 | // FIXME: Handle array fields of structs. |
| 1949 | if (!D->getType()->isArrayType()) |
| 1950 | continue; |
| 1951 | |
| 1952 | const Expr *Init = D->getInit(); |
| 1953 | if (!Init) |
| 1954 | continue; |
| 1955 | if (!isa<StringLiteral>(Init)) |
| 1956 | continue; |
| 1957 | |
Anna Zaks | c9abbe2 | 2011-10-26 21:06:44 +0000 | [diff] [blame] | 1958 | Loc VarLoc = state->getLValue(D, C.getLocationContext()); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1959 | const MemRegion *MR = VarLoc.getAsRegion(); |
| 1960 | if (!MR) |
| 1961 | continue; |
| 1962 | |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1963 | SVal StrVal = state->getSVal(Init, C.getLocationContext()); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1964 | assert(StrVal.isValid() && "Initializer string is unknown or undefined"); |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 1965 | DefinedOrUnknownSVal strLength = |
| 1966 | getCStringLength(C, state, Init, StrVal).castAs<DefinedOrUnknownSVal>(); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1967 | |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1968 | state = state->set<CStringLength>(MR, strLength); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1971 | C.addTransition(state); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1974 | bool CStringChecker::wantsRegionChangeUpdate(ProgramStateRef state) const { |
Jordan Rose | 0c153cb | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 1975 | CStringLengthTy Entries = state->get<CStringLength>(); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1976 | return !Entries.isEmpty(); |
| 1977 | } |
| 1978 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1979 | ProgramStateRef |
| 1980 | CStringChecker::checkRegionChanges(ProgramStateRef state, |
Anna Zaks | dc15415 | 2012-12-20 00:38:25 +0000 | [diff] [blame] | 1981 | const InvalidatedSymbols *, |
Jordy Rose | 1fad663 | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 1982 | ArrayRef<const MemRegion *> ExplicitRegions, |
Anna Zaks | 3d34834 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1983 | ArrayRef<const MemRegion *> Regions, |
Jordan Rose | 742920c | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1984 | const CallEvent *Call) const { |
Jordan Rose | 0c153cb | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 1985 | CStringLengthTy Entries = state->get<CStringLength>(); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1986 | if (Entries.isEmpty()) |
| 1987 | return state; |
| 1988 | |
| 1989 | llvm::SmallPtrSet<const MemRegion *, 8> Invalidated; |
| 1990 | llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions; |
| 1991 | |
| 1992 | // First build sets for the changed regions and their super-regions. |
Jordy Rose | 1fad663 | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 1993 | for (ArrayRef<const MemRegion *>::iterator |
| 1994 | I = Regions.begin(), E = Regions.end(); I != E; ++I) { |
| 1995 | const MemRegion *MR = *I; |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1996 | Invalidated.insert(MR); |
| 1997 | |
| 1998 | SuperRegions.insert(MR); |
| 1999 | while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) { |
| 2000 | MR = SR->getSuperRegion(); |
| 2001 | SuperRegions.insert(MR); |
| 2002 | } |
| 2003 | } |
| 2004 | |
Jordan Rose | 0c153cb | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 2005 | CStringLengthTy::Factory &F = state->get_context<CStringLength>(); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 2006 | |
| 2007 | // Then loop over the entries in the current state. |
Jordan Rose | 0c153cb | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 2008 | for (CStringLengthTy::iterator I = Entries.begin(), |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 2009 | E = Entries.end(); I != E; ++I) { |
| 2010 | const MemRegion *MR = I.getKey(); |
| 2011 | |
| 2012 | // Is this entry for a super-region of a changed region? |
| 2013 | if (SuperRegions.count(MR)) { |
Ted Kremenek | b3b56c6 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 2014 | Entries = F.remove(Entries, MR); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 2015 | continue; |
| 2016 | } |
| 2017 | |
| 2018 | // Is this entry for a sub-region of a changed region? |
| 2019 | const MemRegion *Super = MR; |
| 2020 | while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) { |
| 2021 | Super = SR->getSuperRegion(); |
| 2022 | if (Invalidated.count(Super)) { |
Ted Kremenek | b3b56c6 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 2023 | Entries = F.remove(Entries, MR); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 2024 | break; |
| 2025 | } |
| 2026 | } |
| 2027 | } |
| 2028 | |
| 2029 | return state->set<CStringLength>(Entries); |
| 2030 | } |
| 2031 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2032 | void CStringChecker::checkLiveSymbols(ProgramStateRef state, |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 2033 | SymbolReaper &SR) const { |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 2034 | // Mark all symbols in our string length map as valid. |
Jordan Rose | 0c153cb | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 2035 | CStringLengthTy Entries = state->get<CStringLength>(); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 2036 | |
Jordan Rose | 0c153cb | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 2037 | for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end(); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 2038 | I != E; ++I) { |
| 2039 | SVal Len = I.getData(); |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 2040 | |
Anna Zaks | ee1a435 | 2011-12-06 23:12:33 +0000 | [diff] [blame] | 2041 | for (SymExpr::symbol_iterator si = Len.symbol_begin(), |
| 2042 | se = Len.symbol_end(); si != se; ++si) |
Jordy Rose | 634c12d | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 2043 | SR.markInUse(*si); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 2044 | } |
| 2045 | } |
| 2046 | |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 2047 | void CStringChecker::checkDeadSymbols(SymbolReaper &SR, |
| 2048 | CheckerContext &C) const { |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 2049 | if (!SR.hasDeadSymbols()) |
| 2050 | return; |
| 2051 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2052 | ProgramStateRef state = C.getState(); |
Jordan Rose | 0c153cb | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 2053 | CStringLengthTy Entries = state->get<CStringLength>(); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 2054 | if (Entries.isEmpty()) |
| 2055 | return; |
| 2056 | |
Jordan Rose | 0c153cb | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 2057 | CStringLengthTy::Factory &F = state->get_context<CStringLength>(); |
| 2058 | for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end(); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 2059 | I != E; ++I) { |
| 2060 | SVal Len = I.getData(); |
| 2061 | if (SymbolRef Sym = Len.getAsSymbol()) { |
| 2062 | if (SR.isDead(Sym)) |
Ted Kremenek | b3b56c6 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 2063 | Entries = F.remove(Entries, I.getKey()); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | state = state->set<CStringLength>(Entries); |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 2068 | C.addTransition(state); |
Jordy Rose | 2a2e21c | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 2069 | } |
Argyrios Kyrtzidis | c26f15d | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 2070 | |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 2071 | #define REGISTER_CHECKER(name) \ |
| 2072 | void ento::register##name(CheckerManager &mgr) { \ |
| 2073 | CStringChecker *checker = mgr.registerChecker<CStringChecker>(); \ |
| 2074 | checker->Filter.Check##name = true; \ |
| 2075 | checker->Filter.CheckName##name = mgr.getCurrentCheckName(); \ |
| 2076 | } |
Anna Zaks | e0c7c27 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 2077 | |
| 2078 | REGISTER_CHECKER(CStringNullArg) |
| 2079 | REGISTER_CHECKER(CStringOutOfBounds) |
| 2080 | REGISTER_CHECKER(CStringBufferOverlap) |
| 2081 | REGISTER_CHECKER(CStringNotNullTerm) |
Anna Zaks | e56167e | 2012-02-17 22:35:31 +0000 | [diff] [blame] | 2082 | |
| 2083 | void ento::registerCStringCheckerBasic(CheckerManager &Mgr) { |
| 2084 | registerCStringNullArg(Mgr); |
| 2085 | } |