-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[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?
[LifetimeSafety] Track moved declarations to prevent false positives #170007
Conversation
|
Warning This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
This stack of pull requests is managed by Graphite. Learn more about stacking. |
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
🐧 Linux x64 Test Results
|
a1b4509 to
00be6fa
Compare
349c748 to
a3842ac
Compare
00be6fa to
c21c11b
Compare
3ead2ab to
f53324e
Compare
354ff2e to
8d7fc4d
Compare
f53324e to
1c7deeb
Compare
8d7fc4d to
68895d3
Compare
1c7deeb to
1ad3ce7
Compare
68895d3 to
84eda37
Compare
4ce85f5 to
ceda23e
Compare
84eda37 to
13948c7
Compare
13948c7 to
9a4cf36
Compare
|
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-temporal-safety Author: Utkarsh Saxena (usx95) ChangesPrevent false positives in lifetime safety analysis when variables are moved using When a value is moved using
void silenced() {
MyObj b;
View v;
{
MyObj a;
v = a;
b = std::move(a); // No warning for 'a' being moved.
}
(void)v;
}Fixes #152520 Full diff: https://github.com/llvm/llvm-project/pull/170007.diff 3 Files Affected:
diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
index 5b5626020e772..0c0581239ce34 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
@@ -101,6 +101,11 @@ class FactsGenerator : public ConstStmtVisitor<FactsGenerator> {
// corresponding to the left-hand side is updated to be a "write", thereby
// exempting it from the check.
llvm::DenseMap<const DeclRefExpr *, UseFact *> UseFacts;
+
+ // Tracks declarations that have been moved via std::move. This is used to
+ // prevent false positives when the original owner is destroyed after the
+ // value has been moved. This tracking is flow-insensitive.
+ llvm::DenseSet<const ValueDecl *> MovedDecls;
};
} // namespace clang::lifetimes::internal
diff --git a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
index b27dcb6163449..ba88af2418056 100644
--- a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
@@ -163,9 +163,27 @@ void FactsGenerator::VisitCXXMemberCallExpr(const CXXMemberCallExpr *MCE) {
}
}
+static bool isStdMove(const FunctionDecl *FD) {
+ 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(
@@ -341,6 +359,11 @@ void FactsGenerator::handleLifetimeEnds(const CFGLifetimeEnds &LifetimeEnds) {
// Iterate through all loans to see if any expire.
for (const auto &Loan : FactMgr.getLoanMgr().getLoans()) {
const AccessPath &LoanPath = Loan.Path;
+ // 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(LoanPath.D))
+ continue;
// Check if the loan is for a stack variable and if that variable
// is the one being destructed.
if (LoanPath.D == LifetimeEndsVD)
diff --git a/clang/test/Sema/warn-lifetime-safety.cpp b/clang/test/Sema/warn-lifetime-safety.cpp
index f22c73cfeb784..97a79cc4ce102 100644
--- a/clang/test/Sema/warn-lifetime-safety.cpp
+++ b/clang/test/Sema/warn-lifetime-safety.cpp
@@ -1,9 +1,14 @@
// RUN: %clang_cc1 -fsyntax-only -fexperimental-lifetime-safety -Wexperimental-lifetime-safety -Wno-dangling -verify %s
+#include "Inputs/lifetime-analysis.h"
+
struct View;
struct [[gsl::Owner]] MyObj {
int id;
+ MyObj();
+ MyObj(int);
+ MyObj(const MyObj&);
~MyObj() {} // Non-trivial destructor
MyObj operator+(MyObj);
@@ -1297,3 +1302,16 @@ void add(int c, MyObj* node) {
arr[4] = node;
}
} // namespace CppCoverage
+
+namespace do_not_warn_on_std_move {
+void silenced() {
+ MyObj b;
+ View v;
+ {
+ MyObj a;
+ v = a;
+ b = std::move(a); // No warning for 'a' being moved.
+ }
+ (void)v;
+}
+} // namespace do_not_warn_on_std_move
|
Just a note. While this fixes false positives, it introduces false negatives. The pointer is not always valid after the owner object is moved, an use-after-free example (when the string uses short string optimization) https://godbolt.org/z/eP7PbaMEn |
Xazax-hun
left a comment
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.
I think this is a step in the right direction but we should refine this a bit further.
| } | ||
| } | ||
|
|
||
| static bool isStdMove(const FunctionDecl *FD) { |
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.
I am not sure how I feel about this heuristic. People can also do "moves" by not using std::move but casting to an rvalue reference manually, or introducing their own wrapper (that might do some additional checks in some cases).
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.
| // 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(LoanPath.D)) |
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.
Is there a way to make this less intrusive? It would be nice if we could disable these checks only for the basic blocks that are reachable from the basic blocks where the move actually happened. So a move at the end of the function should not really prevent us finding bugs at the beginning of the function.
I wonder if we should still warn about select classes like |

Prevent false positives in lifetime safety analysis when variables are moved using
std::move.When a value is moved using
std::move, ownership is transferred from the original variable to another. The lifetime safety analysis was previously generating false positives by warning about use-after-lifetime when the original variable was destroyed after being moved. This change prevents those false positives by tracking moved declarations and exempting them from loan expiration checks.std::movein theFactsGeneratorclassMovedDeclsset to track moved declarations in a flow-insensitive mannerstd::movecalls inVisitCallExprhandleLifetimeEndsto skip loans for declarations that have been movedFixes #152520