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