clang 22.0.0git
MoveChecker.cpp
Go to the documentation of this file.
1// MoveChecker.cpp - Check use of moved-from objects. - C++ ---------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This defines checker which checks for potential misuses of a moved-from
10// object. That means method calls on the object or copying it in moved-from
11// state.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Move.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/ExprCXX.h"
24#include "llvm/ADT/StringSet.h"
25
26using namespace clang;
27using namespace ento;
28
29namespace {
30struct RegionState {
31private:
32 enum Kind { Moved, Reported } K;
33 RegionState(Kind InK) : K(InK) {}
34
35public:
36 bool isReported() const { return K == Reported; }
37 bool isMoved() const { return K == Moved; }
38
39 static RegionState getReported() { return RegionState(Reported); }
40 static RegionState getMoved() { return RegionState(Moved); }
41
42 bool operator==(const RegionState &X) const { return K == X.K; }
43 void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddInteger(K); }
44};
45} // end of anonymous namespace
46
47namespace {
48class MoveChecker
49 : public Checker<check::PreCall, check::PostCall,
50 check::DeadSymbols, check::RegionChanges> {
51public:
52 void checkPreCall(const CallEvent &MC, CheckerContext &C) const;
53 void checkPostCall(const CallEvent &MC, CheckerContext &C) const;
54 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
56 checkRegionChanges(ProgramStateRef State,
57 const InvalidatedSymbols *Invalidated,
58 ArrayRef<const MemRegion *> RequestedRegions,
59 ArrayRef<const MemRegion *> InvalidatedRegions,
60 const LocationContext *LCtx, const CallEvent *Call) const;
61 void printState(raw_ostream &Out, ProgramStateRef State,
62 const char *NL, const char *Sep) const override;
63
64private:
65 enum MisuseKind { MK_FunCall, MK_Copy, MK_Move, MK_Dereference };
66 enum StdObjectKind { SK_NonStd, SK_Unsafe, SK_Safe, SK_SmartPtr };
67
68 enum AggressivenessKind { // In any case, don't warn after a reset.
69 AK_Invalid = -1,
70 AK_KnownsOnly = 0, // Warn only about known move-unsafe classes.
71 AK_KnownsAndLocals = 1, // Also warn about all local objects.
72 AK_All = 2, // Warn on any use-after-move.
73 AK_NumKinds = AK_All
74 };
75
76 static bool misuseCausesCrash(MisuseKind MK) {
77 return MK == MK_Dereference;
78 }
79
80 struct ObjectKind {
81 // Is this a local variable or a local rvalue reference?
82 bool IsLocal;
83 // Is this an STL object? If so, of what kind?
84 StdObjectKind StdKind;
85 };
86
87 // STL smart pointers are automatically re-initialized to null when moved
88 // from. So we can't warn on many methods, but we can warn when it is
89 // dereferenced, which is UB even if the resulting lvalue never gets read.
90 const llvm::StringSet<> StdSmartPtrClasses = {
91 "shared_ptr",
92 "unique_ptr",
93 "weak_ptr",
94 };
95
96 // Not all of these are entirely move-safe, but they do provide *some*
97 // guarantees, and it means that somebody is using them after move
98 // in a valid manner.
99 // TODO: We can still try to identify *unsafe* use after move,
100 // like we did with smart pointers.
101 const llvm::StringSet<> StdSafeClasses = {
102 "basic_filebuf",
103 "basic_ios",
104 "future",
105 "optional",
106 "packaged_task",
107 "promise",
108 "shared_future",
109 "shared_lock",
110 "thread",
111 "unique_lock",
112 };
113
114 // Should we bother tracking the state of the object?
115 bool shouldBeTracked(ObjectKind OK) const {
116 // In non-aggressive mode, only warn on use-after-move of local variables
117 // (or local rvalue references) and of STL objects. The former is possible
118 // because local variables (or local rvalue references) are not tempting
119 // their user to re-use the storage. The latter is possible because STL
120 // objects are known to end up in a valid but unspecified state after the
121 // move and their state-reset methods are also known, which allows us to
122 // predict precisely when use-after-move is invalid.
123 // Some STL objects are known to conform to additional contracts after move,
124 // so they are not tracked. However, smart pointers specifically are tracked
125 // because we can perform extra checking over them.
126 // In aggressive mode, warn on any use-after-move because the user has
127 // intentionally asked us to completely eliminate use-after-move
128 // in his code.
129 return (Aggressiveness == AK_All) ||
130 (Aggressiveness >= AK_KnownsAndLocals && OK.IsLocal) ||
131 OK.StdKind == SK_Unsafe || OK.StdKind == SK_SmartPtr;
132 }
133
134 // Some objects only suffer from some kinds of misuses, but we need to track
135 // them anyway because we cannot know in advance what misuse will we find.
136 bool shouldWarnAbout(ObjectKind OK, MisuseKind MK) const {
137 // Additionally, only warn on smart pointers when they are dereferenced (or
138 // local or we are aggressive).
139 return shouldBeTracked(OK) &&
140 ((Aggressiveness == AK_All) ||
141 (Aggressiveness >= AK_KnownsAndLocals && OK.IsLocal) ||
142 OK.StdKind != SK_SmartPtr || MK == MK_Dereference);
143 }
144
145 // Obtains ObjectKind of an object. Because class declaration cannot always
146 // be easily obtained from the memory region, it is supplied separately.
147 ObjectKind classifyObject(ProgramStateRef State, const MemRegion *MR,
148 const CXXRecordDecl *RD) const;
149
150 // Classifies the object and dumps a user-friendly description string to
151 // the stream.
152 void explainObject(ProgramStateRef State, llvm::raw_ostream &OS,
153 const MemRegion *MR, const CXXRecordDecl *RD,
154 MisuseKind MK) const;
155
156 bool belongsTo(const CXXRecordDecl *RD, const llvm::StringSet<> &Set) const;
157
158 class MovedBugVisitor : public BugReporterVisitor {
159 public:
160 MovedBugVisitor(const MoveChecker &Chk, const MemRegion *R,
161 const CXXRecordDecl *RD, MisuseKind MK)
162 : Chk(Chk), Region(R), RD(RD), MK(MK), Found(false) {}
163
164 void Profile(llvm::FoldingSetNodeID &ID) const override {
165 static int X = 0;
166 ID.AddPointer(&X);
167 ID.AddPointer(Region);
168 // Don't add RD because it's, in theory, uniquely determined by
169 // the region. In practice though, it's not always possible to obtain
170 // the declaration directly from the region, that's why we store it
171 // in the first place.
172 }
173
174 PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
175 BugReporterContext &BRC,
176 PathSensitiveBugReport &BR) override;
177
178 private:
179 const MoveChecker &Chk;
180 // The tracked region.
181 const MemRegion *Region;
182 // The class of the tracked object.
183 const CXXRecordDecl *RD;
184 // How exactly the object was misused.
185 const MisuseKind MK;
186 bool Found;
187 };
188
189 AggressivenessKind Aggressiveness = AK_KnownsAndLocals;
190
191public:
192 void setAggressiveness(StringRef Str, CheckerManager &Mgr) {
193 Aggressiveness =
194 llvm::StringSwitch<AggressivenessKind>(Str)
195 .Case("KnownsOnly", AK_KnownsOnly)
196 .Case("KnownsAndLocals", AK_KnownsAndLocals)
197 .Case("All", AK_All)
198 .Default(AK_Invalid);
199
200 if (Aggressiveness == AK_Invalid)
201 Mgr.reportInvalidCheckerOptionValue(this, "WarnOn",
202 "either \"KnownsOnly\", \"KnownsAndLocals\" or \"All\" string value");
203 };
204
205private:
206 BugType BT{this, "Use-after-move", categories::CXXMoveSemantics};
207
208 // Check if the given form of potential misuse of a given object
209 // should be reported. If so, get it reported. The callback from which
210 // this function was called should immediately return after the call
211 // because this function adds one or two transitions.
212 void modelUse(ProgramStateRef State, const MemRegion *Region,
213 const CXXRecordDecl *RD, MisuseKind MK,
214 CheckerContext &C) const;
215
216 // Returns the exploded node against which the report was emitted.
217 // The caller *must* add any further transitions against this node.
218 // Returns nullptr and does not report if such node already exists.
219 ExplodedNode *tryToReportBug(const MemRegion *Region, const CXXRecordDecl *RD,
220 CheckerContext &C, MisuseKind MK) const;
221
222 bool isInMoveSafeContext(const LocationContext *LC) const;
223 bool isStateResetMethod(const CXXMethodDecl *MethodDec) const;
224 bool isMoveSafeMethod(const CXXMethodDecl *MethodDec) const;
225 const ExplodedNode *getMoveLocation(const ExplodedNode *N,
226 const MemRegion *Region,
227 CheckerContext &C) const;
228};
229} // end anonymous namespace
230
231REGISTER_MAP_WITH_PROGRAMSTATE(TrackedRegionMap, const MemRegion *, RegionState)
232
233// Define the inter-checker API.
234namespace clang {
235namespace ento {
236namespace move {
237bool isMovedFrom(ProgramStateRef State, const MemRegion *Region) {
238 const RegionState *RS = State->get<TrackedRegionMap>(Region);
239 return RS && (RS->isMoved() || RS->isReported());
240}
241} // namespace move
242} // namespace ento
243} // namespace clang
244
245// If a region is removed all of the subregions needs to be removed too.
247 const MemRegion *Region,
248 bool Strict = false) {
249 if (!Region)
250 return State;
251 for (auto &E : State->get<TrackedRegionMap>()) {
252 if ((!Strict || E.first != Region) && E.first->isSubRegionOf(Region))
253 State = State->remove<TrackedRegionMap>(E.first);
254 }
255 return State;
256}
257
259 const MemRegion *Region) {
260 for (auto &E : State->get<TrackedRegionMap>()) {
261 if (Region->isSubRegionOf(E.first) && E.second.isReported())
262 return true;
263 }
264 return false;
265}
266
268 if (const auto *SR = dyn_cast_or_null<SymbolicRegion>(MR)) {
269 SymbolRef Sym = SR->getSymbol();
270 if (Sym->getType()->isRValueReferenceType())
271 if (const MemRegion *OriginMR = Sym->getOriginRegion())
272 return OriginMR;
273 }
274 return MR;
275}
276
278MoveChecker::MovedBugVisitor::VisitNode(const ExplodedNode *N,
281 // We need only the last move of the reported object's region.
282 // The visitor walks the ExplodedGraph backwards.
283 if (Found)
284 return nullptr;
285 ProgramStateRef State = N->getState();
286 ProgramStateRef StatePrev = N->getFirstPred()->getState();
287 const RegionState *TrackedObject = State->get<TrackedRegionMap>(Region);
288 const RegionState *TrackedObjectPrev =
289 StatePrev->get<TrackedRegionMap>(Region);
290 if (!TrackedObject)
291 return nullptr;
292 if (TrackedObjectPrev && TrackedObject)
293 return nullptr;
294
295 // Retrieve the associated statement.
296 const Stmt *S = N->getStmtForDiagnostics();
297 if (!S)
298 return nullptr;
299 Found = true;
300
301 SmallString<128> Str;
302 llvm::raw_svector_ostream OS(Str);
303
304 ObjectKind OK = Chk.classifyObject(State, Region, RD);
305 switch (OK.StdKind) {
306 case SK_SmartPtr:
307 if (MK == MK_Dereference) {
308 OS << "Smart pointer";
309 Chk.explainObject(State, OS, Region, RD, MK);
310 OS << " is reset to null when moved from";
311 break;
312 }
313
314 // If it's not a dereference, we don't care if it was reset to null
315 // or that it is even a smart pointer.
316 [[fallthrough]];
317 case SK_NonStd:
318 case SK_Safe:
319 OS << "Object";
320 Chk.explainObject(State, OS, Region, RD, MK);
321 OS << " is moved";
322 break;
323 case SK_Unsafe:
324 OS << "Object";
325 Chk.explainObject(State, OS, Region, RD, MK);
326 OS << " is left in a valid but unspecified state after move";
327 break;
328 }
329
330 // Generate the extra diagnostic.
331 PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
332 N->getLocationContext());
333 return std::make_shared<PathDiagnosticEventPiece>(Pos, OS.str(), true);
334}
335
336const ExplodedNode *MoveChecker::getMoveLocation(const ExplodedNode *N,
337 const MemRegion *Region,
338 CheckerContext &C) const {
339 // Walk the ExplodedGraph backwards and find the first node that referred to
340 // the tracked region.
341 const ExplodedNode *MoveNode = N;
342
343 while (N) {
344 ProgramStateRef State = N->getState();
345 if (!State->get<TrackedRegionMap>(Region))
346 break;
347 MoveNode = N;
348 N = N->pred_empty() ? nullptr : *(N->pred_begin());
349 }
350 return MoveNode;
351}
352
353void MoveChecker::modelUse(ProgramStateRef State, const MemRegion *Region,
354 const CXXRecordDecl *RD, MisuseKind MK,
355 CheckerContext &C) const {
356 assert(!C.isDifferent() && "No transitions should have been made by now");
357 const RegionState *RS = State->get<TrackedRegionMap>(Region);
358 ObjectKind OK = classifyObject(State, Region, RD);
359
360 // Just in case: if it's not a smart pointer but it does have operator *,
361 // we shouldn't call the bug a dereference.
362 if (MK == MK_Dereference && OK.StdKind != SK_SmartPtr)
363 MK = MK_FunCall;
364
365 if (!RS || !shouldWarnAbout(OK, MK)
366 || isInMoveSafeContext(C.getLocationContext())) {
367 // Finalize changes made by the caller.
368 C.addTransition(State);
369 return;
370 }
371
372 // Don't report it in case if any base region is already reported.
373 // But still generate a sink in case of UB.
374 // And still finalize changes made by the caller.
375 if (isAnyBaseRegionReported(State, Region)) {
376 if (misuseCausesCrash(MK)) {
377 C.generateSink(State, C.getPredecessor());
378 } else {
379 C.addTransition(State);
380 }
381 return;
382 }
383
384 ExplodedNode *N = tryToReportBug(Region, RD, C, MK);
385
386 // If the program has already crashed on this path, don't bother.
387 if (!N || N->isSink())
388 return;
389
390 State = State->set<TrackedRegionMap>(Region, RegionState::getReported());
391 C.addTransition(State, N);
392}
393
394ExplodedNode *MoveChecker::tryToReportBug(const MemRegion *Region,
395 const CXXRecordDecl *RD,
396 CheckerContext &C,
397 MisuseKind MK) const {
398 if (ExplodedNode *N = misuseCausesCrash(MK) ? C.generateErrorNode()
399 : C.generateNonFatalErrorNode()) {
400 // Uniqueing report to the same object.
401 PathDiagnosticLocation LocUsedForUniqueing;
402 const ExplodedNode *MoveNode = getMoveLocation(N, Region, C);
403
404 if (const Stmt *MoveStmt = MoveNode->getStmtForDiagnostics())
405 LocUsedForUniqueing = PathDiagnosticLocation::createBegin(
406 MoveStmt, C.getSourceManager(), MoveNode->getLocationContext());
407
408 // Creating the error message.
409 llvm::SmallString<128> Str;
410 llvm::raw_svector_ostream OS(Str);
411 ProgramStateRef State = N->getState();
412 switch(MK) {
413 case MK_FunCall:
414 OS << "Method called on moved-from object";
415 explainObject(State, OS, Region, RD, MK);
416 break;
417 case MK_Copy:
418 OS << "Moved-from object";
419 explainObject(State, OS, Region, RD, MK);
420 OS << " is copied";
421 break;
422 case MK_Move:
423 OS << "Moved-from object";
424 explainObject(State, OS, Region, RD, MK);
425 OS << " is moved";
426 break;
427 case MK_Dereference:
428 OS << "Dereference of null smart pointer";
429 explainObject(State, OS, Region, RD, MK);
430 break;
431 }
432
433 auto R = std::make_unique<PathSensitiveBugReport>(
434 BT, OS.str(), N, LocUsedForUniqueing,
435 MoveNode->getLocationContext()->getDecl());
436 R->addVisitor(std::make_unique<MovedBugVisitor>(*this, Region, RD, MK));
437 C.emitReport(std::move(R));
438 return N;
439 }
440 return nullptr;
441}
442
443void MoveChecker::checkPostCall(const CallEvent &Call,
444 CheckerContext &C) const {
445 const auto *AFC = dyn_cast<AnyFunctionCall>(&Call);
446 if (!AFC)
447 return;
448
449 ProgramStateRef State = C.getState();
450 const auto MethodDecl = dyn_cast_or_null<CXXMethodDecl>(AFC->getDecl());
451 if (!MethodDecl)
452 return;
453
454 // Check if an object became moved-from.
455 // Object can become moved from after a call to move assignment operator or
456 // move constructor .
457 const auto *ConstructorDecl = dyn_cast<CXXConstructorDecl>(MethodDecl);
458 if (ConstructorDecl && !ConstructorDecl->isMoveConstructor())
459 return;
460
461 if (!ConstructorDecl && !MethodDecl->isMoveAssignmentOperator())
462 return;
463
464 const auto ArgRegion = AFC->getArgSVal(0).getAsRegion();
465 if (!ArgRegion)
466 return;
467
468 // Skip moving the object to itself.
469 const auto *CC = dyn_cast_or_null<CXXConstructorCall>(&Call);
470 if (CC && CC->getCXXThisVal().getAsRegion() == ArgRegion)
471 return;
472
473 if (const auto *IC = dyn_cast<CXXInstanceCall>(AFC))
474 if (IC->getCXXThisVal().getAsRegion() == ArgRegion)
475 return;
476
477 const MemRegion *BaseRegion = ArgRegion->getBaseRegion();
478 // Skip temp objects because of their short lifetime.
479 if (BaseRegion->getAs<CXXTempObjectRegion>() ||
480 AFC->getArgExpr(0)->isPRValue())
481 return;
482 // If it has already been reported do not need to modify the state.
483
484 if (State->get<TrackedRegionMap>(ArgRegion))
485 return;
486
487 const CXXRecordDecl *RD = MethodDecl->getParent();
488 ObjectKind OK = classifyObject(State, ArgRegion, RD);
489 if (shouldBeTracked(OK)) {
490 // Mark object as moved-from.
491 State = State->set<TrackedRegionMap>(ArgRegion, RegionState::getMoved());
492 C.addTransition(State);
493 return;
494 }
495 assert(!C.isDifferent() && "Should not have made transitions on this path!");
496}
497
498bool MoveChecker::isMoveSafeMethod(const CXXMethodDecl *MethodDec) const {
499 // We abandon the cases where bool/void/void* conversion happens.
500 if (const auto *ConversionDec =
501 dyn_cast_or_null<CXXConversionDecl>(MethodDec)) {
502 const Type *Tp = ConversionDec->getConversionType().getTypePtrOrNull();
503 if (!Tp)
504 return false;
505 if (Tp->isBooleanType() || Tp->isVoidType() || Tp->isVoidPointerType())
506 return true;
507 }
508 // Function call `empty` can be skipped.
509 return (MethodDec && MethodDec->getDeclName().isIdentifier() &&
510 (MethodDec->getName().lower() == "empty" ||
511 MethodDec->getName().lower() == "isempty"));
512}
513
514bool MoveChecker::isStateResetMethod(const CXXMethodDecl *MethodDec) const {
515 if (!MethodDec)
516 return false;
517 if (MethodDec->hasAttr<ReinitializesAttr>())
518 return true;
519 if (MethodDec->getDeclName().isIdentifier()) {
520 std::string MethodName = MethodDec->getName().lower();
521 // TODO: Some of these methods (eg., resize) are not always resetting
522 // the state, so we should consider looking at the arguments.
523 if (MethodName == "assign" || MethodName == "clear" ||
524 MethodName == "destroy" || MethodName == "reset" ||
525 MethodName == "resize" || MethodName == "shrink")
526 return true;
527 }
528 return false;
529}
530
531// Don't report an error inside a move related operation.
532// We assume that the programmer knows what she does.
533bool MoveChecker::isInMoveSafeContext(const LocationContext *LC) const {
534 do {
535 const auto *CtxDec = LC->getDecl();
536 auto *CtorDec = dyn_cast_or_null<CXXConstructorDecl>(CtxDec);
537 auto *DtorDec = dyn_cast_or_null<CXXDestructorDecl>(CtxDec);
538 auto *MethodDec = dyn_cast_or_null<CXXMethodDecl>(CtxDec);
539 if (DtorDec || (CtorDec && CtorDec->isCopyOrMoveConstructor()) ||
540 (MethodDec && MethodDec->isOverloadedOperator() &&
541 MethodDec->getOverloadedOperator() == OO_Equal) ||
542 isStateResetMethod(MethodDec) || isMoveSafeMethod(MethodDec))
543 return true;
544 } while ((LC = LC->getParent()));
545 return false;
546}
547
548bool MoveChecker::belongsTo(const CXXRecordDecl *RD,
549 const llvm::StringSet<> &Set) const {
550 const IdentifierInfo *II = RD->getIdentifier();
551 return II && Set.count(II->getName());
552}
553
554MoveChecker::ObjectKind
555MoveChecker::classifyObject(ProgramStateRef State, const MemRegion *MR,
556 const CXXRecordDecl *RD) const {
557 // Local variables and local rvalue references are classified as "Local".
558 // For the purposes of this checker, we classify move-safe STL types
559 // as not-"STL" types, because that's how the checker treats them.
561 bool IsLocal =
562 isa_and_nonnull<VarRegion, CXXLifetimeExtendedObjectRegion>(MR) &&
563 MR->hasMemorySpace<StackSpaceRegion>(State);
564
565 if (!RD || !RD->getDeclContext()->isStdNamespace())
566 return { IsLocal, SK_NonStd };
567
568 if (belongsTo(RD, StdSmartPtrClasses))
569 return { IsLocal, SK_SmartPtr };
570
571 if (belongsTo(RD, StdSafeClasses))
572 return { IsLocal, SK_Safe };
573
574 return { IsLocal, SK_Unsafe };
575}
576
577void MoveChecker::explainObject(ProgramStateRef State, llvm::raw_ostream &OS,
578 const MemRegion *MR, const CXXRecordDecl *RD,
579 MisuseKind MK) const {
580 // We may need a leading space every time we actually explain anything,
581 // and we never know if we are to explain anything until we try.
582 if (const auto DR =
583 dyn_cast_or_null<DeclRegion>(unwrapRValueReferenceIndirection(MR))) {
584 const auto *RegionDecl = cast<NamedDecl>(DR->getDecl());
585 OS << " '" << RegionDecl->getDeclName() << "'";
586 }
587
588 ObjectKind OK = classifyObject(State, MR, RD);
589 switch (OK.StdKind) {
590 case SK_NonStd:
591 case SK_Safe:
592 break;
593 case SK_SmartPtr:
594 if (MK != MK_Dereference)
595 break;
596
597 // We only care about the type if it's a dereference.
598 [[fallthrough]];
599 case SK_Unsafe:
600 OS << " of type '" << RD->getQualifiedNameAsString() << "'";
601 break;
602 };
603}
604
605void MoveChecker::checkPreCall(const CallEvent &Call, CheckerContext &C) const {
606 ProgramStateRef State = C.getState();
607
608 // Remove the MemRegions from the map on which a ctor/dtor call or assignment
609 // happened.
610
611 // Checking constructor calls.
612 if (const auto *CC = dyn_cast<CXXConstructorCall>(&Call)) {
613 State = removeFromState(State, CC->getCXXThisVal().getAsRegion());
614 auto CtorDec = CC->getDecl();
615 // Check for copying a moved-from object and report the bug.
616 if (CtorDec && CtorDec->isCopyOrMoveConstructor()) {
617 const MemRegion *ArgRegion = CC->getArgSVal(0).getAsRegion();
618 const CXXRecordDecl *RD = CtorDec->getParent();
619 MisuseKind MK = CtorDec->isMoveConstructor() ? MK_Move : MK_Copy;
620 modelUse(State, ArgRegion, RD, MK, C);
621 return;
622 }
623 }
624
625 const auto IC = dyn_cast<CXXInstanceCall>(&Call);
626 if (!IC)
627 return;
628
629 const MemRegion *ThisRegion = IC->getCXXThisVal().getAsRegion();
630 if (!ThisRegion)
631 return;
632
633 // The remaining part is check only for method call on a moved-from object.
634 const auto MethodDecl = dyn_cast_or_null<CXXMethodDecl>(IC->getDecl());
635 if (!MethodDecl)
636 return;
637
638 // Calling a destructor on a moved object is fine.
639 if (isa<CXXDestructorDecl>(MethodDecl))
640 return;
641
642 // We want to investigate the whole object, not only sub-object of a parent
643 // class in which the encountered method defined.
644 ThisRegion = ThisRegion->getMostDerivedObjectRegion();
645
646 if (isStateResetMethod(MethodDecl)) {
647 State = removeFromState(State, ThisRegion);
648 C.addTransition(State);
649 return;
650 }
651
652 if (isMoveSafeMethod(MethodDecl))
653 return;
654
655 // Store class declaration as well, for bug reporting purposes.
656 const CXXRecordDecl *RD = MethodDecl->getParent();
657
658 if (MethodDecl->isOverloadedOperator()) {
659 OverloadedOperatorKind OOK = MethodDecl->getOverloadedOperator();
660
661 if (OOK == OO_Equal) {
662 // Remove the tracked object for every assignment operator, but report bug
663 // only for move or copy assignment's argument.
664 State = removeFromState(State, ThisRegion);
665
666 if (MethodDecl->isCopyAssignmentOperator() ||
667 MethodDecl->isMoveAssignmentOperator()) {
668 const MemRegion *ArgRegion = IC->getArgSVal(0).getAsRegion();
669 MisuseKind MK =
670 MethodDecl->isMoveAssignmentOperator() ? MK_Move : MK_Copy;
671 modelUse(State, ArgRegion, RD, MK, C);
672 return;
673 }
674 C.addTransition(State);
675 return;
676 }
677
678 if (OOK == OO_Star || OOK == OO_Arrow) {
679 modelUse(State, ThisRegion, RD, MK_Dereference, C);
680 return;
681 }
682 }
683
684 modelUse(State, ThisRegion, RD, MK_FunCall, C);
685}
686
687void MoveChecker::checkDeadSymbols(SymbolReaper &SymReaper,
688 CheckerContext &C) const {
689 ProgramStateRef State = C.getState();
690 TrackedRegionMapTy TrackedRegions = State->get<TrackedRegionMap>();
691 for (auto E : TrackedRegions) {
692 const MemRegion *Region = E.first;
693 bool IsRegDead = !SymReaper.isLiveRegion(Region);
694
695 // Remove the dead regions from the region map.
696 if (IsRegDead) {
697 State = State->remove<TrackedRegionMap>(Region);
698 }
699 }
700 C.addTransition(State);
701}
702
703ProgramStateRef MoveChecker::checkRegionChanges(
704 ProgramStateRef State, const InvalidatedSymbols *Invalidated,
705 ArrayRef<const MemRegion *> RequestedRegions,
706 ArrayRef<const MemRegion *> InvalidatedRegions,
707 const LocationContext *LCtx, const CallEvent *Call) const {
708 if (Call) {
709 // Relax invalidation upon function calls: only invalidate parameters
710 // that are passed directly via non-const pointers or non-const references
711 // or rvalue references.
712 // In case of an InstanceCall don't invalidate the this-region since
713 // it is fully handled in checkPreCall and checkPostCall, but do invalidate
714 // its strict subregions, as they are not handled.
715
716 // Requested ("explicit") regions are the regions passed into the call
717 // directly, but not all of them end up being invalidated.
718 // But when they do, they appear in the InvalidatedRegions array as well.
719 for (const auto *Region : RequestedRegions) {
720 if (llvm::is_contained(InvalidatedRegions, Region))
721 State = removeFromState(State, Region,
722 /*Strict=*/isa<CXXInstanceCall>(Call));
723 }
724 } else {
725 // For invalidations that aren't caused by calls, assume nothing. In
726 // particular, direct write into an object's field invalidates the status.
727 for (const auto *Region : InvalidatedRegions)
728 State = removeFromState(State, Region->getBaseRegion());
729 }
730
731 return State;
732}
733
734void MoveChecker::printState(raw_ostream &Out, ProgramStateRef State,
735 const char *NL, const char *Sep) const {
736
737 TrackedRegionMapTy RS = State->get<TrackedRegionMap>();
738
739 if (!RS.isEmpty()) {
740 Out << Sep << "Moved-from objects :" << NL;
741 for (auto I: RS) {
742 I.first->dumpToStream(Out);
743 if (I.second.isMoved())
744 Out << ": moved";
745 else
746 Out << ": moved and reported";
747 Out << NL;
748 }
749 }
750}
751void ento::registerMoveChecker(CheckerManager &mgr) {
752 MoveChecker *chk = mgr.registerChecker<MoveChecker>();
753 chk->setAggressiveness(
754 mgr.getAnalyzerOptions().getCheckerStringOption(chk, "WarnOn"), mgr);
755}
756
757bool ento::shouldRegisterMoveChecker(const CheckerManager &mgr) {
758 return true;
759}
Defines the clang::Expr interface and subclasses for C++ expressions.
#define X(type, name)
Definition Value.h:97
static bool isAnyBaseRegionReported(ProgramStateRef State, const MemRegion *Region)
static ProgramStateRef removeFromState(ProgramStateRef State, const MemRegion *Region, bool Strict=false)
static const MemRegion * unwrapRValueReferenceIndirection(const MemRegion *MR)
#define REGISTER_MAP_WITH_PROGRAMSTATE(Name, Key, Value)
Declares an immutable map of type NameTy, suitable for placement into the ProgramState.
StringRef getCheckerStringOption(StringRef CheckerName, StringRef OptionName, bool SearchInParents=false) const
Query an option's string value.
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition DeclBase.h:2109
bool isStdNamespace() const
DeclContext * getDeclContext()
Definition DeclBase.h:448
bool hasAttr() const
Definition DeclBase.h:577
bool isIdentifier() const
Predicate functions for querying what type of name this is.
bool isOverloadedOperator() const
Whether this function declaration represents an C++ overloaded operator, e.g., "operator+".
Definition Decl.h:2933
OverloadedOperatorKind getOverloadedOperator() const
getOverloadedOperator - Which C++ overloaded operator this function represents, if any.
Definition Decl.cpp:4126
StringRef getName() const
Return the actual identifier string.
const Decl * getDecl() const
const LocationContext * getParent() const
It might return null.
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition Decl.h:295
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:301
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition Decl.h:340
std::string getQualifiedNameAsString() const
Definition Decl.cpp:1680
bool isVoidType() const
Definition TypeBase.h:8871
bool isBooleanType() const
Definition TypeBase.h:9001
bool isRValueReferenceType() const
Definition TypeBase.h:8547
bool isVoidPointerType() const
Definition Type.cpp:712
const SourceManager & getSourceManager() const
const AnalyzerOptions & getAnalyzerOptions() const
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
void reportInvalidCheckerOptionValue(const CheckerFrontend *Checker, StringRef OptionName, StringRef ExpectedValueDesc) const
Emits an error through a DiagnosticsEngine about an invalid user supplied checker option value.
Simple checker classes that implement one frontend (i.e.
Definition Checker.h:553
const ProgramStateRef & getState() const
const Stmt * getStmtForDiagnostics() const
If the node's program point corresponds to a statement, retrieve that statement.
const LocationContext * getLocationContext() const
ExplodedNode * getFirstPred()
MemRegion - The root abstract class for all memory regions.
Definition MemRegion.h:98
bool hasMemorySpace(ProgramStateRef State) const
Definition MemRegion.h:148
virtual bool isSubRegionOf(const MemRegion *R) const
Check if the region is a subregion of the given region.
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getBaseRegion() const
const RegionTy * getAs() const
Definition MemRegion.h:1416
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getMostDerivedObjectRegion() const
Recursively retrieve the region of the most derived class instance of regions of C++ base class insta...
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
virtual const MemRegion * getOriginRegion() const
Find the region from which this symbol originates.
Definition SymExpr.h:124
virtual QualType getType() const =0
bool isLiveRegion(const MemRegion *region)
const char *const CXXMoveSemantics
bool isMovedFrom(ProgramStateRef State, const MemRegion *Region)
Returns true if the object is known to have been recently std::moved.
llvm::DenseSet< SymbolRef > InvalidatedSymbols
Definition Store.h:51
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
const SymExpr * SymbolRef
Definition SymExpr.h:133
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
std::shared_ptr< PathDiagnosticPiece > PathDiagnosticPieceRef
The JSON file list parser is used to communicate input to InstallAPI.
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
bool isa(CodeGen::Address addr)
Definition Address.h:330
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
Definition CallGraph.h:204
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ Type
The name was classified as a type.
Definition Sema.h:562
U cast(CodeGen::Address addr)
Definition Address.h:327
#define false
Definition stdbool.h:26