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);
41 void VisitMemberExpr(const MemberExpr *ME);
42 void VisitCallExpr(const CallExpr *CE);
44 void VisitCastExpr(const CastExpr *CE);
45 void VisitUnaryOperator(const UnaryOperator *UO);
46 void VisitReturnStmt(const ReturnStmt *RS);
47 void VisitBinaryOperator(const BinaryOperator *BO);
51 void VisitInitListExpr(const InitListExpr *ILE);
54 void VisitLambdaExpr(const LambdaExpr *LE);
56
57private:
58 OriginList *getOriginsList(const ValueDecl &D);
59 OriginList *getOriginsList(const Expr &E);
60
61 bool hasOrigins(QualType QT) const;
62 bool hasOrigins(const Expr *E) const;
63
64 void flow(OriginList *Dst, OriginList *Src, bool Kill);
65
66 void handleAssignment(const Expr *LHSExpr, const Expr *RHSExpr);
67
68 void handlePointerArithmetic(const BinaryOperator *BO);
69
70 void handleCXXCtorInitializer(const CXXCtorInitializer *CII);
71
72 void handleLifetimeEnds(const CFGLifetimeEnds &LifetimeEnds);
73
74 void handleFullExprCleanup(const CFGFullExprCleanup &FullExprCleanup);
75
76 void handleExitBlock();
77
78 /// Mark all fields of the implicit object as used for an instance method
79 /// call, since the callee may access any part of the object.
80 void handleImplicitObjectFieldUses(const Expr *Call, const FunctionDecl *FD);
81
82 void handleGSLPointerConstruction(const CXXConstructExpr *CCE);
83
84 /// Detects arguments passed to rvalue reference parameters and creates
85 /// MovedOriginFact for them. The MovedLoansAnalysis then uses these facts
86 /// to track in a flow-sensitive manner which loans have been moved at each
87 /// program point, allowing warnings to distinguish potentially moved storage
88 /// from other use-after-free errors.
89 void handleMovedArgsInCall(const FunctionDecl *FD,
91
92 /// Checks if a call-like expression creates a borrow by passing a value to a
93 /// reference parameter, creating an IssueFact if it does.
94 /// \param IsGslConstruction True if this is a GSL construction where all
95 /// argument origins should flow to the returned origin.
96 void handleFunctionCall(const Expr *Call, const FunctionDecl *FD,
98 bool IsGslConstruction = false);
99
100 // Detect container methods that invalidate iterators/references.
101 // For instance methods, Args[0] is the implicit 'this' pointer.
102 void handleInvalidatingCall(const Expr *Call, const FunctionDecl *FD,
104
105 template <typename Destination, typename Source>
106 void flowOrigin(const Destination &D, const Source &S) {
107 flow(getOriginsList(D), getOriginsList(S), /*Kill=*/false);
108 }
109
110 template <typename Destination, typename Source>
111 void killAndFlowOrigin(const Destination &D, const Source &S) {
112 flow(getOriginsList(D), getOriginsList(S), /*Kill=*/true);
113 }
114
115 /// Checks if the expression is a `void("__lifetime_test_point_...")` cast.
116 /// If so, creates a `TestPointFact` and returns true.
117 bool handleTestPoint(const CXXFunctionalCastExpr *FCE);
118
119 // Treats an expression as a use of the referenced object. It will be
120 // checked for use-after-free unless it is later marked as being written to
121 // (e.g. on the left-hand side of an assignment in the case of a DeclRefExpr).
122 void handleUse(const Expr *E);
123
124 void markUseAsWrite(const DeclRefExpr *DRE);
125
126 bool escapesViaReturn(OriginID OID) const;
127
128 llvm::SmallVector<Fact *> issuePlaceholderLoans();
129 FactManager &FactMgr;
131 llvm::SmallVector<Fact *> CurrentBlockFacts;
132 // Collect origins that escape the function in this block (OriginEscapesFact),
133 // appended at the end of CurrentBlockFacts to ensure they appear after
134 // ExpireFact entries.
135 llvm::SmallVector<Fact *> EscapesInCurrentBlock;
136 // To distinguish between reads and writes for use-after-free checks, this map
137 // stores the `UseFact` for each `DeclRefExpr`. We initially identify all
138 // `DeclRefExpr`s as "read" uses. When an assignment is processed, the use
139 // corresponding to the left-hand side is updated to be a "write", thereby
140 // exempting it from the check.
141 llvm::DenseMap<const Expr *, UseFact *> UseFacts;
142 const CFGBlock *CurrentBlock;
143};
144
145} // namespace clang::lifetimes::internal
146
147#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.
ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
Definition Expr.h:2724
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:4041
Represents a single basic block in a source-level CFG.
Definition CFG.h:632
Represents the point where the lifetime of an automatic object ends.
Definition CFG.h:294
Represents binding an expression to a temporary.
Definition ExprCXX.h:1497
Represents a call to a C++ constructor.
Definition ExprCXX.h:1552
Represents a C++ base or member initializer.
Definition DeclCXX.h:2389
A use of a default initializer in a constructor or in aggregate initialization.
Definition ExprCXX.h:1381
Represents an explicit C++ type conversion that uses "functional" notation (C++ [expr....
Definition ExprCXX.h:1835
Represents a call to a member function that may be written either with member call syntax (e....
Definition ExprCXX.h:183
The null pointer literal (C++11 [lex.nullptr])
Definition ExprCXX.h:772
A call to an overloaded operator written using operator syntax.
Definition ExprCXX.h:85
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2946
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition Expr.h:3679
ConditionalOperator - The ?
Definition Expr.h:4394
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1273
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition Stmt.h:1637
This represents one expression.
Definition Expr.h:112
Represents a function declaration or definition.
Definition Decl.h:2015
Describes an C or C++ initializer list.
Definition Expr.h:5302
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition ExprCXX.h:1972
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:3367
A (possibly-)qualified type.
Definition TypeBase.h:937
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Definition Stmt.h:3166
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2247
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 VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *FCE)
void VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *DIE)
void VisitInitListExpr(const InitListExpr *ILE)
void VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *N)
void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *OCE)
void VisitArraySubscriptExpr(const ArraySubscriptExpr *ASE)
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:95
utils::ID< struct OriginTag > OriginID
Definition Origins.h:28