Skip to content

Commit 28ba2e1

Browse files
committed
lifetime-safety-multi-origin
1 parent 3c87119 commit 28ba2e1

File tree

15 files changed

+545
-258
lines changed

15 files changed

+545
-258
lines changed

clang/include/clang/Analysis/Analyses/LifetimeSafety/Facts.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,19 @@ class ReturnOfOriginFact : public Fact {
152152

153153
class UseFact : public Fact {
154154
const Expr *UseExpr;
155-
OriginID OID;
155+
// The origins of the expression being used.
156+
llvm::SmallVector<OriginID, 1> OIDs;
156157
// True if this use is a write operation (e.g., left-hand side of assignment).
157158
// Write operations are exempted from use-after-free checks.
158159
bool IsWritten = false;
159160

160161
public:
161162
static bool classof(const Fact *F) { return F->getKind() == Kind::Use; }
162163

163-
UseFact(const Expr *UseExpr, OriginManager &OM)
164-
: Fact(Kind::Use), UseExpr(UseExpr), OID(OM.get(*UseExpr)) {}
164+
UseFact(const Expr *UseExpr, llvm::ArrayRef<OriginID> OIDs)
165+
: Fact(Kind::Use), UseExpr(UseExpr), OIDs(OIDs.begin(), OIDs.end()) {}
165166

166-
OriginID getUsedOrigin() const { return OID; }
167+
llvm::ArrayRef<OriginID> getUsedOrigins() const { return OIDs; }
167168
const Expr *getUseExpr() const { return UseExpr; }
168169
void markAsWritten() { IsWritten = true; }
169170
bool isWritten() const { return IsWritten; }

clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class FactsGenerator : public ConstStmtVisitor<FactsGenerator> {
5050
void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *MTE);
5151

5252
private:
53+
OriginTree *getTree(const ValueDecl &D);
54+
OriginTree *getTree(const Expr &E);
55+
56+
void flow(OriginTree *Dst, OriginTree *Src, bool Kill);
57+
5358
void handleDestructor(const CFGAutomaticObjDtor &DtorOpt);
5459

5560
void handleGSLPointerConstruction(const CXXConstructExpr *CCE);
@@ -64,32 +69,24 @@ class FactsGenerator : public ConstStmtVisitor<FactsGenerator> {
6469

6570
template <typename Destination, typename Source>
6671
void flowOrigin(const Destination &D, const Source &S) {
67-
OriginID DestOID = FactMgr.getOriginMgr().getOrCreate(D);
68-
OriginID SrcOID = FactMgr.getOriginMgr().get(S);
69-
CurrentBlockFacts.push_back(FactMgr.createFact<OriginFlowFact>(
70-
DestOID, SrcOID, /*KillDest=*/false));
72+
flow(getTree(D), getTree(S), /*Kill=*/false);
7173
}
7274

7375
template <typename Destination, typename Source>
7476
void killAndFlowOrigin(const Destination &D, const Source &S) {
75-
OriginID DestOID = FactMgr.getOriginMgr().getOrCreate(D);
76-
OriginID SrcOID = FactMgr.getOriginMgr().get(S);
77-
CurrentBlockFacts.push_back(
78-
FactMgr.createFact<OriginFlowFact>(DestOID, SrcOID, /*KillDest=*/true));
77+
flow(getTree(D), getTree(S), /*Kill=*/true);
7978
}
8079

8180
/// Checks if the expression is a `void("__lifetime_test_point_...")` cast.
8281
/// If so, creates a `TestPointFact` and returns true.
8382
bool handleTestPoint(const CXXFunctionalCastExpr *FCE);
8483

85-
void handleAssignment(const Expr *LHSExpr, const Expr *RHSExpr);
86-
8784
// A DeclRefExpr will be treated as a use of the referenced decl. It will be
8885
// checked for use-after-free unless it is later marked as being written to
8986
// (e.g. on the left-hand side of an assignment).
9087
void handleUse(const DeclRefExpr *DRE);
9188

92-
void markUseAsWrite(const DeclRefExpr *DRE);
89+
void markUseAsWrite(const Expr *E);
9390

9491
FactManager &FactMgr;
9592
AnalysisDeclContext &AC;

clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeAnnotations.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ bool isAssignmentOperatorLifetimeBound(const CXXMethodDecl *CMD);
3838
/// method or because it's a normal assignment operator.
3939
bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD);
4040

41+
// Tells whether the type is annotated with [[gsl::Pointer]].
42+
bool isGslPointerType(QualType QT);
43+
// Tells whether the type is annotated with [[gsl::Owner]].
44+
bool isGslOwnerType(QualType QT);
45+
4146
} // namespace clang::lifetimes
4247

4348
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMEANNOTATIONS_H

clang/include/clang/Analysis/Analyses/LifetimeSafety/Origins.h

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,41 +52,76 @@ struct Origin {
5252
}
5353
};
5454

55+
/// A tree of origins representing levels of indirection for pointer-like
56+
/// types, or a single origin for non-pointer lvalues.
57+
struct OriginTree {
58+
OriginID OID;
59+
OriginTree *Pointee = nullptr;
60+
61+
OriginTree(OriginID OID) : OID(OID) {}
62+
63+
size_t getDepth() const {
64+
size_t Depth = 1;
65+
const OriginTree *T = this;
66+
while (T->Pointee) {
67+
T = T->Pointee;
68+
Depth++;
69+
}
70+
return Depth;
71+
}
72+
};
73+
74+
bool isPointerLikeType(QualType QT);
75+
5576
/// Manages the creation, storage, and retrieval of origins for pointer-like
5677
/// variables and expressions.
5778
class OriginManager {
5879
public:
59-
OriginManager() = default;
80+
/// Gets or creates the OriginTree for a given ValueDecl.
81+
OriginTree *getOrCreateTree(const ValueDecl *D);
6082

61-
Origin &addOrigin(OriginID ID, const clang::ValueDecl &D);
62-
Origin &addOrigin(OriginID ID, const clang::Expr &E);
63-
64-
// TODO: Mark this method as const once we remove the call to getOrCreate.
65-
OriginID get(const Expr &E);
66-
67-
OriginID get(const ValueDecl &D);
68-
69-
OriginID getOrCreate(const Expr &E);
83+
/// Gets or creates the OriginTree for a given Expr.
84+
/// Returns nullptr for rvalues of non-pointer type as these do not have
85+
/// origins.
86+
OriginTree *getOrCreateTree(const Expr *E, ASTContext &Ctx);
7087

7188
const Origin &getOrigin(OriginID ID) const;
7289

7390
llvm::ArrayRef<Origin> getOrigins() const { return AllOrigins; }
7491

75-
OriginID getOrCreate(const ValueDecl &D);
76-
7792
unsigned getNumOrigins() const { return NextOriginID.Value; }
7893

7994
void dump(OriginID OID, llvm::raw_ostream &OS) const;
8095

8196
private:
8297
OriginID getNextOriginID() { return NextOriginID++; }
8398

99+
OriginID createOrigin(const ValueDecl *D) {
100+
OriginID NewID = getNextOriginID();
101+
AllOrigins.emplace_back(NewID, D);
102+
return NewID;
103+
}
104+
105+
OriginID createOrigin(const Expr *E) {
106+
OriginID NewID = getNextOriginID();
107+
AllOrigins.emplace_back(NewID, E);
108+
return NewID;
109+
}
110+
111+
OriginTree *createNode(OriginID OID) {
112+
return new (TreeAllocator.Allocate<OriginTree>()) OriginTree(OID);
113+
}
114+
115+
template <typename T>
116+
OriginTree *buildTreeForType(QualType QT, const T *Node);
117+
84118
OriginID NextOriginID{0};
85-
/// TODO(opt): Profile and evaluate the usefullness of small buffer
119+
/// TODO(opt): Profile and evaluate the usefulness of small buffer
86120
/// optimisation.
87121
llvm::SmallVector<Origin> AllOrigins;
88-
llvm::DenseMap<const clang::ValueDecl *, OriginID> DeclToOriginID;
89-
llvm::DenseMap<const clang::Expr *, OriginID> ExprToOriginID;
122+
llvm::BumpPtrAllocator TreeAllocator;
123+
llvm::DenseMap<const clang::ValueDecl *, OriginTree *> DeclToTreeMap;
124+
llvm::DenseMap<const clang::Expr *, OriginTree *> ExprToTreeMap;
90125
};
91126
} // namespace clang::lifetimes::internal
92127

clang/lib/Analysis/LifetimeSafety/Facts.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ void ReturnOfOriginFact::dump(llvm::raw_ostream &OS, const LoanManager &,
5353
void UseFact::dump(llvm::raw_ostream &OS, const LoanManager &,
5454
const OriginManager &OM) const {
5555
OS << "Use (";
56-
OM.dump(getUsedOrigin(), OS);
56+
for (OriginID OID : getUsedOrigins()) {
57+
OM.dump(OID, OS);
58+
OS << " ";
59+
}
5760
OS << ", " << (isWritten() ? "Write" : "Read") << ")\n";
5861
}
5962

0 commit comments

Comments
 (0)