-
Notifications
You must be signed in to change notification settings - Fork 15.6k
[LifetimeSafety] Track moved declarations to prevent false positives #170007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: users/usx95/11-29-dereference_operator
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -183,9 +183,27 @@ void FactsGenerator::VisitCXXMemberCallExpr(const CXXMemberCallExpr *MCE) { | |
| } | ||
| } | ||
|
|
||
| static bool isStdMove(const FunctionDecl *FD) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure how I feel about this heuristic. People can also do "moves" by not using I think a better way to check if a value was moved whether it was passed to a move constructor. Unfortunately, sometimes we do not see the move ctor call. But we can assume move when the value is passed to a function as an rvalue reference. So I think I'd rather check if something is passed as an rvalue ref (to move ctor or another function) rather than checking for std::move. |
||
| return FD && FD->isInStdNamespace() && FD->getIdentifier() && | ||
| FD->getName() == "move"; | ||
| } | ||
|
|
||
| void FactsGenerator::VisitCallExpr(const CallExpr *CE) { | ||
| handleFunctionCall(CE, CE->getDirectCallee(), | ||
| {CE->getArgs(), CE->getNumArgs()}); | ||
| // Track declarations that are moved via std::move. | ||
| // This is a flow-insensitive approximation: once a declaration is moved | ||
| // anywhere in the function, it's treated as moved everywhere. This can lead | ||
| // to false negatives on control flow paths where the value is not actually | ||
| // moved, but these are considered lower priority than the false positives | ||
| // this tracking prevents. | ||
| // TODO: The ideal solution would be flow-sensitive ownership tracking that | ||
| // records where values are moved from and to, but this is more complex. | ||
| if (isStdMove(CE->getDirectCallee())) | ||
| if (CE->getNumArgs() == 1) | ||
| if (auto *DRE = | ||
| dyn_cast<DeclRefExpr>(CE->getArg(0)->IgnoreParenImpCasts())) | ||
| MovedDecls.insert(DRE->getDecl()); | ||
| } | ||
|
|
||
| void FactsGenerator::VisitCXXNullPtrLiteralExpr( | ||
|
|
@@ -364,6 +382,11 @@ void FactsGenerator::handleLifetimeEnds(const CFGLifetimeEnds &LifetimeEnds) { | |
| // Iterate through all loans to see if any expire. | ||
| for (const auto *Loan : FactMgr.getLoanMgr().getLoans()) { | ||
| if (const auto *BL = dyn_cast<PathLoan>(Loan)) { | ||
| // Skip loans for declarations that have been moved. When a value is | ||
| // moved, the original owner no longer has ownership and its destruction | ||
| // should not cause the loan to expire, preventing false positives. | ||
| if (MovedDecls.contains(BL->getAccessPath().D)) | ||
| continue; | ||
| // Check if the loan is for a stack variable and if that variable | ||
| // is the one being destructed. | ||
| if (BL->getAccessPath().D == LifetimeEndsVD) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider moving the comments on lines 195-201 here, or at least mentioning them. But, that's a matter of taste :)
Also, whether in comments or test maybe highlight some example impacts? e.g. modifying your test below:
Or, a branch?