clang 23.0.0git
FactsGenerator.h
Go to the documentation of this file.
1//===- FactsGenerator.h - Lifetime Facts Generation -------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the FactsGenerator, which traverses the AST to generate
10// lifetime-relevant facts (such as loan issuance, expiration, origin flow,
11// and use) from CFG statements. These facts are used by the dataflow analyses
12// to track pointer lifetimes and detect use-after-free errors.
13//
14//===----------------------------------------------------------------------===//
15#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_FACTSGENERATOR_H
16#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_FACTSGENERATOR_H
17
22#include "clang/Analysis/CFG.h"
23#include "llvm/ADT/SmallVector.h"
24
26
27class FactsGenerator : public ConstStmtVisitor<FactsGenerator> {
29
30public:
32 : FactMgr(FactMgr), AC(AC) {}
33
34 void run();
35
36 void VisitDeclStmt(const DeclStmt *DS);
37 void VisitDeclRefExpr(const DeclRefExpr *DRE);
40 void VisitMemberExpr(const MemberExpr *ME);
41 void VisitCallExpr(const CallExpr *CE);
44 void VisitUnaryOperator(const UnaryOperator *UO);
45 void VisitReturnStmt(const ReturnStmt *RS);
46 void VisitBinaryOperator(const BinaryOperator *BO);
50 void VisitInitListExpr(const InitListExpr *ILE);
53
54private:
55 OriginList *getOriginsList(const ValueDecl &D);
56 OriginList *getOriginsList(const Expr &E);
57
58 void flow(OriginList *Dst, OriginList *Src, bool Kill);
59
60 void handleAssignment(const Expr *LHSExpr, const Expr *RHSExpr);
61
62 void handleCXXCtorInitializer(const CXXCtorInitializer *CII);
63 void handleLifetimeEnds(const CFGLifetimeEnds &LifetimeEnds);
64 void handleTemporaryDtor(const CFGTemporaryDtor &TemporaryDtor);
65
66 void handleExitBlock();
67
68 void handleGSLPointerConstruction(const CXXConstructExpr *CCE);
69
70 /// Checks if a call-like expression creates a borrow by passing a value to a
71 /// reference parameter, creating an IssueFact if it does.
72 /// \param IsGslConstruction True if this is a GSL construction where all
73 /// argument origins should flow to the returned origin.
74 void handleFunctionCall(const Expr *Call, const FunctionDecl *FD,
76 bool IsGslConstruction = false);
77
78 template <typename Destination, typename Source>
79 void flowOrigin(const Destination &D, const Source &S) {
80 flow(getOriginsList(D), getOriginsList(S), /*Kill=*/false);
81 }
82
83 template <typename Destination, typename Source>
84 void killAndFlowOrigin(const Destination &D, const Source &S) {
85 flow(getOriginsList(D), getOriginsList(S), /*Kill=*/true);
86 }
87
88 /// Checks if the expression is a `void("__lifetime_test_point_...")` cast.
89 /// If so, creates a `TestPointFact` and returns true.
90 bool handleTestPoint(const CXXFunctionalCastExpr *FCE);
91
92 // A DeclRefExpr will be treated as a use of the referenced decl. It will be
93 // checked for use-after-free unless it is later marked as being written to
94 // (e.g. on the left-hand side of an assignment).
95 void handleUse(const DeclRefExpr *DRE);
96
97 void markUseAsWrite(const DeclRefExpr *DRE);
98
99 llvm::SmallVector<Fact *> issuePlaceholderLoans();
100 FactManager &FactMgr;
102 llvm::SmallVector<Fact *> CurrentBlockFacts;
103 // Collect origins that escape the function in this block (OriginEscapesFact),
104 // appended at the end of CurrentBlockFacts to ensure they appear after
105 // ExpireFact entries.
106 llvm::SmallVector<Fact *> EscapesInCurrentBlock;
107 // To distinguish between reads and writes for use-after-free checks, this map
108 // stores the `UseFact` for each `DeclRefExpr`. We initially identify all
109 // `DeclRefExpr`s as "read" uses. When an assignment is processed, the use
110 // corresponding to the left-hand side is updated to be a "write", thereby
111 // exempting it from the check.
112 llvm::DenseMap<const DeclRefExpr *, UseFact *> UseFacts;
113
114 // This is a flow-insensitive approximation: once a declaration is moved
115 // anywhere in the function, it's treated as moved everywhere. This can lead
116 // to false negatives on control flow paths where the value is not actually
117 // moved, but these are considered lower priority than the false positives
118 // this tracking prevents.
119 // TODO: The ideal solution would be flow-sensitive ownership tracking that
120 // records where values are moved from and to, but this is more complex.
121 llvm::DenseSet<const ValueDecl *> MovedDecls;
122};
123
124} // namespace clang::lifetimes::internal
125
126#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_FACTSGENERATOR_H
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, QualType LValType, APValue &Val)
Perform an assignment of Val to LVal. Takes ownership of Val.
AnalysisDeclContext contains the context data for the function, method or block under analysis.
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:4038
Represents the point where the lifetime of an automatic object ends.
Definition CFG.h:293
Represents C++ object destructor implicitly generated at the end of full expression for temporary obj...
Definition CFG.h:511
Represents binding an expression to a temporary.
Definition ExprCXX.h:1493
Represents a call to a C++ constructor.
Definition ExprCXX.h:1548
Represents a C++ base or member initializer.
Definition DeclCXX.h:2369
Represents an explicit C++ type conversion that uses "functional" notation (C++ [expr....
Definition ExprCXX.h:1831
Represents a call to a member function that may be written either with member call syntax (e....
Definition ExprCXX.h:179
The null pointer literal (C++11 [lex.nullptr])
Definition ExprCXX.h:768
A call to an overloaded operator written using operator syntax.
Definition ExprCXX.h:84
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2943
ConditionalOperator - The ?
Definition Expr.h:4391
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1270
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition Stmt.h:1623
This represents one expression.
Definition Expr.h:112
Represents a function declaration or definition.
Definition Decl.h:2000
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
Definition Expr.h:3853
Describes an C or C++ initializer list.
Definition Expr.h:5299
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition ExprCXX.h:4920
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition Expr.h:3364
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Definition Stmt.h:3152
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2244
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
void VisitDeclRefExpr(const DeclRefExpr *DRE)
void VisitBinaryOperator(const BinaryOperator *BO)
void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *MTE)
FactsGenerator(FactManager &FactMgr, AnalysisDeclContext &AC)
void VisitCXXConstructExpr(const CXXConstructExpr *CCE)
void VisitImplicitCastExpr(const ImplicitCastExpr *ICE)
void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *FCE)
void VisitInitListExpr(const InitListExpr *ILE)
void VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *N)
void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *OCE)
void VisitUnaryOperator(const UnaryOperator *UO)
void VisitConditionalOperator(const ConditionalOperator *CO)
void VisitCXXMemberCallExpr(const CXXMemberCallExpr *MCE)
void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *BTE)
A list of origins representing levels of indirection for pointer-like types.
Definition Origins.h:94