Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions clang/include/clang/Analysis/Analyses/LifetimeSafety/Facts.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,19 @@ class ReturnOfOriginFact : public Fact {

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

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

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

OriginID getUsedOrigin() const { return OID; }
llvm::ArrayRef<OriginID> getUsedOrigins() const { return OIDs; }
const Expr *getUseExpr() const { return UseExpr; }
void markAsWritten() { IsWritten = true; }
bool isWritten() const { return IsWritten; }
Expand Down Expand Up @@ -191,8 +192,8 @@ class TestPointFact : public Fact {

class FactManager {
public:
void init(const CFG &Cfg) {
assert(BlockToFacts.empty() && "FactManager already initialized");
FactManager(const AnalysisDeclContext &AC, const CFG &Cfg)
: OriginMgr(AC.getASTContext()) {
BlockToFacts.resize(Cfg.getNumBlockIDs());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class FactsGenerator : public ConstStmtVisitor<FactsGenerator> {
void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *MTE);

private:
OriginTree *getTree(const ValueDecl &D);
OriginTree *getTree(const Expr &E);

void flow(OriginTree *Dst, OriginTree *Src, bool Kill);

void handleDestructor(const CFGAutomaticObjDtor &DtorOpt);

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

template <typename Destination, typename Source>
void flowOrigin(const Destination &D, const Source &S) {
OriginID DestOID = FactMgr.getOriginMgr().getOrCreate(D);
OriginID SrcOID = FactMgr.getOriginMgr().get(S);
CurrentBlockFacts.push_back(FactMgr.createFact<OriginFlowFact>(
DestOID, SrcOID, /*KillDest=*/false));
flow(getTree(D), getTree(S), /*Kill=*/false);
}

template <typename Destination, typename Source>
void killAndFlowOrigin(const Destination &D, const Source &S) {
OriginID DestOID = FactMgr.getOriginMgr().getOrCreate(D);
OriginID SrcOID = FactMgr.getOriginMgr().get(S);
CurrentBlockFacts.push_back(
FactMgr.createFact<OriginFlowFact>(DestOID, SrcOID, /*KillDest=*/true));
flow(getTree(D), getTree(S), /*Kill=*/true);
}

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

void handleAssignment(const Expr *LHSExpr, const Expr *RHSExpr);

// A DeclRefExpr will be treated as a use of the referenced decl. It will be
// checked for use-after-free unless it is later marked as being written to
// (e.g. on the left-hand side of an assignment).
void handleUse(const DeclRefExpr *DRE);

void markUseAsWrite(const DeclRefExpr *DRE);
void markUseAsWrite(const Expr *E);

FactManager &FactMgr;
AnalysisDeclContext &AC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ bool isAssignmentOperatorLifetimeBound(const CXXMethodDecl *CMD);
/// method or because it's a normal assignment operator.
bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD);

// Tells whether the type is annotated with [[gsl::Pointer]].
bool isGslPointerType(QualType QT);
// Tells whether the type is annotated with [[gsl::Owner]].
bool isGslOwnerType(QualType QT);

} // namespace clang::lifetimes

#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMEANNOTATIONS_H
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ class LifetimeSafetyAnalysis {
return *LoanPropagation;
}
LiveOriginsAnalysis &getLiveOrigins() const { return *LiveOrigins; }
FactManager &getFactManager() { return FactMgr; }
FactManager &getFactManager() { return *FactMgr; }

private:
AnalysisDeclContext &AC;
LifetimeSafetyReporter *Reporter;
LifetimeFactory Factory;
FactManager FactMgr;
std::unique_ptr<FactManager> FactMgr;
std::unique_ptr<LiveOriginsAnalysis> LiveOrigins;
std::unique_ptr<LoanPropagationAnalysis> LoanPropagation;
};
Expand Down
109 changes: 88 additions & 21 deletions clang/include/clang/Analysis/Analyses/LifetimeSafety/Origins.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OriginID ID) {

/// An Origin is a symbolic identifier that represents the set of possible
/// loans a pointer-like object could hold at any given time.
/// TODO: Enhance the origin model to handle complex types, pointer
/// indirection and reborrowing. The plan is to move from a single origin per
/// variable/expression to a "list of origins" governed by the Type.
/// For example, the type 'int**' would have two origins.
/// See discussion:
/// https://github.com/llvm/llvm-project/pull/142313/commits/0cd187b01e61b200d92ca0b640789c1586075142#r2137644238
///
/// Each Origin corresponds to a single level of indirection. For complex types
/// with multiple levels of indirection (e.g., `int**`), multiple Origins are
/// organized into an OriginTree structure (see below).
struct Origin {
OriginID ID;
/// A pointer to the AST node that this origin represents. This union
Expand All @@ -52,41 +50,110 @@ struct Origin {
}
};

/// A tree of origins representing levels of indirection for pointer-like types.
///
/// Each node in the tree contains an OriginID representing a level of
/// indirection. The tree structure captures the multi-level nature of
/// pointer and reference types in the lifetime analysis.
///
/// Examples:
/// - For `int& x`, the tree has depth 2:
/// * Root: origin for the reference storage itself (the lvalue `x`)
/// * Pointee: origin for what `x` refers to
///
/// - For `int* p`, the tree has depth 2:
/// * Root: origin for the pointer variable `p`
/// * Pointee: origin for what `p` points to
///
/// - For `View v` (where View is gsl::Pointer), the tree has depth 2:
/// * Root: origin for the view object itself
/// * Pointee: origin for what the view refers to
///
/// - For `int** pp`, the tree has depth 3:
/// * Root: origin for `pp` itself
/// * Pointee: origin for `*pp` (what `pp` points to)
/// * Pointee->Pointee: origin for `**pp` (what `*pp` points to)
///
/// The tree structure enables the analysis to track how loans flow through
/// different levels of indirection when assignments and dereferences occur.
struct OriginTree {
OriginID OID;
OriginTree *Pointee = nullptr;

OriginTree(OriginID OID) : OID(OID) {}

size_t getDepth() const {
size_t Depth = 1;
const OriginTree *T = this;
while (T->Pointee) {
T = T->Pointee;
Depth++;
}
return Depth;
}
};

bool isPointerLikeType(QualType QT);

/// Manages the creation, storage, and retrieval of origins for pointer-like
/// variables and expressions.
class OriginManager {
public:
OriginManager() = default;

Origin &addOrigin(OriginID ID, const clang::ValueDecl &D);
Origin &addOrigin(OriginID ID, const clang::Expr &E);

// TODO: Mark this method as const once we remove the call to getOrCreate.
OriginID get(const Expr &E);

OriginID get(const ValueDecl &D);

OriginID getOrCreate(const Expr &E);
explicit OriginManager(ASTContext& AST) : AST(AST) {}

/// Gets or creates the OriginTree for a given ValueDecl.
///
/// Creates a tree structure mirroring the levels of indirection in the
/// declaration's type (e.g., `int** p` creates depth 2).
///
/// \returns The OriginTree, or nullptr if the type is not pointer-like.
OriginTree *getOrCreateTree(const ValueDecl *D);

/// Gets or creates the OriginTree for a given Expr.
///
/// Creates a tree based on the expression's type and value category:
/// - Lvalues get an implicit reference level (modeling addressability)
/// - Rvalues of non-pointer type return nullptr (no trackable origin)
/// - DeclRefExpr may reuse the underlying declaration's tree
///
/// \returns The OriginTree, or nullptr for non-pointer rvalues.
OriginTree *getOrCreateTree(const Expr *E);

const Origin &getOrigin(OriginID ID) const;

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

OriginID getOrCreate(const ValueDecl &D);

unsigned getNumOrigins() const { return NextOriginID.Value; }

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

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

OriginTree *createNode(const ValueDecl *D) {
OriginID NewID = getNextOriginID();
AllOrigins.emplace_back(NewID, D);
return new (TreeAllocator.Allocate<OriginTree>()) OriginTree(NewID);
}

OriginTree *createNode(const Expr *E) {
OriginID NewID = getNextOriginID();
AllOrigins.emplace_back(NewID, E);
return new (TreeAllocator.Allocate<OriginTree>()) OriginTree(NewID);
}

template <typename T>
OriginTree *buildTreeForType(QualType QT, const T *Node);

ASTContext& AST;
OriginID NextOriginID{0};
/// TODO(opt): Profile and evaluate the usefullness of small buffer
/// TODO(opt): Profile and evaluate the usefulness of small buffer
/// optimisation.
llvm::SmallVector<Origin> AllOrigins;
llvm::DenseMap<const clang::ValueDecl *, OriginID> DeclToOriginID;
llvm::DenseMap<const clang::Expr *, OriginID> ExprToOriginID;
llvm::BumpPtrAllocator TreeAllocator;
llvm::DenseMap<const clang::ValueDecl *, OriginTree *> DeclToTree;
llvm::DenseMap<const clang::Expr *, OriginTree *> ExprToTree;
};
} // namespace clang::lifetimes::internal

Expand Down
5 changes: 4 additions & 1 deletion clang/lib/Analysis/LifetimeSafety/Facts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ void ReturnOfOriginFact::dump(llvm::raw_ostream &OS, const LoanManager &,
void UseFact::dump(llvm::raw_ostream &OS, const LoanManager &,
const OriginManager &OM) const {
OS << "Use (";
OM.dump(getUsedOrigin(), OS);
for (OriginID OID : getUsedOrigins()) {
OM.dump(OID, OS);
OS << ", ";
}
OS << ", " << (isWritten() ? "Write" : "Read") << ")\n";
}

Expand Down
Loading