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