Skip to content

Commit 3d27d8f

Browse files
committed
Identify DeclRefExpr as a use of an origin
1 parent 28730f8 commit 3d27d8f

File tree

2 files changed

+88
-26
lines changed

2 files changed

+88
-26
lines changed

clang/lib/Analysis/LifetimeSafety.cpp

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class OriginManager {
119119
return AllOrigins.back();
120120
}
121121

122+
// TODO: Mark this method as const once we remove the call to getOrCreate.
122123
OriginID get(const Expr &E) {
123124
// Origin of DeclRefExpr is that of the declaration it refers to.
124125
if (const auto *DRE = dyn_cast<DeclRefExpr>(&E))
@@ -315,22 +316,28 @@ class ReturnOfOriginFact : public Fact {
315316
};
316317

317318
class UseFact : public Fact {
318-
OriginID UsedOrigin;
319319
const Expr *UseExpr;
320+
// True if this use is a write operation (e.g., left-hand side of assignment).
321+
// Write operations are exempted from use-after-free checks.
322+
bool IsWritten = false;
320323

321324
public:
322325
static bool classof(const Fact *F) { return F->getKind() == Kind::Use; }
323326

324-
UseFact(OriginID UsedOrigin, const Expr *UseExpr)
325-
: Fact(Kind::Use), UsedOrigin(UsedOrigin), UseExpr(UseExpr) {}
327+
UseFact(const Expr *UseExpr) : Fact(Kind::Use), UseExpr(UseExpr) {}
326328

327-
OriginID getUsedOrigin() const { return UsedOrigin; }
329+
OriginID getUsedOrigin(const OriginManager &OM) const {
330+
// TODO: Remove const cast and make OriginManager::get as const.
331+
return const_cast<OriginManager &>(OM).get(*UseExpr);
332+
}
328333
const Expr *getUseExpr() const { return UseExpr; }
334+
void markAsWritten() { IsWritten = true; }
335+
bool isWritten() const { return IsWritten; }
329336

330337
void dump(llvm::raw_ostream &OS, const OriginManager &OM) const override {
331338
OS << "Use (";
332-
OM.dump(getUsedOrigin(), OS);
333-
OS << ")\n";
339+
OM.dump(getUsedOrigin(OM), OS);
340+
OS << " " << (isWritten() ? "Write" : "Read") << ")\n";
334341
}
335342
};
336343

@@ -423,6 +430,8 @@ class FactGeneratorVisitor : public ConstStmtVisitor<FactGeneratorVisitor> {
423430
addAssignOriginFact(*VD, *InitExpr);
424431
}
425432

433+
void VisitDeclRefExpr(const DeclRefExpr *DRE) { handleUse(DRE); }
434+
426435
void VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *N) {
427436
/// TODO: Handle nullptr expr as a special 'null' loan. Uninitialized
428437
/// pointers can use the same type of loan.
@@ -456,10 +465,6 @@ class FactGeneratorVisitor : public ConstStmtVisitor<FactGeneratorVisitor> {
456465
}
457466
}
458467
}
459-
} else if (UO->getOpcode() == UO_Deref) {
460-
// This is a pointer use, like '*p'.
461-
OriginID OID = FactMgr.getOriginMgr().get(*UO->getSubExpr());
462-
CurrentBlockFacts.push_back(FactMgr.createFact<UseFact>(OID, UO));
463468
}
464469
}
465470

@@ -474,20 +479,13 @@ class FactGeneratorVisitor : public ConstStmtVisitor<FactGeneratorVisitor> {
474479
}
475480

476481
void VisitBinaryOperator(const BinaryOperator *BO) {
477-
if (BO->isAssignmentOp()) {
478-
const Expr *LHSExpr = BO->getLHS();
479-
const Expr *RHSExpr = BO->getRHS();
480-
481-
// We are interested in assignments like `ptr1 = ptr2` or `ptr = &var`
482-
// LHS must be a pointer/reference type that can be an origin.
483-
// RHS must also represent an origin (either another pointer/ref or an
484-
// address-of).
485-
if (const auto *DRE_LHS = dyn_cast<DeclRefExpr>(LHSExpr))
486-
if (const auto *VD_LHS =
487-
dyn_cast<ValueDecl>(DRE_LHS->getDecl()->getCanonicalDecl());
488-
VD_LHS && hasOrigin(VD_LHS->getType()))
489-
addAssignOriginFact(*VD_LHS, *RHSExpr);
490-
}
482+
if (BO->isAssignmentOp())
483+
handleAssignment(BO->getLHS(), BO->getRHS());
484+
}
485+
486+
void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *OCE) {
487+
if (OCE->isAssignmentOp() && OCE->getNumArgs() == 2)
488+
handleAssignment(OCE->getArg(0), OCE->getArg(1));
491489
}
492490

493491
void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *FCE) {
@@ -554,8 +552,46 @@ class FactGeneratorVisitor : public ConstStmtVisitor<FactGeneratorVisitor> {
554552
return false;
555553
}
556554

555+
void handleAssignment(const Expr *LHSExpr, const Expr *RHSExpr) {
556+
// Find the underlying variable declaration for the left-hand side.
557+
if (const auto *DRE_LHS =
558+
dyn_cast<DeclRefExpr>(LHSExpr->IgnoreParenImpCasts())) {
559+
markUseAsWrite(DRE_LHS);
560+
if (const auto *VD_LHS = dyn_cast<ValueDecl>(DRE_LHS->getDecl()))
561+
if (hasOrigin(LHSExpr->getType()))
562+
// We are interested in assignments like `ptr1 = ptr2` or `ptr = &var`
563+
// LHS must be a pointer/reference type that can be an origin.
564+
// RHS must also represent an origin (either another pointer/ref or an
565+
// address-of).
566+
addAssignOriginFact(*VD_LHS, *RHSExpr);
567+
}
568+
}
569+
570+
// A DeclRefExpr is a use of the referenced decl. It is checked for
571+
// use-after-free unless it is being written to (e.g. on the left-hand side
572+
// of an assignment).
573+
void handleUse(const DeclRefExpr *DRE) {
574+
if (hasOrigin(DRE->getType())) {
575+
UseFact *UF = FactMgr.createFact<UseFact>(DRE);
576+
CurrentBlockFacts.push_back(UF);
577+
assert(!UseFacts.contains(DRE));
578+
UseFacts[DRE] = UF;
579+
}
580+
}
581+
582+
void markUseAsWrite(const DeclRefExpr *DRE) {
583+
assert(UseFacts.contains(DRE));
584+
UseFacts[DRE]->markAsWritten();
585+
}
586+
557587
FactManager &FactMgr;
558588
llvm::SmallVector<Fact *> CurrentBlockFacts;
589+
// To distinguish between reads and writes for use-after-free checks, this map
590+
// stores the `UseFact` for each `DeclRefExpr`. We initially identify all
591+
// `DeclRefExpr`s as "read" uses. When an assignment is processed, the use
592+
// corresponding to the left-hand side is updated to be a "write", thereby
593+
// exempting it from the check.
594+
llvm::DenseMap<const DeclRefExpr *, UseFact *> UseFacts;
559595
};
560596

561597
class FactGenerator : public RecursiveASTVisitor<FactGenerator> {
@@ -1069,8 +1105,9 @@ class LifetimeChecker {
10691105
/// graph. It determines if the loans held by the used origin have expired
10701106
/// at the point of use.
10711107
void checkUse(const UseFact *UF) {
1072-
1073-
OriginID O = UF->getUsedOrigin();
1108+
if (UF->isWritten())
1109+
return;
1110+
OriginID O = UF->getUsedOrigin(FactMgr.getOriginMgr());
10741111

10751112
// Get the set of loans that the origin might hold at this program point.
10761113
LoanSet HeldLoans = LoanPropagation.getLoans(O, UF);

clang/test/Sema/warn-lifetime-safety-dataflow.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,28 @@ void ternary_operator() {
313313
// CHECK: End of Block
314314
}
315315

316+
// CHECK-LABEL: Function: test_use_facts
317+
void usePointer(MyObj*);
318+
void test_use_facts() {
319+
// CHECK: Block B{{[0-9]+}}:
320+
MyObj x;
321+
MyObj *p;
322+
p = &x;
323+
// CHECK: Use ([[O_P:[0-9]+]] (Decl: p) Write)
324+
(void)*p;
325+
// CHECK: Use ([[O_P]] (Decl: p) Read)
326+
usePointer(p);
327+
// CHECK: Use ([[O_P]] (Decl: p) Read)
328+
p->id = 1;
329+
// CHECK: Use ([[O_P]] (Decl: p) Read)
330+
331+
332+
MyObj* q;
333+
q = p;
334+
// CHECK: Use ([[O_P]] (Decl: p) Read)
335+
// CHECK: Use ([[O_Q:[0-9]+]] (Decl: q) Write)
336+
usePointer(q);
337+
// CHECK: Use ([[O_Q]] (Decl: q) Read)
338+
q->id = 2;
339+
// CHECK: Use ([[O_Q]] (Decl: q) Read)
340+
}

0 commit comments

Comments
 (0)